refactor(bootstrap): migrate basic puzzles to seed files
Replaces GlobalsImporter's ImportPuzzleGroups/Puzzles/Missions methods (plus the DeriveTargetPuzzleGroupId regex helper) with a dedicated PuzzleImporter that reads three flat seed JSONs (puzzle-groups, puzzles, puzzle-missions) produced by the Python extractor. Groups run before puzzles to satisfy the FK; missions upsert by sequential id. Wired into Program.cs and SVSimTestFactory after PaymentItemImporter so existing GlobalsImporterPuzzleTests continue to pass unchanged via SeedGlobalsAsync. The original prod-capture JSONs are deleted now that the seeds are authoritative. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Enums;
|
||||
@@ -31,8 +30,6 @@ public class GlobalsImporter
|
||||
JsonElement? mypageIndex = LoadCapture(capturesDir, "mypage-index");
|
||||
JsonElement? deckInfo = LoadCapture(capturesDir, "deck-info");
|
||||
JsonElement? packInfo = LoadCapture(capturesDir, "pack-info");
|
||||
JsonElement? basicPuzzleInfo = LoadCapture(capturesDir, "basic-puzzle-info");
|
||||
JsonElement? basicPuzzleMission = LoadCapture(capturesDir, "basic-puzzle-mission");
|
||||
|
||||
int total = 0;
|
||||
|
||||
@@ -73,17 +70,6 @@ public class GlobalsImporter
|
||||
total += await ImportPacks(context, packInfo.Value);
|
||||
}
|
||||
|
||||
if (basicPuzzleInfo.HasValue)
|
||||
{
|
||||
total += await ImportPuzzleGroups(context, basicPuzzleInfo.Value);
|
||||
total += await ImportPuzzles(context, basicPuzzleInfo.Value);
|
||||
}
|
||||
|
||||
if (basicPuzzleMission.HasValue)
|
||||
{
|
||||
total += await ImportPuzzleMissions(context, basicPuzzleMission.Value);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine($"[GlobalsImporter] Done: {total} total rows changed.");
|
||||
return total;
|
||||
@@ -819,140 +805,6 @@ public class GlobalsImporter
|
||||
return created + updated;
|
||||
}
|
||||
|
||||
// ---------- Basic Puzzle Groups + Puzzles ----------
|
||||
|
||||
/// <summary>
|
||||
/// /basic_puzzle/info capture is an array of group objects keyed on puzzle_master_id.
|
||||
/// Numeric wire fields come through as strings — GetInt tolerates both. Idempotent upsert
|
||||
/// by puzzle_master_id; rows missing from a partial capture are left intact.
|
||||
/// </summary>
|
||||
private async Task<int> ImportPuzzleGroups(SVSimDbContext context, JsonElement infoData)
|
||||
{
|
||||
if (infoData.ValueKind != JsonValueKind.Array) return 0;
|
||||
|
||||
var existing = await context.PuzzleGroups.ToDictionaryAsync(e => e.Id);
|
||||
int created = 0, updated = 0;
|
||||
|
||||
foreach (var row in infoData.EnumerateArray())
|
||||
{
|
||||
int masterId = GetInt(row, "puzzle_master_id");
|
||||
if (masterId == 0) continue;
|
||||
|
||||
var entry = existing.TryGetValue(masterId, out var ex) ? ex : new PuzzleGroupEntry { Id = masterId };
|
||||
entry.BasicTitleTextId = GetString(row, "basic_title_text_id");
|
||||
entry.PuzzleCharaId = GetInt(row, "puzzle_chara_id");
|
||||
entry.CharaId = GetInt(row, "chara_id");
|
||||
entry.SortType = GetInt(row, "sort_type");
|
||||
entry.DifficultyNameListJson = row.TryGetProperty("puzzle_difficulty_name_list", out var d)
|
||||
? Serialize(d)
|
||||
: "{}";
|
||||
|
||||
if (ex is null) { context.PuzzleGroups.Add(entry); created++; }
|
||||
else updated++;
|
||||
}
|
||||
|
||||
Console.WriteLine($"[GlobalsImporter] PuzzleGroups: +{created}/~{updated}");
|
||||
return created + updated;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Walks each group's puzzle_data array and upserts PuzzleEntry rows keyed on puzzle_id.
|
||||
/// Groups must have been imported first (FK PuzzleEntry.GroupId → PuzzleGroupEntry.Id).
|
||||
/// </summary>
|
||||
private async Task<int> ImportPuzzles(SVSimDbContext context, JsonElement infoData)
|
||||
{
|
||||
if (infoData.ValueKind != JsonValueKind.Array) return 0;
|
||||
|
||||
var existing = await context.Puzzles.ToDictionaryAsync(e => e.Id);
|
||||
int created = 0, updated = 0;
|
||||
|
||||
foreach (var group in infoData.EnumerateArray())
|
||||
{
|
||||
int masterId = GetInt(group, "puzzle_master_id");
|
||||
if (masterId == 0 || !group.TryGetProperty("puzzle_data", out var puzzleArray)) continue;
|
||||
if (puzzleArray.ValueKind != JsonValueKind.Array) continue;
|
||||
|
||||
foreach (var p in puzzleArray.EnumerateArray())
|
||||
{
|
||||
int puzzleId = GetInt(p, "puzzle_id");
|
||||
if (puzzleId == 0) continue;
|
||||
|
||||
var entry = existing.TryGetValue(puzzleId, out var ex) ? ex : new PuzzleEntry { Id = puzzleId };
|
||||
entry.GroupId = masterId;
|
||||
entry.PuzzleDifficulty = GetInt(p, "puzzle_difficulty");
|
||||
entry.IsAdditional = GetBool(p, "is_additional");
|
||||
entry.IsPlayable = GetBool(p, "is_playable");
|
||||
entry.ReleaseConditionTextId = GetString(p, "release_condition_text_id");
|
||||
|
||||
if (ex is null) { context.Puzzles.Add(entry); created++; }
|
||||
else updated++;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"[GlobalsImporter] Puzzles: +{created}/~{updated}");
|
||||
return created + updated;
|
||||
}
|
||||
|
||||
// ---------- Basic Puzzle Missions ----------
|
||||
|
||||
private static readonly Regex RoundMissionPattern =
|
||||
new(@"^Clear all Round (\d+) puzzles$", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>Maps the captured mission_name to its target puzzle_master_id. Returns null for
|
||||
/// Special-Round entries — Phase 1 surfaces them with total_count=0 (see design § Out of Scope).</summary>
|
||||
internal static int? DeriveTargetPuzzleGroupId(string missionName)
|
||||
{
|
||||
var m = RoundMissionPattern.Match(missionName);
|
||||
return m.Success ? 300 + int.Parse(m.Groups[1].Value) : null;
|
||||
}
|
||||
|
||||
private async Task<int> ImportPuzzleMissions(SVSimDbContext context, JsonElement missionData)
|
||||
{
|
||||
if (missionData.ValueKind != JsonValueKind.Array) return 0;
|
||||
|
||||
// Key by 1-based sequence (the wire has no stable id); first run inserts, re-runs match by index.
|
||||
var existing = await context.PuzzleMissions.ToDictionaryAsync(e => e.Id);
|
||||
int created = 0, updated = 0, unmapped = 0;
|
||||
|
||||
int seq = 1;
|
||||
foreach (var row in missionData.EnumerateArray())
|
||||
{
|
||||
string name = GetString(row, "mission_name");
|
||||
if (string.IsNullOrEmpty(name)) { seq++; continue; }
|
||||
|
||||
var entry = existing.TryGetValue(seq, out var ex) ? ex : new PuzzleMissionEntry { Id = seq };
|
||||
entry.MissionName = name;
|
||||
entry.AchievedMessage = RoundMissionPattern.IsMatch(name)
|
||||
? RoundMissionPattern.Replace(name, m => $"Cleared all Round {m.Groups[1].Value} puzzles")
|
||||
: "Mission achieved"; // Special-Round fallback; only surfaces if a Special mission ever flips, which won't in Phase 1.
|
||||
entry.RequireNumber = GetInt(row, "require_number");
|
||||
entry.CampaignCommenceTime = GetLong(row, "campaign_commence_time");
|
||||
entry.OrderId = GetInt(row, "order_id");
|
||||
|
||||
// reward_list[0] — single reward per mission. Skip if missing/empty.
|
||||
if (row.TryGetProperty("reward_list", out var rl) && rl.ValueKind == JsonValueKind.Array && rl.GetArrayLength() > 0)
|
||||
{
|
||||
var r = rl[0];
|
||||
entry.RewardType = GetInt(r, "reward_type");
|
||||
entry.RewardDetailId = GetLong(r, "reward_detail_id");
|
||||
entry.RewardNumber = GetInt(r, "reward_number");
|
||||
}
|
||||
|
||||
entry.TargetPuzzleGroupId = DeriveTargetPuzzleGroupId(name);
|
||||
if (entry.TargetPuzzleGroupId is null) unmapped++;
|
||||
|
||||
if (ex is null) { context.PuzzleMissions.Add(entry); created++; }
|
||||
else updated++;
|
||||
|
||||
seq++;
|
||||
}
|
||||
|
||||
if (unmapped > 0)
|
||||
Console.WriteLine($"[GlobalsImporter] PuzzleMissions: {unmapped} Special-Round missions left unmapped (Phase 1 deferral).");
|
||||
Console.WriteLine($"[GlobalsImporter] PuzzleMissions: +{created}/~{updated}");
|
||||
return created + updated;
|
||||
}
|
||||
|
||||
// ---------- Helpers ----------
|
||||
|
||||
private static void WarnOrphans(string label, int count)
|
||||
|
||||
142
SVSim.Bootstrap/Importers/PuzzleImporter.cs
Normal file
142
SVSim.Bootstrap/Importers/PuzzleImporter.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
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 the basic-puzzle catalog from <c>seeds/puzzle-groups.json</c>,
|
||||
/// <c>seeds/puzzles.json</c>, and <c>seeds/puzzle-missions.json</c>. Groups must be imported
|
||||
/// before puzzles (FK on <see cref="PuzzleEntry.GroupId"/> -> <see cref="PuzzleGroupEntry.Id"/>).
|
||||
/// Rows missing from the seed are LEFT INTACT (consistent with other per-importer seeds).
|
||||
/// </summary>
|
||||
public class PuzzleImporter
|
||||
{
|
||||
public async Task<int> ImportGroupsAsync(SVSimDbContext context, string seedDir)
|
||||
{
|
||||
string path = Path.Combine(seedDir, "puzzle-groups.json");
|
||||
var seed = SeedLoader.LoadList<PuzzleGroupSeed>(path);
|
||||
if (seed.Count == 0)
|
||||
{
|
||||
Console.WriteLine("[PuzzleImporter] No group seed rows; skipping.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var existing = await context.PuzzleGroups.ToDictionaryAsync(e => e.Id);
|
||||
int created = 0, updated = 0;
|
||||
|
||||
foreach (var s in seed)
|
||||
{
|
||||
if (s.Id == 0) continue;
|
||||
|
||||
var entry = existing.TryGetValue(s.Id, out var ex)
|
||||
? ex : new PuzzleGroupEntry { Id = s.Id };
|
||||
|
||||
entry.BasicTitleTextId = s.BasicTitleTextId;
|
||||
entry.PuzzleCharaId = s.PuzzleCharaId;
|
||||
entry.CharaId = s.CharaId;
|
||||
entry.SortType = s.SortType;
|
||||
entry.DifficultyNameListJson = s.DifficultyNameList.ValueKind == JsonValueKind.Undefined
|
||||
? "{}"
|
||||
: JsonSerializer.Serialize(s.DifficultyNameList);
|
||||
|
||||
if (ex is null)
|
||||
{
|
||||
context.PuzzleGroups.Add(entry);
|
||||
existing[s.Id] = entry;
|
||||
created++;
|
||||
}
|
||||
else updated++;
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine($"[PuzzleImporter] Groups +{created}/~{updated}");
|
||||
return created + updated;
|
||||
}
|
||||
|
||||
public async Task<int> ImportPuzzlesAsync(SVSimDbContext context, string seedDir)
|
||||
{
|
||||
string path = Path.Combine(seedDir, "puzzles.json");
|
||||
var seed = SeedLoader.LoadList<PuzzleSeed>(path);
|
||||
if (seed.Count == 0)
|
||||
{
|
||||
Console.WriteLine("[PuzzleImporter] No puzzle seed rows; skipping.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var existing = await context.Puzzles.ToDictionaryAsync(e => e.Id);
|
||||
int created = 0, updated = 0;
|
||||
|
||||
foreach (var s in seed)
|
||||
{
|
||||
if (s.Id == 0) continue;
|
||||
|
||||
var entry = existing.TryGetValue(s.Id, out var ex)
|
||||
? ex : new PuzzleEntry { Id = s.Id };
|
||||
|
||||
entry.GroupId = s.GroupId;
|
||||
entry.PuzzleDifficulty = s.PuzzleDifficulty;
|
||||
entry.IsAdditional = s.IsAdditional;
|
||||
entry.IsPlayable = s.IsPlayable;
|
||||
entry.ReleaseConditionTextId = s.ReleaseConditionTextId;
|
||||
|
||||
if (ex is null)
|
||||
{
|
||||
context.Puzzles.Add(entry);
|
||||
existing[s.Id] = entry;
|
||||
created++;
|
||||
}
|
||||
else updated++;
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine($"[PuzzleImporter] Puzzles +{created}/~{updated}");
|
||||
return created + updated;
|
||||
}
|
||||
|
||||
public async Task<int> ImportMissionsAsync(SVSimDbContext context, string seedDir)
|
||||
{
|
||||
string path = Path.Combine(seedDir, "puzzle-missions.json");
|
||||
var seed = SeedLoader.LoadList<PuzzleMissionSeed>(path);
|
||||
if (seed.Count == 0)
|
||||
{
|
||||
Console.WriteLine("[PuzzleImporter] No mission seed rows; skipping.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var existing = await context.PuzzleMissions.ToDictionaryAsync(e => e.Id);
|
||||
int created = 0, updated = 0;
|
||||
|
||||
foreach (var s in seed)
|
||||
{
|
||||
if (s.Id == 0) continue;
|
||||
|
||||
var entry = existing.TryGetValue(s.Id, out var ex)
|
||||
? ex : new PuzzleMissionEntry { Id = s.Id };
|
||||
|
||||
entry.MissionName = s.MissionName;
|
||||
entry.AchievedMessage = s.AchievedMessage;
|
||||
entry.RequireNumber = s.RequireNumber;
|
||||
entry.CampaignCommenceTime = s.CampaignCommenceTime;
|
||||
entry.OrderId = s.OrderId;
|
||||
entry.RewardType = s.RewardType;
|
||||
entry.RewardDetailId = s.RewardDetailId;
|
||||
entry.RewardNumber = s.RewardNumber;
|
||||
entry.TargetPuzzleGroupId = s.TargetPuzzleGroupId;
|
||||
|
||||
if (ex is null)
|
||||
{
|
||||
context.PuzzleMissions.Add(entry);
|
||||
existing[s.Id] = entry;
|
||||
created++;
|
||||
}
|
||||
else updated++;
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine($"[PuzzleImporter] Missions +{created}/~{updated}");
|
||||
return created + updated;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user