This commit is contained in:
2023-01-28 13:21:47 -06:00
parent 11b387b928
commit 8a25c0ed35
26 changed files with 443 additions and 359 deletions

View File

@@ -3,9 +3,4 @@
<GameBoardPresentation Perspective="WhichPlayer.Player1" />
@code {
protected override void OnInitialized()
{
base.OnInitialized();
Console.WriteLine("Empty Game Board.");
}
}

View File

@@ -1,9 +1,11 @@
@using Shogi.Contracts.Api
@using Shogi.Contracts.Socket;
@using Shogi.Contracts.Types;
@using System.Text.RegularExpressions;
@inject IShogiApi ShogiApi
@inject AccountState Account;
@inject PromotePrompt PromotePrompt;
@inject ShogiSocket ShogiSocket;
@if (session == null)
{
@@ -11,11 +13,11 @@
}
else if (isSpectating)
{
<SpectatorGameBoard Session="session" />
<SpectatorGameBoard Session="session" OnRefetchSession="RefetchSession" />
}
else
{
<SeatedGameBoard Perspective="perspective" Session="session" OnRefetchSession="RefetchSession" />
<SeatedGameBoard Perspective="perspective" Session="session" />
}
@@ -27,6 +29,13 @@ else
private WhichPlayer perspective;
private bool isSpectating;
protected override void OnInitialized()
{
base.OnInitialized();
ShogiSocket.OnPlayerMoved += OnPlayerMoved_RefetchSession;
ShogiSocket.OnSessionJoined +=
}
protected override async Task OnParametersSetAsync()
{
await RefetchSession();
@@ -34,7 +43,7 @@ else
async Task RefetchSession()
{
if (!string.IsNullOrWhiteSpace(SessionName))
if (!string.IsNullOrWhiteSpace(SessionName))
{
this.session = await ShogiApi.GetSession(SessionName);
if (this.session != null)
@@ -44,9 +53,18 @@ else
this.perspective = accountId == session.Player2 ? WhichPlayer.Player2 : WhichPlayer.Player1;
this.isSpectating = !(accountId == this.session.Player1 || accountId == this.session.Player2);
Console.WriteLine($"IsSpectating - {isSpectating}. AccountId - {accountId}. Player1 - {this.session.Player1}. Player2 - {this.session.Player2}");
}
StateHasChanged();
}
}
Task OnPlayerMoved_RefetchSession(PlayerHasMovedMessage args)
{
if (args.SessionName == SessionName)
{
return RefetchSession();
}
return Task.CompletedTask;
}
}

View File

