Before changing Piece[,] to Dictionary<string,Piece>
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class BoardState : CouchDocument
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public Piece?[,] Board { get; set; }
|
||||
public Piece[] Player1Hand { get; set; }
|
||||
public Piece[] Player2Hand { get; set; }
|
||||
/// <summary>
|
||||
/// Move is null for first BoardState of a session - before anybody has made moves.
|
||||
/// </summary>
|
||||
public Move? Move { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor and setters are for deserialization.
|
||||
/// </summary>
|
||||
public BoardState() : base()
|
||||
{
|
||||
Name = string.Empty;
|
||||
Board = new Piece[9, 9];
|
||||
Player1Hand = Array.Empty<Piece>();
|
||||
Player2Hand = Array.Empty<Piece>();
|
||||
}
|
||||
|
||||
public BoardState(string sessionName, Models.BoardState boardState) : base($"{sessionName}-{DateTime.Now:O}", nameof(BoardState))
|
||||
{
|
||||
Name = sessionName;
|
||||
Board = new Piece[9, 9];
|
||||
|
||||
for (var x = 0; x < 9; x++)
|
||||
for (var y = 0; y < 9; y++)
|
||||
{
|
||||
var piece = boardState.Board[x, y];
|
||||
if (piece != null)
|
||||
{
|
||||
Board[x, y] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
|
||||
Player1Hand = boardState.Player1Hand.Select(model => new Piece(model)).ToArray();
|
||||
Player2Hand = boardState.Player2Hand.Select(model => new Piece(model)).ToArray();
|
||||
if (boardState.Move != null)
|
||||
{
|
||||
Move = new Move(boardState.Move);
|
||||
}
|
||||
}
|
||||
|
||||
public Models.BoardState ToDomainModel()
|
||||
{
|
||||
/*
|
||||
* Board = new Piece[9, 9];
|
||||
for (var x = 0; x < 9; x++)
|
||||
for (var y = 0; y < 9; y++)
|
||||
{
|
||||
var piece = boardState.Board[x, y];
|
||||
if (piece != null)
|
||||
{
|
||||
Board[x, y] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
|
||||
Player1Hand = boardState.Player1Hand.Select(_ => new Piece(_)).ToList();
|
||||
Player2Hand = boardState.Player2Hand.Select(_ => new Piece(_)).ToList();
|
||||
if (boardState.Move != null)
|
||||
{
|
||||
Move = new Move(boardState.Move);
|
||||
}
|
||||
*/
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class BoardStateDocument : CouchDocument
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public Piece?[,] Board { get; set; }
|
||||
|
||||
public Piece[] Player1Hand { get; set; }
|
||||
|
||||
public Piece[] Player2Hand { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Move is null for first BoardState of a session - before anybody has made moves.
|
||||
/// </summary>
|
||||
public Move? Move { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor and setters are for deserialization.
|
||||
/// </summary>
|
||||
public BoardStateDocument() : base(WhichDocumentType.BoardState)
|
||||
{
|
||||
Name = string.Empty;
|
||||
Board = new Piece[9, 9];
|
||||
Player1Hand = Array.Empty<Piece>();
|
||||
Player2Hand = Array.Empty<Piece>();
|
||||
}
|
||||
|
||||
public BoardStateDocument(string sessionName, Models.Shogi shogi)
|
||||
: base($"{sessionName}-{DateTime.Now:O}", WhichDocumentType.BoardState)
|
||||
{
|
||||
Name = sessionName;
|
||||
Board = new Piece[9, 9];
|
||||
|
||||
for (var x = 0; x < 9; x++)
|
||||
for (var y = 0; y < 9; y++)
|
||||
{
|
||||
var piece = shogi.Board[y, x];
|
||||
if (piece != null)
|
||||
{
|
||||
Board[y, x] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
|
||||
Player1Hand = shogi.Hands[WhichPlayer.Player1].Select(model => new Piece(model)).ToArray();
|
||||
Player2Hand = shogi.Hands[WhichPlayer.Player2].Select(model => new Piece(model)).ToArray();
|
||||
if (shogi.MoveHistory.Count > 0)
|
||||
{
|
||||
Move = new Move(shogi.MoveHistory[^1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,21 +5,21 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public abstract class CouchDocument
|
||||
{
|
||||
[JsonProperty("_id")]
|
||||
public string Id { get; set; }
|
||||
public string Type { get; set; }
|
||||
[JsonProperty("_id")] public string Id { get; set; }
|
||||
public WhichDocumentType DocumentType { get; }
|
||||
public DateTimeOffset CreatedDate { get; set; }
|
||||
|
||||
public CouchDocument()
|
||||
{
|
||||
Id = string.Empty;
|
||||
Type = string.Empty;
|
||||
CreatedDate = DateTimeOffset.UtcNow;
|
||||
}
|
||||
public CouchDocument(string id, string type)
|
||||
public CouchDocument(WhichDocumentType documentType)
|
||||
: this(string.Empty, documentType, DateTimeOffset.UtcNow) { }
|
||||
|
||||
public CouchDocument(string id, WhichDocumentType documentType)
|
||||
: this(id, documentType, DateTimeOffset.UtcNow) { }
|
||||
|
||||
public CouchDocument(string id, WhichDocumentType documentType, DateTimeOffset createdDate)
|
||||
{
|
||||
Id = id;
|
||||
Type = type;
|
||||
DocumentType = documentType;
|
||||
CreatedDate = createdDate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
@@ -32,14 +32,25 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
|
||||
public Move(Models.Move move)
|
||||
{
|
||||
From = move.From?.ToBoardNotation();
|
||||
if (move.From.HasValue)
|
||||
{
|
||||
From = ToBoardNotation(move.From.Value);
|
||||
}
|
||||
IsPromotion = move.IsPromotion;
|
||||
To = move.To.ToBoardNotation();
|
||||
To = ToBoardNotation(move.To);
|
||||
PieceFromHand = move.PieceFromHand;
|
||||
}
|
||||
|
||||
private static readonly char A = 'A';
|
||||
private static string ToBoardNotation(Vector2 vector)
|
||||
{
|
||||
var file = (char)(vector.X + A);
|
||||
var rank = vector.Y + 1;
|
||||
return $"{file}{rank}";
|
||||
}
|
||||
|
||||
public Models.Move ToDomainModel() => PieceFromHand.HasValue
|
||||
? new((ServiceModels.Socket.Types.WhichPiece)PieceFromHand, Coords.FromBoardNotation(To))
|
||||
: new(Coords.FromBoardNotation(From!), Coords.FromBoardNotation(To), IsPromotion);
|
||||
? new(PieceFromHand.Value, To)
|
||||
: new(From!, To, IsPromotion);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,6 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
WhichPiece = piece.WhichPiece;
|
||||
}
|
||||
|
||||
public Models.Piece ToDomainModel() => new(IsPromoted, Owner, WhichPiece);
|
||||
public Models.Piece ToDomainModel() => new(WhichPiece, Owner, IsPromoted);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
### Couch Models
|
||||
|
||||
Couch models should accept domain models during construction and offer a ToDomainModel method which constructs a domain model.
|
||||
In this way, domain models have the freedom to define their valid states.
|
||||
@@ -1,30 +0,0 @@
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class Session : CouchDocument
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Player1 { get; set; }
|
||||
public string? Player2 { get; set; }
|
||||
public bool IsPrivate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor and setters are for deserialization.
|
||||
/// </summary>
|
||||
public Session() : base()
|
||||
{
|
||||
Name = string.Empty;
|
||||
Player1 = string.Empty;
|
||||
Player2 = string.Empty;
|
||||
}
|
||||
|
||||
public Session(string id, Models.Session session) : base(id, nameof(Session))
|
||||
{
|
||||
Name = session.Name;
|
||||
Player1 = session.Player1;
|
||||
Player2 = session.Player2;
|
||||
IsPrivate = session.IsPrivate;
|
||||
}
|
||||
|
||||
public Models.Session ToDomainModel() => new(Name, IsPrivate, Player1, Player2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class SessionDocument : CouchDocument
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Player1 { get; set; }
|
||||
public string? Player2 { get; set; }
|
||||
public bool IsPrivate { get; set; }
|
||||
public IList<BoardStateDocument> History { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor and setters are for deserialization.
|
||||
/// </summary>
|
||||
public SessionDocument() : base(WhichDocumentType.Session)
|
||||
{
|
||||
Name = string.Empty;
|
||||
Player1 = string.Empty;
|
||||
Player2 = string.Empty;
|
||||
History = new List<BoardStateDocument>(0);
|
||||
}
|
||||
|
||||
public SessionDocument(Models.Session session)
|
||||
: base(session.Name, WhichDocumentType.Session)
|
||||
{
|
||||
Name = session.Name;
|
||||
Player1 = session.Player1;
|
||||
Player2 = session.Player2;
|
||||
IsPrivate = session.IsPrivate;
|
||||
History = new List<BoardStateDocument>(0);
|
||||
}
|
||||
|
||||
public SessionDocument(Models.SessionMetadata sessionMetaData)
|
||||
: base(sessionMetaData.Name, WhichDocumentType.Session)
|
||||
{
|
||||
Name = sessionMetaData.Name;
|
||||
Player1 = sessionMetaData.Player1;
|
||||
Player2 = sessionMetaData.Player2;
|
||||
IsPrivate = sessionMetaData.IsPrivate;
|
||||
History = new List<BoardStateDocument>(0);
|
||||
}
|
||||
|
||||
public Models.Session ToDomainModel(Models.Shogi shogi) => new(Name, IsPrivate, shogi, Player1, Player2);
|
||||
|
||||
public Models.SessionMetadata ToDomainModel() => new(Name, IsPrivate, Player1, Player2);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class User : CouchDocument
|
||||
public class UserDocument : CouchDocument
|
||||
{
|
||||
public static string GetDocumentId(string userName) => $"org.couchdb.user:{userName}";
|
||||
|
||||
@@ -14,7 +12,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
|
||||
public string Name { get; set; }
|
||||
public LoginPlatform Platform { get; set; }
|
||||
public User(string name, LoginPlatform platform) : base($"org.couchdb.user:{name}", nameof(User))
|
||||
public UserDocument(string name, LoginPlatform platform) : base($"org.couchdb.user:{name}", WhichDocumentType.User)
|
||||
{
|
||||
Name = name;
|
||||
Platform = platform;
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public enum WhichDocumentType
|
||||
{
|
||||
User,
|
||||
Session,
|
||||
BoardState
|
||||
}
|
||||
}
|
||||
@@ -12,14 +12,14 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
{
|
||||
public interface IGameboardRepository
|
||||
{
|
||||
Task<bool> CreateBoardState(string sessionName, Models.BoardState boardState, Models.Move? move);
|
||||
Task<bool> CreateGuestUser(string userName);
|
||||
Task<bool> CreateSession(Models.Session session);
|
||||
Task<IList<Models.Session>> ReadSessions();
|
||||
Task<bool> CreateSession(Models.SessionMetadata session);
|
||||
Task<IList<Models.SessionMetadata>> ReadSessionMetadatas();
|
||||
Task<bool> IsGuestUser(string userName);
|
||||
Task<string> PostJoinCode(string gameName, string userName);
|
||||
Task<Models.Session?> ReadSession(string name);
|
||||
Task<IList<Models.BoardState>> ReadBoardStates(string name);
|
||||
Task<Models.Shogi?> ReadShogi(string name);
|
||||
Task<bool> UpdateSession(Models.Session session);
|
||||
}
|
||||
|
||||
public class GameboardRepository : IGameboardRepository
|
||||
@@ -34,75 +34,99 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task<IList<Models.Session>> ReadSessions()
|
||||
public async Task<IList<Models.SessionMetadata>> ReadSessionMetadatas()
|
||||
{
|
||||
var selector = $@"{{ ""{nameof(Session.Type)}"": ""{nameof(Session)}"" }}";
|
||||
var query = $@"{{ ""selector"": {selector} }}";
|
||||
var content = new StringContent(query, Encoding.UTF8, ApplicationJson);
|
||||
var selector = new Dictionary<string, object>(2)
|
||||
{
|
||||
[nameof(SessionDocument.DocumentType)] = WhichDocumentType.Session
|
||||
};
|
||||
var q = new { Selector = selector };
|
||||
var content = new StringContent(JsonConvert.SerializeObject(q), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync("_find", content);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchFindResult<Session>>(responseContent);
|
||||
var sessions = JsonConvert.DeserializeObject<CouchFindResult<SessionDocument>>(responseContent).docs;
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
logger.LogError("Unable to deserialize couchdb result during {0}.", nameof(this.ReadSessions));
|
||||
return Array.Empty<Models.Session>();
|
||||
}
|
||||
return result.docs
|
||||
.Select(_ => _.ToDomainModel())
|
||||
return sessions
|
||||
.Select(s => new Models.SessionMetadata(s.Name, s.IsPrivate, s.Player1, s.Player2))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<Models.Session?> ReadSession(string name)
|
||||
{
|
||||
var readShogiTask = ReadShogi(name);
|
||||
var response = await client.GetAsync(name);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var couchModel = JsonConvert.DeserializeObject<Session>(responseContent);
|
||||
return couchModel.ToDomainModel();
|
||||
var couchModel = JsonConvert.DeserializeObject<SessionDocument>(responseContent);
|
||||
var shogi = await readShogiTask;
|
||||
if (shogi == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return couchModel.ToDomainModel(shogi);
|
||||
}
|
||||
|
||||
public async Task<IList<Models.BoardState>> ReadBoardStates(string name)
|
||||
public async Task<Models.Shogi?> ReadShogi(string name)
|
||||
{
|
||||
var selector = $@"{{ ""{nameof(BoardState.Type)}"": ""{nameof(BoardState)}"", ""{nameof(BoardState.Name)}"": ""{name}"" }}";
|
||||
var sort = $@"{{ ""{nameof(BoardState.CreatedDate)}"" : ""desc"" }}";
|
||||
var query = $@"{{ ""selector"": {selector}, ""sort"": {sort} }}";
|
||||
var selector = new Dictionary<string, object>(2)
|
||||
{
|
||||
[nameof(BoardStateDocument.DocumentType)] = WhichDocumentType.BoardState,
|
||||
[nameof(BoardStateDocument.Name)] = name
|
||||
};
|
||||
var sort = new Dictionary<string, object>(1)
|
||||
{
|
||||
[nameof(BoardStateDocument.CreatedDate)] = "desc"
|
||||
};
|
||||
var query = JsonConvert.SerializeObject(new { selector, sort = new[] { sort } });
|
||||
|
||||
var content = new StringContent(query, Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync("_find", content);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchFindResult<BoardState>>(responseContent);
|
||||
|
||||
if (result == null)
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
logger.LogError("Unable to deserialize couchdb result during {0}.", nameof(this.ReadSessions));
|
||||
return Array.Empty<Models.BoardState>();
|
||||
logger.LogError("Couch error during _find in {func}: {error}.\n\nQuery: {query}", nameof(ReadShogi), responseContent, query);
|
||||
return null;
|
||||
}
|
||||
return result.docs
|
||||
.Select(_ => new Models.BoardState(_))
|
||||
.ToList();
|
||||
var boardStates = JsonConvert
|
||||
.DeserializeObject<CouchFindResult<BoardStateDocument>>(responseContent)
|
||||
.docs;
|
||||
if (boardStates.Length == 0) return null;
|
||||
|
||||
// Skip(1) because the first BoardState has no move; it represents the initial board state of a new Session.
|
||||
var moves = boardStates.Skip(1).Select(couchModel =>
|
||||
{
|
||||
var move = couchModel.Move;
|
||||
Models.Move model = move!.PieceFromHand.HasValue
|
||||
? new Models.Move(move.PieceFromHand.Value, move.To)
|
||||
: new Models.Move(move.From!, move.To, move.IsPromotion);
|
||||
return model;
|
||||
}).ToList();
|
||||
return new Models.Shogi(moves);
|
||||
}
|
||||
|
||||
//public async Task DeleteGame(string gameName)
|
||||
//{
|
||||
// //var uri = $"Session/{gameName}";
|
||||
// //await client.DeleteAsync(Uri.EscapeUriString(uri));
|
||||
//}
|
||||
|
||||
public async Task<bool> CreateSession(Models.Session session)
|
||||
public async Task<bool> CreateSession(Models.SessionMetadata session)
|
||||
{
|
||||
var couchModel = new Session(session.Name, session);
|
||||
var sessionDocument = new SessionDocument(session);
|
||||
var sessionContent = new StringContent(JsonConvert.SerializeObject(sessionDocument), Encoding.UTF8, ApplicationJson);
|
||||
var postSessionDocumentTask = client.PostAsync(string.Empty, sessionContent);
|
||||
|
||||
var boardStateDocument = new BoardStateDocument(session.Name, new Models.Shogi());
|
||||
var boardStateContent = new StringContent(JsonConvert.SerializeObject(boardStateDocument), Encoding.UTF8, ApplicationJson);
|
||||
|
||||
if ((await postSessionDocumentTask).IsSuccessStatusCode)
|
||||
{
|
||||
var response = await client.PostAsync(string.Empty, boardStateContent);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateSession(Models.Session session)
|
||||
{
|
||||
var couchModel = new SessionDocument(session);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(couchModel), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
var response = await client.PutAsync(couchModel.Id, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task<bool> CreateBoardState(string sessionName, Models.BoardState boardState, Models.Move? move)
|
||||
{
|
||||
var couchModel = new BoardState(sessionName, boardState, move);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(couchModel), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
//public async Task<bool> PutJoinPublicSession(PutJoinPublicSession request)
|
||||
//{
|
||||
// var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
@@ -169,7 +193,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
|
||||
public async Task<bool> CreateGuestUser(string userName)
|
||||
{
|
||||
var couchModel = new User(userName, User.LoginPlatform.Guest);
|
||||
var couchModel = new UserDocument(userName, UserDocument.LoginPlatform.Guest);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(couchModel), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
@@ -177,7 +201,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
|
||||
public async Task<bool> IsGuestUser(string userName)
|
||||
{
|
||||
var req = new HttpRequestMessage(HttpMethod.Head, new Uri($"{client.BaseAddress}/{User.GetDocumentId(userName)}"));
|
||||
var req = new HttpRequestMessage(HttpMethod.Head, new Uri($"{client.BaseAddress}/{UserDocument.GetDocumentId(userName)}"));
|
||||
var response = await client.SendAsync(req);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user