This commit is contained in:
gamer147
2026-05-25 12:03:47 -04:00
parent d067f8a64a
commit 558e8288eb
44 changed files with 6512 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;
using SVSim.Database;
using SVSim.Database.Enums;
@@ -32,6 +33,8 @@ public class GlobalsImporter
JsonElement? paymentItemList = LoadCapture(capturesDir, "payment-item-list");
JsonElement? practiceInfo = LoadCapture(capturesDir, "practice-info");
JsonElement? packInfo = LoadCapture(capturesDir, "pack-info");
JsonElement? basicPuzzleInfo = LoadCapture(capturesDir, "basic-puzzle-info");
JsonElement? basicPuzzleMission = LoadCapture(capturesDir, "basic-puzzle-mission");
int total = 0;
@@ -83,6 +86,17 @@ 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;
@@ -929,6 +943,140 @@ 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)