Before changing Piece[,] to Dictionary<string,Piece>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user