@@ -68,10 +68,10 @@
<aside class="side-board">
<div class="player-area">
<div class="hand">
@if (OpponentHand.Any())
@if (opponentHand.Any())
{
@foreach (var piece in OpponentHand)
@foreach (var piece in opponentHand)
{
<div class="tile">
<GamePiece Piece="piece" Perspective="Perspective" />
@@ -86,6 +86,8 @@
</div>
<div class="spacer place-self-center">
<p>@opponentName</p>
<p>@userName</p>
</div>
<div class="player-area">
@@ -93,15 +95,15 @@
{
<div class="place-self-center">
<p>Seat is Empty</p>
<button @onclick="OnClickJoinGameInternal()">Join Game</button>
<button @onclick="OnClickJoinGameInternal">Join Game</button>
</div>
}
else
{
<div class="hand">
@if (UserHand.Any())
@if (userHand.Any())
{
@foreach (var piece in UserHand)
@foreach (var piece in userHand)
{
<div class="title" @onclick="OnClickHandInternal(piece)">
<GamePiece Piece="piece" Perspective="Perspective" />
@@ -121,6 +123,11 @@
</article>
@code {
static readonly string[] Files = new[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
/// <summary>
/// When true, an icon is displayed indicating that the user is spectating.
/// </summary>
[Parameter] public bool IsSpectating { get; set; } = false;
[Parameter] public WhichPlayer Perspective { get; set; }
[Parameter] public Session? Session { get; set; }
@@ -130,32 +137,48 @@
[Parameter] public Func<Piece, Task>? OnClickHand { get; set; }
[Parameter] public Func<Task>? OnClickJoinGame { get; set; }
static readonly string[] Files = new[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
private IReadOnlyCollection<Piece> opponentHand;
private IReadOnlyCollection<Piece> userHand;
private string? userName;
private string? opponentName;
private IReadOnlyCollection<Piece> OpponentHand
public GameBoardPresentation()
{
get
{
if (this.Session == null) return Array.Empty<Piece>();
return Perspective == WhichPlayer.Player1
? this.Session.BoardState.Player1Hand
: this.Session.BoardState.Player2Hand;
}
opponentHand = Array.Empty<Piece>();
userHand = Array.Empty<Piece>();
userName = string.Empty;
opponentName = string.Empty;
}
IReadOnlyCollection<Piece> UserHand
{
get
{
if (this.Session == null) return Array.Empty<Piece>();
return Perspective == WhichPlayer.Player1
protected override void OnParametersSet()
{
Console.WriteLine("Params changed. SelectedPosition = {0}", SelectedPosition);
base.OnParametersSet();
if (Session == null)
{
opponentHand = Array.Empty<Piece>();
userHand = Array.Empty<Piece>();
userName = string.Empty;
opponentName = string.Empty;
}
else
{
opponentHand = Perspective == WhichPlayer.Player1
? this.Session.BoardState.Player2Hand
: this.Session.BoardState.Player1Hand;
userHand = Perspective == WhichPlayer.Player1
? this.Session.BoardState.Player1Hand
: this.Session.BoardState.Player2Hand;
userName = Perspective == WhichPlayer.Player1
? this.Session.Player1
: this.Session.Player2;
opponentName = Perspective == WhichPlayer.Player1
? this.Session.Player2
: this.Session.Player1;
}
}
private Action OnClickTileInternal(Piece? piece, string position) => () => OnClickTile?.Invoke(piece, position);
private Action OnClickHandInternal(Piece piece) => () => OnClickHand?.Invoke(piece);
private Action OnClickJoinGameInternal() => () => OnClickJoinGame?.Invoke();
private void OnClickJoinGameInternal() => OnClickJoinGame?.Invoke();
}

View File

@@ -9,18 +9,26 @@
Perspective="Perspective"
OnClickHand="OnClickHand"
OnClickTile="OnClickTile"
OnClickJoinGame="OnClickJoinGame" />
SelectedPosition="@selectedBoardPosition" />
@code {
[Parameter, EditorRequired]
public WhichPlayer Perspective { get; set; }
[Parameter, EditorRequired]
public Session Session { get; set; }
[Parameter] public Func<Task>? OnRefetchSession { get; set; }
private bool IsMyTurn => Session?.BoardState.WhoseTurn == Perspective;
private string? selectedBoardPosition;
private WhichPiece? selectedPieceFromHand;
protected override void OnParametersSet()
{
base.OnParametersSet();
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]"))
@@ -36,12 +44,16 @@
async Task OnClickTile(Piece? piece, string position)
{
Console.WriteLine("Is my turn?");
Console.WriteLine(true);
if (!IsMyTurn) return;
if (selectedBoardPosition == null || piece?.Owner == Perspective)
{
// Select a position.
Console.WriteLine("Position {0}", position);
selectedBoardPosition = position;
StateHasChanged();
return;
}
if (selectedBoardPosition == position)
@@ -77,16 +89,4 @@
selectedPieceFromHand = piece.WhichPiece;
await Task.CompletedTask;
}
async Task OnClickJoinGame()
{
if (Session != null && OnRefetchSession != null)
{
var status = await ShogiApi.PatchJoinGame(Session.SessionName);
if (status == HttpStatusCode.OK)
{
await OnRefetchSession.Invoke();
}
}
}
}

View File

@@ -1,7 +1,27 @@
@using Contracts.Types;
@using System.Net;
@inject IShogiApi ShogiApi;
<GameBoardPresentation IsSpectating="true" Perspective="WhichPlayer.Player1" Session="Session" />
<GameBoardPresentation IsSpectating="true"
Perspective="WhichPlayer.Player1"
Session="Session"
OnClickJoinGame="OnClickJoinGame" />
@code {
[Parameter] public Session Session { get; set; }
protected override void OnParametersSet()
{
base.OnParametersSet();
if (Session == null)
{
throw new ArgumentException($"{nameof(Session)} cannot be null.", nameof(Session));
}
}
async Task OnClickJoinGame()
{
var response = await ShogiApi.PatchJoinGame(Session.SessionName);
response.EnsureSuccessStatusCode();
}
}