Files
Shogi/Shogi.UI/Pages/Play/GameBoard/GameBoardPresentation.razor
2024-11-16 12:37:56 -06:00

209 lines
5.9 KiB
Plaintext

@using Shogi.Contracts.Types;
@using System.Text.Json;
<article class="game-board">
<!-- Controls -->
<header class="controls">
<form @onsubmit:preventDefault>
<fieldset class="history" disabled=@Yep>
<legend>Replay</legend>
<button disabled=@Yep>&lt;</button>
<button>&gt;</button>
</fieldset>
</form>
</header>
<!-- Game board -->
<section class="board" data-perspective="@Perspective">
@for (var rank = 1; rank < 10; rank++)
{
foreach (var file in Files)
{
var position = $"{file}{rank}";
var piece = Session?.BoardState.Board[position];
var isSelected = piece != null && SelectedPosition == position;
<div class="tile" @onclick="OnClickTileInternal(position)"
data-position="@(position)"
data-selected="@(isSelected)"
style="grid-area: @position">
@if (piece != null)
{
<GamePiece Piece="piece.WhichPiece" RenderUpsideDown="@(piece.Owner != Perspective)" IsPromoted="piece.IsPromoted" />
}
</div>
}
}
<div class="ruler vertical" style="grid-area: rank">
<span>9</span>
<span>8</span>
<span>7</span>
<span>6</span>
<span>5</span>
<span>4</span>
<span>3</span>
<span>2</span>
<span>1</span>
</div>
<div class="ruler" style="grid-area: file">
<span>A</span>
<span>B</span>
<span>C</span>
<span>D</span>
<span>E</span>
<span>F</span>
<span>G</span>
<span>H</span>
<span>I</span>
</div>
</section>
<!-- Side board -->
@if (Session != null && UseSideboard == true)
{
<aside class="side-board PrimaryTheme ThemeVariant--Contrast">
<div class="player-area">
<div class="hand">
@if (opponentHand.Any())
{
@foreach (var piece in opponentHand)
{
<div class="tile">
<GamePiece Piece="piece" RenderUpsideDown="true" />
</div>
}
}
</div>
<p class="text-center">Opponent's Hand</p>
</div>
<div class="place-self-center">
<PlayerName Name="@opponentName" IsTurn="!IsMyTurn" InCheck="IsOpponentInCheck" IsVictor="IsOpponentVictor" />
<hr />
<PlayerName Name="@userName" IsTurn="IsMyTurn" InCheck="IsPlayerInCheck" IsVictor="IsPlayerVictor" />
</div>
<div class="player-area">
@if (Perspective == WhichPlayer.Player2 && string.IsNullOrEmpty(Session.Player2))
{
<AuthorizeView>
<div class="place-self-center">
<button @onclick="OnClickJoinGameInternal">Join Game</button>
</div>
</AuthorizeView>
}
else
{
<p class="text-center">Your Hand</p>
<div class="hand">
@if (userHand.Any())
{
@foreach (var whichPiece in userHand)
{
<div @onclick="OnClickHandInternal(whichPiece)"
class="tile"
data-selected="@(whichPiece == SelectedPieceFromHand)">
<GamePiece Piece="whichPiece" RenderUpsideDown="false" />
</div>
}
}
</div>
}
</div>
</aside>
}
</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; }
[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 EventCallback<string> OnClickTile { get; set; }
[Parameter] public EventCallback<WhichPiece> OnClickHand { get; set; }
[Parameter] public EventCallback OnClickJoinGame { get; set; }
[Parameter] public bool UseSideboard { get; set; } = true;
[Parameter] public IList<BoardState> History { get; set; }
private bool Yep => History.Count == 0;
private IReadOnlyCollection<WhichPiece> opponentHand;
private IReadOnlyCollection<WhichPiece> userHand;
private string? userName;
private string? opponentName;
private int historyIndex;
public GameBoardPresentation()
{
opponentHand = [];
userHand = [];
userName = string.Empty;
opponentName = string.Empty;
History = [];
}
protected override void OnParametersSet()
{
base.OnParametersSet();
if (Session == null)
{
opponentHand = [];
userHand = [];
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 ?? "Empty Seat";
opponentName = Perspective == WhichPlayer.Player1
? this.Session.Player2 ?? "Empty Seat"
: this.Session.Player1;
}
Console.WriteLine("Count: {0}", History.Count);
}
private bool IsMyTurn => Session?.BoardState.WhoseTurn == Perspective;
private bool IsPlayerInCheck => Session?.BoardState.PlayerInCheck == Perspective;
private bool IsOpponentInCheck => Session?.BoardState.PlayerInCheck != null && Session.BoardState.PlayerInCheck != Perspective;
private bool IsPlayerVictor => Session?.BoardState.Victor == Perspective;
private bool IsOpponentVictor => Session?.BoardState.Victor != null && Session.BoardState.Victor != Perspective;
private Func<Task> OnClickTileInternal(string position) => () =>
{
if (IsMyTurn)
{
return OnClickTile.InvokeAsync(position);
}
return Task.CompletedTask;
};
private Func<Task> OnClickHandInternal(WhichPiece piece) => () =>
{
if (IsMyTurn)
{
return OnClickHand.InvokeAsync(piece);
}
return Task.CompletedTask;
};
private Task OnClickJoinGameInternal() => OnClickJoinGame.InvokeAsync();
}