75 lines
1.8 KiB
C#
75 lines
1.8 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 AssignPlayer2ToSession(string sessionName, User user);
|
|
Task<User?> ReadUser(ClaimsPrincipal user);
|
|
Task<User?> CreateUser(ClaimsPrincipal user);
|
|
}
|
|
|
|
public class GameboardManager : IGameboardManager
|
|
{
|
|
private readonly IGameboardRepository repository;
|
|
|
|
public GameboardManager(IGameboardRepository repository)
|
|
{
|
|
this.repository = repository;
|
|
}
|
|
|
|
public async Task<User> CreateUser(ClaimsPrincipal principal)
|
|
{
|
|
var id = principal.UserId();
|
|
if (string.IsNullOrEmpty(id))
|
|
{
|
|
throw new InvalidOperationException("Cannot create user from given claims.");
|
|
}
|
|
|
|
var user = principal.IsMicrosoft()
|
|
? User.CreateMsalUser(id)
|
|
: User.CreateGuestUser(id);
|
|
|
|
await repository.CreateUser(user);
|
|
return user;
|
|
}
|
|
|
|
public Task<User?> ReadUser(ClaimsPrincipal principal)
|
|
{
|
|
var userId = principal.UserId();
|
|
if (!string.IsNullOrEmpty(userId))
|
|
{
|
|
return repository.ReadUser(userId);
|
|
}
|
|
|
|
return Task.FromResult<User?>(null);
|
|
}
|
|
|
|
|
|
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 AssignPlayer2ToSession(string sessionName, User user)
|
|
{
|
|
var session = await repository.ReadSessionMetaData(sessionName);
|
|
if (session != null && !session.IsPrivate && session.Player2 == null)
|
|
{
|
|
session.SetPlayer2(user.Id);
|
|
await repository.UpdateSession(session);
|
|
}
|
|
}
|
|
}
|
|
}
|