refactor(bootstrap): migrate default decks to seed file

Extracts /deck/info's default_deck_list into seeds/default-decks.json
via the new extract-default-decks.ps1 PowerShell script and imports
through DefaultDeckImporter. The importer carries the same orphan-
card-id warning the old GlobalsImporter path emitted; production cards
yield 0 orphans. WarnOrphans stays inside GlobalsImporter for now —
SpotCards/ReprintedCards/UnlimitedRestrictions/LoadingExclusionCards
still use it until Task 9.

Part of the bootstrap seed refactor (Task 6).
This commit is contained in:
gamer147
2026-05-26 14:44:21 -04:00
parent a5e4f35c32
commit 83298a2d47
8 changed files with 593 additions and 530 deletions

View File

@@ -0,0 +1,60 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using SVSim.Bootstrap.Models.Seed;
using SVSim.Database;
using SVSim.Database.Models;
namespace SVSim.Bootstrap.Importers;
/// <summary>
/// Idempotent upsert of default decks from <c>seeds/default-decks.json</c>. Warns on orphan card
/// references (card_id not in Cards table) but never fails — CardImporter must run first for a
/// clean warning-free run. Rows missing from the seed are LEFT INTACT.
/// </summary>
public class DefaultDeckImporter
{
public async Task<int> ImportAsync(SVSimDbContext context, string seedDir)
{
var seed = SeedLoader.LoadList<DefaultDeckSeed>(Path.Combine(seedDir, "default-decks.json"));
if (seed.Count == 0)
{
Console.WriteLine("[DefaultDeckImporter] No seed rows; skipping.");
return 0;
}
var existing = await context.DefaultDecks.ToDictionaryAsync(e => e.Id);
var knownCards = new HashSet<long>(await context.Cards.Select(c => c.Id).ToListAsync());
int created = 0, updated = 0, orphans = 0;
foreach (var s in seed)
{
if (s.Id == 0) continue;
var entry = existing.TryGetValue(s.Id, out var ex) ? ex : new DefaultDeckEntry { Id = s.Id };
entry.ClassId = s.ClassId;
entry.SleeveId = s.SleeveId;
entry.LeaderSkinId = s.LeaderSkinId;
entry.DeckName = s.DeckName;
entry.CardIdArray = JsonSerializer.Serialize(s.CardIdArray);
// Orphan count against card master — informational, never throws.
foreach (var cardId in s.CardIdArray)
{
if (!knownCards.Contains(cardId)) orphans++;
}
if (ex is null) { context.DefaultDecks.Add(entry); existing[s.Id] = entry; created++; }
else updated++;
}
await context.SaveChangesAsync();
WarnOrphans("DefaultDecks.card_id_array", orphans);
Console.WriteLine($"[DefaultDeckImporter] +{created}/~{updated}");
return created + updated;
}
private static void WarnOrphans(string label, int count)
{
if (count > 0)
Console.Error.WriteLine($"[DefaultDeckImporter] Warning: {label} has {count} orphan card_id(s) — run CardImporter first for clean references.");
}
}

View File

@@ -10,7 +10,8 @@ namespace SVSim.Bootstrap.Importers;
/// <summary>
/// Imports prod-captured globals from <c>{capturesDir}/{endpoint}-*.json</c> snapshots into the
/// DB via idempotent upserts. Source endpoints: <c>load-index</c>, <c>mypage-index</c>, <c>deck-info</c>.
/// DB via idempotent upserts. Source endpoints: <c>load-index</c>, <c>pack-info</c>. Per-endpoint
/// seed-file importers (DefaultDeckImporter, MyPageGlobalsImporter, etc.) cover the rest.
///
/// Topological order: GameConfiguration extensions → standalone tables → card-referencing tables →
/// rotation CardSet flag update. Card-referencing importers warn on orphans (missing card rows)
@@ -27,7 +28,6 @@ public class GlobalsImporter
Console.WriteLine($"[GlobalsImporter] Loading captures from {capturesDir}...");
JsonElement? loadIndex = LoadCapture(capturesDir, "load-index");
JsonElement? deckInfo = LoadCapture(capturesDir, "deck-info");
JsonElement? packInfo = LoadCapture(capturesDir, "pack-info");
int total = 0;
@@ -50,11 +50,6 @@ public class GlobalsImporter
total += await UpdateRotationCardSetFlags(context, loadIndex.Value);
}
if (deckInfo.HasValue)
{
total += await ImportDefaultDecks(context, deckInfo.Value);
}
if (packInfo.HasValue)
{
total += await ImportPacks(context, packInfo.Value);
@@ -511,45 +506,6 @@ public class GlobalsImporter
return updated;
}
// ---------- Deck/info: Default Decks ----------
private async Task<int> ImportDefaultDecks(SVSimDbContext context, JsonElement deckInfo)
{
if (!deckInfo.TryGetProperty("default_deck_list", out var info) || info.ValueKind != JsonValueKind.Object) return 0;
var existing = await context.DefaultDecks.ToDictionaryAsync(e => e.Id);
var knownSet = new HashSet<long>(await context.Cards.Select(c => c.Id).ToListAsync());
int created = 0, updated = 0, orphans = 0;
foreach (var kv in info.EnumerateObject())
{
if (!int.TryParse(kv.Name, out int deckNo)) continue;
var v = kv.Value;
var entry = existing.TryGetValue(deckNo, out var ex) ? ex : new DefaultDeckEntry { Id = deckNo };
entry.ClassId = GetInt(v, "class_id");
entry.SleeveId = GetLong(v, "sleeve_id");
entry.LeaderSkinId = GetInt(v, "leader_skin_id");
entry.DeckName = GetString(v, "deck_name");
entry.CardIdArray = v.TryGetProperty("card_id_array", out var arr) ? Serialize(arr) : "[]";
// Count orphans against card master
if (arr.ValueKind == JsonValueKind.Array)
{
foreach (var c in arr.EnumerateArray())
{
if (c.ValueKind != JsonValueKind.Number) continue;
if (!knownSet.Contains(c.GetInt64())) orphans++;
}
}
if (ex is null) { context.DefaultDecks.Add(entry); created++; }
else updated++;
}
WarnOrphans("DefaultDecks.card_id_array", orphans);
Console.WriteLine($"[GlobalsImporter] DefaultDecks: +{created}/~{updated}");
return created + updated;
}
// ---------- Pack catalog ----------
/// <summary>