feat(missions): MissionCatalogImporter, idempotent upsert by id
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
57
SVSim.Bootstrap/Importers/MissionCatalogImporter.cs
Normal file
57
SVSim.Bootstrap/Importers/MissionCatalogImporter.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SVSim.Bootstrap.Models.Seed;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Models;
|
||||
|
||||
namespace SVSim.Bootstrap.Importers;
|
||||
|
||||
/// <summary>
|
||||
/// Idempotent upsert of mission catalog rows from <c>seeds/mission-catalog.json</c>.
|
||||
/// Rows missing from the seed are LEFT INTACT (so hand-added catalog rows survive).
|
||||
/// </summary>
|
||||
public class MissionCatalogImporter
|
||||
{
|
||||
public async Task<int> ImportAsync(SVSimDbContext context, string seedDir)
|
||||
{
|
||||
var seed = SeedLoader.LoadList<MissionCatalogSeed>(Path.Combine(seedDir, "mission-catalog.json"));
|
||||
if (seed.Count == 0)
|
||||
{
|
||||
Console.WriteLine("[MissionCatalogImporter] No seed rows; skipping.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var existing = await context.MissionCatalog.ToDictionaryAsync(e => e.Id);
|
||||
int created = 0, updated = 0;
|
||||
var unmapped = new List<int>();
|
||||
foreach (var s in seed)
|
||||
{
|
||||
if (s.Id == 0) continue;
|
||||
var entry = existing.TryGetValue(s.Id, out var ex) ? ex : new MissionCatalogEntry { Id = s.Id };
|
||||
entry.Name = s.Name;
|
||||
entry.LotType = s.LotType;
|
||||
entry.RequireNumber = s.RequireNumber;
|
||||
entry.RewardType = s.RewardType;
|
||||
entry.RewardDetailId = s.RewardDetailId;
|
||||
entry.RewardNumber = s.RewardNumber;
|
||||
entry.BattlePassPoint = s.BattlePassPoint;
|
||||
entry.DefaultFlag = s.DefaultFlag;
|
||||
entry.EventType = s.EventType;
|
||||
entry.EventArg = s.EventArg;
|
||||
entry.StartTime = s.StartTime;
|
||||
entry.EndTime = s.EndTime;
|
||||
if (ex is null) { context.MissionCatalog.Add(entry); existing[s.Id] = entry; created++; }
|
||||
else updated++;
|
||||
if (s.EventType is null) unmapped.Add(s.Id);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine($"[MissionCatalogImporter] +{created}/~{updated}");
|
||||
if (unmapped.Count > 0)
|
||||
{
|
||||
Console.WriteLine($"[MissionCatalogImporter] WARN: {unmapped.Count} mission_ids with " +
|
||||
$"no event_type: [{string.Join(", ", unmapped)}] — add to MISSION_EVENT_MAP " +
|
||||
"in data_dumps/extract/extract-missions.py and re-run the extractor");
|
||||
}
|
||||
return created + updated;
|
||||
}
|
||||
}
|
||||
21
SVSim.Bootstrap/Models/Seed/MissionCatalogSeed.cs
Normal file
21
SVSim.Bootstrap/Models/Seed/MissionCatalogSeed.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SVSim.Bootstrap.Models.Seed;
|
||||
|
||||
/// <summary>Mirrors a single entry in <c>seeds/mission-catalog.json</c>.</summary>
|
||||
public sealed class MissionCatalogSeed
|
||||
{
|
||||
[JsonPropertyName("id")] public int Id { get; set; }
|
||||
[JsonPropertyName("name")] public string Name { get; set; } = "";
|
||||
[JsonPropertyName("lot_type")] public int LotType { get; set; }
|
||||
[JsonPropertyName("require_number")] public int RequireNumber { get; set; }
|
||||
[JsonPropertyName("reward_type")] public int RewardType { get; set; }
|
||||
[JsonPropertyName("reward_detail_id")] public long RewardDetailId { get; set; }
|
||||
[JsonPropertyName("reward_number")] public int RewardNumber { get; set; }
|
||||
[JsonPropertyName("battle_pass_point")] public int BattlePassPoint { get; set; }
|
||||
[JsonPropertyName("default_flag")] public bool DefaultFlag { get; set; }
|
||||
[JsonPropertyName("event_type")] public string? EventType { get; set; }
|
||||
[JsonPropertyName("event_arg")] public int? EventArg { get; set; }
|
||||
[JsonPropertyName("start_time")] public long StartTime { get; set; }
|
||||
[JsonPropertyName("end_time")] public long? EndTime { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user