122 lines
3.9 KiB
Plaintext
122 lines
3.9 KiB
Plaintext
@using Shogi.Contracts.Api;
|
|
@using Shogi.Contracts.Types;
|
|
@using System.Text.RegularExpressions;
|
|
@using System.Net;
|
|
@inject PromotePrompt PromotePrompt;
|
|
@inject IShogiApi ShogiApi;
|
|
|
|
<GameBoardPresentation Session="Session"
|
|
Perspective="Perspective"
|
|
OnClickHand="OnClickHand"
|
|
OnClickTile="OnClickTile"
|
|
SelectedPosition="@selectedBoardPosition"
|
|
SelectedPieceFromHand="@selectedPieceFromHand"
|
|
IsMyTurn="IsMyTurn" />
|
|
|
|
@code {
|
|
[Parameter, EditorRequired]
|
|
public WhichPlayer Perspective { get; set; }
|
|
[Parameter, EditorRequired]
|
|
public Session Session { get; set; }
|
|
private bool IsMyTurn => Session?.BoardState.WhoseTurn == Perspective;
|
|
private string? selectedBoardPosition;
|
|
private WhichPiece? selectedPieceFromHand;
|
|
|
|
protected override void OnParametersSet()
|
|
{
|
|
base.OnParametersSet();
|
|
selectedBoardPosition = null;
|
|
selectedPieceFromHand = null;
|
|
if (Session == null)
|
|
{
|
|
throw new ArgumentException($"{nameof(Session)} cannot be null.", nameof(Session));
|
|
}
|
|
}
|
|
|
|
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 Task OnClickTile(Piece? pieceAtPosition, string position)
|
|
{
|
|
if (!IsMyTurn) return;
|
|
|
|
|
|
if (selectedBoardPosition == position)
|
|
{
|
|
// Deselect the selected position.
|
|
selectedBoardPosition = null;
|
|
StateHasChanged();
|
|
return;
|
|
}
|
|
|
|
if (selectedBoardPosition == null && pieceAtPosition?.Owner == Perspective)
|
|
{
|
|
// Select an owned piece.
|
|
Console.WriteLine("Selecting piece owned by {0} while I am perspective {1}", pieceAtPosition?.Owner, Perspective);
|
|
selectedBoardPosition = position;
|
|
// Prevent selecting pieces from the hand and board at the same time.
|
|
selectedPieceFromHand = null;
|
|
StateHasChanged();
|
|
return;
|
|
}
|
|
|
|
if (selectedPieceFromHand is not null)
|
|
{
|
|
if (pieceAtPosition is null)
|
|
{
|
|
// Placing a piece from the hand to an empty space.
|
|
await ShogiApi.Move(
|
|
Session.SessionName,
|
|
new MovePieceCommand(selectedPieceFromHand.Value, position));
|
|
}
|
|
StateHasChanged();
|
|
return;
|
|
}
|
|
|
|
if (selectedBoardPosition != null)
|
|
{
|
|
if (pieceAtPosition == null || pieceAtPosition?.Owner != Perspective)
|
|
{
|
|
// Moving to an empty space or capturing an opponent's piece.
|
|
if (ShouldPromptForPromotion(position) || ShouldPromptForPromotion(selectedBoardPosition))
|
|
{
|
|
PromotePrompt.Show(
|
|
Session.SessionName,
|
|
new MovePieceCommand(selectedBoardPosition, position, false));
|
|
}
|
|
else
|
|
{
|
|
await ShogiApi.Move(Session.SessionName, new MovePieceCommand(selectedBoardPosition, position, false));
|
|
}
|
|
StateHasChanged();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
async Task OnClickHand(Piece piece)
|
|
{
|
|
if (!IsMyTurn) return;
|
|
|
|
// Prevent selecting from both the hand and the board.
|
|
selectedBoardPosition = null;
|
|
|
|
selectedPieceFromHand = piece.WhichPiece == selectedPieceFromHand
|
|
// Deselecting the already-selected piece
|
|
? selectedPieceFromHand = null
|
|
: selectedPieceFromHand = piece.WhichPiece;
|
|
|
|
StateHasChanged();
|
|
}
|
|
}
|