Allow unauthorized users to search and spectate.
This commit is contained in:
@@ -69,8 +69,6 @@ public class ShogiApplication(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ public class SessionsController(
|
|||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("{sessionId}")]
|
[HttpGet("{sessionId}")]
|
||||||
|
[AllowAnonymous]
|
||||||
public async Task<ActionResult<Session>> GetSession(Guid sessionId)
|
public async Task<ActionResult<Session>> GetSession(Guid sessionId)
|
||||||
{
|
{
|
||||||
var session = await application.ReadSession(sessionId.ToString());
|
var session = await application.ReadSession(sessionId.ToString());
|
||||||
@@ -78,12 +79,11 @@ public class SessionsController(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet()]
|
[HttpGet]
|
||||||
|
[AllowAnonymous]
|
||||||
public async Task<ActionResult<SessionMetadata[]>> ReadAllSessionsMetadata()
|
public async Task<ActionResult<SessionMetadata[]>> ReadAllSessionsMetadata()
|
||||||
{
|
{
|
||||||
var id = this.User.GetId();
|
var id = this.User.GetId() ?? string.Empty;
|
||||||
if (id == null) return this.Unauthorized();
|
|
||||||
|
|
||||||
var dtos = await application.ReadAllSessionMetadatas(id);
|
var dtos = await application.ReadAllSessionMetadatas(id);
|
||||||
return dtos
|
return dtos
|
||||||
.Select(dto => new SessionMetadata
|
.Select(dto => new SessionMetadata
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ app.UseCors(policy =>
|
|||||||
policy.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod().AllowCredentials();
|
policy.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod().AllowCredentials();
|
||||||
});
|
});
|
||||||
|
|
||||||
app.MapHub<GameHub>("/gamehub").RequireAuthorization();
|
app.MapHub<GameHub>("/gamehub");
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,11 @@
|
|||||||
<a href="">Home</a>
|
<a href="">Home</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<AuthorizeView>
|
<p>
|
||||||
<p>
|
<a href="search">Search</a>
|
||||||
<a href="search">Search</a>
|
</p>
|
||||||
</p>
|
|
||||||
|
|
||||||
|
<AuthorizeView>
|
||||||
<p>
|
<p>
|
||||||
<button class="href" @onclick="CreateSession">Create</button>
|
<button class="href" @onclick="CreateSession">Create</button>
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -56,9 +56,16 @@ else
|
|||||||
if (this.session != null)
|
if (this.session != null)
|
||||||
{
|
{
|
||||||
var state = await authenticationState;
|
var state = await authenticationState;
|
||||||
var accountId = state.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
|
var accountId = state.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
|
||||||
this.perspective = accountId == session.Player1 ? WhichPlayer.Player1 : WhichPlayer.Player2;
|
if (accountId == null)
|
||||||
this.isSpectating = !(accountId == this.session.Player1 || accountId == this.session.Player2);
|
{
|
||||||
|
this.isSpectating = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.perspective = accountId == session.Player1 ? WhichPlayer.Player1 : WhichPlayer.Player2;
|
||||||
|
this.isSpectating = !(accountId == this.session.Player1 || accountId == this.session.Player2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,9 +74,11 @@
|
|||||||
<div class="player-area">
|
<div class="player-area">
|
||||||
@if (Perspective == WhichPlayer.Player2 && string.IsNullOrEmpty(Session.Player2))
|
@if (Perspective == WhichPlayer.Player2 && string.IsNullOrEmpty(Session.Player2))
|
||||||
{
|
{
|
||||||
<div class="place-self-center">
|
<AuthorizeView>
|
||||||
<button @onclick="OnClickJoinGameInternal">Join Game</button>
|
<div class="place-self-center">
|
||||||
</div>
|
<button @onclick="OnClickJoinGameInternal">Join Game</button>
|
||||||
|
</div>
|
||||||
|
</AuthorizeView>
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -115,23 +115,19 @@
|
|||||||
{
|
{
|
||||||
var pieceBeingMoved = Session.BoardState.Board[selectedBoardPosition];
|
var pieceBeingMoved = Session.BoardState.Board[selectedBoardPosition];
|
||||||
var isPromotedAlready = pieceBeingMoved != null && pieceBeingMoved.IsPromoted;
|
var isPromotedAlready = pieceBeingMoved != null && pieceBeingMoved.IsPromoted;
|
||||||
Console.WriteLine("Is promoted? {0}", isPromotedAlready);
|
|
||||||
// Moving to an empty space or capturing an opponent's piece.
|
// Moving to an empty space or capturing an opponent's piece.
|
||||||
if (!isPromotedAlready && (IsWithinPromoteArea(position) || IsWithinPromoteArea(selectedBoardPosition)))
|
if (!isPromotedAlready && (IsWithinPromoteArea(position) || IsWithinPromoteArea(selectedBoardPosition)))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Prompt!");
|
|
||||||
moveTo = position;
|
moveTo = position;
|
||||||
showPromotePrompt = true;
|
showPromotePrompt = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var success = await ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedBoardPosition, position, false));
|
var success = await ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedBoardPosition, position, false));
|
||||||
Console.WriteLine("Success? {0}", success);
|
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
selectedBoardPosition = null;
|
selectedBoardPosition = null;
|
||||||
showError = true;
|
showError = true;
|
||||||
Console.WriteLine("Show error");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|||||||
@@ -2,10 +2,12 @@
|
|||||||
@using System.Net
|
@using System.Net
|
||||||
@inject ShogiApi ShogiApi
|
@inject ShogiApi ShogiApi
|
||||||
|
|
||||||
<GameBoardPresentation IsSpectating="true"
|
<Stretch style="position: relative;">
|
||||||
Perspective="WhichPlayer.Player2"
|
<GameBoardPresentation IsSpectating="true"
|
||||||
Session="Session"
|
Perspective="WhichPlayer.Player2"
|
||||||
OnClickJoinGame="OnClickJoinGame" />
|
Session="Session"
|
||||||
|
OnClickJoinGame="OnClickJoinGame" />
|
||||||
|
</Stretch>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
[Parameter]
|
[Parameter]
|
||||||
|
|||||||
@@ -13,14 +13,12 @@
|
|||||||
<span></span>
|
<span></span>
|
||||||
</row>
|
</row>
|
||||||
<hr />
|
<hr />
|
||||||
<AuthorizeView>
|
@foreach (var session in allSessions)
|
||||||
@foreach (var session in allSessions)
|
{
|
||||||
{
|
<row>
|
||||||
<row>
|
<GameBrowserEntry Session="session" OnSessionDeleted="FetchSessions" />
|
||||||
<GameBrowserEntry Session="session" OnSessionDeleted="FetchSessions" />
|
</row>
|
||||||
</row>
|
}
|
||||||
}
|
|
||||||
</AuthorizeView>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@if (allSessions.Length == 0)
|
@if (allSessions.Length == 0)
|
||||||
@@ -40,9 +38,10 @@
|
|||||||
async Task FetchSessions()
|
async Task FetchSessions()
|
||||||
{
|
{
|
||||||
var sessions = await ShogiApi.GetAllSessionsMetadata();
|
var sessions = await ShogiApi.GetAllSessionsMetadata();
|
||||||
|
Console.WriteLine("Session count {0}", sessions.Length);
|
||||||
if (sessions != null)
|
if (sessions != null)
|
||||||
{
|
{
|
||||||
this.allSessions = sessions.ToArray();
|
this.allSessions = sessions;
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,39 +3,39 @@
|
|||||||
@inject ShogiApi Api
|
@inject ShogiApi Api
|
||||||
|
|
||||||
<gameBrowserEntry>
|
<gameBrowserEntry>
|
||||||
<AuthorizeView>
|
@if (showDeletePrompt)
|
||||||
@if (showDeletePrompt)
|
{
|
||||||
{
|
<modal class="PrimaryTheme ThemeVariant--Contrast">
|
||||||
<modal class="PrimaryTheme ThemeVariant--Contrast">
|
<div style="display: flex; gap: 1rem; justify-content: flex-end;">
|
||||||
<div style="display: flex; gap: 1rem; justify-content: flex-end;">
|
@if (showDeleteError)
|
||||||
@if (showDeleteError)
|
{
|
||||||
{
|
<p style="color: darkred;">An error occurred.</p>
|
||||||
<p style="color: darkred;">An error occurred.</p>
|
<div style="flex: 1;" />
|
||||||
<div style="flex: 1;" />
|
<button @onclick="HideModal">Cancel</button>
|
||||||
<button @onclick="HideModal">Cancel</button>
|
}
|
||||||
}
|
else
|
||||||
else
|
{
|
||||||
{
|
<p>Do you wish to delete this session?</p>
|
||||||
<p>Do you wish to delete this session?</p>
|
<div style="flex: 1;" />
|
||||||
<div style="flex: 1;" />
|
<button @onclick="HideModal">No</button>
|
||||||
<button @onclick="HideModal">No</button>
|
<button @onclick="DeleteSession">Yes</button>
|
||||||
<button @onclick="DeleteSession">Yes</button>
|
}
|
||||||
}
|
</div>
|
||||||
</div>
|
</modal>
|
||||||
</modal>
|
}
|
||||||
}
|
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<a href="play/@Session.SessionId">@Session.Player1</a>
|
<a href="play/@Session.SessionId">@Session.Player1</a>
|
||||||
</div>
|
</div>
|
||||||
@if (string.IsNullOrEmpty(Session.Player2))
|
@if (string.IsNullOrEmpty(Session.Player2))
|
||||||
{
|
{
|
||||||
<span>1 / 2</span>
|
<span>1 / 2</span>
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
<span>Full</span>
|
<span>Full</span>
|
||||||
}
|
}
|
||||||
|
<AuthorizeView>
|
||||||
@if (context.User.Identity?.Name == Session.Player1)
|
@if (context.User.Identity?.Name == Session.Player1)
|
||||||
{
|
{
|
||||||
<IconButton OnClick="() => showDeletePrompt = true">
|
<IconButton OnClick="() => showDeletePrompt = true">
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
@attribute [Authorize]
|
@page "/play/{sessionId}"
|
||||||
@page "/play/{sessionId}"
|
|
||||||
|
|
||||||
@inject GameHubNode node
|
@inject GameHubNode node
|
||||||
|
|
||||||
@@ -8,9 +7,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
<main class="PrimaryTheme">
|
<main class="PrimaryTheme">
|
||||||
<AuthorizeView>
|
<GameBoard SessionId="@SessionId" />
|
||||||
<GameBoard SessionId="@SessionId" />
|
|
||||||
</AuthorizeView>
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
|
|||||||
@@ -447,9 +447,6 @@ namespace UnitTests
|
|||||||
// P2 King retreat
|
// P2 King retreat
|
||||||
shogi.Move("E8", "E9", false);
|
shogi.Move("E8", "E9", false);
|
||||||
|
|
||||||
console.WriteLine(shogi.ToStringStateAsAscii());
|
|
||||||
|
|
||||||
|
|
||||||
// Act - P1 Pawn wins by checkmate.
|
// Act - P1 Pawn wins by checkmate.
|
||||||
shogi.Move("E7", "E8", false);
|
shogi.Move("E7", "E8", false);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user