This commit is contained in:
2022-11-11 20:29:42 -06:00
parent 79b70d6fa5
commit 716470f24d
3 changed files with 106 additions and 9 deletions

View File

@@ -1,6 +1,9 @@
@using Shogi.Contracts.Types;
@using Shogi.Contracts.Api
@using Shogi.Contracts.Types;
@using System.Text.RegularExpressions;
@inject IShogiApi ShogiApi
@inject AccountState Account;
@inject PromotePrompt PromotePrompt;
<article class="game-board">
<!-- Game board -->
@@ -42,6 +45,15 @@
<span>H</span>
<span>I</span>
</div>
<!-- Promote prompt -->
<div class="promote-prompt">
<p>Do you wish to promote?</p>
<div>
<button type="button">Yes</button>
<button type="button">No</button>
<button type="button">Cancel</button>
</div>
</div>
</section>
<!-- Side board -->
@if (session != null)
@@ -67,15 +79,15 @@
}
</div>
</aside>
}
</article>
@code {
static readonly string[] Files = new[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
[Parameter]
public string? SessionName { get; set; }
static readonly string[] Files = new[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
WhichPlayer Perspective => Account.User?.Id == session?.Player1
? WhichPlayer.Player1
: WhichPlayer.Player2;
@@ -114,8 +126,22 @@
}
}
void OnClickTile(Piece? piece, string position)
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 (SessionName == null) return;
if (selectedPosition == null)
{
selectedPosition = position;
@@ -128,12 +154,23 @@
}
else if (piece != null)
{
ShogiApi.PostMove(SessionName!, new Contracts.Api.MovePieceCommand
if (ShouldPromptForPromotion(position) || ShouldPromptForPromotion(selectedPosition))
{
From = selectedPosition,
To = position,
IsP
});
PromotePrompt.Show(SessionName, new MovePieceCommand
{
From = selectedPosition,
To = position
});
}
else
{
await ShogiApi.PostMove(SessionName, new MovePieceCommand
{
From = selectedPosition,
IsPromotion = false,
To = position
});
}
}
}
void OnClickHand(Piece piece)

View File

@@ -0,0 +1,58 @@
using Shogi.Contracts.Api;
using Shogi.UI.Pages.Home.Api;
namespace Shogi.UI.Pages.Home;
public class PromotePrompt
{
private readonly IShogiApi shogiApi;
private string? sessionName;
private MovePieceCommand? command;
public PromotePrompt(IShogiApi shogiApi)
{
this.shogiApi = shogiApi;
IsVisible = false;
OnClickCancel = Hide;
}
public bool IsVisible { get; private set; }
public Action OnClickCancel;
public Func<Task>? OnClickNo;
public Func<Task>? OnClickYes;
public void Show(string sessionName, MovePieceCommand command)
{
this.sessionName = sessionName;
this.command = command;
IsVisible = true;
OnClickNo = Move;
OnClickYes = MoveAndPromote;
}
public void Hide()
{
IsVisible = false;
OnClickNo = null;
OnClickYes = null;
}
private Task Move()
{
if (command != null && sessionName != null)
{
command.IsPromotion = false;
return shogiApi.PostMove(sessionName, command);
}
return Task.CompletedTask;
}
private Task MoveAndPromote()
{
if (command != null && sessionName != null)
{
command.IsPromotion = true;
return shogiApi.PostMove(sessionName, command);
}
return Task.CompletedTask;
}
}