68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
|
using Gameboard.ShogiUI.Sockets.Repositories;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
|
{
|
|
public class JoinByCodeHandler : IActionHandler
|
|
{
|
|
private readonly ILogger<JoinByCodeHandler> logger;
|
|
private readonly IGameboardRepository repository;
|
|
private readonly ISocketCommunicationManager communicationManager;
|
|
|
|
public JoinByCodeHandler(
|
|
ILogger<JoinByCodeHandler> logger,
|
|
ISocketCommunicationManager communicationManager,
|
|
IGameboardRepository repository)
|
|
{
|
|
this.logger = logger;
|
|
this.repository = repository;
|
|
this.communicationManager = communicationManager;
|
|
}
|
|
|
|
public async Task Handle(string json, string userName)
|
|
{
|
|
var request = JsonConvert.DeserializeObject<JoinByCode>(json);
|
|
var joinGameResponse = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
|
|
{
|
|
PlayerName = userName,
|
|
JoinCode = request.JoinCode
|
|
});
|
|
|
|
if (joinGameResponse.JoinSucceeded)
|
|
{
|
|
// Other members of the game see a regular JoinGame occur.
|
|
var response = new JoinGameResponse(ClientAction.JoinGame)
|
|
{
|
|
PlayerName = userName,
|
|
GameName = joinGameResponse.SessionName
|
|
};
|
|
// At this time, userName hasn't subscribed and won't receive this message.
|
|
await communicationManager.BroadcastToGame(joinGameResponse.SessionName, response);
|
|
|
|
// The player joining sees the JoinByCode occur.
|
|
response = new JoinGameResponse(ClientAction.JoinByCode)
|
|
{
|
|
PlayerName = userName,
|
|
GameName = joinGameResponse.SessionName
|
|
};
|
|
await communicationManager.BroadcastToPlayers(response, userName);
|
|
}
|
|
else
|
|
{
|
|
var response = new JoinGameResponse(ClientAction.JoinByCode)
|
|
{
|
|
PlayerName = userName,
|
|
GameName = joinGameResponse.SessionName,
|
|
Error = "Error joining game."
|
|
};
|
|
await communicationManager.BroadcastToPlayers(response, userName);
|
|
}
|
|
}
|
|
}
|
|
}
|