71 lines
1.8 KiB
Plaintext
71 lines
1.8 KiB
Plaintext
@using Shogi.Contracts.Api
|
|
@using Shogi.Contracts.Socket;
|
|
@using Shogi.Contracts.Types;
|
|
@using System.Text.RegularExpressions;
|
|
@inject IShogiApi ShogiApi
|
|
@inject AccountState Account;
|
|
@inject PromotePrompt PromotePrompt;
|
|
@inject ShogiSocket ShogiSocket;
|
|
|
|
@if (session == null)
|
|
{
|
|
<EmptyGameBoard />
|
|
}
|
|
else if (isSpectating)
|
|
{
|
|
<SpectatorGameBoard Session="session" OnRefetchSession="RefetchSession" />
|
|
}
|
|
else
|
|
{
|
|
<SeatedGameBoard Perspective="perspective" Session="session" />
|
|
}
|
|
|
|
|
|
@code {
|
|
[Parameter]
|
|
public string? SessionName { get; set; }
|
|
|
|
Session? session;
|
|
private WhichPlayer perspective;
|
|
private bool isSpectating;
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
base.OnInitialized();
|
|
ShogiSocket.OnPlayerMoved += OnPlayerMoved_RefetchSession;
|
|
ShogiSocket.OnSessionJoined +=
|
|
}
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
await RefetchSession();
|
|
}
|
|
|
|
async Task RefetchSession()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(SessionName))
|
|
{
|
|
this.session = await ShogiApi.GetSession(SessionName);
|
|
if (this.session != null)
|
|
{
|
|
var accountId = Account.User?.Id;
|
|
|
|
this.perspective = accountId == session.Player2 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
|
this.isSpectating = !(accountId == this.session.Player1 || accountId == this.session.Player2);
|
|
Console.WriteLine($"IsSpectating - {isSpectating}. AccountId - {accountId}. Player1 - {this.session.Player1}. Player2 - {this.session.Player2}");
|
|
|
|
}
|
|
StateHasChanged();
|
|
}
|
|
}
|
|
|
|
Task OnPlayerMoved_RefetchSession(PlayerHasMovedMessage args)
|
|
{
|
|
if (args.SessionName == SessionName)
|
|
{
|
|
return RefetchSession();
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|