first commit

This commit is contained in:
2026-08-28 19:44:46 +08:00
commit d99ccbbb0c
114 changed files with 16246 additions and 0 deletions
@@ -0,0 +1,200 @@
using FunGame.Core.Entity;
using FunGame.Core.Model.Framework;
using FunGame.Server.Contracts.Constants;
using FunGame.Server.Contracts.Payloads;
using FunGame.Server.Host.Auth;
using FunGame.Server.Host.Services;
using FunGame.Server.Infrastructure.Entities.Game;
using Microsoft.AspNetCore.Mvc;
namespace FunGame.Server.Host.Controllers;
/// <summary>
/// 回放中心:对局记录、回合摘要、单回合详情、最终统计<para/>
/// 数据来源:GameRoundRecordSink 归档(records/*.zip)+ game_records / round_records 表
/// </summary>
[Route("api/games")]
[Permission(PermissionCodes.LogsRead)]
public class ReplayController(ReplayArchiveService replay) : ControllerBase
{
/// <summary>GET /api/games/{id}/meta —— 对局元信息(含全角色清单与队伍)</summary>
[HttpGet("{id:guid}/meta")]
public async Task<IActionResult> Meta(Guid id)
{
GameRecord? record = await replay.GetGameRecordAsync(id);
if (record == null)
{
return NotFound(ApiResponse.Fail(404, $"对局 {id} 不存在。"));
}
Dictionary<int, RoundRecord>? rounds = replay.ReadRounds(record);
if (rounds == null || rounds.Count == 0)
{
return NotFound(ApiResponse.Fail(404, "该对局没有回合记录(可能仍在进行中或归档失败)。"));
}
RoundRecord last = rounds.Values.OrderBy(r => r.Round).Last();
bool isTeam = last.GameResult.Any(e => e.IsTeam);
List<Character> characters = rounds.Values
.SelectMany(r => r.AllCharacters)
.GroupBy(c => c.Guid)
.Select(g => g.First())
.ToList();
return Ok(ApiResponse.Success(new
{
record.Id,
record.Mode,
record.State,
record.StartedAt,
record.EndedAt,
record.TotalRounds,
record.Winner,
roundCount = rounds.Count,
totalTime = last.TotalTime,
modeType = isTeam ? "团队" : "混战",
characters = characters.Select(c => new { guid = c.Guid, c.Name, c.NickName, c.Level }).ToList(),
teams = last.GameResult.Where(e => e.IsTeam && e.Team != null).Select(e => new { e.Team!.Name, e.Team.Score, e.Team.IsWinner }).ToList()
}));
}
/// <summary>GET /api/games/{id}/rounds/summary?from=&to= —— 回合摘要列表(供时间轴)</summary>
[HttpGet("{id:guid}/rounds/summary")]
public async Task<IActionResult> Summary(Guid id, [FromQuery] int? from, [FromQuery] int? to)
{
GameRecord? record = await replay.GetGameRecordAsync(id);
if (record == null)
{
return NotFound(ApiResponse.Fail(404, $"对局 {id} 不存在。"));
}
Dictionary<int, RoundRecord>? rounds = replay.ReadRounds(record);
if (rounds == null || rounds.Count == 0)
{
return NotFound(ApiResponse.Fail(404, "该对局没有回合记录。"));
}
// 回合号不连续(行动顺序驱动),按真实回合号排序后取区间
int[] keys = rounds.Keys.OrderBy(k => k).ToArray();
int fromN = Math.Clamp(from ?? 1, keys[0], keys[^1]);
int toN = Math.Clamp(to ?? keys[^1], fromN, keys[^1]);
List<object> list = [];
foreach (int n in keys.Where(k => k >= fromN && k <= toN))
{
RoundRecord r = rounds[n];
list.Add(new
{
r.Round,
actorGuid = r.Actor?.Guid.ToString() ?? "",
actorName = r.Actor?.NickName ?? r.Actor?.Name ?? "",
r.HasKill,
damageTotal = Math.Round(r.Damages.Values.Sum(), 2),
healTotal = Math.Round(r.Heals.Values.Sum(), 2),
actionCount = r.Actions.Count,
effectCount = r.Effects.Count + r.ApplyEffects.Count,
hasCheckpoint = r.Checkpoint is { Count: > 0 },
r.TotalTime
});
}
return Ok(ApiResponse.Success(list));
}
/// <summary>GET /api/games/{id}/rounds/{n} —— 单回合完整数据(与归档一致的 JSON)</summary>
[HttpGet("{id:guid}/rounds/{n:int}")]
public async Task<IActionResult> Round(Guid id, int n)
{
GameRecord? record = await replay.GetGameRecordAsync(id);
if (record == null)
{
return NotFound(ApiResponse.Fail(404, $"对局 {id} 不存在。"));
}
Dictionary<int, RoundRecord>? rounds = replay.ReadRounds(record);
if (rounds == null || !rounds.TryGetValue(n, out RoundRecord? round))
{
return NotFound(ApiResponse.Fail(404, $"回合 {n} 不存在,有效范围 1 ~ {rounds?.Count ?? 0}。"));
}
return new JsonResult(round, JsonHub.Options);
}
/// <summary>GET /api/games/{id}/statistics —— 最终统计(Rating 排行 + 队伍结果)</summary>
[HttpGet("{id:guid}/statistics")]
public async Task<IActionResult> Statistics(Guid id)
{
GameRecord? record = await replay.GetGameRecordAsync(id);
if (record == null)
{
return NotFound(ApiResponse.Fail(404, $"对局 {id} 不存在。"));
}
Dictionary<int, RoundRecord>? rounds = replay.ReadRounds(record);
if (rounds == null || rounds.Count == 0)
{
return NotFound(ApiResponse.Fail(404, "该对局没有回合记录。"));
}
RoundRecord last = rounds.Values.OrderBy(r => r.Round).Last();
// 优先使用归档中的最终统计(statistics.json),回退到回合记录内嵌统计
Dictionary<Guid, CharacterStatistics>? statsByGuid = replay.ReadStatistics(record);
Dictionary<Guid, string> teamMap = last.TeamMap ?? [];
Dictionary<Guid, Character> charactersByGuid = rounds.Values
.SelectMany(r => r.AllCharacters)
.GroupBy(c => c.Guid)
.ToDictionary(g => g.Key, g => g.First());
List<StatRow> rows = [];
if (statsByGuid is { Count: > 0 })
{
foreach ((Guid guid, CharacterStatistics s) in statsByGuid)
{
charactersByGuid.TryGetValue(guid, out Character? character);
teamMap.TryGetValue(guid, out string? team);
rows.Add(ToStatRow(guid, character, team ?? "", s));
}
}
else
{
foreach ((Character character, CharacterStatistics s) in last.CharacterStatistics ?? [])
{
teamMap.TryGetValue(character.Guid, out string? team);
rows.Add(ToStatRow(character.Guid, character, team ?? "", s));
}
}
rows = [.. rows.OrderByDescending(r => r.Rating)];
return Ok(ApiResponse.Success(new
{
roundCount = rounds.Count,
totalTime = last.TotalTime,
winner = record.Winner,
gameResult = last.GameResult,
rows
}));
}
private static StatRow ToStatRow(Guid guid, Character? character, string team, CharacterStatistics s) => new(
guid.ToString(),
character?.Name ?? "",
character?.NickName ?? "",
team,
s.Rating,
s.Kills,
s.Deaths,
s.Assists,
s.TotalDamage,
s.TotalHeal,
s.TotalShield,
s.Winrate,
s.MVPs,
s.LastRank,
s.AvgRank,
s.LiveRound,
s.TotalEarnedMoney,
s.DamagePerRound,
s.DamagePerSecond,
s.ControlTime);
private record StatRow(
string Guid, string Name, string NickName, string Team, double Rating,
int Kills, int Deaths, int Assists, double TotalDamage, double TotalHeal, double TotalShield,
double Winrate, int Mvps, int LastRank, double AvgRank, int LiveRound, int TotalEarnedMoney,
double DamagePerRound, double DamagePerSecond, double ControlTime);
}