44 lines
1.1 KiB
C#
44 lines
1.1 KiB
C#
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Websockets.Repositories;
|
|
|
|
namespace AspShogiSockets.Repositories.RepositoryManagers
|
|
{
|
|
public interface IGameboardRepositoryManager
|
|
{
|
|
Task<string> CreateGuestUser();
|
|
}
|
|
|
|
public class GameboardRepositoryManager : IGameboardRepositoryManager
|
|
{
|
|
private readonly IGameboardRepository repository;
|
|
private const int MaxTries = 3;
|
|
|
|
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 request = new PostPlayer
|
|
{
|
|
PlayerName = clientId
|
|
};
|
|
var response = await repository.PostPlayer(request);
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return clientId;
|
|
}
|
|
}
|
|
throw new OperationCanceledException($"Failed to create guest user after {MaxTries} tries.");
|
|
}
|
|
}
|
|
}
|