checkpoint
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public interface IJoinGameHandler
|
||||
{
|
||||
Task Handle(JoinGameRequest request, string userName);
|
||||
}
|
||||
public class JoinGameHandler : IJoinGameHandler
|
||||
{
|
||||
private readonly IGameboardManager gameboardManager;
|
||||
private readonly ISocketConnectionManager connectionManager;
|
||||
public JoinGameHandler(
|
||||
ISocketConnectionManager communicationManager,
|
||||
IGameboardManager gameboardManager)
|
||||
{
|
||||
this.gameboardManager = gameboardManager;
|
||||
this.connectionManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(JoinGameRequest request, string userName)
|
||||
{
|
||||
var joinSucceeded = await gameboardManager.AssignPlayer2ToSession(request.GameName, userName);
|
||||
|
||||
var response = new JoinGameResponse()
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = request.GameName
|
||||
};
|
||||
if (joinSucceeded)
|
||||
{
|
||||
await connectionManager.BroadcastToAll(response);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Error = "Game is full or does not exist.";
|
||||
await connectionManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
{
|
||||
public interface IGameboardManager
|
||||
{
|
||||
Task<bool> IsPlayer1(string sessionName, string playerName);
|
||||
Task<bool> AssignPlayer2ToSession(string sessionName, string userName);
|
||||
Task<bool> AssignPlayer2ToSession(string sessionName, User user);
|
||||
Task<User?> ReadUser(ClaimsPrincipal user);
|
||||
Task<bool> CreateUser(ClaimsPrincipal user);
|
||||
}
|
||||
|
||||
public class GameboardManager : IGameboardManager
|
||||
@@ -23,14 +23,25 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public Task<User?> ReadUser(ClaimsPrincipal user)
|
||||
public Task<bool> CreateUser(ClaimsPrincipal principal)
|
||||
{
|
||||
var userId = user.UserId();
|
||||
if (user.IsGuest() && Guid.TryParse(userId, out var webSessionId))
|
||||
var id = principal.UserId();
|
||||
if (string.IsNullOrEmpty(id))
|
||||
{
|
||||
return repository.ReadGuestUser(webSessionId);
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(userId))
|
||||
|
||||
var user = principal.IsGuest()
|
||||
? User.CreateGuestUser(id)
|
||||
: User.CreateMsalUser(id);
|
||||
|
||||
return repository.CreateUser(user);
|
||||
}
|
||||
|
||||
public Task<User?> ReadUser(ClaimsPrincipal principal)
|
||||
{
|
||||
var userId = principal.UserId();
|
||||
if (!string.IsNullOrEmpty(userId))
|
||||
{
|
||||
return repository.ReadUser(userId);
|
||||
}
|
||||
@@ -38,12 +49,6 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
return Task.FromResult<User?>(null);
|
||||
}
|
||||
|
||||
public async Task<bool> IsPlayer1(string sessionName, string playerName)
|
||||
{
|
||||
//var session = await repository.GetGame(sessionName);
|
||||
//return session?.Player1 == playerName;
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<string> CreateJoinCode(string sessionName, string playerName)
|
||||
{
|
||||
@@ -55,19 +60,15 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public async Task<bool> AssignPlayer2ToSession(string sessionName, string userName)
|
||||
public async Task<bool> AssignPlayer2ToSession(string sessionName, User user)
|
||||
{
|
||||
var isSuccess = false;
|
||||
var session = await repository.ReadSessionMetaData(sessionName);
|
||||
if (session != null && !session.IsPrivate && string.IsNullOrEmpty(session.Player2))
|
||||
if (session != null && !session.IsPrivate && session.Player2 == null)
|
||||
{
|
||||
session.SetPlayer2(userName);
|
||||
if (await repository.UpdateSession(session))
|
||||
{
|
||||
isSuccess = true;
|
||||
}
|
||||
session.SetPlayer2(user);
|
||||
return await repository.UpdateSession(session);
|
||||
}
|
||||
return isSuccess;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.WebSockets;
|
||||
@@ -106,9 +107,32 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
foreach (var kvp in connections)
|
||||
{
|
||||
var socket = kvp.Value;
|
||||
tasks.Add(socket.SendTextAsync(message));
|
||||
try
|
||||
{
|
||||
|
||||
tasks.Add(socket.SendTextAsync(message));
|
||||
}
|
||||
catch (WebSocketException webSocketException)
|
||||
{
|
||||
logger.LogInformation("Tried sending a message to socket connection for user [{user}], but found the connection has closed.", kvp.Key);
|
||||
UnsubscribeFromBroadcastAndGames(kvp.Key);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogInformation("Tried sending a message to socket connection for user [{user}], but found the connection has closed.", kvp.Key);
|
||||
UnsubscribeFromBroadcastAndGames(kvp.Key);
|
||||
}
|
||||
}
|
||||
return Task.WhenAll(tasks);
|
||||
try
|
||||
{
|
||||
var task = Task.WhenAll(tasks);
|
||||
return task;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Yo");
|
||||
}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
//public Task BroadcastToGame(string gameName, IResponse forPlayer1, IResponse forPlayer2)
|
||||
|
||||
Reference in New Issue
Block a user