@using Fluxor @using Koogle.Application.DTOs @using Koogle.Application.Games @using Koogle.Application.Games.Training @using Koogle.Web.Store.GameState @using Koogle.Web.Store.DayState @using MudBlazor @inherits Fluxor.Blazor.Web.Components.FluxorComponent @implements IDisposable @inject IState GameState @inject IState DayState @* Training - Tafel *@ @if (_playerStats.Count == 0) { Noch keine Statistiken vorhanden. Starte mit dem Werfen! } else { Spieler Würfe Kegel Abgeräumt Kränze alle 9 Gossen Glocke @if (context.IsCurrentPlayer) { @context.PlayerName } else { @context.PlayerName } @context.ThrowCount @context.PinCount @context.ClearedCount @if (context.CircleCount > 0) { @context.CircleCount } else { 0 } @if (context.StrikeCount > 0) { @context.StrikeCount } else { 0 } @if (context.GutterCount > 0) { @context.GutterCount } else { 0 } @if (context.BellCount > 0) { @context.BellCount } else { 0 } @context.Average.ToString("F1") @* Summary row *@ Gesamt: @_totalThrows Würfe, @_totalPins Kegel Team-⌀: @_teamAverage.ToString("F2") } @code { private List _playerStats = []; private int _totalThrows; private int _totalPins; private double _teamAverage; protected override void OnInitialized() { base.OnInitialized(); GameState.StateChanged += OnGameStateChanged; UpdateStats(); } private void OnGameStateChanged(object? sender, EventArgs e) { UpdateStats(); InvokeAsync(StateHasChanged); } private void UpdateStats() { _playerStats.Clear(); _totalThrows = 0; _totalPins = 0; var gameState = GameState.Value; if (gameState.GameModel is not TrainingGameModel model) { // Try to deserialize if it's a JsonElement if (gameState.GameModel is System.Text.Json.JsonElement jsonElement) { try { model = System.Text.Json.JsonSerializer.Deserialize( jsonElement.GetRawText(), GameModelFactory.JsonSerializerOptions); } catch { return; } } else { return; } } if (model?.PlayerStatistics == null) { return; } var currentPlayerId = gameState.Participants.CurrentPlayerId; var persons = DayState.Value.AvailablePersons; foreach (var (playerId, stats) in model.PlayerStatistics) { var person = persons.FirstOrDefault(p => p.Id == playerId); var playerName = person?.Name ?? "Unbekannt"; _playerStats.Add(new PlayerStatsRow { PlayerId = playerId, PlayerName = playerName, ThrowCount = stats.ThrowCount, ClearedCount = stats.ClearedCount, PinCount = stats.PinCount, CircleCount = stats.CircleCount, StrikeCount = stats.StrikeCount, GutterCount = stats.GutterCount, BellCount = stats.BellCount, Average = stats.Average, IsCurrentPlayer = playerId == currentPlayerId }); _totalThrows += stats.ThrowCount; _totalPins += stats.PinCount; } // Sort by pin count descending (best player first) _playerStats = _playerStats.OrderByDescending(p => p.PinCount).ToList(); _teamAverage = _totalThrows > 0 ? (double)_totalPins / _totalThrows : 0; } public void Dispose() { GameState.StateChanged -= OnGameStateChanged; } private record PlayerStatsRow { public Guid PlayerId { get; init; } public string PlayerName { get; init; } = ""; public int ThrowCount { get; init; } public int ClearedCount { get; init; } public int PinCount { get; init; } public int CircleCount { get; init; } public int StrikeCount { get; init; } public int GutterCount { get; init; } public int BellCount { get; init; } public double Average { get; init; } public bool IsCurrentPlayer { get; init; } } }