feat(missions): MissionProgressService — counter upsert + achievement claimable on threshold
Also wires IMissionCatalogRepository + IViewerMissionRepository + IMissionProgressService into DI. Task 17's separate DI step is now subsumed by these registrations. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -88,6 +88,11 @@ public class Program
|
||||
builder.Services.AddScoped<SVSim.Database.Repositories.BattlePass.IViewerBattlePassRepository,
|
||||
SVSim.Database.Repositories.BattlePass.ViewerBattlePassRepository>();
|
||||
builder.Services.AddScoped<IBattlePassService, BattlePassService>();
|
||||
builder.Services.AddScoped<SVSim.Database.Repositories.Mission.IMissionCatalogRepository,
|
||||
SVSim.Database.Repositories.Mission.MissionCatalogRepository>();
|
||||
builder.Services.AddScoped<SVSim.Database.Repositories.Mission.IViewerMissionRepository,
|
||||
SVSim.Database.Repositories.Mission.ViewerMissionRepository>();
|
||||
builder.Services.AddScoped<IMissionProgressService, MissionProgressService>();
|
||||
builder.Services.AddSingleton<TimeProvider>(TimeProvider.System);
|
||||
builder.Services.AddScoped<IStoryMasterRepository, StoryMasterRepository>();
|
||||
builder.Services.AddScoped<IViewerStoryProgressRepository, ViewerStoryProgressRepository>();
|
||||
|
||||
17
SVSim.EmulatedEntrypoint/Services/IMissionProgressService.cs
Normal file
17
SVSim.EmulatedEntrypoint/Services/IMissionProgressService.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace SVSim.EmulatedEntrypoint.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Single primitive for "something happened that mission/achievement progress should react to."
|
||||
/// Emitters at battle/story finish call this with the FULL list of event keys their event matches
|
||||
/// at all granularity levels (broad → narrow). Service does dumb exact-match against catalog.
|
||||
/// </summary>
|
||||
public interface IMissionProgressService
|
||||
{
|
||||
/// <param name="viewerId">Viewer the event applies to.</param>
|
||||
/// <param name="eventKeys">Broad-to-narrow keys. Example: a swordcraft ranked win
|
||||
/// passes ["ranked_win", "ranked_win:swordcraft"]. The order doesn't matter for counter
|
||||
/// increments (each key gets its own counter) but the order is the conventional one used
|
||||
/// by emitters for readability.</param>
|
||||
/// <param name="delta">Count delta per key (default 1).</param>
|
||||
Task RecordEventAsync(long viewerId, IReadOnlyList<string> eventKeys, int delta = 1, CancellationToken ct = default);
|
||||
}
|
||||
62
SVSim.EmulatedEntrypoint/Services/MissionProgressService.cs
Normal file
62
SVSim.EmulatedEntrypoint/Services/MissionProgressService.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Repositories.Mission;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Services;
|
||||
|
||||
public sealed class MissionProgressService : IMissionProgressService
|
||||
{
|
||||
private readonly SVSimDbContext _db;
|
||||
private readonly IMissionCatalogRepository _catalog;
|
||||
private readonly IViewerMissionRepository _viewerRepo;
|
||||
private readonly TimeProvider _time;
|
||||
|
||||
public MissionProgressService(
|
||||
SVSimDbContext db,
|
||||
IMissionCatalogRepository catalog,
|
||||
IViewerMissionRepository viewerRepo,
|
||||
TimeProvider time)
|
||||
{
|
||||
_db = db;
|
||||
_catalog = catalog;
|
||||
_viewerRepo = viewerRepo;
|
||||
_time = time;
|
||||
}
|
||||
|
||||
public async Task RecordEventAsync(long viewerId, IReadOnlyList<string> eventKeys, int delta = 1, CancellationToken ct = default)
|
||||
{
|
||||
if (eventKeys.Count == 0) return;
|
||||
var now = _time.GetUtcNow();
|
||||
var periods = JstPeriod.AllPeriods(now);
|
||||
|
||||
// 1. Increment counters for every (key, period).
|
||||
foreach (var key in eventKeys)
|
||||
{
|
||||
foreach (var period in periods)
|
||||
{
|
||||
await _viewerRepo.UpsertCounterAsync(viewerId, key, period, delta, ct);
|
||||
}
|
||||
}
|
||||
await _db.SaveChangesAsync(ct);
|
||||
|
||||
// 2. Find catalog rows referencing any of these event keys; mark claimable on threshold.
|
||||
var achievements = await _catalog.GetAchievementsByEventTypesAsync(eventKeys, ct);
|
||||
if (achievements.Count > 0)
|
||||
{
|
||||
var byType = achievements.GroupBy(a => a.AchievementType).ToDictionary(g => g.Key, g => g.ToList());
|
||||
foreach (var (achType, catalogRows) in byType)
|
||||
{
|
||||
var viewerRow = await _viewerRepo.GetAchievementAsync(viewerId, achType, ct);
|
||||
if (viewerRow is null) continue;
|
||||
var atLevel = catalogRows.FirstOrDefault(r => r.Level == viewerRow.Level);
|
||||
if (atLevel is null || atLevel.EventType is null) continue;
|
||||
|
||||
var count = await _viewerRepo.GetCounterAsync(viewerId, atLevel.EventType, JstPeriod.AllTime, ct);
|
||||
if (count >= atLevel.RequireNumber && viewerRow.AchievementStatus == 0)
|
||||
{
|
||||
viewerRow.AchievementStatus = 1;
|
||||
}
|
||||
}
|
||||
await _db.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user