From 0a62eb75821e11bd92c73e6ef46fcc771566fcf7 Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Sun, 3 Nov 2024 15:53:13 -0600 Subject: [PATCH] Allow unauthorized users to search and spectate. --- Shogi.Api/Application/ShogiApplication.cs | 2 - Shogi.Api/Controllers/SessionsController.cs | 8 +-- Shogi.Api/Program.cs | 2 +- Shogi.UI/Layout/NavMenu.razor | 8 +-- Shogi.UI/Pages/Play/GameBoard/GameBoard.razor | 13 +++- .../GameBoard/GameBoardPresentation.razor | 8 ++- .../Play/GameBoard/SeatedGameBoard.razor | 4 -- .../Play/GameBoard/SpectatorGameBoard.razor | 10 +-- Shogi.UI/Pages/Play/GameBrowser.razor | 17 +++-- Shogi.UI/Pages/Play/GameBrowserEntry.razor | 64 +++++++++---------- Shogi.UI/Pages/Play/PlayPage.razor | 7 +- Tests/UnitTests/ShogiShould.cs | 3 - 12 files changed, 72 insertions(+), 74 deletions(-) diff --git a/Shogi.Api/Application/ShogiApplication.cs b/Shogi.Api/Application/ShogiApplication.cs index e659b46..792e18a 100644 --- a/Shogi.Api/Application/ShogiApplication.cs +++ b/Shogi.Api/Application/ShogiApplication.cs @@ -69,8 +69,6 @@ public class ShogiApplication( } } - - return session; } diff --git a/Shogi.Api/Controllers/SessionsController.cs b/Shogi.Api/Controllers/SessionsController.cs index 4df8378..95b3a2b 100644 --- a/Shogi.Api/Controllers/SessionsController.cs +++ b/Shogi.Api/Controllers/SessionsController.cs @@ -54,6 +54,7 @@ public class SessionsController( /// /// [HttpGet("{sessionId}")] + [AllowAnonymous] public async Task> GetSession(Guid sessionId) { var session = await application.ReadSession(sessionId.ToString()); @@ -78,12 +79,11 @@ public class SessionsController( }; } - [HttpGet()] + [HttpGet] + [AllowAnonymous] public async Task> ReadAllSessionsMetadata() { - var id = this.User.GetId(); - if (id == null) return this.Unauthorized(); - + var id = this.User.GetId() ?? string.Empty; var dtos = await application.ReadAllSessionMetadatas(id); return dtos .Select(dto => new SessionMetadata diff --git a/Shogi.Api/Program.cs b/Shogi.Api/Program.cs index 05a587e..673199a 100644 --- a/Shogi.Api/Program.cs +++ b/Shogi.Api/Program.cs @@ -56,7 +56,7 @@ app.UseCors(policy => policy.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod().AllowCredentials(); }); -app.MapHub("/gamehub").RequireAuthorization(); +app.MapHub("/gamehub"); app.Run(); diff --git a/Shogi.UI/Layout/NavMenu.razor b/Shogi.UI/Layout/NavMenu.razor index 962e17d..441f068 100644 --- a/Shogi.UI/Layout/NavMenu.razor +++ b/Shogi.UI/Layout/NavMenu.razor @@ -7,11 +7,11 @@ Home

- -

- Search -

+

+ Search +

+

diff --git a/Shogi.UI/Pages/Play/GameBoard/GameBoard.razor b/Shogi.UI/Pages/Play/GameBoard/GameBoard.razor index 1b313eb..1724e1b 100644 --- a/Shogi.UI/Pages/Play/GameBoard/GameBoard.razor +++ b/Shogi.UI/Pages/Play/GameBoard/GameBoard.razor @@ -56,9 +56,16 @@ else if (this.session != null) { var state = await authenticationState; - var accountId = state.User.Claims.First(c => c.Type == ClaimTypes.Name).Value; - this.perspective = accountId == session.Player1 ? WhichPlayer.Player1 : WhichPlayer.Player2; - this.isSpectating = !(accountId == this.session.Player1 || accountId == this.session.Player2); + var accountId = state.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; + if (accountId == null) + { + this.isSpectating = true; + } + else + { + this.perspective = accountId == session.Player1 ? WhichPlayer.Player1 : WhichPlayer.Player2; + this.isSpectating = !(accountId == this.session.Player1 || accountId == this.session.Player2); + } } StateHasChanged(); } diff --git a/Shogi.UI/Pages/Play/GameBoard/GameBoardPresentation.razor b/Shogi.UI/Pages/Play/GameBoard/GameBoardPresentation.razor index a7c180a..8dbd0b0 100644 --- a/Shogi.UI/Pages/Play/GameBoard/GameBoardPresentation.razor +++ b/Shogi.UI/Pages/Play/GameBoard/GameBoardPresentation.razor @@ -74,9 +74,11 @@
@if (Perspective == WhichPlayer.Player2 && string.IsNullOrEmpty(Session.Player2)) { -
- -
+ +
+ +
+
} else { diff --git a/Shogi.UI/Pages/Play/GameBoard/SeatedGameBoard.razor b/Shogi.UI/Pages/Play/GameBoard/SeatedGameBoard.razor index 0b4f2f7..fc66d1c 100644 --- a/Shogi.UI/Pages/Play/GameBoard/SeatedGameBoard.razor +++ b/Shogi.UI/Pages/Play/GameBoard/SeatedGameBoard.razor @@ -115,23 +115,19 @@ { var pieceBeingMoved = Session.BoardState.Board[selectedBoardPosition]; var isPromotedAlready = pieceBeingMoved != null && pieceBeingMoved.IsPromoted; - Console.WriteLine("Is promoted? {0}", isPromotedAlready); // Moving to an empty space or capturing an opponent's piece. if (!isPromotedAlready && (IsWithinPromoteArea(position) || IsWithinPromoteArea(selectedBoardPosition))) { - Console.WriteLine("Prompt!"); moveTo = position; showPromotePrompt = true; } else { var success = await ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedBoardPosition, position, false)); - Console.WriteLine("Success? {0}", success); if (!success) { selectedBoardPosition = null; showError = true; - Console.WriteLine("Show error"); } } StateHasChanged(); diff --git a/Shogi.UI/Pages/Play/GameBoard/SpectatorGameBoard.razor b/Shogi.UI/Pages/Play/GameBoard/SpectatorGameBoard.razor index fe52683..fcc8dd2 100644 --- a/Shogi.UI/Pages/Play/GameBoard/SpectatorGameBoard.razor +++ b/Shogi.UI/Pages/Play/GameBoard/SpectatorGameBoard.razor @@ -2,10 +2,12 @@ @using System.Net @inject ShogiApi ShogiApi - + + + @code { [Parameter] diff --git a/Shogi.UI/Pages/Play/GameBrowser.razor b/Shogi.UI/Pages/Play/GameBrowser.razor index df4dd50..81d47fd 100644 --- a/Shogi.UI/Pages/Play/GameBrowser.razor +++ b/Shogi.UI/Pages/Play/GameBrowser.razor @@ -13,14 +13,12 @@
- - @foreach (var session in allSessions) - { - - - - } - + @foreach (var session in allSessions) + { + + + + }
@if (allSessions.Length == 0) @@ -40,9 +38,10 @@ async Task FetchSessions() { var sessions = await ShogiApi.GetAllSessionsMetadata(); + Console.WriteLine("Session count {0}", sessions.Length); if (sessions != null) { - this.allSessions = sessions.ToArray(); + this.allSessions = sessions; StateHasChanged(); } } diff --git a/Shogi.UI/Pages/Play/GameBrowserEntry.razor b/Shogi.UI/Pages/Play/GameBrowserEntry.razor index 5e104fa..389db93 100644 --- a/Shogi.UI/Pages/Play/GameBrowserEntry.razor +++ b/Shogi.UI/Pages/Play/GameBrowserEntry.razor @@ -3,39 +3,39 @@ @inject ShogiApi Api - - @if (showDeletePrompt) - { - -
- @if (showDeleteError) - { -

An error occurred.

-
- - } - else - { -

Do you wish to delete this session?

-
- - - } -
- - } + @if (showDeletePrompt) + { + +
+ @if (showDeleteError) + { +

An error occurred.

+
+ + } + else + { +

Do you wish to delete this session?

+
+ + + } +
+ + } - - @if (string.IsNullOrEmpty(Session.Player2)) - { - 1 / 2 - } - else - { - Full - } + + @if (string.IsNullOrEmpty(Session.Player2)) + { + 1 / 2 + } + else + { + Full + } + @if (context.User.Identity?.Name == Session.Player1) { diff --git a/Shogi.UI/Pages/Play/PlayPage.razor b/Shogi.UI/Pages/Play/PlayPage.razor index 0f568d6..212553a 100644 --- a/Shogi.UI/Pages/Play/PlayPage.razor +++ b/Shogi.UI/Pages/Play/PlayPage.razor @@ -1,5 +1,4 @@ -@attribute [Authorize] -@page "/play/{sessionId}" +@page "/play/{sessionId}" @inject GameHubNode node @@ -8,9 +7,7 @@ return; }
- - - +
@code { diff --git a/Tests/UnitTests/ShogiShould.cs b/Tests/UnitTests/ShogiShould.cs index bd0f949..064e303 100644 --- a/Tests/UnitTests/ShogiShould.cs +++ b/Tests/UnitTests/ShogiShould.cs @@ -447,9 +447,6 @@ namespace UnitTests // P2 King retreat shogi.Move("E8", "E9", false); - console.WriteLine(shogi.ToStringStateAsAscii()); - - // Act - P1 Pawn wins by checkmate. shogi.Move("E7", "E8", false);