Files
Shogi/Gameboard.ShogiUI.Sockets/Managers/GameboardManager.cs
2021-09-03 22:43:06 -05:00

74 lines
2.1 KiB
C#

using Gameboard.ShogiUI.Sockets.Extensions;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Repositories;
using System;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers
{
public interface IGameboardManager
{
Task<bool> IsPlayer1(string sessionName, string playerName);
Task<bool> AssignPlayer2ToSession(string sessionName, string userName);
Task<User?> ReadUser(ClaimsPrincipal user);
}
public class GameboardManager : IGameboardManager
{
private readonly IGameboardRepository repository;
public GameboardManager(IGameboardRepository repository)
{
this.repository = repository;
}
public Task<User?> ReadUser(ClaimsPrincipal user)
{
var userId = user.UserId();
if (user.IsGuest() && Guid.TryParse(userId, out var webSessionId))
{
return repository.ReadGuestUser(webSessionId);
}
else if (!string.IsNullOrEmpty(userId))
{
return repository.ReadUser(userId);
}
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)
{
//var session = await repository.GetGame(sessionName);
//if (playerName == session?.Player1)
//{
// return await repository.PostJoinCode(sessionName, playerName);
//}
return string.Empty;
}
public async Task<bool> AssignPlayer2ToSession(string sessionName, string userName)
{
var isSuccess = false;
var session = await repository.ReadSessionMetaData(sessionName);
if (session != null && !session.IsPrivate && string.IsNullOrEmpty(session.Player2))
{
session.SetPlayer2(userName);
if (await repository.UpdateSession(session))
{
isSuccess = true;
}
}
return isSuccess;
}
}
}