72 lines
2.1 KiB
Plaintext
72 lines
2.1 KiB
Plaintext
@using Shogi.Contracts.Api;
|
|
@using Shogi.Contracts.Types;
|
|
@using System.Text.RegularExpressions;
|
|
@inject PromotePrompt PromotePrompt;
|
|
@inject IShogiApi ShogiApi;
|
|
|
|
<GameBoardPresentation OnClickHand="OnClickHand" OnClickTile="OnClickTile" Session="Session" Perspective="Perspective" />
|
|
|
|
@code {
|
|
[Parameter] public WhichPlayer Perspective { get; set; }
|
|
[Parameter] public Session Session { get; set; }
|
|
private bool IsMyTurn => Session?.BoardState.WhoseTurn == Perspective;
|
|
private string? selectedBoardPosition;
|
|
private WhichPiece? selectedPieceFromHand;
|
|
|
|
bool ShouldPromptForPromotion(string position)
|
|
{
|
|
if (Perspective == WhichPlayer.Player1 && Regex.IsMatch(position, ".[7-9]"))
|
|
{
|
|
return true;
|
|
}
|
|
if (Perspective == WhichPlayer.Player2 && Regex.IsMatch(position, ".[1-3]"))
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async void OnClickTile(Piece? piece, string position)
|
|
{
|
|
if (!IsMyTurn) return;
|
|
|
|
if (selectedBoardPosition == null || piece?.Owner == Perspective)
|
|
{
|
|
// Select a position.
|
|
selectedBoardPosition = position;
|
|
return;
|
|
}
|
|
if (selectedBoardPosition == position)
|
|
{
|
|
// Deselect the selected position.
|
|
selectedBoardPosition = null;
|
|
return;
|
|
}
|
|
if (piece == null)
|
|
{
|
|
if (ShouldPromptForPromotion(position) || ShouldPromptForPromotion(selectedBoardPosition))
|
|
{
|
|
PromotePrompt.Show(Session.SessionName, new MovePieceCommand
|
|
{
|
|
From = selectedBoardPosition,
|
|
To = position
|
|
});
|
|
}
|
|
else
|
|
{
|
|
await ShogiApi.Move(Session.SessionName, new MovePieceCommand
|
|
{
|
|
From = selectedBoardPosition,
|
|
IsPromotion = false,
|
|
To = position
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnClickHand(Piece piece)
|
|
{
|
|
selectedPieceFromHand = piece.WhichPiece;
|
|
}
|
|
}
|