checkpoint
This commit is contained in:
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Shogi.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
@@ -11,282 +12,302 @@ using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
{
|
||||
public interface IGameboardRepository
|
||||
{
|
||||
Task<bool> CreateBoardState(Models.Session session);
|
||||
Task<bool> CreateSession(Models.SessionMetadata session);
|
||||
Task<bool> CreateUser(Models.User user);
|
||||
Task<Collection<Models.SessionMetadata>> ReadSessionMetadatas();
|
||||
Task<Models.Session?> ReadSession(string name);
|
||||
Task<bool> UpdateSession(Models.SessionMetadata session);
|
||||
Task<Models.SessionMetadata?> ReadSessionMetaData(string name);
|
||||
Task<Models.User?> ReadUser(string userName);
|
||||
}
|
||||
public interface IGameboardRepository
|
||||
{
|
||||
Task<bool> CreateBoardState(Session session);
|
||||
Task<bool> CreateSession(Models.SessionMetadata session);
|
||||
Task<bool> CreateUser(Models.User user);
|
||||
Task<Collection<Models.SessionMetadata>> ReadSessionMetadatas();
|
||||
Task<Session?> ReadSession(string name);
|
||||
Task<bool> UpdateSession(Models.SessionMetadata session);
|
||||
Task<Models.SessionMetadata?> ReadSessionMetaData(string name);
|
||||
Task<Models.User?> ReadUser(string userName);
|
||||
}
|
||||
|
||||
public class GameboardRepository : IGameboardRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns session, board state, and user documents, grouped by session.
|
||||
/// </summary>
|
||||
private static readonly string View_SessionWithBoardState = "_design/session/_view/session-with-boardstate";
|
||||
/// <summary>
|
||||
/// Returns session and user documents, grouped by session.
|
||||
/// </summary>
|
||||
private static readonly string View_SessionMetadata = "_design/session/_view/session-metadata";
|
||||
private static readonly string View_User = "_design/user/_view/user";
|
||||
private const string ApplicationJson = "application/json";
|
||||
private readonly HttpClient client;
|
||||
private readonly ILogger<GameboardRepository> logger;
|
||||
public class GameboardRepository : IGameboardRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns session, board state, and user documents, grouped by session.
|
||||
/// </summary>
|
||||
private static readonly string View_SessionWithBoardState = "_design/session/_view/session-with-boardstate";
|
||||
/// <summary>
|
||||
/// Returns session and user documents, grouped by session.
|
||||
/// </summary>
|
||||
private static readonly string View_SessionMetadata = "_design/session/_view/session-metadata";
|
||||
private static readonly string View_User = "_design/user/_view/user";
|
||||
private const string ApplicationJson = "application/json";
|
||||
private readonly HttpClient client;
|
||||
private readonly ILogger<GameboardRepository> logger;
|
||||
|
||||
public GameboardRepository(IHttpClientFactory clientFactory, ILogger<GameboardRepository> logger)
|
||||
{
|
||||
client = clientFactory.CreateClient("couchdb");
|
||||
this.logger = logger;
|
||||
}
|
||||
public GameboardRepository(IHttpClientFactory clientFactory, ILogger<GameboardRepository> logger)
|
||||
{
|
||||
client = clientFactory.CreateClient("couchdb");
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task<Collection<Models.SessionMetadata>> ReadSessionMetadatas()
|
||||
{
|
||||
var queryParams = new QueryBuilder { { "include_docs", "true" } }.ToQueryString();
|
||||
var response = await client.GetAsync($"{View_SessionMetadata}{queryParams}");
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchViewResult<JObject>>(responseContent);
|
||||
if (result != null)
|
||||
{
|
||||
var groupedBySession = result.rows.GroupBy(row => row.id);
|
||||
var sessions = new List<Models.SessionMetadata>(result.total_rows / 3);
|
||||
foreach (var group in groupedBySession)
|
||||
{
|
||||
/**
|
||||
public async Task<Collection<Models.SessionMetadata>> ReadSessionMetadatas()
|
||||
{
|
||||
var queryParams = new QueryBuilder { { "include_docs", "true" } }.ToQueryString();
|
||||
var response = await client.GetAsync($"{View_SessionMetadata}{queryParams}");
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchViewResult<JObject>>(responseContent);
|
||||
if (result != null)
|
||||
{
|
||||
var groupedBySession = result.rows.GroupBy(row => row.id);
|
||||
var sessions = new List<Models.SessionMetadata>(result.total_rows / 3);
|
||||
foreach (var group in groupedBySession)
|
||||
{
|
||||
/**
|
||||
* A group contains 3 elements.
|
||||
* 1) The session metadata.
|
||||
* 2) User document of Player1.
|
||||
* 3) User document of Player2.
|
||||
*/
|
||||
var session = group.FirstOrDefault()?.doc.ToObject<SessionDocument>();
|
||||
var player1Doc = group.Skip(1).FirstOrDefault()?.doc.ToObject<UserDocument>();
|
||||
var player2Doc = group.Skip(2).FirstOrDefault()?.doc.ToObject<UserDocument>();
|
||||
if (session != null && player1Doc != null)
|
||||
{
|
||||
var player2 = player2Doc == null ? null : new Models.User(player2Doc);
|
||||
sessions.Add(new Models.SessionMetadata(session.Name, session.IsPrivate, new(player1Doc), player2));
|
||||
}
|
||||
}
|
||||
return new Collection<Models.SessionMetadata>(sessions);
|
||||
}
|
||||
return new Collection<Models.SessionMetadata>(Array.Empty<Models.SessionMetadata>());
|
||||
}
|
||||
var session = group.FirstOrDefault()?.doc.ToObject<SessionDocument>();
|
||||
var player1Doc = group.Skip(1).FirstOrDefault()?.doc.ToObject<UserDocument>();
|
||||
var player2Doc = group.Skip(2).FirstOrDefault()?.doc.ToObject<UserDocument>();
|
||||
if (session != null && player1Doc != null)
|
||||
{
|
||||
var player2 = player2Doc == null ? null : new Models.User(player2Doc);
|
||||
sessions.Add(new Models.SessionMetadata(session.Name, session.IsPrivate, new(player1Doc), player2));
|
||||
}
|
||||
}
|
||||
return new Collection<Models.SessionMetadata>(sessions);
|
||||
}
|
||||
return new Collection<Models.SessionMetadata>(Array.Empty<Models.SessionMetadata>());
|
||||
}
|
||||
|
||||
public async Task<Models.Session?> ReadSession(string name)
|
||||
{
|
||||
var queryParams = new QueryBuilder
|
||||
{
|
||||
{ "include_docs", "true" },
|
||||
{ "startkey", JsonConvert.SerializeObject(new [] {name}) },
|
||||
{ "endkey", JsonConvert.SerializeObject(new object [] {name, int.MaxValue}) }
|
||||
}.ToQueryString();
|
||||
var query = $"{View_SessionWithBoardState}{queryParams}";
|
||||
logger.LogInformation("ReadSession() query: {query}", query);
|
||||
var response = await client.GetAsync(query);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchViewResult<JObject>>(responseContent);
|
||||
if (result != null && result.rows.Length > 2)
|
||||
{
|
||||
var group = result.rows;
|
||||
/**
|
||||
* A group contains 3 type of elements.
|
||||
* 1) The session metadata.
|
||||
* 2) User documents of Player1 and Player2.
|
||||
public async Task<Session?> ReadSession(string name)
|
||||
{
|
||||
static Shogi.Domain.Pieces.Piece? MapPiece(Piece? piece)
|
||||
{
|
||||
return piece == null
|
||||
? null
|
||||
: Shogi.Domain.Pieces.Piece.Create(piece.WhichPiece, piece.Owner, piece.IsPromoted);
|
||||
}
|
||||
|
||||
var queryParams = new QueryBuilder
|
||||
{
|
||||
{ "include_docs", "true" },
|
||||
{ "startkey", JsonConvert.SerializeObject(new [] {name}) },
|
||||
{ "endkey", JsonConvert.SerializeObject(new object [] {name, int.MaxValue}) }
|
||||
}.ToQueryString();
|
||||
var query = $"{View_SessionWithBoardState}{queryParams}";
|
||||
logger.LogInformation("ReadSession() query: {query}", query);
|
||||
var response = await client.GetAsync(query);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchViewResult<JObject>>(responseContent);
|
||||
if (result != null && result.rows.Length > 2)
|
||||
{
|
||||
var group = result.rows;
|
||||
/**
|
||||
* A group contains multiple elements.
|
||||
* 0) The session metadata.
|
||||
* 1) User documents of Player1.
|
||||
* 2) User documents of Player1.
|
||||
* 2.a) If the Player2 document doesn't exist, CouchDB will return the SessionDocument instead :(
|
||||
* 3) BoardState
|
||||
* Everything Else) Snapshots of the boardstate after every player move.
|
||||
*/
|
||||
var session = group[0].doc.ToObject<SessionDocument>();
|
||||
var player1Doc = group[1].doc.ToObject<UserDocument>();
|
||||
var group2DocumentType = group[2].doc.Property(nameof(UserDocument.DocumentType).ToCamelCase())?.Value.Value<string>();
|
||||
var player2Doc = group2DocumentType == WhichDocumentType.User.ToString()
|
||||
? group[2].doc.ToObject<UserDocument>()
|
||||
: null;
|
||||
var moves = group
|
||||
.Skip(4) // Skip 4 because group[3] will not have a .Move property since it's the first/initial BoardState of the session.
|
||||
// TODO: Deserialize just the Move property.
|
||||
.Select(row => row.doc.ToObject<BoardStateDocument>())
|
||||
.Select(boardState =>
|
||||
{
|
||||
var move = boardState!.Move!;
|
||||
return move.PieceFromHand.HasValue
|
||||
? new Models.Move(move.PieceFromHand.Value, move.To)
|
||||
: new Models.Move(move.From!, move.To, move.IsPromotion);
|
||||
})
|
||||
.ToList();
|
||||
var session = group[0].doc.ToObject<SessionDocument>();
|
||||
var player1Doc = group[1].doc.ToObject<UserDocument>();
|
||||
var group2DocumentType = group[2].doc.Property(nameof(UserDocument.DocumentType).ToCamelCase())?.Value.Value<string>();
|
||||
var player2Doc = group2DocumentType == WhichDocumentType.User.ToString()
|
||||
? group[2].doc.ToObject<UserDocument>()
|
||||
: null;
|
||||
var boardState = group.Last().doc.ToObject<BoardStateDocument>();
|
||||
|
||||
var shogi = new Models.Shogi(moves);
|
||||
if (session != null && player1Doc != null)
|
||||
{
|
||||
var player2 = player2Doc == null ? null : new Models.User(player2Doc);
|
||||
return new Models.Session(session.Name, session.IsPrivate, shogi, new(player1Doc), player2);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (session != null && player1Doc != null && boardState != null)
|
||||
{
|
||||
var player2 = player2Doc == null ? null : new Models.User(player2Doc);
|
||||
var metaData = new SessionMetadata(session.Name, session.IsPrivate, player1Doc.DisplayName, player2Doc?.DisplayName);
|
||||
var shogiBoardState = new BoardState(boardState.Board.ToDictionary(kvp => kvp.Key, kvp => MapPiece(kvp.Value)));
|
||||
return new Session(shogiBoardState, metaData);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<Models.SessionMetadata?> ReadSessionMetaData(string name)
|
||||
{
|
||||
var queryParams = new QueryBuilder
|
||||
{
|
||||
{ "include_docs", "true" },
|
||||
{ "startkey", JsonConvert.SerializeObject(new [] {name}) },
|
||||
{ "endkey", JsonConvert.SerializeObject(new object [] {name, int.MaxValue}) }
|
||||
}.ToQueryString();
|
||||
var response = await client.GetAsync($"{View_SessionMetadata}{queryParams}");
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchViewResult<JObject>>(responseContent);
|
||||
if (result != null && result.rows.Length > 2)
|
||||
{
|
||||
var group = result.rows;
|
||||
/**
|
||||
public async Task<Models.SessionMetadata?> ReadSessionMetaData(string name)
|
||||
{
|
||||
var queryParams = new QueryBuilder
|
||||
{
|
||||
{ "include_docs", "true" },
|
||||
{ "startkey", JsonConvert.SerializeObject(new [] {name}) },
|
||||
{ "endkey", JsonConvert.SerializeObject(new object [] {name, int.MaxValue}) }
|
||||
}.ToQueryString();
|
||||
var response = await client.GetAsync($"{View_SessionMetadata}{queryParams}");
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchViewResult<JObject>>(responseContent);
|
||||
if (result != null && result.rows.Length > 2)
|
||||
{
|
||||
var group = result.rows;
|
||||
/**
|
||||
* A group contains 3 elements.
|
||||
* 1) The session metadata.
|
||||
* 2) User document of Player1.
|
||||
* 3) User document of Player2.
|
||||
*/
|
||||
var session = group[0].doc.ToObject<SessionDocument>();
|
||||
var player1Doc = group[1].doc.ToObject<UserDocument>();
|
||||
var group2DocumentType = group[2].doc.Property(nameof(UserDocument.DocumentType).ToCamelCase())?.Value.Value<string>();
|
||||
var player2Doc = group2DocumentType == WhichDocumentType.User.ToString()
|
||||
? group[2].doc.ToObject<UserDocument>()
|
||||
: null;
|
||||
if (session != null && player1Doc != null)
|
||||
{
|
||||
var player2 = player2Doc == null ? null : new Models.User(player2Doc);
|
||||
return new Models.SessionMetadata(session.Name, session.IsPrivate, new(player1Doc), player2);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
var session = group[0].doc.ToObject<SessionDocument>();
|
||||
var player1Doc = group[1].doc.ToObject<UserDocument>();
|
||||
var group2DocumentType = group[2].doc.Property(nameof(UserDocument.DocumentType).ToCamelCase())?.Value.Value<string>();
|
||||
var player2Doc = group2DocumentType == WhichDocumentType.User.ToString()
|
||||
? group[2].doc.ToObject<UserDocument>()
|
||||
: null;
|
||||
if (session != null && player1Doc != null)
|
||||
{
|
||||
var player2 = player2Doc == null ? null : new Models.User(player2Doc);
|
||||
return new Models.SessionMetadata(session.Name, session.IsPrivate, new(player1Doc), player2);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a snapshot of board state and the most recent move.
|
||||
/// </summary>
|
||||
public async Task<bool> CreateBoardState(Models.Session session)
|
||||
{
|
||||
var boardStateDocument = new BoardStateDocument(session.Name, session.Shogi);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(boardStateDocument), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
/// <summary>
|
||||
/// Saves a snapshot of board state and the most recent move.
|
||||
/// </summary>
|
||||
public async Task<bool> CreateBoardState(Session session)
|
||||
{
|
||||
Piece? MapPiece(Shogi.Domain.Pieces.Piece? piece)
|
||||
{
|
||||
return piece == null
|
||||
? null
|
||||
: new Piece { IsPromoted = piece.IsPromoted, Owner = piece.Owner, WhichPiece = piece.WhichPiece };
|
||||
}
|
||||
|
||||
public async Task<bool> CreateSession(Models.SessionMetadata 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, session);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(boardStateDocument), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
var boardStateDocument = new BoardStateDocument(session.Name, new Models.Shogi());
|
||||
var boardStateContent = new StringContent(JsonConvert.SerializeObject(boardStateDocument), Encoding.UTF8, ApplicationJson);
|
||||
public async Task<bool> CreateSession(Models.SessionMetadata session)
|
||||
{
|
||||
var sessionDocument = new SessionDocument(session);
|
||||
var sessionContent = new StringContent(JsonConvert.SerializeObject(sessionDocument), Encoding.UTF8, ApplicationJson);
|
||||
var postSessionDocumentTask = client.PostAsync(string.Empty, sessionContent);
|
||||
|
||||
if ((await postSessionDocumentTask).IsSuccessStatusCode)
|
||||
{
|
||||
var response = await client.PostAsync(string.Empty, boardStateContent);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
var boardStateDocument = new BoardStateDocument(session.Name, new Session());
|
||||
var boardStateContent = new StringContent(JsonConvert.SerializeObject(boardStateDocument), Encoding.UTF8, ApplicationJson);
|
||||
|
||||
public async Task<bool> UpdateSession(Models.SessionMetadata session)
|
||||
{
|
||||
// GET existing session to get revisionId.
|
||||
var readResponse = await client.GetAsync(session.Name);
|
||||
if (!readResponse.IsSuccessStatusCode) return false;
|
||||
var sessionDocument = JsonConvert.DeserializeObject<SessionDocument>(await readResponse.Content.ReadAsStringAsync());
|
||||
if ((await postSessionDocumentTask).IsSuccessStatusCode)
|
||||
{
|
||||
var response = await client.PostAsync(string.Empty, boardStateContent);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// PUT the document with the revisionId.
|
||||
var couchModel = new SessionDocument(session)
|
||||
{
|
||||
RevisionId = sessionDocument?.RevisionId
|
||||
};
|
||||
var content = new StringContent(JsonConvert.SerializeObject(couchModel), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PutAsync(couchModel.Id, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
//public async Task<bool> PutJoinPublicSession(PutJoinPublicSession request)
|
||||
//{
|
||||
// var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
// var response = await client.PutAsync(JoinSessionRoute, content);
|
||||
// var json = await response.Content.ReadAsStringAsync();
|
||||
// return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json).JoinSucceeded;
|
||||
//}
|
||||
public async Task<bool> UpdateSession(Models.SessionMetadata session)
|
||||
{
|
||||
// GET existing session to get revisionId.
|
||||
var readResponse = await client.GetAsync(session.Name);
|
||||
if (!readResponse.IsSuccessStatusCode) return false;
|
||||
var sessionDocument = JsonConvert.DeserializeObject<SessionDocument>(await readResponse.Content.ReadAsStringAsync());
|
||||
|
||||
//public async Task<string> PostJoinPrivateSession(PostJoinPrivateSession request)
|
||||
//{
|
||||
// var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
// var response = await client.PostAsync(JoinSessionRoute, content);
|
||||
// var json = await response.Content.ReadAsStringAsync();
|
||||
// var deserialized = JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
|
||||
// if (deserialized.JoinSucceeded)
|
||||
// {
|
||||
// return deserialized.SessionName;
|
||||
// }
|
||||
// return null;
|
||||
//}
|
||||
// PUT the document with the revisionId.
|
||||
var couchModel = new SessionDocument(session)
|
||||
{
|
||||
RevisionId = sessionDocument?.RevisionId
|
||||
};
|
||||
var content = new StringContent(JsonConvert.SerializeObject(couchModel), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PutAsync(couchModel.Id, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
//public async Task<bool> PutJoinPublicSession(PutJoinPublicSession request)
|
||||
//{
|
||||
// var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
// var response = await client.PutAsync(JoinSessionRoute, content);
|
||||
// var json = await response.Content.ReadAsStringAsync();
|
||||
// return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json).JoinSucceeded;
|
||||
//}
|
||||
|
||||
//public async Task<List<Move>> GetMoves(string gameName)
|
||||
//{
|
||||
// var uri = $"Session/{gameName}/Moves";
|
||||
// var get = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
// var json = await get.Content.ReadAsStringAsync();
|
||||
// if (string.IsNullOrWhiteSpace(json))
|
||||
// {
|
||||
// return new List<Move>();
|
||||
// }
|
||||
// var response = JsonConvert.DeserializeObject<GetMovesResponse>(json);
|
||||
// return response.Moves.Select(m => new Move(m)).ToList();
|
||||
//}
|
||||
//public async Task<string> PostJoinPrivateSession(PostJoinPrivateSession request)
|
||||
//{
|
||||
// var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
// var response = await client.PostAsync(JoinSessionRoute, content);
|
||||
// var json = await response.Content.ReadAsStringAsync();
|
||||
// var deserialized = JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
|
||||
// if (deserialized.JoinSucceeded)
|
||||
// {
|
||||
// return deserialized.SessionName;
|
||||
// }
|
||||
// return null;
|
||||
//}
|
||||
|
||||
//public async Task PostMove(string gameName, PostMove request)
|
||||
//{
|
||||
// var uri = $"Session/{gameName}/Move";
|
||||
// var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
// await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
//}
|
||||
//public async Task<List<Move>> GetMoves(string gameName)
|
||||
//{
|
||||
// var uri = $"Session/{gameName}/Moves";
|
||||
// var get = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
// var json = await get.Content.ReadAsStringAsync();
|
||||
// if (string.IsNullOrWhiteSpace(json))
|
||||
// {
|
||||
// return new List<Move>();
|
||||
// }
|
||||
// var response = JsonConvert.DeserializeObject<GetMovesResponse>(json);
|
||||
// return response.Moves.Select(m => new Move(m)).ToList();
|
||||
//}
|
||||
|
||||
public async Task<string> PostJoinCode(string gameName, string userName)
|
||||
{
|
||||
// var uri = $"JoinCode/{gameName}";
|
||||
// var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName });
|
||||
// var content = new StringContent(serialized, Encoding.UTF8, MediaType);
|
||||
// var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync();
|
||||
// return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json).JoinCode;
|
||||
return string.Empty;
|
||||
}
|
||||
//public async Task PostMove(string gameName, PostMove request)
|
||||
//{
|
||||
// var uri = $"Session/{gameName}/Move";
|
||||
// var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
// await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
//}
|
||||
|
||||
public async Task<Models.User?> ReadUser(string id)
|
||||
{
|
||||
var queryParams = new QueryBuilder
|
||||
{
|
||||
{ "include_docs", "true" },
|
||||
{ "key", JsonConvert.SerializeObject(id) },
|
||||
}.ToQueryString();
|
||||
var response = await client.GetAsync($"{View_User}{queryParams}");
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchViewResult<UserDocument>>(responseContent);
|
||||
if (result != null && result.rows.Length > 0)
|
||||
{
|
||||
return new Models.User(result.rows[0].doc);
|
||||
}
|
||||
public async Task<string> PostJoinCode(string gameName, string userName)
|
||||
{
|
||||
// var uri = $"JoinCode/{gameName}";
|
||||
// var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName });
|
||||
// var content = new StringContent(serialized, Encoding.UTF8, MediaType);
|
||||
// var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync();
|
||||
// return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json).JoinCode;
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public async Task<Models.User?> ReadUser(string id)
|
||||
{
|
||||
var queryParams = new QueryBuilder
|
||||
{
|
||||
{ "include_docs", "true" },
|
||||
{ "key", JsonConvert.SerializeObject(id) },
|
||||
}.ToQueryString();
|
||||
var response = await client.GetAsync($"{View_User}{queryParams}");
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<CouchViewResult<UserDocument>>(responseContent);
|
||||
if (result != null && result.rows.Length > 0)
|
||||
{
|
||||
return new Models.User(result.rows[0].doc);
|
||||
}
|
||||
|
||||
public async Task<bool> CreateUser(Models.User user)
|
||||
{
|
||||
var couchModel = new UserDocument(user.Id, user.DisplayName, user.LoginPlatform);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(couchModel), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
public async Task<bool> CreateUser(Models.User user)
|
||||
{
|
||||
var couchModel = new UserDocument(user.Id, user.DisplayName, user.LoginPlatform);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(couchModel), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public void ReadMoveHistory()
|
||||
{
|
||||
//TODO: Separate move history into a separate request.
|
||||
//var moves = group
|
||||
// .Skip(4) // Skip 4 because group[3] will not have a .Move property since it's the first/initial BoardState of the session.
|
||||
// // TODO: Deserialize just the Move property.
|
||||
// .Select(row => row.doc.ToObject<BoardStateDocument>())
|
||||
// .Select(boardState =>
|
||||
// {
|
||||
// var move = boardState!.Move!;
|
||||
// return move.PieceFromHand.HasValue
|
||||
// ? new Models.Move(move.PieceFromHand.Value, move.To)
|
||||
// : new Models.Move(move.From!, move.To, move.IsPromotion);
|
||||
// })
|
||||
// .ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user