massive checkpoint
This commit is contained in:
@@ -1,26 +1,21 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
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<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);
|
||||
Task<User?> ReadUser(ClaimsPrincipal user);
|
||||
}
|
||||
|
||||
public class GameboardManager : IGameboardManager
|
||||
{
|
||||
private const int MaxTries = 3;
|
||||
private readonly IGameboardRepository repository;
|
||||
|
||||
public GameboardManager(IGameboardRepository repository)
|
||||
@@ -28,30 +23,21 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public async Task<string> CreateGuestUser(Guid webSessionId)
|
||||
public Task<User?> ReadUser(ClaimsPrincipal user)
|
||||
{
|
||||
var count = 0;
|
||||
while (count < MaxTries)
|
||||
var userId = user.UserId();
|
||||
if (user.IsGuest() && Guid.TryParse(userId, out var webSessionId))
|
||||
{
|
||||
count++;
|
||||
var userName = $"Guest-{Guid.NewGuid()}";
|
||||
var isCreated = await repository.CreateUser(new User(userName, webSessionId));
|
||||
if (isCreated)
|
||||
{
|
||||
return userName;
|
||||
}
|
||||
return repository.ReadGuestUser(webSessionId);
|
||||
}
|
||||
throw new OperationCanceledException($"Failed to create guest user after {count} tries.");
|
||||
else if (!string.IsNullOrEmpty(userId))
|
||||
{
|
||||
return repository.ReadUser(userId);
|
||||
}
|
||||
|
||||
return Task.FromResult<User?>(null);
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -69,31 +55,6 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user