feat(S9): persist simulation statistics and logs to SQLite, retire zip archive

This commit is contained in:
2026-11-10 01:44:27 +08:00
parent c69fa53d37
commit e838c9566f
13 changed files with 500 additions and 23 deletions
+17 -11
View File
@@ -84,6 +84,7 @@ namespace OshimaBot.Game.Services
public static async Task<List<string>> StartSimulationGame(SimulationOptions options, SimulationRunContext context)
{
ArgumentNullException.ThrowIfNull(context);
context.ArchiveRounds = options.ArchiveRounds;
using IDisposable scope = BeginRun(context);
return await StartSimulationGameCore(options).ConfigureAwait(false);
}
@@ -701,18 +702,23 @@ namespace OshimaBot.Game.Services
}
}
string zipFileName = "rounds_archive.zip";
try
// S9:默认不再无条件落盘 zip;结果由 simulation_runs 表承载,
// 只有显式开启 ArchiveRounds(复盘 / 平衡测试)才生成 rounds_archive.zip。
if (Current.ArchiveRounds)
{
WriteRoundsToZip(actionQueue.Rounds.ToDictionary(kv => kv.Round, kv => kv), zipFileName);
}
catch (Exception ex)
{
// 档案落盘失败(IO/权限/磁盘满等)不应连带丢弃整局模拟结果
string msg = $"回合记录写入失败({zipFileName}):{ex.Message}";
Console.WriteLine(ex);
result.Add(msg);
lastRecord.Add(msg);
string zipFileName = "rounds_archive.zip";
try
{
WriteRoundsToZip(actionQueue.Rounds.ToDictionary(kv => kv.Round, kv => kv), zipFileName);
}
catch (Exception ex)
{
// 档案落盘失败(IO/权限/磁盘满等)不应连带丢弃整局模拟结果
string msg = $"回合记录写入失败({zipFileName}):{ex.Message}";
Console.WriteLine(ex);
result.Add(msg);
lastRecord.Add(msg);
}
}
LastRecord = result;
@@ -32,6 +32,12 @@ namespace OshimaBot.Game.Services
/// <summary>调试模式(输出更多运行细节)</summary>
public bool IsDebug { get; set; }
/// <summary>
/// S9:是否把回合记录写入 rounds_archive.zip。默认 false —— 归档改由
/// simulation_runs 表承担,只有显式开启(复盘/平衡测试)才落盘。
/// </summary>
public bool ArchiveRounds { get; set; }
/// <summary>
/// 是否启用「角色绑定的回合奖励」(对应 <c>InitRoundRewards</c> 的 bindToCharacter)。<para/>
/// <b>默认 true</b>:不启用时【命运XX】系技能(抢夺 / 剥夺 / 馈赠)与【强运】会全部静默失效。
@@ -22,6 +22,9 @@ public sealed class SimulationRunContext
public Random Random { get; set; } = new();
public GameMap Map { get; set; } = new FastAutoMap();
/// <summary>S9:本局是否额外写 rounds_archive.zip(默认关闭,结果统一入库)。</summary>
public bool ArchiveRounds { get; set; }
/// <summary>绑定到本实例的日志回调:不依赖 AsyncLocal,跨线程回调也不会串局。</summary>
public void WriteLine(string text)
{
@@ -18,6 +18,12 @@ public interface ISimulationStatisticsStore
/// <summary>在读锁内执行读取,避免与正在写入的模拟互相干扰。</summary>
T Read<T>(Func<ISimulationStatisticsStore, T> reader);
/// <summary>
/// 一局模拟结束后把统计与最近日志持久化。内存实现为空操作;
/// S9 的 EF Core 实现写 simulation_statistics,重启后可恢复。
/// </summary>
Task PersistAsync(CancellationToken cancellationToken = default);
}
public sealed class InMemorySimulationStatisticsStore : ISimulationStatisticsStore
@@ -49,4 +55,6 @@ public sealed class InMemorySimulationStatisticsStore : ISimulationStatisticsSto
{
lock (_sync) return reader(this);
}
public Task PersistAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
}