KoogleApp/KoogleApp/Components/Pages/Game.razor

255 lines
6.4 KiB
Plaintext

@page "/game"
@using KoogleApp.Model
@using KoogleApp.Services
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.SignalR.Client
@using KoogleApp.Components.Controls
@using KoogleApp.Components.Dialogs
@using KoogleApp.Model.EventMessages
@using KoogleApp.Model.Framework
@using KoogleApp.Store.DayFeature
@using KoogleApp.Store.Game
@using KoogleApp.Store.Game.ThrowPanel
@using KoogleApp.Store.Game.ThrowTimer
@using KoogleApp.Store.Game.UndoRedo
@inherits FluxorComponent
@implements IMyHandle<GameViewChangedMessage>
@implements IAsyncDisposable
@attribute [Authorize()]
@inject NavigationManager Navigation
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IMyEventAggregator EventAggregator
@inject IState<ThrowPanelState> ThrowPanelState
@inject IState<DayState> DayState
@inject IDispatcher Dispatcher
@inject IDialogService DialogService
@* @inject IGameStatusDataService _dataService; *@
@switch (_gameView)
{
case GameView.Throw:
<PanelToolbar>
@* @GetDayStr() *@
@if (DayState.Value.Status != DayStatus.Started)
{
<DayListMenu/>
}
else
{
<ThrowPanelMenu/>
}
</PanelToolbar>
break;
case GameView.Board:
break;
case GameView.Players:
break;
case GameView.Player:
break;
}
<!-- Scrollbarer Inhalt -->
<MudContainer Class="mt-4">
@* <p>@GetDayStr()</p> *@
@switch (_gameView)
{
case GameView.Throw:
if (DayState.Value.Status != DayStatus.Started)
{
<DaysList/>
}
else
{
<ThrowPanel/>
}
break;
case GameView.Board:
<BoardPanel/>
break;
case GameView.Players:
<PlayersPanel ShowMenu="true"/>
break;
case GameView.Player:
<PlayerPanel/>
break;
}
</MudContainer>
@code {
private GameView _gameView = GameView.Throw;
private string? _userName;
private bool isAuthenticated;
protected override void OnInitialized()
{
base.OnInitialized();
// Verbindung zum Hub aufbauen (nur einmal)
// if (!ThrowPanelState.Value.IsConnected)
{
Dispatcher.Dispatch(new ConnectToHubAction());
Dispatcher.Dispatch(new InitializeDayAction());
Dispatcher.Dispatch(new UpdateUndoRedoStateAction());
}
}
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
EventAggregator.Subscribe(this);
}
}
private string GetDayStr()
{
if (DayState.Value == null)
{
return "";
}
var day = DayState.Value;
var res = $"{day.Date.ToShortDateString()} - {day.Status}/{(int)day.Status} ({day.Id})";
if (day.PlayerIds.Length > 0)
{
res += $" (P:{day.PlayerIds.Select(_ => _.ToString()).Aggregate((a, b) => $"{a},{b}")})";
}
return res;
}
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
// Beim Laden (auch nach F5) werden die gespeicherten Daten geladen
// hubConnection = new HubConnectionBuilder()
// .WithUrl(Navigation.ToAbsoluteUri("/sharedmodelhub"))
// .WithAutomaticReconnect()
// .Build();
// hubConnection.On<GameStatusSnapshot>("InitialData", async (newText) =>
// {
// GameStatus = newText.Status;
// _sharedText = newText.Status.Content;
// _version = newText.Version;
// await InvokeAsync(StateHasChanged);
// });
// hubConnection.On<DataChangeEvent>("ReceiveDataUpdate", async (newText) =>
// {
// GameStatus = newText.Content;
// _sharedText = GameStatus.Content;
// _version = newText.Version;
// await InvokeAsync(StateHasChanged);
// });
// hubConnection.On<UndoRedoState>("UpdateUndoRedoState", async state =>
// {
// _canUndo = state.CanUndo;
// _canRedo = state.CanRedo;
// await InvokeAsync(StateHasChanged);
// });
// hubConnection.On<ThrowPanelState>("UpdateThrowPanelState", async state =>
// {
// // _throwPanelState = state;
// // _throwPanel.UpdatePanelState(state);
// // _boardPanel.UpdatePanelState(state);
// // await InvokeAsync(StateHasChanged);
// });
// await hubConnection.StartAsync();
}
// private async Task OnBoardPanelStateChanged(ThrowPanelState state)
// {
// if (hubConnection is not null)
// {
// // hubConnection.
// // await hubConnection.SendAsync("UpdateBoardPanelState", state, _userName);
// }
// }
public GameStatus GameStatus { get; set; }
// private async Task SendUpdateAsync(string text)
// {
// if (hubConnection is not null)
// {
// await hubConnection.SendAsync("SendDataUpdate", GameStatus, _userName);
// }
// }
public new async ValueTask DisposeAsync()
{
EventAggregator.Unsubscribe(this);
// if (hubConnection is not null)
// {
// await hubConnection.DisposeAsync();
// }
await base.DisposeAsync();
}
private async Task SaveClick(MouseEventArgs arg)
{
// GameStatus.Content = _sharedText;
// await SendUpdateAsync(_sharedText);
}
public async Task HandleAsync(GameViewChangedMessage message)
{
if (message is { GameView: GameView.Throw, AutoReturnFromTimer: false })
{
Dispatcher.Dispatch(new StopTimerAction());
}
_gameView = message.GameView;
await InvokeAsync(StateHasChanged);
}
// private async Task OnThrowPanelStateChanged(ThrowPanelState state)
// {
// await BroadcastThrowPanelStateAction(state);
// }
// private async Task BroadcastThrowPanelStateAction(ThrowPanelState state)
// {
// if (hubConnection is not null)
// {
// await hubConnection.SendAsync("UpdateThrowPanelState", state, _userName);
// }
// }
}