This commit is contained in:
2023-01-29 16:53:26 -06:00
parent 8a25c0ed35
commit e2eff4f8b5
5 changed files with 35 additions and 15 deletions

View File

@@ -118,7 +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()); await communicationManager.BroadcastToAll(new SessionJoinedByPlayerSocketMessage(session.Name));
return this.Ok(); return this.Ok();
} }
return this.Conflict("This game already has two players."); return this.Conflict("This game already has two players.");

View File

@@ -1,9 +1,15 @@
using Shogi.Contracts.Types; using Shogi.Contracts.Types;
namespace Shogi.Contracts.Socket namespace Shogi.Contracts.Socket;
public class SessionJoinedByPlayerSocketMessage : ISocketResponse
{ {
public class SessionJoinedByPlayerSocketMessage : ISocketResponse
{
public SocketAction Action => SocketAction.SessionJoined; public SocketAction Action => SocketAction.SessionJoined;
public string SessionName { get; }
public SessionJoinedByPlayerSocketMessage(string sessionName)
{
SessionName = sessionName;
} }
} }

View File

@@ -13,7 +13,7 @@
} }
else if (isSpectating) else if (isSpectating)
{ {
<SpectatorGameBoard Session="session" OnRefetchSession="RefetchSession" /> <SpectatorGameBoard Session="session" />
} }
else else
{ {
@@ -32,16 +32,25 @@ else
protected override void OnInitialized() protected override void OnInitialized()
{ {
base.OnInitialized(); base.OnInitialized();
ShogiSocket.OnPlayerMoved += OnPlayerMoved_RefetchSession; ShogiSocket.OnPlayerMoved += OnPlayerMoved_FetchSession;
ShogiSocket.OnSessionJoined += ShogiSocket.OnSessionJoined += OnSessionJoined_FetchSession;
} }
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
{ {
await RefetchSession(); await FetchSession();
} }
async Task RefetchSession() Task OnSessionJoined_FetchSession(SessionJoinedByPlayerSocketMessage args)
{
if (args.SessionName == SessionName)
{
return FetchSession();
}
return Task.CompletedTask;
}
async Task FetchSession()
{ {
if (!string.IsNullOrWhiteSpace(SessionName)) if (!string.IsNullOrWhiteSpace(SessionName))
{ {
@@ -59,11 +68,11 @@ else
} }
} }
Task OnPlayerMoved_RefetchSession(PlayerHasMovedMessage args) Task OnPlayerMoved_FetchSession(PlayerHasMovedMessage args)
{ {
if (args.SessionName == SessionName) if (args.SessionName == SessionName)
{ {
return RefetchSession(); return FetchSession();
} }
return Task.CompletedTask; return Task.CompletedTask;
} }

View File

@@ -1,5 +1,6 @@
@implements IDisposable; @implements IDisposable;
@using Shogi.Contracts.Socket;
@using Shogi.Contracts.Types; @using Shogi.Contracts.Types;
@using System.ComponentModel.DataAnnotations; @using System.ComponentModel.DataAnnotations;
@using System.Net; @using System.Net;
@@ -98,7 +99,7 @@
{ {
base.OnInitialized(); base.OnInitialized();
ShogiSocket.OnSessionCreated += FetchSessions; ShogiSocket.OnSessionCreated += FetchSessions;
ShogiSocket.OnSessionJoined += FetchSessions; ShogiSocket.OnSessionJoined += OnSessionJoined_FetchSessions;
Account.LoginChangedEvent += LoginChangedEvent_FetchSessions; Account.LoginChangedEvent += LoginChangedEvent_FetchSessions;
} }
@@ -109,6 +110,9 @@
activeSession = s; activeSession = s;
ActiveSessionChanged?.Invoke(s); ActiveSessionChanged?.Invoke(s);
} }
Task OnSessionJoined_FetchSessions(SessionJoinedByPlayerSocketMessage args) => FetchSessions();
Task LoginChangedEvent_FetchSessions(LoginEventArgs args) Task LoginChangedEvent_FetchSessions(LoginEventArgs args)
{ {
if (args.User != null) if (args.User != null)
@@ -137,7 +141,7 @@
public void Dispose() public void Dispose()
{ {
ShogiSocket.OnSessionCreated -= FetchSessions; ShogiSocket.OnSessionCreated -= FetchSessions;
ShogiSocket.OnSessionJoined -= FetchSessions; ShogiSocket.OnSessionJoined -= OnSessionJoined_FetchSessions;
Account.LoginChangedEvent -= LoginChangedEvent_FetchSessions; Account.LoginChangedEvent -= LoginChangedEvent_FetchSessions;
} }

View File

@@ -11,7 +11,7 @@ namespace Shogi.UI.Shared;
public class ShogiSocket : IDisposable public class ShogiSocket : IDisposable
{ {
public event AsyncEventHandler? OnSessionCreated; public event AsyncEventHandler? OnSessionCreated;
public event AsyncEventHandler? OnSessionJoined; public event AsyncEventHandler<SessionJoinedByPlayerSocketMessage>? OnSessionJoined;
public event AsyncEventHandler<PlayerHasMovedMessage>? OnPlayerMoved; public event AsyncEventHandler<PlayerHasMovedMessage>? OnPlayerMoved;
private readonly ClientWebSocket socket; private readonly ClientWebSocket socket;
@@ -76,7 +76,8 @@ public class ShogiSocket : IDisposable
case SocketAction.SessionJoined: case SocketAction.SessionJoined:
if (this.OnSessionJoined is not null) if (this.OnSessionJoined is not null)
{ {
await this.OnSessionJoined(); var args = JsonSerializer.Deserialize<SessionJoinedByPlayerSocketMessage>(memory[..result.Count], serializerOptions);
await this.OnSessionJoined(args!);
} }
break; break;
case SocketAction.PieceMoved: case SocketAction.PieceMoved: