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

@@ -77,6 +77,11 @@ public class SessionsController : ControllerBase
return Ok(await this.queryRespository.ReadSessionPlayerCount(this.User.GetShogiUserId())); return Ok(await this.queryRespository.ReadSessionPlayerCount(this.User.GetShogiUserId()));
} }
/// <summary>
/// Fetch the session and latest board state. Also subscribe the user to socket events for this session.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[HttpGet("{name}")] [HttpGet("{name}")]
public async Task<ActionResult<ReadSessionResponse>> GetSession(string name) public async Task<ActionResult<ReadSessionResponse>> GetSession(string name)
{ {
@@ -113,6 +118,7 @@ public class SessionsController : ControllerBase
session.AddPlayer2(User.GetShogiUserId()); session.AddPlayer2(User.GetShogiUserId());
await sessionRepository.SetPlayer2(name, User.GetShogiUserId()); await sessionRepository.SetPlayer2(name, User.GetShogiUserId());
await communicationManager.BroadcastToAll(new SessionJoinedByPlayerSocketMessage());
return this.Ok(); return this.Ok();
} }
return this.Conflict("This game already has two players."); return this.Conflict("This game already has two players.");
@@ -144,6 +150,8 @@ public class SessionsController : ControllerBase
return this.Conflict(e.Message); return this.Conflict(e.Message);
} }
await sessionRepository.CreateMove(sessionName, command); await sessionRepository.CreateMove(sessionName, command);
// Send socket message to both players so their clients know that new board state is available.
await communicationManager.BroadcastToPlayers( await communicationManager.BroadcastToPlayers(
new PlayerHasMovedMessage new PlayerHasMovedMessage
{ {

View File

@@ -14,60 +14,68 @@ namespace Shogi.Api.Controllers;
[Authorize] [Authorize]
public class UserController : ControllerBase public class UserController : ControllerBase
{ {
private readonly ISocketTokenCache tokenCache; private readonly ISocketTokenCache tokenCache;
private readonly ISocketConnectionManager connectionManager; private readonly ISocketConnectionManager connectionManager;
private readonly IUserRepository userRepository; private readonly IUserRepository userRepository;
private readonly IShogiUserClaimsTransformer claimsTransformation; private readonly IShogiUserClaimsTransformer claimsTransformation;
private readonly AuthenticationProperties authenticationProps; private readonly AuthenticationProperties authenticationProps;
public UserController( public UserController(
ILogger<UserController> logger, ILogger<UserController> logger,
ISocketTokenCache tokenCache, ISocketTokenCache tokenCache,
ISocketConnectionManager connectionManager, ISocketConnectionManager connectionManager,
IUserRepository userRepository, IUserRepository userRepository,
IShogiUserClaimsTransformer claimsTransformation) IShogiUserClaimsTransformer claimsTransformation)
{ {
this.tokenCache = tokenCache; this.tokenCache = tokenCache;
this.connectionManager = connectionManager; this.connectionManager = connectionManager;
this.userRepository = userRepository; this.userRepository = userRepository;
this.claimsTransformation = claimsTransformation; this.claimsTransformation = claimsTransformation;
authenticationProps = new AuthenticationProperties authenticationProps = new AuthenticationProperties
{ {
AllowRefresh = true, AllowRefresh = true,
IsPersistent = true IsPersistent = true
}; };
} }
[HttpGet("Token")] [HttpGet("Token")]
public ActionResult<CreateTokenResponse> GetWebSocketToken() public ActionResult<CreateTokenResponse> GetWebSocketToken()
{ {
var userId = User.GetShogiUserId(); var userId = User.GetShogiUserId();
var displayName = User.GetShogiUserDisplayname(); var displayName = User.GetShogiUserDisplayname();
var token = tokenCache.GenerateToken(userId); var token = tokenCache.GenerateToken(userId);
return new CreateTokenResponse return new CreateTokenResponse
{ {
DisplayName = displayName, DisplayName = displayName,
OneTimeToken = token, OneTimeToken = token,
UserId = userId UserId = userId
}; };
} }
[AllowAnonymous] /// <summary>
[HttpGet("LoginAsGuest")] /// </summary>
public async Task<IActionResult> GuestLogin() /// <param name="returnUrl">Used by cookie authentication.</param>
{ /// <returns></returns>
var principal = await this.claimsTransformation.CreateClaimsFromGuestPrincipal(User); [AllowAnonymous]
if (principal != null) [HttpGet("LoginAsGuest")]
{ public async Task<IActionResult> GuestLogin([FromQuery] string returnUrl)
await HttpContext.SignInAsync( {
CookieAuthenticationDefaults.AuthenticationScheme, var principal = await this.claimsTransformation.CreateClaimsFromGuestPrincipal(User);
principal, if (principal != null)
authenticationProps {
); await HttpContext.SignInAsync(
} CookieAuthenticationDefaults.AuthenticationScheme,
return Ok(); principal,
} authenticationProps
);
}
if (!string.IsNullOrWhiteSpace(returnUrl))
{
return Redirect(returnUrl);
}
return Ok();
}
[HttpPut("GuestLogout")] [HttpPut("GuestLogout")]
public async Task<IActionResult> GuestLogout() public async Task<IActionResult> GuestLogout()

View File

@@ -9,91 +9,96 @@ using System.Text.Json;
namespace Shogi.Api.Services namespace Shogi.Api.Services
{ {
public interface ISocketService public interface ISocketService
{
Task HandleSocketRequest(HttpContext context);
}
/// <summary>
/// Services a single websocket connection. Authenticates the socket connection, accepts messages, and sends messages.
/// </summary>
public class SocketService : ISocketService
{
private readonly ILogger<SocketService> logger;
private readonly ISocketConnectionManager communicationManager;
private readonly ISocketTokenCache tokenManager;
public SocketService(
ILogger<SocketService> logger,
ISocketConnectionManager communicationManager,
ISocketTokenCache tokenManager) : base()
{ {
this.logger = logger; Task HandleSocketRequest(HttpContext context);
this.communicationManager = communicationManager;
this.tokenManager = tokenManager;
} }
public async Task HandleSocketRequest(HttpContext context) /// <summary>
/// Services a single websocket connection. Authenticates the socket connection, accepts messages, and sends messages.
/// </summary>
public class SocketService : ISocketService
{ {
if (!context.Request.Query.Keys.Contains("token")) private readonly ILogger<SocketService> logger;
{ private readonly ISocketConnectionManager communicationManager;
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; private readonly ISocketTokenCache tokenManager;
return;
}
var token = Guid.Parse(context.Request.Query["token"][0]);
var userName = tokenManager.GetUsername(token);
if (string.IsNullOrEmpty(userName)) public SocketService(
{ ILogger<SocketService> logger,
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; ISocketConnectionManager communicationManager,
return; ISocketTokenCache tokenManager) : base()
} {
var socket = await context.WebSockets.AcceptWebSocketAsync(); this.logger = logger;
this.communicationManager = communicationManager;
this.tokenManager = tokenManager;
}
communicationManager.Subscribe(socket, userName); public async Task HandleSocketRequest(HttpContext context)
while (socket.State == WebSocketState.Open)
{
try
{ {
var message = await socket.ReceiveTextAsync(); if (!context.Request.Query.Keys.Contains("token"))
if (string.IsNullOrWhiteSpace(message)) continue; {
logger.LogInformation("Request \n{0}\n", message); context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
var request = JsonSerializer.Deserialize<ISocketRequest>(message); return;
if (request == null || !Enum.IsDefined(typeof(SocketAction), request.Action)) }
{ var token = Guid.Parse(context.Request.Query["token"][0] ?? throw new InvalidOperationException("Token expected during socket connection request, but was not sent."));
await socket.SendTextAsync("Error: Action not recognized."); var userName = tokenManager.GetUsername(token);
continue;
} if (string.IsNullOrEmpty(userName))
switch (request.Action) {
{ context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
default: return;
await socket.SendTextAsync($"Received your message with action {request.Action}, but did no work."); }
break; var socket = await context.WebSockets.AcceptWebSocketAsync();
}
communicationManager.Subscribe(socket, userName);
// TODO: I probably don't need this while-loop anymore? Perhaps unsubscribe when a disconnect is detected instead.
while (socket.State.HasFlag(WebSocketState.Open))
{
try
{
var message = await socket.ReceiveTextAsync();
if (string.IsNullOrWhiteSpace(message)) continue;
logger.LogInformation("Request \n{0}\n", message);
var request = JsonSerializer.Deserialize<ISocketRequest>(message);
if (request == null || !Enum.IsDefined(typeof(SocketAction), request.Action))
{
await socket.SendTextAsync("Error: Action not recognized.");
continue;
}
switch (request.Action)
{
default:
await socket.SendTextAsync($"Received your message with action {request.Action}, but did no work.");
break;
}
}
catch (OperationCanceledException ex)
{
logger.LogError(ex.Message);
}
catch (WebSocketException ex)
{
logger.LogInformation($"{nameof(WebSocketException)} in {nameof(SocketConnectionManager)}.");
logger.LogInformation("Probably tried writing to a closed socket.");
logger.LogError(ex.Message);
}
communicationManager.Unsubscribe(userName);
if (!socket.State.HasFlag(WebSocketState.Closed) && !socket.State.HasFlag(WebSocketState.Aborted))
{
try
{
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure,
"Socket closed",
CancellationToken.None);
}
catch (Exception ex)
{
Console.WriteLine($"Ignored exception during socket closing. {ex.Message}");
}
}
}
} }
catch (OperationCanceledException ex)
{
logger.LogError(ex.Message);
}
catch (WebSocketException ex)
{
logger.LogInformation($"{nameof(WebSocketException)} in {nameof(SocketConnectionManager)}.");
logger.LogInformation("Probably tried writing to a closed socket.");
logger.LogError(ex.Message);
}
communicationManager.Unsubscribe(userName);
}
} }
public async Task<bool> ValidateRequestAndReplyIfInvalid<TRequest>(WebSocket socket, IValidator<TRequest> validator, TRequest request)
{
var results = validator.Validate(request);
if (!results.IsValid)
{
var errors = string.Join('\n', results.Errors.Select(_ => _.ErrorMessage));
await socket.SendTextAsync(errors);
}
return results.IsValid;
}
}
} }

View File

@@ -4,15 +4,17 @@ namespace Shogi.Contracts.Socket;
public class PlayerHasMovedMessage : ISocketResponse public class PlayerHasMovedMessage : ISocketResponse
{ {
public SocketAction Action { get; } public SocketAction Action { get; }
public string SessionName { get; set; } public string SessionName { get; set; }
/// <summary> /// <summary>
/// The player that made the move. /// The player that made the move.
/// </summary> /// </summary>
public string PlayerName { get; set; } public string PlayerName { get; set; }
public PlayerHasMovedMessage() public PlayerHasMovedMessage()
{ {
Action = SocketAction.PieceMoved; Action = SocketAction.PieceMoved;
} SessionName = string.Empty;
PlayerName = string.Empty;
}
} }

View File

@@ -4,10 +4,5 @@ namespace Shogi.Contracts.Socket;
public class SessionCreatedSocketMessage : ISocketResponse public class SessionCreatedSocketMessage : ISocketResponse
{ {
public SocketAction Action { get; } public SocketAction Action => SocketAction.SessionCreated;
public SessionCreatedSocketMessage()
{
Action = SocketAction.SessionCreated;
}
} }

View File

@@ -0,0 +1,9 @@
using Shogi.Contracts.Types;
namespace Shogi.Contracts.Socket
{
public class SessionJoinedByPlayerSocketMessage : ISocketResponse
{
public SocketAction Action => SocketAction.SessionJoined;
}
}

View File

@@ -3,22 +3,6 @@
<Router AppAssembly="@typeof(App).Assembly"> <Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData"> <Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
@*<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<Authorizing>
Authorizing!!
</Authorizing>
<NotAuthorized>
@if (context.User.Identity?.IsAuthenticated != true)
{
<RedirectToLogin />
}
else
{
<p role="alert">You are not authorized to access this resource.</p>
}
</NotAuthorized>
</AuthorizeRouteView>*@
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found> </Found>
<NotFound> <NotFound>
<PageTitle>Not found</PageTitle> <PageTitle>Not found</PageTitle>

View File

@@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Shogi.UI.Pages.Home.Api; using Shogi.UI.Pages.Home.Api;
using Shogi.UI.Shared; using Shogi.UI.Shared;
using System.Text.Json;
namespace Shogi.UI.Pages.Home.Account; namespace Shogi.UI.Pages.Home.Account;
@@ -35,21 +34,23 @@ public class AccountManager
this.shogiSocket = shogiSocket; this.shogiSocket = shogiSocket;
} }
private User? MyUser { get => accountState.User; set => accountState.User = value; } private User? MyUser => accountState.User;
private Task SetUser(User user) => accountState.SetUser(user);
public async Task LoginWithGuestAccount() public async Task LoginWithGuestAccount()
{ {
var response = await shogiApi.GetToken(WhichAccountPlatform.Guest); var response = await shogiApi.GetToken(WhichAccountPlatform.Guest);
if (response != null) if (response != null)
{ {
MyUser = new User await SetUser(new User
{ {
DisplayName = response.DisplayName, DisplayName = response.DisplayName,
Id = response.UserId, Id = response.UserId,
OneTimeSocketToken = response.OneTimeToken,
WhichAccountPlatform = WhichAccountPlatform.Guest WhichAccountPlatform = WhichAccountPlatform.Guest
}; });
await shogiSocket.OpenAsync(MyUser.Value.OneTimeSocketToken.ToString()); await shogiSocket.OpenAsync(response.OneTimeToken.ToString());
await localStorage.SetAccountPlatform(WhichAccountPlatform.Guest); await localStorage.SetAccountPlatform(WhichAccountPlatform.Guest);
} }
} }
@@ -80,11 +81,12 @@ public class AccountManager
var response = await shogiApi.GetToken(WhichAccountPlatform.Guest); var response = await shogiApi.GetToken(WhichAccountPlatform.Guest);
if (response != null) if (response != null)
{ {
MyUser = new User( await accountState.SetUser(new User(
Id: response.UserId, Id: response.UserId,
DisplayName: response.DisplayName, DisplayName: response.DisplayName,
WhichAccountPlatform: WhichAccountPlatform.Guest, WhichAccountPlatform: WhichAccountPlatform.Guest));
OneTimeSocketToken: response.OneTimeToken); await shogiSocket.OpenAsync(response.OneTimeToken.ToString());
return true;
} }
} }
else if (platform == WhichAccountPlatform.Microsoft) else if (platform == WhichAccountPlatform.Microsoft)
@@ -101,26 +103,21 @@ public class AccountManager
} }
var id = state.User.Claims.Single(claim => claim.Type == "oid").Value; var id = state.User.Claims.Single(claim => claim.Type == "oid").Value;
var displayName = state.User.Identity.Name; var displayName = state.User.Identity.Name;
MyUser = new User( await accountState.SetUser(new User(
Id: id, Id: id,
DisplayName: displayName, DisplayName: displayName,
WhichAccountPlatform: WhichAccountPlatform.Microsoft, WhichAccountPlatform: WhichAccountPlatform.Microsoft));
OneTimeSocketToken: response.OneTimeToken); await shogiSocket.OpenAsync(response.OneTimeToken.ToString());
return true;
} }
} }
if (MyUser != null)
{
await shogiSocket.OpenAsync(MyUser.Value.OneTimeSocketToken.ToString());
return true;
}
return false; return false;
} }
public async Task LogoutAsync() public async Task LogoutAsync()
{ {
MyUser = null; await accountState.SetUser(null);
var platform = await localStorage.GetAccountPlatform(); var platform = await localStorage.GetAccountPlatform();
await localStorage.DeleteAccountPlatform(); await localStorage.DeleteAccountPlatform();

View File

@@ -1,28 +1,27 @@
namespace Shogi.UI.Pages.Home.Account; using static Shogi.UI.Shared.Events;
namespace Shogi.UI.Pages.Home.Account;
public class AccountState public class AccountState
{ {
public event EventHandler<LoginEventArgs>? LoginChangedEvent; public event AsyncEventHandler<LoginEventArgs>? LoginChangedEvent;
private User? user; public User? User { get; private set; }
public User? User
public Task SetUser(User? user)
{ {
get => user; User = user;
set return EmitLoginChangedEvent();
}
private async Task EmitLoginChangedEvent()
{
if (LoginChangedEvent is not null)
{ {
if (user != value) await LoginChangedEvent.Invoke(new LoginEventArgs
{ {
user = value; User = User
EmitLoginChangedEvent(); });
}
} }
} }
private void EmitLoginChangedEvent()
{
LoginChangedEvent?.Invoke(this, new LoginEventArgs
{
User = User
});
}
} }

View File

@@ -3,8 +3,7 @@
public readonly record struct User( public readonly record struct User(
string Id, string Id,
string DisplayName, string DisplayName,
WhichAccountPlatform WhichAccountPlatform, WhichAccountPlatform WhichAccountPlatform)
Guid OneTimeSocketToken)
{ {
} }
} }

