Skip to content

Commit

Permalink
Modify Game.razor
Browse files Browse the repository at this point in the history
  • Loading branch information
oneheed committed Oct 3, 2023
1 parent 82e9751 commit 6e4235a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 44 deletions.
110 changes: 71 additions & 39 deletions src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Game.razor
Original file line number Diff line number Diff line change
@@ -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;

<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -77,13 +78,29 @@
}
}
</div>

<div class="form-group">
<label>
Message:
<input @bind="messageInput" size="50" />
</label>
</div>
<button @onclick="Send" disabled="@(!IsConnected)">Send</button>

@if (winnerId.HasValue)
{
<div>
勝利者: @winnerName
</div>
}

<ul id="messagesList">
@foreach (var message in messages)
{
<li>@message</li>
}
</ul>

@if (errorResult != null)
{
<div>@errorResult.Message</div>
Expand Down Expand Up @@ -184,7 +201,9 @@
[SupplyParameterFromQuery]
public string? Token { get; set; }

private Timer timer;
private HubConnection hubConnection;
private List<string> messages = new List<string>();
private string messageInput;

private ErrorResult? errorResult;

Expand All @@ -206,19 +225,29 @@

private List<bool> moveClick = new List<bool>();

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<User>();

var gameModel = await PostAsJsonAsync<GameInfoRequest, GameViewModel>("Game/GameInfo", request);
var bytes = Convert.FromBase64String(user.Id);

playerId = new Guid(bytes.Take(16).ToArray());
name = user.NickName;

var baseUrl = Configuration.GetValue<string>("BaseUrl");
hubConnection = new HubConnectionBuilder()
.WithUrl($"{baseUrl}/gameHub")
.Build();

hubConnection.On<SendMessageModel>("ReceiveMessage", (model) =>
{
var encodedMsg = $"{model.PlayerName}: {model.Message}";
messages.Add(encodedMsg);
StateHasChanged();
});

hubConnection.On<GameViewModel>("GameInfo", async (gameModel) =>
{
if (gameModel != null)
{
if (board == null)
Expand Down Expand Up @@ -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<User>();

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 &&

Check failure on line 296 in src/InterfaceAdapter/Gaas.GobbletGobblers.Client/Pages/Game.razor

View workflow job for this annotation

GitHub Actions / build

The name 'gameId' does not exist in the current context
hubConnection.State == HubConnectionState.Connected;

protected async Task PutCock(int x, int y)
{
if (winnerId.HasValue)
Expand Down Expand Up @@ -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<TResponse> PostAsJsonAsync<TRequest, TResponse>(string requestUri, TRequest request)
where TResponse : new()
{
Expand All @@ -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<TResponse> GetAsJsonAsync<TResponse>()
where TResponse : new()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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

<!DOCTYPE html>
Expand Down Expand Up @@ -224,7 +223,7 @@

hubConnection.On<SendMessageModel>("ReceiveMessage", (model) =>
{
var encodedMsg = $"Json {model.PlayerName}: {model.Message}";
var encodedMsg = $"{model.PlayerName}: {model.Message}";
messages.Add(encodedMsg);
StateHasChanged();
});
Expand Down Expand Up @@ -263,7 +262,7 @@
await hubConnection.StopAsync();
}

StateHasChanged();
await InvokeAsync(StateHasChanged);
});

await hubConnection.StartAsync();
Expand Down

0 comments on commit 6e4235a

Please sign in to comment.