refactor(admin,item-purchase): migrate to IGameCalendarService
Swaps 6 JstPeriod.* callsites across AdminController (mission counter resolution for the /admin catalog) and ItemPurchaseController (monthly reset bucket key + all-time period). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -24,13 +24,15 @@ public class AdminController : SVSimController
|
|||||||
private readonly IViewerRepository _viewerRepository;
|
private readonly IViewerRepository _viewerRepository;
|
||||||
private readonly SVSimDbContext _dbContext;
|
private readonly SVSimDbContext _dbContext;
|
||||||
private readonly ILogger<AdminController> _logger;
|
private readonly ILogger<AdminController> _logger;
|
||||||
|
private readonly IGameCalendarService _calendar;
|
||||||
|
|
||||||
public AdminController(IViewerRepository viewerRepository, SVSimDbContext dbContext,
|
public AdminController(IViewerRepository viewerRepository, SVSimDbContext dbContext,
|
||||||
ILogger<AdminController> logger)
|
ILogger<AdminController> logger, IGameCalendarService calendar)
|
||||||
{
|
{
|
||||||
_viewerRepository = viewerRepository;
|
_viewerRepository = viewerRepository;
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_calendar = calendar;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -610,13 +612,13 @@ public class AdminController : SVSimController
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: unify with MissionAssembler.cs — same logic duplicated here.
|
// TODO: unify with MissionAssembler.cs — same logic duplicated here.
|
||||||
private static (string EventKey, string Period)? ResolveMissionCounter(MissionCatalogEntry catalog, DateTimeOffset nowUtc)
|
private (string EventKey, string Period)? ResolveMissionCounter(MissionCatalogEntry catalog, DateTimeOffset nowUtc)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(catalog.EventType)) return null;
|
if (string.IsNullOrEmpty(catalog.EventType)) return null;
|
||||||
var period = catalog.LotType switch
|
var period = catalog.LotType switch
|
||||||
{
|
{
|
||||||
6 => JstPeriod.DayKey(nowUtc),
|
6 => _calendar.DayKey(nowUtc),
|
||||||
2 => JstPeriod.WeekKey(nowUtc),
|
2 => _calendar.WeekKey(nowUtc),
|
||||||
_ => null
|
_ => null
|
||||||
};
|
};
|
||||||
if (period is null) return null;
|
if (period is null) return null;
|
||||||
@@ -627,7 +629,7 @@ public class AdminController : SVSimController
|
|||||||
private static (string EventKey, string Period)? ResolveAchievementCounter(AchievementCatalogEntry catalog)
|
private static (string EventKey, string Period)? ResolveAchievementCounter(AchievementCatalogEntry catalog)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(catalog.EventType)) return null;
|
if (string.IsNullOrEmpty(catalog.EventType)) return null;
|
||||||
return (catalog.EventType!, JstPeriod.AllTime);
|
return (catalog.EventType!, GameCalendarService.AllTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -24,12 +24,14 @@ public class ItemPurchaseController : SVSimController
|
|||||||
private readonly SVSimDbContext _db;
|
private readonly SVSimDbContext _db;
|
||||||
private readonly IInventoryService _inv;
|
private readonly IInventoryService _inv;
|
||||||
private readonly TimeProvider _time;
|
private readonly TimeProvider _time;
|
||||||
|
private readonly IGameCalendarService _calendar;
|
||||||
|
|
||||||
public ItemPurchaseController(SVSimDbContext db, IInventoryService inv, TimeProvider time)
|
public ItemPurchaseController(SVSimDbContext db, IInventoryService inv, TimeProvider time, IGameCalendarService calendar)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_inv = inv;
|
_inv = inv;
|
||||||
_time = time;
|
_time = time;
|
||||||
|
_calendar = calendar;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("info")]
|
[HttpPost("info")]
|
||||||
@@ -43,7 +45,7 @@ public class ItemPurchaseController : SVSimController
|
|||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
var now = _time.GetUtcNow();
|
var now = _time.GetUtcNow();
|
||||||
var monthKey = JstPeriod.MonthKey(now);
|
var monthKey = _calendar.MonthKey(now);
|
||||||
var keys = catalog.Select(c => CounterKey(c.Id)).ToList();
|
var keys = catalog.Select(c => CounterKey(c.Id)).ToList();
|
||||||
var counters = await _db.ViewerEventCounters
|
var counters = await _db.ViewerEventCounters
|
||||||
.Where(c => c.ViewerId == viewerId && keys.Contains(c.EventKey))
|
.Where(c => c.ViewerId == viewerId && keys.Contains(c.EventKey))
|
||||||
@@ -104,7 +106,7 @@ public class ItemPurchaseController : SVSimController
|
|||||||
return BadRequest(new { error = "unknown_purchase" });
|
return BadRequest(new { error = "unknown_purchase" });
|
||||||
|
|
||||||
var now = _time.GetUtcNow();
|
var now = _time.GetUtcNow();
|
||||||
var period = entry.IsMonthlyReset ? JstPeriod.MonthKey(now) : JstPeriod.AllTime;
|
var period = entry.IsMonthlyReset ? _calendar.MonthKey(now) : GameCalendarService.AllTime;
|
||||||
var key = CounterKey(entry.Id);
|
var key = CounterKey(entry.Id);
|
||||||
|
|
||||||
var counter = await _db.ViewerEventCounters
|
var counter = await _db.ViewerEventCounters
|
||||||
@@ -162,7 +164,7 @@ public class ItemPurchaseController : SVSimController
|
|||||||
|
|
||||||
private static int CounterCount(List<ViewerEventCounter> counters, ItemPurchaseCatalogEntry entry, string monthKey)
|
private static int CounterCount(List<ViewerEventCounter> counters, ItemPurchaseCatalogEntry entry, string monthKey)
|
||||||
{
|
{
|
||||||
var period = entry.IsMonthlyReset ? monthKey : JstPeriod.AllTime;
|
var period = entry.IsMonthlyReset ? monthKey : GameCalendarService.AllTime;
|
||||||
return counters.FirstOrDefault(c => c.EventKey == CounterKey(entry.Id) && c.Period == period)?.Count ?? 0;
|
return counters.FirstOrDefault(c => c.EventKey == CounterKey(entry.Id) && c.Period == period)?.Count ?? 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user