View File

@@ -12,6 +12,6 @@ public interface IShogiApi
Task<CreateTokenResponse?> GetToken(WhichAccountPlatform whichAccountPlatform); Task<CreateTokenResponse?> GetToken(WhichAccountPlatform whichAccountPlatform);
Task GuestLogout(); Task GuestLogout();
Task Move(string sessionName, MovePieceCommand move); Task Move(string sessionName, MovePieceCommand move);
Task<HttpStatusCode> PatchJoinGame(string name); Task<HttpResponseMessage> PatchJoinGame(string name);
Task<HttpStatusCode> PostSession(string name, bool isPrivate); Task<HttpStatusCode> PostSession(string name, bool isPrivate);
} }

View File

@@ -65,8 +65,16 @@ namespace Shogi.UI.Pages.Home.Api
var httpClient = whichAccountPlatform == WhichAccountPlatform.Microsoft var httpClient = whichAccountPlatform == WhichAccountPlatform.Microsoft
? clientFactory.CreateClient(MsalClientName) ? clientFactory.CreateClient(MsalClientName)
: clientFactory.CreateClient(GuestClientName); : clientFactory.CreateClient(GuestClientName);
var response = await httpClient.GetFromJsonAsync<CreateTokenResponse>(RelativeUri("User/Token"), serializerOptions); var response = await httpClient.GetAsync(RelativeUri("User/Token"));
return response; if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
if (!string.IsNullOrEmpty(content))
{
return await response.Content.ReadFromJsonAsync<CreateTokenResponse>(serializerOptions);
}
}
return null;
} }
public async Task Move(string sessionName, MovePieceCommand command) public async Task Move(string sessionName, MovePieceCommand command)
@@ -83,10 +91,9 @@ namespace Shogi.UI.Pages.Home.Api
return response.StatusCode; return response.StatusCode;
} }
public async Task<HttpStatusCode> PatchJoinGame(string name) public Task<HttpResponseMessage> PatchJoinGame(string name)
{ {
var response = await HttpClient.PatchAsync(RelativeUri($"Sessions/{name}/Join"), null); return HttpClient.PatchAsync(RelativeUri($"Sessions/{name}/Join"), null);
return response.StatusCode;
} }
private static Uri RelativeUri(string path) => new Uri(path, UriKind.Relative); private static Uri RelativeUri(string path) => new Uri(path, UriKind.Relative);

