diff --git a/src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Game.razor b/src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Game.razor index f56a5bd..9330cfa 100644 --- a/src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Game.razor +++ b/src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Game.razor @@ -1,11 +1,12 @@ @page "/games/{gameId?}" @inject HttpClient Http -@using System.Timers; +@inject IConfiguration Configuration @using System.Net; +@using System.Text.Json; @using Gaas.GobbletGobblers.Application; @using Gaas.GobbletGobblers.Application.UseCases; @using Gaas.GobbletGobblers.Domain; -@using System.Text.Json; +@using Microsoft.AspNetCore.SignalR.Client; @@ -77,6 +78,15 @@ } } + +
+ +
+ + @if (winnerId.HasValue) {
@@ -84,6 +94,13 @@
} + + @if (errorResult != null) {
@errorResult.Message
@@ -184,7 +201,9 @@ [SupplyParameterFromQuery] public string? Token { get; set; } - private Timer timer; + private HubConnection hubConnection; + private List messages = new List(); + private string messageInput; private ErrorResult? errorResult; @@ -206,19 +225,29 @@ private List moveClick = new List(); - public Game() + protected override async Task OnInitializedAsync() { - timer = new(); - timer.Interval = 2000; - timer.Elapsed += async (object? sender, ElapsedEventArgs e) => - { - var request = new GameInfoRequest - { - Id = Guid.Parse(this.GameId), - }; + var user = await GetAsJsonAsync(); - var gameModel = await PostAsJsonAsync("Game/GameInfo", request); + var bytes = Convert.FromBase64String(user.Id); + + playerId = new Guid(bytes.Take(16).ToArray()); + name = user.NickName; + var baseUrl = Configuration.GetValue("BaseUrl"); + hubConnection = new HubConnectionBuilder() + .WithUrl($"{baseUrl}/gameHub") + .Build(); + + hubConnection.On("ReceiveMessage", (model) => + { + var encodedMsg = $"{model.PlayerName}: {model.Message}"; + messages.Add(encodedMsg); + StateHasChanged(); + }); + + hubConnection.On("GameInfo", async (gameModel) => + { if (gameModel != null) { if (board == null) @@ -253,25 +282,20 @@ { //Console.WriteLine(winnerId.ToString()); winnerName = gameModel.Players.FirstOrDefault(p => p.Id == winnerId).Name; - timer.Stop(); + + await hubConnection.StopAsync(); } await InvokeAsync(StateHasChanged); - }; - } - - protected override async Task OnInitializedAsync() - { - var user = await GetAsJsonAsync(); - - var bytes = Convert.FromBase64String(user.Id); - - playerId = new Guid(bytes.Take(16).ToArray()); - name = user.NickName; + }); - timer.Enabled = true; + await hubConnection.StartAsync(); } + public bool IsConnected => !string.IsNullOrEmpty(name) && + gameId != default && + hubConnection.State == HubConnectionState.Connected; + protected async Task PutCock(int x, int y) { if (winnerId.HasValue) @@ -318,19 +342,6 @@ StateHasChanged(); } - protected async Task ChangeCock(CockViewModel cock, int index) - { - moveClick = moveClick.Select(x => false).ToList(); - - //Console.WriteLine(index); - changeCockId = index; - playerViewModel.Cocks.ToList().ForEach(x => { x.IsClick = false; }); - cock.IsClick = true; - Console.WriteLine(JsonSerializer.Serialize(moveClick)); - - StateHasChanged(); - } - private async Task PostAsJsonAsync(string requestUri, TRequest request) where TResponse : new() { @@ -350,6 +361,27 @@ } } + protected async Task ChangeCock(CockViewModel cock, int index) + { + moveClick = moveClick.Select(x => false).ToList(); + + //Console.WriteLine(index); + changeCockId = index; + playerViewModel.Cocks.ToList().ForEach(x => { x.IsClick = false; }); + cock.IsClick = true; + Console.WriteLine(JsonSerializer.Serialize(moveClick)); + + StateHasChanged(); + } + + async Task Send() => + await hubConnection.SendAsync("SendMessage", GameId, name, messageInput); + + public void Dispose() + { + _ = hubConnection?.DisposeAsync(); + } + private async Task GetAsJsonAsync() where TResponse : new() { diff --git a/src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Index.razor b/src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Index.razor index 58cfae8..e78c5f4 100644 --- a/src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Index.razor +++ b/src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Index.razor @@ -1,12 +1,11 @@ @page "/" @inject HttpClient Http @inject IConfiguration Configuration +@using System.Net; +@using System.Text.Json; @using Gaas.GobbletGobblers.Application.UseCases; @using Gaas.GobbletGobblers.Application; -@using System.Text.Json; -@using System.Timers; @using Gaas.GobbletGobblers.Domain; -@using System.Net; @using Microsoft.AspNetCore.SignalR.Client @@ -224,7 +223,7 @@ hubConnection.On("ReceiveMessage", (model) => { - var encodedMsg = $"Json {model.PlayerName}: {model.Message}"; + var encodedMsg = $"{model.PlayerName}: {model.Message}"; messages.Add(encodedMsg); StateHasChanged(); }); @@ -263,7 +262,7 @@ await hubConnection.StopAsync(); } - StateHasChanged(); + await InvokeAsync(StateHasChanged); }); await hubConnection.StartAsync();