72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using Gameboard.ShogiUI.Sockets.Models;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
|
|
{
|
|
public interface IGameboardRepositoryManager
|
|
{
|
|
Task<string> CreateGuestUser();
|
|
Task<bool> IsPlayer1(string sessionName, string playerName);
|
|
bool IsGuest(string playerName);
|
|
Task<bool> CreateSession(Session session);
|
|
}
|
|
|
|
public class GameboardRepositoryManager : IGameboardRepositoryManager
|
|
{
|
|
private const int MaxTries = 3;
|
|
private const string GuestPrefix = "Guest-";
|
|
private readonly IGameboardRepository repository;
|
|
|
|
public GameboardRepositoryManager(IGameboardRepository repository)
|
|
{
|
|
this.repository = repository;
|
|
}
|
|
|
|
public async Task<string> CreateGuestUser()
|
|
{
|
|
var count = 0;
|
|
while (count < MaxTries)
|
|
{
|
|
count++;
|
|
var clientId = $"Guest-{Guid.NewGuid()}";
|
|
var isCreated = await repository.CreateGuestUser(clientId);
|
|
if (isCreated)
|
|
{
|
|
return clientId;
|
|
}
|
|
}
|
|
throw new OperationCanceledException($"Failed to create guest user after {MaxTries} tries.");
|
|
}
|
|
|
|
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 null;
|
|
}
|
|
|
|
public async Task<bool> CreateSession(Session session)
|
|
{
|
|
var success = await repository.CreateSession(session);
|
|
if (success)
|
|
{
|
|
return await repository.CreateBoardState(session.Name, new BoardState(), null);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool IsGuest(string playerName) => playerName.StartsWith(GuestPrefix);
|
|
}
|
|
}
|