yep
This commit is contained in:
@@ -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,13 +154,24 @@
|
||||
}
|
||||
else if (piece != null)
|
||||
{
|
||||
ShogiApi.PostMove(SessionName!, new Contracts.Api.MovePieceCommand
|
||||
if (ShouldPromptForPromotion(position) || ShouldPromptForPromotion(selectedPosition))
|
||||
{
|
||||
PromotePrompt.Show(SessionName, new MovePieceCommand
|
||||
{
|
||||
From = selectedPosition,
|
||||
To = position,
|
||||
IsP
|
||||
To = position
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
await ShogiApi.PostMove(SessionName, new MovePieceCommand
|
||||
{
|
||||
From = selectedPosition,
|
||||
IsPromotion = false,
|
||||
To = position
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
void OnClickHand(Piece piece)
|
||||
{
|
||||
|
||||
58
Shogi.UI/Pages/Home/PromotePrompt.cs
Normal file
58
Shogi.UI/Pages/Home/PromotePrompt.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Shogi.UI;
|
||||
using Shogi.UI.Pages.Home;
|
||||
using Shogi.UI.Pages.Home.Account;
|
||||
using Shogi.UI.Pages.Home.Api;
|
||||
using Shogi.UI.Shared;
|
||||
@@ -54,6 +55,7 @@ static void ConfigureDependencies(IServiceCollection services, IConfiguration co
|
||||
services.AddScoped<MsalMessageHandler>();
|
||||
services.AddScoped<CookieCredentialsMessageHandler>();
|
||||
services.AddScoped<IShogiApi, ShogiApi>();
|
||||
services.AddScoped<PromotePrompt>();
|
||||
|
||||
var serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user