Files
SVSimServer/SVSim.Bootstrap/Importers/ItemImporter.cs
gamer147 6a03ff1bf6 feat(items): catalog import with Type + ThumbnailPath columns
ItemEntry gains Type (client item_type enum, 1=challenge, 2=card-pack
ticket, 3=premium orb, 4=colosseum, 5=orb piece, 6=skin/event ticket,
7=other) and ThumbnailPath. ItemImporter mirrors PaymentItemImporter
shape: find-or-create per item_id, save once, idempotent. Wired into
Bootstrap.Program and SVSimTestFactory.SeedGlobalsAsync. Unblocks
/item_purchase/info (filters card-pack tickets by Type==2) and any
reward grant of UserGoodsType.Item, which previously threw because
the catalog was empty.

466 tests pass (was 461).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-27 21:44:24 -04:00

53 lines
1.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using SVSim.Bootstrap.Models.Seed;
using SVSim.Database;
using SVSim.Database.Models;
namespace SVSim.Bootstrap.Importers;
/// <summary>
/// Idempotent upsert of the item catalog from <c>seeds/items.json</c>. Source is the client's
/// <c>item_master.csv</c> + <c>itemtext.json</c> (extracted via
/// <c>data_dumps/extract/extract-items.py</c>). Rows missing from the seed are LEFT INTACT.
/// </summary>
public class ItemImporter
{
public async Task<int> ImportAsync(SVSimDbContext context, string seedDir)
{
string path = Path.Combine(seedDir, "items.json");
var seed = SeedLoader.LoadList<ItemSeed>(path);
if (seed.Count == 0)
{
Console.WriteLine("[ItemImporter] No seed rows; skipping.");
return 0;
}
var existing = await context.Items.ToDictionaryAsync(e => e.Id);
int created = 0, updated = 0;
foreach (var s in seed)
{
if (s.ItemId == 0) continue;
var entry = existing.TryGetValue(s.ItemId, out var ex)
? ex : new ItemEntry { Id = s.ItemId };
entry.Name = s.Name;
entry.Type = s.Type;
entry.ThumbnailPath = s.ThumbnailPath;
if (ex is null)
{
context.Items.Add(entry);
existing[s.ItemId] = entry;
created++;
}
else updated++;
}
await context.SaveChangesAsync();
Console.WriteLine($"[ItemImporter] +{created}/~{updated}");
return created + updated;
}
}