View File

@@ -3,9 +3,4 @@
<GameBoardPresentation Perspective="WhichPlayer.Player1" /> <GameBoardPresentation Perspective="WhichPlayer.Player1" />
@code { @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.Api
@using Shogi.Contracts.Socket;
@using Shogi.Contracts.Types; @using Shogi.Contracts.Types;
@using System.Text.RegularExpressions; @using System.Text.RegularExpressions;
@inject IShogiApi ShogiApi @inject IShogiApi ShogiApi
@inject AccountState Account; @inject AccountState Account;
@inject PromotePrompt PromotePrompt; @inject PromotePrompt PromotePrompt;
@inject ShogiSocket ShogiSocket;
@if (session == null) @if (session == null)
{ {
@@ -11,11 +13,11 @@
} }
else if (isSpectating) else if (isSpectating)
{ {
<SpectatorGameBoard Session="session" /> <SpectatorGameBoard Session="session" OnRefetchSession="RefetchSession" />
} }
else else
{ {
<SeatedGameBoard Perspective="perspective" Session="session" OnRefetchSession="RefetchSession" /> <SeatedGameBoard Perspective="perspective" Session="session" />
} }
@@ -27,6 +29,13 @@ else
private WhichPlayer perspective; private WhichPlayer perspective;
private bool isSpectating; private bool isSpectating;
protected override void OnInitialized()
{
base.OnInitialized();
ShogiSocket.OnPlayerMoved += OnPlayerMoved_RefetchSession;
ShogiSocket.OnSessionJoined +=
}
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
{ {
await RefetchSession(); await RefetchSession();
@@ -34,7 +43,7 @@ else
async Task RefetchSession() async Task RefetchSession()
{ {
if (!string.IsNullOrWhiteSpace(SessionName)) if (!string.IsNullOrWhiteSpace(SessionName))
{ {
this.session = await ShogiApi.GetSession(SessionName); this.session = await ShogiApi.GetSession(SessionName);
if (this.session != null) if (this.session != null)
@@ -44,9 +53,18 @@ else
this.perspective = accountId == session.Player2 ? WhichPlayer.Player2 : WhichPlayer.Player1; this.perspective = accountId == session.Player2 ? WhichPlayer.Player2 : WhichPlayer.Player1;
this.isSpectating = !(accountId == this.session.Player1 || accountId == this.session.Player2); this.isSpectating = !(accountId == this.session.Player1 || accountId == this.session.Player2);
Console.WriteLine($"IsSpectating - {isSpectating}. AccountId - {accountId}. Player1 - {this.session.Player1}. Player2 - {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"> <aside class="side-board">
<div class="player-area"> <div class="player-area">
<div class="hand"> <div class="hand">
@if (OpponentHand.Any()) @if (opponentHand.Any())
{ {
@foreach (var piece in OpponentHand) @foreach (var piece in opponentHand)
{ {
<div class="tile"> <div class="tile">
<GamePiece Piece="piece" Perspective="Perspective" /> <GamePiece Piece="piece" Perspective="Perspective" />
@@ -86,6 +86,8 @@
</div> </div>
<div class="spacer place-self-center"> <div class="spacer place-self-center">
<p>@opponentName</p>
<p>@userName</p>
</div> </div>
<div class="player-area"> <div class="player-area">
@@ -93,15 +95,15 @@
{ {
<div class="place-self-center"> <div class="place-self-center">
<p>Seat is Empty</p> <p>Seat is Empty</p>
<button @onclick="OnClickJoinGameInternal()">Join Game</button> <button @onclick="OnClickJoinGameInternal">Join Game</button>
</div> </div>
} }
else else
{ {
<div class="hand"> <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)"> <div class="title" @onclick="OnClickHandInternal(piece)">
<GamePiece Piece="piece" Perspective="Perspective" /> <GamePiece Piece="piece" Perspective="Perspective" />
@@ -121,6 +123,11 @@
</article> </article>
@code { @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 bool IsSpectating { get; set; } = false;
[Parameter] public WhichPlayer Perspective { get; set; } [Parameter] public WhichPlayer Perspective { get; set; }
[Parameter] public Session? Session { get; set; } [Parameter] public Session? Session { get; set; }
@@ -130,32 +137,48 @@
[Parameter] public Func<Piece, Task>? OnClickHand { get; set; } [Parameter] public Func<Piece, Task>? OnClickHand { get; set; }
[Parameter] public Func<Task>? OnClickJoinGame { 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 opponentHand = Array.Empty<Piece>();
{ userHand = Array.Empty<Piece>();
if (this.Session == null) return Array.Empty<Piece>(); userName = string.Empty;
opponentName = string.Empty;
return Perspective == WhichPlayer.Player1
? this.Session.BoardState.Player1Hand
: this.Session.BoardState.Player2Hand;
}
} }
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.Player1Hand
: this.Session.BoardState.Player2Hand; : 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 OnClickTileInternal(Piece? piece, string position) => () => OnClickTile?.Invoke(piece, position);
private Action OnClickHandInternal(Piece piece) => () => OnClickHand?.Invoke(piece); 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" Perspective="Perspective"
OnClickHand="OnClickHand" OnClickHand="OnClickHand"
OnClickTile="OnClickTile" OnClickTile="OnClickTile"
OnClickJoinGame="OnClickJoinGame" /> SelectedPosition="@selectedBoardPosition" />
@code { @code {
[Parameter, EditorRequired] [Parameter, EditorRequired]
public WhichPlayer Perspective { get; set; } public WhichPlayer Perspective { get; set; }
[Parameter, EditorRequired] [Parameter, EditorRequired]
public Session Session { get; set; } public Session Session { get; set; }
[Parameter] public Func<Task>? OnRefetchSession { get; set; }
private bool IsMyTurn => Session?.BoardState.WhoseTurn == Perspective; private bool IsMyTurn => Session?.BoardState.WhoseTurn == Perspective;
private string? selectedBoardPosition; private string? selectedBoardPosition;
private WhichPiece? selectedPieceFromHand; 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) bool ShouldPromptForPromotion(string position)
{ {
if (Perspective == WhichPlayer.Player1 && Regex.IsMatch(position, ".[7-9]")) if (Perspective == WhichPlayer.Player1 && Regex.IsMatch(position, ".[7-9]"))
@@ -36,12 +44,16 @@
async Task OnClickTile(Piece? piece, string position) async Task OnClickTile(Piece? piece, string position)
{ {
Console.WriteLine("Is my turn?");
Console.WriteLine(true);
if (!IsMyTurn) return; if (!IsMyTurn) return;
if (selectedBoardPosition == null || piece?.Owner == Perspective) if (selectedBoardPosition == null || piece?.Owner == Perspective)
{ {
// Select a position. // Select a position.
Console.WriteLine("Position {0}", position);
selectedBoardPosition = position; selectedBoardPosition = position;
StateHasChanged();
return; return;
} }
if (selectedBoardPosition == position) if (selectedBoardPosition == position)
@@ -77,16 +89,4 @@
selectedPieceFromHand = piece.WhichPiece; selectedPieceFromHand = piece.WhichPiece;
await Task.CompletedTask; 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 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 { @code {
[Parameter] public Session Session { get; set; } [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();
}
} }

View File

@@ -1,7 +1,10 @@
@using Shogi.Contracts.Types; @implements IDisposable;
@using Shogi.Contracts.Types;
@using System.ComponentModel.DataAnnotations; @using System.ComponentModel.DataAnnotations;
@using System.Net; @using System.Net;
@using System.Text.Json; @using System.Text.Json;
@inject IShogiApi ShogiApi; @inject IShogiApi ShogiApi;
@inject ShogiSocket ShogiSocket; @inject ShogiSocket ShogiSocket;
@inject AccountState Account; @inject AccountState Account;
@@ -76,7 +79,6 @@
The name you chose is taken; choose another. The name you chose is taken; choose another.
</div> </div>
} }
</EditForm> </EditForm>
</div> </div>
</div> </div>
@@ -92,17 +94,12 @@
private SessionMetadata? activeSession; private SessionMetadata? activeSession;
private HttpStatusCode? createSessionStatusCode; private HttpStatusCode? createSessionStatusCode;
protected override async Task OnInitializedAsync() protected override void OnInitialized()
{ {
ShogiSocket.OnCreateGameMessage += async (sender, message) => await FetchSessions(); base.OnInitialized();
Account.LoginChangedEvent += async (sender, message) => ShogiSocket.OnSessionCreated += FetchSessions;
{ ShogiSocket.OnSessionJoined += FetchSessions;
Console.WriteLine($"LoginEvent. Message={JsonSerializer.Serialize(message)}."); Account.LoginChangedEvent += LoginChangedEvent_FetchSessions;
if (message.User != null)
{
await FetchSessions();
}
};
} }
string ActiveCss(SessionMetadata s) => s == activeSession ? "active" : string.Empty; string ActiveCss(SessionMetadata s) => s == activeSession ? "active" : string.Empty;
@@ -112,6 +109,14 @@
activeSession = s; activeSession = s;
ActiveSessionChanged?.Invoke(s); ActiveSessionChanged?.Invoke(s);
} }
Task LoginChangedEvent_FetchSessions(LoginEventArgs args)
{
if (args.User != null)
{
return FetchSessions();
}
return Task.CompletedTask;
}
async Task FetchSessions() async Task FetchSessions()
{ {
@@ -129,6 +134,13 @@
createSessionStatusCode = await ShogiApi.PostSession(createForm.Name, createForm.IsPrivate); createSessionStatusCode = await ShogiApi.PostSession(createForm.Name, createForm.IsPrivate);
} }
public void Dispose()
{
ShogiSocket.OnSessionCreated -= FetchSessions;
ShogiSocket.OnSessionJoined -= FetchSessions;
Account.LoginChangedEvent -= LoginChangedEvent_FetchSessions;
}
private class CreateForm private class CreateForm
{ {
[Required] [Required]

View File

@@ -26,9 +26,16 @@
</main> </main>
@code { @code {
bool welcomeModalIsVisible = false; private bool welcomeModalIsVisible;
string activeSessionName = string.Empty; private string activeSessionName;
ClientWebSocket socket = new ClientWebSocket(); private ClientWebSocket socket;
public Home()
{
welcomeModalIsVisible = false;
activeSessionName = string.Empty;
socket = new ClientWebSocket();
}
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
@@ -40,10 +47,11 @@
} }
} }
private void OnLoginChanged(object? sender, LoginEventArgs args) private Task OnLoginChanged(LoginEventArgs args)
{ {
welcomeModalIsVisible = args.User == null; welcomeModalIsVisible = args.User == null;
StateHasChanged(); StateHasChanged();
return Task.CompletedTask;
} }
private void OnChangeSession(SessionMetadata s) private void OnChangeSession(SessionMetadata s)
{ {

View File

@@ -5,10 +5,10 @@
<h1>Shogi</h1> <h1>Shogi</h1>
@if (user != null) @if (user != null)
{ {
<div class="user"> <div class="user">
<div>@user.Value.DisplayName</div> <div>@user.Value.DisplayName</div>
<button type="button" class="logout" @onclick="AccountManager.LogoutAsync">Logout</button> <button type="button" class="logout" @onclick="AccountManager.LogoutAsync">Logout</button>
</div> </div>
} }
@*<LoginDisplay />*@ @*<LoginDisplay />*@
</div> </div>
@@ -21,11 +21,12 @@
Account.LoginChangedEvent += OnLoginChange; Account.LoginChangedEvent += OnLoginChange;
} }
private void OnLoginChange(object? sender, LoginEventArgs args) private Task OnLoginChange(LoginEventArgs args)
{ {
if (args == null) if (args == null)
throw new ArgumentException(nameof(args)); throw new ArgumentException(nameof(args));
user = args.User; user = args.User;
StateHasChanged(); StateHasChanged();
return Task.CompletedTask;
} }
} }

View File

@@ -17,49 +17,55 @@ await builder.Build().RunAsync();
static void ConfigureDependencies(IServiceCollection services, IConfiguration configuration) static void ConfigureDependencies(IServiceCollection services, IConfiguration configuration)
{ {
/** /**
* Why two HTTP clients? * Why two HTTP clients?
* See qhttps://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?source=recommendations&view=aspnetcore-6.0#unauthenticated-or-unauthorized-web-api-requests-in-an-app-with-a-secure-default-client * See qhttps://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?source=recommendations&view=aspnetcore-6.0#unauthenticated-or-unauthorized-web-api-requests-in-an-app-with-a-secure-default-client
*/ */
var shogiApiUrl = new Uri(configuration["ShogiApiUrl"], UriKind.Absolute); var baseUrl = configuration["ShogiApiUrl"];
services if (string.IsNullOrWhiteSpace(baseUrl))
.AddHttpClient(ShogiApi.MsalClientName, client => client.BaseAddress = shogiApiUrl) {
.AddHttpMessageHandler<MsalMessageHandler>(); throw new InvalidOperationException("ShogiApiUrl configuration is missing.");
services }
.AddHttpClient(ShogiApi.GuestClientName, client => client.BaseAddress = shogiApiUrl)
.AddHttpMessageHandler<CookieCredentialsMessageHandler>();
// Authorization var shogiApiUrl = new Uri(baseUrl, UriKind.Absolute);
services.AddMsalAuthentication(options => services
{ .AddHttpClient(ShogiApi.MsalClientName, client => client.BaseAddress = shogiApiUrl)
configuration.Bind("AzureAd", options.ProviderOptions.Authentication); .AddHttpMessageHandler<MsalMessageHandler>();
options.ProviderOptions.LoginMode = "redirect"; services
}); .AddHttpClient(ShogiApi.GuestClientName, client => client.BaseAddress = shogiApiUrl)
services.AddOidcAuthentication(options => .AddHttpMessageHandler<CookieCredentialsMessageHandler>();
{
// Configure your authentication provider options here.
// For more information, see https://aka.ms/blazor-standalone-auth
configuration.Bind("AzureAd", options.ProviderOptions);
options.ProviderOptions.ResponseType = "code";
});
// https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-6.0#service-lifetime // Authorization
services.AddScoped<ClientWebSocket>(); services.AddMsalAuthentication(options =>
services.AddScoped<AccountManager>(); {
services.AddScoped<AccountState>(); configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
services.AddScoped<ShogiSocket>(); options.ProviderOptions.LoginMode = "redirect";
services.AddScoped<ModalService>(); });
services.AddScoped<ILocalStorage, LocalStorage>(); services.AddOidcAuthentication(options =>
services.AddScoped<MsalMessageHandler>(); {
services.AddScoped<CookieCredentialsMessageHandler>(); // Configure your authentication provider options here.
services.AddScoped<IShogiApi, ShogiApi>(); // For more information, see https://aka.ms/blazor-standalone-auth
services.AddScoped<PromotePrompt>(); configuration.Bind("AzureAd", options.ProviderOptions);
options.ProviderOptions.ResponseType = "code";
});
var serializerOptions = new JsonSerializerOptions // https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-6.0#service-lifetime
{ services.AddScoped<ClientWebSocket>();
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, services.AddScoped<AccountManager>();
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, services.AddScoped<AccountState>();
WriteIndented = true services.AddScoped<ShogiSocket>();
}; services.AddScoped<ModalService>();
services.AddScoped((sp) => serializerOptions); services.AddScoped<ILocalStorage, LocalStorage>();
services.AddScoped<MsalMessageHandler>();
services.AddScoped<CookieCredentialsMessageHandler>();
services.AddScoped<IShogiApi, ShogiApi>();
services.AddScoped<PromotePrompt>();
var serializerOptions = new JsonSerializerOptions
{
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
services.AddScoped((sp) => serializerOptions);
} }

View File

@@ -0,0 +1,8 @@
namespace Shogi.UI.Shared
{
public static class Events
{
public delegate Task AsyncEventHandler();
public delegate Task AsyncEventHandler<TArgs>(TArgs args);
}
}

View File

@@ -1,24 +0,0 @@
@*@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication*@
@*@inject NavigationManager Navigation
@inject SignOutSessionStateManager SignOutManager*@
@*<AuthorizeView>
<Authorized>
Hello, @context.User.Identity?.Name!
<button class="nav-link btn btn-link" @onclick="BeginSignOut">Log out</button>
</Authorized>
<NotAuthorized>
<a href="authentication/login">Log in</a>
</NotAuthorized>
</AuthorizeView>*@
@code{
// https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?view=aspnetcore-6.0#customize-the-authentication-user-interface
//private async Task BeginSignOut(MouseEventArgs args)
//{
// await SignOutManager.SetSignOutState();
// Navigation.NavigateTo("authentication/logout");
//}
}

View File

@@ -1,5 +0,0 @@
<h3>MyNotAuthorized</h3>
@code {
}

View File

@@ -1,14 +0,0 @@
@inject NavigationManager Navigation
@inject ModalService ModalService
@inject AccountManager ShogiService
@*<button @onclick="() => ShogiService.ConnectAsync(asGuest: true)">Guest Login</button>*@
<div>Not implemented!</div>
@code {
protected override void OnInitialized()
{
//ModalService.ShowLoginModal();
Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}");
}
}

View File

@@ -4,12 +4,15 @@ using Shogi.Contracts.Types;
using System.Buffers; using System.Buffers;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text.Json; using System.Text.Json;
using static Shogi.UI.Shared.Events;
namespace Shogi.UI.Shared; namespace Shogi.UI.Shared;
public class ShogiSocket : IDisposable public class ShogiSocket : IDisposable
{ {
public event EventHandler<SessionCreatedSocketMessage>? OnCreateGameMessage; public event AsyncEventHandler? OnSessionCreated;
public event AsyncEventHandler? OnSessionJoined;
public event AsyncEventHandler<PlayerHasMovedMessage>? OnPlayerMoved;
private readonly ClientWebSocket socket; private readonly ClientWebSocket socket;
private readonly JsonSerializerOptions serializerOptions; private readonly JsonSerializerOptions serializerOptions;
@@ -37,8 +40,11 @@ public class ShogiSocket : IDisposable
await socket.ConnectAsync(this.uriBuilder.Uri, cancelToken.Token); await socket.ConnectAsync(this.uriBuilder.Uri, cancelToken.Token);
Console.WriteLine("Socket Connected"); Console.WriteLine("Socket Connected");
// Fire and forget! I'm way too lazy to write my own javascript interop to a web worker. Nooo thanks. // Fire and forget! I'm way too lazy to write my own javascript interop to a web worker. Nooo thanks.
var listening = Listen().ContinueWith(antecedent => _ = Listen().ContinueWith(async antecedent =>
{ {
Console.WriteLine($"Socket fault. {antecedent.Exception}");
this.cancelToken.Cancel();
await this.socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Page was probably closed or refresh.", CancellationToken.None);
if (antecedent.Exception != null) if (antecedent.Exception != null)
{ {
throw antecedent.Exception; throw antecedent.Exception;
@@ -58,16 +64,33 @@ public class ShogiSocket : IDisposable
.GetProperty(nameof(ISocketResponse.Action)) .GetProperty(nameof(ISocketResponse.Action))
.Deserialize<SocketAction>(); .Deserialize<SocketAction>();
Console.WriteLine($"Socket action: {action}");
switch (action) switch (action)
{ {
case SocketAction.SessionCreated: case SocketAction.SessionCreated:
Console.WriteLine("Session created event."); if (this.OnSessionCreated is not null)
this.OnCreateGameMessage?.Invoke(this, JsonSerializer.Deserialize<SessionCreatedSocketMessage>(memory, this.serializerOptions)!); {
await this.OnSessionCreated();
}
break;
case SocketAction.SessionJoined:
if (this.OnSessionJoined is not null)
{
await this.OnSessionJoined();
}
break;
case SocketAction.PieceMoved:
if (this.OnPlayerMoved is not null)
{
var args = JsonSerializer.Deserialize<PlayerHasMovedMessage>(memory[..result.Count], serializerOptions);
await this.OnPlayerMoved(args!);
}
break; break;
default: default:
break; throw new NotImplementedException($"Socket message for action:{action} is not implemented.");
} }
} }
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Socket closed because cancellation token was cancelled.", CancellationToken.None);
if (!cancelToken.IsCancellationRequested) if (!cancelToken.IsCancellationRequested)
{ {
throw new InvalidOperationException("Stopped socket listening without cancelling."); throw new InvalidOperationException("Stopped socket listening without cancelling.");