113 lines
3.3 KiB
C#
113 lines
3.3 KiB
C#
using Gameboard.ShogiUI.Sockets.Models;
|
|
using Gameboard.ShogiUI.Sockets.Repositories;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Managers
|
|
{
|
|
public interface IGameboardManager
|
|
{
|
|
Task<string> CreateGuestUser(Guid webSessionId);
|
|
Task<bool> IsPlayer1(string sessionName, string playerName);
|
|
Task<bool> CreateSession(SessionMetadata session);
|
|
Task<Session?> ReadSession(string gameName);
|
|
Task<bool> UpdateSession(SessionMetadata session);
|
|
Task<bool> AssignPlayer2ToSession(string sessionName, string userName);
|
|
Task<bool> CreateBoardState(string sessionName, Shogi shogi);
|
|
Task<User?> ReadUser(string userName);
|
|
Task<User?> ReadUser(Guid webSessionId);
|
|
}
|
|
|
|
public class GameboardManager : IGameboardManager
|
|
{
|
|
private const int MaxTries = 3;
|
|
private readonly IGameboardRepository repository;
|
|
|
|
public GameboardManager(IGameboardRepository repository)
|
|
{
|
|
this.repository = repository;
|
|
}
|
|
|
|
public async Task<string> CreateGuestUser(Guid webSessionId)
|
|
{
|
|
var count = 0;
|
|
while (count < MaxTries)
|
|
{
|
|
count++;
|
|
var userName = $"Guest-{Guid.NewGuid()}";
|
|
var isCreated = await repository.CreateUser(new User(userName, webSessionId));
|
|
if (isCreated)
|
|
{
|
|
return userName;
|
|
}
|
|
}
|
|
throw new OperationCanceledException($"Failed to create guest user after {count} tries.");
|
|
}
|
|
|
|
public Task<User?> ReadUser(Guid webSessionId)
|
|
{
|
|
return repository.ReadGuestUser(webSessionId);
|
|
}
|
|
public Task<User?> ReadUser(string userName)
|
|
{
|
|
return repository.ReadUser(userName);
|
|
}
|
|
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 Task<bool> CreateSession(SessionMetadata session)
|
|
{
|
|
return repository.CreateSession(session);
|
|
}
|
|
|
|
public Task<Session?> ReadSession(string sessionName)
|
|
{
|
|
return repository.ReadSession(sessionName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves the session to storage.
|
|
/// </summary>
|
|
/// <param name="session">The session to save.</param>
|
|
/// <returns>True if the session was saved successfully.</returns>
|
|
public Task<bool> UpdateSession(SessionMetadata session)
|
|
{
|
|
return repository.UpdateSession(session);
|
|
}
|
|
|
|
public Task<bool> CreateBoardState(string sessionName, Shogi shogi)
|
|
{
|
|
return repository.CreateBoardState(sessionName, shogi);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|