This commit is contained in:
2024-10-25 10:30:47 -05:00
parent 3593785421
commit 7d47fafea0
11 changed files with 124 additions and 107 deletions

View File

@@ -19,7 +19,7 @@
var position = $"{file}{rank}";
var piece = Session?.BoardState.Board[position];
var isSelected = piece != null && SelectedPosition == position;
<div class="tile" @onclick="OnClickTileInternal(piece, position)"
<div class="tile" @onclick="OnClickTileInternal(position)"
data-position="@(position)"
data-selected="@(isSelected)"
style="grid-area: @position">
@@ -80,7 +80,7 @@
</div>
<div class="player-area">
@if (this.OnClickJoinGame != null && string.IsNullOrEmpty(Session.Player2) && !string.IsNullOrEmpty(Session.Player1))
@if (string.IsNullOrEmpty(Session.Player2) && !string.IsNullOrEmpty(Session.Player1))
{
<div class="place-self-center">
<button @onclick="OnClickJoinGameInternal">Join Game</button>
@@ -121,9 +121,9 @@
[Parameter] public string? SelectedPosition { get; set; }
[Parameter] public WhichPiece? SelectedPieceFromHand { get; set; }
// TODO: Exchange these OnClick actions for events like "SelectionChangedEvent" and "MoveFromBoardEvent" and "MoveFromHandEvent".
[Parameter] public Func<Piece?, string, Task>? OnClickTile { get; set; }
[Parameter] public Func<Piece, Task>? OnClickHand { get; set; }
[Parameter] public Func<Task>? OnClickJoinGame { get; set; }
[Parameter] public EventCallback<string> OnClickTile { get; set; }
[Parameter] public EventCallback<Piece> OnClickHand { get; set; }
[Parameter] public EventCallback OnClickJoinGame { get; set; }
[Parameter] public bool IsMyTurn { get; set; }
[Parameter] public bool UseSideboard { get; set; } = true;
@@ -167,7 +167,9 @@
}
}
private Action OnClickTileInternal(Piece? piece, string position) => () => OnClickTile?.Invoke(piece, position);
private Action OnClickHandInternal(Piece piece) => () => OnClickHand?.Invoke(piece);
private void OnClickJoinGameInternal() => OnClickJoinGame?.Invoke();
private Func<Task> OnClickTileInternal(string position) => () => OnClickTile.InvokeAsync(position);
private Func<Task> OnClickHandInternal(Piece piece) => () => OnClickHand.InvokeAsync(piece);
private Task OnClickJoinGameInternal() => OnClickJoinGame.InvokeAsync();
}

View File

@@ -50,7 +50,7 @@
}
}
bool ShouldPromptForPromotion(string position)
bool IsWithinPromoteArea(string position)
{
if (Perspective == WhichPlayer.Player1 && Regex.IsMatch(position, ".[7-9]"))
{
@@ -63,11 +63,11 @@
return false;
}
async Task OnClickTile(Piece? pieceAtPosition, string position)
async Task OnClickTile(string position)
{
if (!IsMyTurn) return;
if (!IsMyTurn || showPromotePrompt) return;
var pieceAtPosition = Session.BoardState.Board[position];
if (selectedBoardPosition == position)
{
// Deselect the selected position.
@@ -105,12 +105,13 @@
if (selectedBoardPosition != null)
{
Console.WriteLine("pieceAtPosition is null? {0}", pieceAtPosition == null);
if (pieceAtPosition == null || pieceAtPosition?.Owner != Perspective)
{
var pieceBeingMoved = Session.BoardState.Board[selectedBoardPosition];
var isPromotedAlready = pieceBeingMoved != null && pieceBeingMoved.IsPromoted;
Console.WriteLine("Is promoted? {0}", isPromotedAlready);
// Moving to an empty space or capturing an opponent's piece.
if (ShouldPromptForPromotion(position) || ShouldPromptForPromotion(selectedBoardPosition))
if (!isPromotedAlready && (IsWithinPromoteArea(position) || IsWithinPromoteArea(selectedBoardPosition)))
{
Console.WriteLine("Prompt!");
moveTo = position;
@@ -118,11 +119,7 @@
}
else
{
Console.WriteLine("OnClick to move to {0}", position);
var success = await ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedBoardPosition, position, false));
Console.WriteLine("Success? {0}", success);
if (!success)
{
selectedBoardPosition = null;
@@ -134,9 +131,9 @@
}
}
async Task OnClickHand(Piece piece)
void OnClickHand(Piece piece)
{
if (!IsMyTurn) return;
if (!IsMyTurn || showPromotePrompt) return;
// Prevent selecting from both the hand and the board.
selectedBoardPosition = null;
@@ -149,11 +146,13 @@
StateHasChanged();
}
private Task OnClickPromotionChoice(bool shouldPromote)
private async Task OnClickPromotionChoice(bool shouldPromote)
{
if (selectedBoardPosition == null && selectedPieceFromHand.HasValue && moveTo != null)
if (selectedBoardPosition != null && moveTo != null)
{
return ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedPieceFromHand.Value, moveTo));
await ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedBoardPosition, moveTo, shouldPromote));
showPromotePrompt = false;
return;
}
throw new InvalidOperationException("Unexpected scenario during OnClickPromotionChoice.");

View File

@@ -0,0 +1,7 @@
namespace Shogi.UI.Pages.Play.GameBoard
{
public class TestCbParams
{
public string Name { get; set; }
}
}