feat(bp-monthly): BattlePassMonthlyMissionImporter, idempotent by (Y, M, order)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SVSim.Bootstrap.Models.Seed;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Models;
|
||||
|
||||
namespace SVSim.Bootstrap.Importers;
|
||||
|
||||
/// <summary>
|
||||
/// Idempotent upsert of BP monthly mission rows from <c>seeds/bp-monthly-missions.json</c>.
|
||||
/// Keyed by (Year, Month, OrderNum). Rows missing from the seed are LEFT INTACT.
|
||||
/// </summary>
|
||||
public class BattlePassMonthlyMissionImporter
|
||||
{
|
||||
public async Task<int> ImportAsync(SVSimDbContext context, string seedDir)
|
||||
{
|
||||
var seed = SeedLoader.LoadList<BattlePassMonthlyMissionSeed>(
|
||||
Path.Combine(seedDir, "bp-monthly-missions.json"));
|
||||
if (seed.Count == 0)
|
||||
{
|
||||
Console.WriteLine("[BattlePassMonthlyMissionImporter] No seed rows; skipping.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var existing = await context.BattlePassMonthlyMissions
|
||||
.ToDictionaryAsync(e => (e.Year, e.Month, e.OrderNum));
|
||||
int created = 0, updated = 0;
|
||||
var unmapped = new List<string>();
|
||||
foreach (var s in seed)
|
||||
{
|
||||
if (s.Year == 0 || s.Month == 0) continue;
|
||||
var key = (s.Year, s.Month, s.OrderNum);
|
||||
var entry = existing.TryGetValue(key, out var ex)
|
||||
? ex
|
||||
: new BattlePassMonthlyMissionEntry
|
||||
{
|
||||
Year = s.Year, Month = s.Month, OrderNum = s.OrderNum,
|
||||
};
|
||||
entry.Name = s.Name;
|
||||
entry.RequireNumber = s.RequireNumber;
|
||||
entry.BattlePassPoint = s.BattlePassPoint;
|
||||
entry.RewardType = s.RewardType;
|
||||
entry.RewardDetailId = s.RewardDetailId;
|
||||
entry.RewardNumber = s.RewardNumber;
|
||||
entry.EventType = s.EventType;
|
||||
entry.EventArg = s.EventArg;
|
||||
if (ex is null) { context.BattlePassMonthlyMissions.Add(entry); existing[key] = entry; created++; }
|
||||
else updated++;
|
||||
if (s.EventType is null) unmapped.Add($"{s.Year}-{s.Month:00}/{s.OrderNum}");
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine($"[BattlePassMonthlyMissionImporter] +{created}/~{updated}");
|
||||
if (unmapped.Count > 0)
|
||||
{
|
||||
Console.WriteLine($"[BattlePassMonthlyMissionImporter] WARN: {unmapped.Count} rows " +
|
||||
$"with no event_type: [{string.Join(", ", unmapped)}] — add name to " +
|
||||
"BP_MONTHLY_EVENT_MAP in data_dumps/extract/extract-bp-monthly-missions.py");
|
||||
}
|
||||
return created + updated;
|
||||
}
|
||||
}
|
||||
18
SVSim.Bootstrap/Models/Seed/BattlePassMonthlyMissionSeed.cs
Normal file
18
SVSim.Bootstrap/Models/Seed/BattlePassMonthlyMissionSeed.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SVSim.Bootstrap.Models.Seed;
|
||||
|
||||
public sealed class BattlePassMonthlyMissionSeed
|
||||
{
|
||||
[JsonPropertyName("year")] public int Year { get; set; }
|
||||
[JsonPropertyName("month")] public int Month { get; set; }
|
||||
[JsonPropertyName("order_num")] public int OrderNum { get; set; }
|
||||
[JsonPropertyName("name")] public string Name { get; set; } = "";
|
||||
[JsonPropertyName("require_number")] public int RequireNumber { get; set; }
|
||||
[JsonPropertyName("battle_pass_point")] public int BattlePassPoint { 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("event_type")] public string? EventType { get; set; }
|
||||
[JsonPropertyName("event_arg")] public int? EventArg { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SVSim.Bootstrap.Importers;
|
||||
using SVSim.Database;
|
||||
using SVSim.UnitTests.Infrastructure;
|
||||
|
||||
namespace SVSim.UnitTests.Importers;
|
||||
|
||||
public class BattlePassMonthlyMissionImporterTests
|
||||
{
|
||||
private static string SeedDir => Path.Combine(AppContext.BaseDirectory, "Data", "seeds");
|
||||
|
||||
[Test]
|
||||
public async Task Imports_may_2026_captured_rows()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
await new BattlePassMonthlyMissionImporter().ImportAsync(db, SeedDir);
|
||||
|
||||
int mayCount = await db.BattlePassMonthlyMissions.CountAsync(r => r.Year == 2026 && r.Month == 5);
|
||||
Assert.That(mayCount, Is.EqualTo(5), "May 2026 captured 5 monthly mission rows");
|
||||
|
||||
var noRewardRow = await db.BattlePassMonthlyMissions
|
||||
.SingleAsync(r => r.Name.StartsWith("Play 5 Challenge"));
|
||||
Assert.That(noRewardRow.RewardType, Is.Null, "Play 5 Challenge has no reward_info on wire");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Multiple_months_coexist()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
db.BattlePassMonthlyMissions.Add(new SVSim.Database.Models.BattlePassMonthlyMissionEntry
|
||||
{
|
||||
Year = 2026, Month = 6, OrderNum = 0,
|
||||
Name = "future placeholder", RequireNumber = 1, BattlePassPoint = 100,
|
||||
EventType = "ranked_or_arena_win",
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await new BattlePassMonthlyMissionImporter().ImportAsync(db, SeedDir);
|
||||
|
||||
int mayCount = await db.BattlePassMonthlyMissions.CountAsync(r => r.Year == 2026 && r.Month == 5);
|
||||
int juneCount = await db.BattlePassMonthlyMissions.CountAsync(r => r.Year == 2026 && r.Month == 6);
|
||||
Assert.That(mayCount, Is.EqualTo(5));
|
||||
Assert.That(juneCount, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Is_idempotent_on_rerun()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
await new BattlePassMonthlyMissionImporter().ImportAsync(db, SeedDir);
|
||||
int before = await db.BattlePassMonthlyMissions.CountAsync();
|
||||
await new BattlePassMonthlyMissionImporter().ImportAsync(db, SeedDir);
|
||||
int after = await db.BattlePassMonthlyMissions.CountAsync();
|
||||
|
||||
Assert.That(after, Is.EqualTo(before));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Empty_seed_is_no_op()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
using var scope = factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
|
||||
string tmp = Path.Combine(Path.GetTempPath(), $"seed-{Guid.NewGuid()}");
|
||||
Directory.CreateDirectory(tmp);
|
||||
try
|
||||
{
|
||||
File.WriteAllText(Path.Combine(tmp, "bp-monthly-missions.json"), "[]");
|
||||
await new BattlePassMonthlyMissionImporter().ImportAsync(db, tmp);
|
||||
int count = await db.BattlePassMonthlyMissions.CountAsync();
|
||||
Assert.That(count, Is.EqualTo(0));
|
||||
}
|
||||
finally { Directory.Delete(tmp, true); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user