Ajoute l Elo et les statistiques de parties

This commit is contained in:
2026-04-16 00:17:52 +02:00
parent db233e7110
commit 407e5e8ed5
13 changed files with 1914 additions and 11 deletions

View File

@@ -54,11 +54,14 @@ public static class MatchEngine
var quota = Presets[config.Preset].Quota;
var match = new MatchState
{
SchemaVersion = 3,
SchemaVersion = 4,
MatchId = Guid.NewGuid().ToString("N"),
Config = config,
Phase = PhaseBlock,
Running = false,
LastTickAt = null,
WhiteSubject = null,
BlackSubject = null,
BlockNumber = 1,
CurrentTurn = ColorWhite,
BlockRemainingMs = config.BlockDurationMs,
@@ -76,6 +79,7 @@ public static class MatchEngine
AwaitingBlockClosure = false,
ClosureReason = string.Empty,
Result = null,
ResultRecordedUtc = null,
Cube = CreateCubeState(),
DoubleCoup = new DoubleCoupState
{
@@ -115,13 +119,19 @@ public static class MatchEngine
};
storedMatch.Moves ??= new PlayerIntPair();
if (string.IsNullOrWhiteSpace(storedMatch.MatchId))
{
storedMatch.MatchId = Guid.NewGuid().ToString("N");
changed = true;
}
var blockDurationMs = GetBlockDurationMs(storedMatch);
var moveLimitMs = GetMoveLimitMs(storedMatch);
var timeInitialMs = GetTimeInitialMs(storedMatch);
if (storedMatch.SchemaVersion != 3)
if (storedMatch.SchemaVersion != 4)
{
storedMatch.SchemaVersion = 3;
storedMatch.SchemaVersion = 4;
changed = true;
}
@@ -826,7 +836,7 @@ public static class MatchEngine
}
public static bool IsSupportedSchemaVersion(int version)
=> version is 2 or 3;
=> version is 2 or 3 or 4;
public static long GetBlockDurationMs(object? matchOrConfig)
{

View File

@@ -0,0 +1,77 @@
using System.Net;
using System.Net.Http.Json;
using ChessCubing.App.Models;
using ChessCubing.App.Models.Stats;
namespace ChessCubing.App.Services;
public sealed class MatchStatsService(HttpClient httpClient)
{
private readonly HttpClient _httpClient = httpClient;
public async Task<bool> TryReportCompletedMatchAsync(MatchState? match, CancellationToken cancellationToken = default)
{
if (match is null || string.IsNullOrWhiteSpace(match.Result) || match.ResultRecordedUtc is not null)
{
return false;
}
if (string.IsNullOrWhiteSpace(match.WhiteSubject) && string.IsNullOrWhiteSpace(match.BlackSubject))
{
return false;
}
var request = BuildReport(match);
try
{
var response = await _httpClient.PostAsJsonAsync("api/users/me/stats/matches", request, cancellationToken);
if (response.StatusCode is HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden)
{
return false;
}
if (!response.IsSuccessStatusCode)
{
return false;
}
match.ResultRecordedUtc = DateTime.UtcNow;
return true;
}
catch
{
return false;
}
}
private static ReportCompletedMatchRequest BuildReport(MatchState match)
=> new()
{
MatchId = string.IsNullOrWhiteSpace(match.MatchId) ? Guid.NewGuid().ToString("N") : match.MatchId,
CollaborationSessionId = match.CollaborationSessionId,
WhiteSubject = NormalizeOptional(match.WhiteSubject),
WhiteName = MatchEngine.PlayerName(match, MatchEngine.ColorWhite),
BlackSubject = NormalizeOptional(match.BlackSubject),
BlackName = MatchEngine.PlayerName(match, MatchEngine.ColorBlack),
Result = match.Result ?? string.Empty,
Mode = match.Config.Mode,
Preset = match.Config.Preset,
MatchLabel = MatchEngine.SanitizeText(match.Config.MatchLabel),
BlockNumber = Math.Max(1, match.BlockNumber),
WhiteMoves = Math.Max(0, match.Moves.White),
BlackMoves = Math.Max(0, match.Moves.Black),
CubeRounds = match.Cube.History
.Select(entry => new ReportCompletedCubeRound
{
BlockNumber = Math.Max(1, entry.BlockNumber),
Number = entry.Number,
White = entry.White,
Black = entry.Black,
})
.ToArray(),
};
private static string? NormalizeOptional(string? value)
=> string.IsNullOrWhiteSpace(value) ? null : value.Trim();
}