using FunGame.Core.Entity; namespace OshimaBot.Game.Services; /// /// 跨运行共享的统计与最近日志。S8 前置把读写从 FunGameSimulation 的静态字典收敛到仓储接口; /// S9 可替换为 EF Core(simulation_statistics / simulation_runs)实现而无需改动模拟器。 /// public interface ISimulationStatisticsStore { Dictionary CharacterStatistics { get; } Dictionary TeamCharacterStatistics { get; } IReadOnlyList LastRecord { get; set; } IReadOnlyList LastFullRecord { get; set; } /// 按当前内容目录重建统计行(进程启动或目录热重载时调用)。 void Reset(IEnumerable characters); /// 在读锁内执行读取,避免与正在写入的模拟互相干扰。 T Read(Func reader); /// /// 一局模拟结束后把统计与最近日志持久化。内存实现为空操作; /// S9 的 EF Core 实现写 simulation_statistics,重启后可恢复。 /// Task PersistAsync(CancellationToken cancellationToken = default); } public sealed class InMemorySimulationStatisticsStore : ISimulationStatisticsStore { private readonly Lock _sync = new(); public Dictionary CharacterStatistics { get; } = []; public Dictionary TeamCharacterStatistics { get; } = []; public IReadOnlyList LastRecord { get; set; } = []; public IReadOnlyList LastFullRecord { get; set; } = []; public void Reset(IEnumerable characters) { lock (_sync) { CharacterStatistics.Clear(); TeamCharacterStatistics.Clear(); LastRecord = []; LastFullRecord = []; foreach (Character character in characters) { CharacterStatistics.Add(character, new()); TeamCharacterStatistics.Add(character, new()); } } } public T Read(Func reader) { lock (_sync) return reader(this); } public Task PersistAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; }