before deleting Rules
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
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,15 @@
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class CouchCreateResult
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public bool Ok { get; set; }
|
||||
public string Rev { get; set; }
|
||||
|
||||
public CouchCreateResult()
|
||||
{
|
||||
Id = string.Empty;
|
||||
Rev = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public abstract class CouchDocument
|
||||
{
|
||||
[JsonProperty("_id")]
|
||||
public string Id { get; set; }
|
||||
public string Type { get; set; }
|
||||
public DateTimeOffset CreatedDate { get; set; }
|
||||
|
||||
public CouchDocument()
|
||||
{
|
||||
Id = string.Empty;
|
||||
Type = string.Empty;
|
||||
CreatedDate = DateTimeOffset.UtcNow;
|
||||
}
|
||||
public CouchDocument(string id, string type)
|
||||
{
|
||||
Id = id;
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
internal class CouchFindResult<T>
|
||||
{
|
||||
public T[] docs;
|
||||
|
||||
public CouchFindResult()
|
||||
{
|
||||
docs = Array.Empty<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
45
Gameboard.ShogiUI.Sockets/Repositories/CouchModels/Move.cs
Normal file
45
Gameboard.ShogiUI.Sockets/Repositories/CouchModels/Move.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class Move
|
||||
{
|
||||
/// <summary>
|
||||
/// A board coordinate, like A3 or G6. When null, look for PieceFromHand to exist.
|
||||
/// </summary>
|
||||
public string? From { get; set; }
|
||||
|
||||
public bool IsPromotion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The piece placed from the player's hand.
|
||||
/// </summary>
|
||||
public WhichPiece? PieceFromHand { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A board coordinate, like A3 or G6.
|
||||
/// </summary>
|
||||
public string To { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor and setters are for deserialization.
|
||||
/// </summary>
|
||||
public Move()
|
||||
{
|
||||
To = string.Empty;
|
||||
}
|
||||
|
||||
public Move(Models.Move move)
|
||||
{
|
||||
From = move.From?.ToBoardNotation();
|
||||
IsPromotion = move.IsPromotion;
|
||||
To = move.To.ToBoardNotation();
|
||||
PieceFromHand = move.PieceFromHand;
|
||||
}
|
||||
|
||||
public Models.Move ToDomainModel() => PieceFromHand.HasValue
|
||||
? new((ServiceModels.Socket.Types.WhichPiece)PieceFromHand, Coords.FromBoardNotation(To))
|
||||
: new(Coords.FromBoardNotation(From!), Coords.FromBoardNotation(To), IsPromotion);
|
||||
}
|
||||
}
|
||||
27
Gameboard.ShogiUI.Sockets/Repositories/CouchModels/Piece.cs
Normal file
27
Gameboard.ShogiUI.Sockets/Repositories/CouchModels/Piece.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class Piece
|
||||
{
|
||||
public bool IsPromoted { get; set; }
|
||||
public WhichPlayer Owner { get; set; }
|
||||
public WhichPiece WhichPiece { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor and setters are for deserialization.
|
||||
/// </summary>
|
||||
public Piece()
|
||||
{
|
||||
}
|
||||
|
||||
public Piece(Models.Piece piece)
|
||||
{
|
||||
IsPromoted = piece.IsPromoted;
|
||||
Owner = piece.Owner;
|
||||
WhichPiece = piece.WhichPiece;
|
||||
}
|
||||
|
||||
public Models.Piece ToDomainModel() => new(IsPromoted, Owner, WhichPiece);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
### 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.
|
||||
@@ -0,0 +1,30 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
23
Gameboard.ShogiUI.Sockets/Repositories/CouchModels/User.cs
Normal file
23
Gameboard.ShogiUI.Sockets/Repositories/CouchModels/User.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class User : CouchDocument
|
||||
{
|
||||
public static string GetDocumentId(string userName) => $"org.couchdb.user:{userName}";
|
||||
|
||||
public enum LoginPlatform
|
||||
{
|
||||
Microsoft,
|
||||
Guest
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public LoginPlatform Platform { get; set; }
|
||||
public User(string name, LoginPlatform platform) : base($"org.couchdb.user:{name}", nameof(User))
|
||||
{
|
||||
Name = name;
|
||||
Platform = platform;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories.Utility;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories.CouchModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -13,141 +12,173 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
{
|
||||
public interface IGameboardRepository
|
||||
{
|
||||
Task DeleteGame(string gameName);
|
||||
Task<Session> GetGame(string gameName);
|
||||
Task<GetSessionsResponse> GetGames();
|
||||
Task<GetSessionsResponse> GetGames(string playerName);
|
||||
Task<List<Move>> GetMoves(string gameName);
|
||||
Task<string> PostSession(PostSession request);
|
||||
Task<string> PostJoinPrivateSession(PostJoinPrivateSession request);
|
||||
Task<bool> PutJoinPublicSession(PutJoinPublicSession request);
|
||||
Task PostMove(string gameName, PostMove request);
|
||||
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> IsGuestUser(string userName);
|
||||
Task<string> PostJoinCode(string gameName, string userName);
|
||||
Task<Player> GetPlayer(string userName);
|
||||
Task<bool> PostPlayer(PostPlayer request);
|
||||
Task<Models.Session?> ReadSession(string name);
|
||||
Task<IList<Models.BoardState>> ReadBoardStates(string name);
|
||||
}
|
||||
|
||||
public class GameboardRepository : IGameboardRepository
|
||||
{
|
||||
private const string GetSessionsRoute = "Sessions";
|
||||
private const string PostSessionRoute = "Session";
|
||||
private const string JoinSessionRoute = "Session/Join";
|
||||
private const string PlayerRoute = "Player";
|
||||
private const string MediaType = "application/json";
|
||||
private readonly IAuthenticatedHttpClient client;
|
||||
public GameboardRepository(IAuthenticatedHttpClient client)
|
||||
private const string ApplicationJson = "application/json";
|
||||
private readonly HttpClient client;
|
||||
private readonly ILogger<GameboardRepository> logger;
|
||||
|
||||
public GameboardRepository(IHttpClientFactory clientFactory, ILogger<GameboardRepository> logger)
|
||||
{
|
||||
this.client = client;
|
||||
client = clientFactory.CreateClient("couchdb");
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task<GetSessionsResponse> GetGames()
|
||||
public async Task<IList<Models.Session>> ReadSessions()
|
||||
{
|
||||
var response = await client.GetAsync(GetSessionsRoute);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetSessionsResponse>(json);
|
||||
}
|
||||
var selector = $@"{{ ""{nameof(Session.Type)}"": ""{nameof(Session)}"" }}";
|
||||
var query = $@"{{ ""selector"": {selector} }}";
|
||||
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<Session>>(responseContent);
|
||||
|
||||
public async Task<GetSessionsResponse> GetGames(string playerName)
|
||||
{
|
||||
var uri = $"Sessions/{playerName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetSessionsResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<Session> GetGame(string gameName)
|
||||
{
|
||||
var uri = $"Session/{gameName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
if (result == null)
|
||||
{
|
||||
return null;
|
||||
logger.LogError("Unable to deserialize couchdb result during {0}.", nameof(this.ReadSessions));
|
||||
return Array.Empty<Models.Session>();
|
||||
}
|
||||
return new Session(JsonConvert.DeserializeObject<GetSessionResponse>(json).Session);
|
||||
return result.docs
|
||||
.Select(_ => _.ToDomainModel())
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task DeleteGame(string gameName)
|
||||
public async Task<Models.Session?> ReadSession(string name)
|
||||
{
|
||||
var uri = $"Session/{gameName}";
|
||||
await client.DeleteAsync(Uri.EscapeUriString(uri));
|
||||
var response = await client.GetAsync(name);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var couchModel = JsonConvert.DeserializeObject<Session>(responseContent);
|
||||
return couchModel.ToDomainModel();
|
||||
}
|
||||
|
||||
public async Task<string> PostSession(PostSession request)
|
||||
public async Task<IList<Models.BoardState>> ReadBoardStates(string name)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
var response = await client.PostAsync(PostSessionRoute, content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostSessionResponse>(json).SessionName;
|
||||
}
|
||||
var selector = $@"{{ ""{nameof(BoardState.Type)}"": ""{nameof(BoardState)}"", ""{nameof(BoardState.Name)}"": ""{name}"" }}";
|
||||
var sort = $@"{{ ""{nameof(BoardState.CreatedDate)}"" : ""desc"" }}";
|
||||
var query = $@"{{ ""selector"": {selector}, ""sort"": {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);
|
||||
|
||||
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<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)
|
||||
if (result == null)
|
||||
{
|
||||
return deserialized.SessionName;
|
||||
logger.LogError("Unable to deserialize couchdb result during {0}.", nameof(this.ReadSessions));
|
||||
return Array.Empty<Models.BoardState>();
|
||||
}
|
||||
return null;
|
||||
return result.docs
|
||||
.Select(_ => new Models.BoardState(_))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<List<Move>> GetMoves(string gameName)
|
||||
//public async Task DeleteGame(string gameName)
|
||||
//{
|
||||
// //var uri = $"Session/{gameName}";
|
||||
// //await client.DeleteAsync(Uri.EscapeUriString(uri));
|
||||
//}
|
||||
|
||||
public async Task<bool> CreateSession(Models.Session session)
|
||||
{
|
||||
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();
|
||||
var couchModel = new Session(session.Name, session);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(couchModel), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task PostMove(string gameName, PostMove request)
|
||||
public async Task<bool> CreateBoardState(string sessionName, Models.BoardState boardState, Models.Move? move)
|
||||
{
|
||||
var uri = $"Session/{gameName}/Move";
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
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);
|
||||
// var response = await client.PutAsync(JoinSessionRoute, content);
|
||||
// var json = await response.Content.ReadAsStringAsync();
|
||||
// return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json).JoinSucceeded;
|
||||
//}
|
||||
|
||||
//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<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 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<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;
|
||||
// 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<Player> GetPlayer(string playerName)
|
||||
//public async Task<Player> GetPlayer(string playerName)
|
||||
//{
|
||||
// var uri = $"Player/{playerName}";
|
||||
// var get = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
// var content = await get.Content.ReadAsStringAsync();
|
||||
// if (!string.IsNullOrWhiteSpace(content))
|
||||
// {
|
||||
// var response = JsonConvert.DeserializeObject<GetPlayerResponse>(content);
|
||||
// return new Player(response.Player.Name);
|
||||
// }
|
||||
// return null;
|
||||
//}
|
||||
|
||||
public async Task<bool> CreateGuestUser(string userName)
|
||||
{
|
||||
var uri = $"Player/{playerName}";
|
||||
var get = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var content = await get.Content.ReadAsStringAsync();
|
||||
if (!string.IsNullOrWhiteSpace(content))
|
||||
{
|
||||
var response = JsonConvert.DeserializeObject<GetPlayerResponse>(content);
|
||||
return new Player(response.Player.Name);
|
||||
}
|
||||
return null;
|
||||
var couchModel = new User(userName, User.LoginPlatform.Guest);
|
||||
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> PostPlayer(PostPlayer request)
|
||||
public async Task<bool> IsGuestUser(string userName)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
var response = await client.PostAsync(PlayerRoute, content);
|
||||
var req = new HttpRequestMessage(HttpMethod.Head, new Uri($"{client.BaseAddress}/{User.GetDocumentId(userName)}"));
|
||||
var response = await client.SendAsync(req);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
|
||||
Task<string> CreateGuestUser();
|
||||
Task<bool> IsPlayer1(string sessionName, string playerName);
|
||||
bool IsGuest(string playerName);
|
||||
Task<bool> PlayerExists(string playerName);
|
||||
Task<bool> CreateSession(Session session);
|
||||
}
|
||||
|
||||
public class GameboardRepositoryManager : IGameboardRepositoryManager
|
||||
@@ -30,11 +30,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
|
||||
{
|
||||
count++;
|
||||
var clientId = $"Guest-{Guid.NewGuid()}";
|
||||
var request = new PostPlayer
|
||||
{
|
||||
PlayerName = clientId
|
||||
};
|
||||
var isCreated = await repository.PostPlayer(request);
|
||||
var isCreated = await repository.CreateGuestUser(clientId);
|
||||
if (isCreated)
|
||||
{
|
||||
return clientId;
|
||||
@@ -45,22 +41,31 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
|
||||
|
||||
public async Task<bool> IsPlayer1(string sessionName, string playerName)
|
||||
{
|
||||
var session = await repository.GetGame(sessionName);
|
||||
return session?.Player1 == 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);
|
||||
}
|
||||
//var session = await repository.GetGame(sessionName);
|
||||
//if (playerName == session?.Player1)
|
||||
//{
|
||||
// return await repository.PostJoinCode(sessionName, playerName);
|
||||
//}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsGuest(string playerName) => playerName.StartsWith(GuestPrefix);
|
||||
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 async Task<bool> PlayerExists(string playerName) => await repository.GetPlayer(playerName) != null;
|
||||
public bool IsGuest(string playerName) => playerName.StartsWith(GuestPrefix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
using IdentityModel.Client;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.Utility
|
||||
{
|
||||
public interface IAuthenticatedHttpClient
|
||||
{
|
||||
Task<HttpResponseMessage> DeleteAsync(string requestUri);
|
||||
Task<HttpResponseMessage> GetAsync(string requestUri);
|
||||
Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content);
|
||||
Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content);
|
||||
}
|
||||
|
||||
public class AuthenticatedHttpClient : HttpClient, IAuthenticatedHttpClient
|
||||
{
|
||||
private readonly ILogger<AuthenticatedHttpClient> logger;
|
||||
private readonly string identityServerUrl;
|
||||
private TokenResponse tokenResponse;
|
||||
private readonly string clientId;
|
||||
private readonly string clientSecret;
|
||||
|
||||
public AuthenticatedHttpClient(ILogger<AuthenticatedHttpClient> logger, IConfiguration configuration) : base()
|
||||
{
|
||||
this.logger = logger;
|
||||
identityServerUrl = configuration["AppSettings:IdentityServer"];
|
||||
clientId = configuration["AppSettings:ClientId"];
|
||||
clientSecret = configuration["AppSettings:ClientSecret"];
|
||||
BaseAddress = new Uri(configuration["AppSettings:GameboardShogiApi"]);
|
||||
}
|
||||
|
||||
private async Task RefreshBearerToken()
|
||||
{
|
||||
var disco = await this.GetDiscoveryDocumentAsync(identityServerUrl);
|
||||
if (disco.IsError)
|
||||
{
|
||||
logger.LogError("{DiscoveryErrorType}", disco.ErrorType);
|
||||
throw new Exception(disco.Error);
|
||||
}
|
||||
|
||||
var request = new ClientCredentialsTokenRequest
|
||||
{
|
||||
Address = disco.TokenEndpoint,
|
||||
ClientId = clientId,
|
||||
ClientSecret = clientSecret
|
||||
};
|
||||
var response = await this.RequestClientCredentialsTokenAsync(request);
|
||||
if (response.IsError)
|
||||
{
|
||||
throw new Exception(response.Error);
|
||||
}
|
||||
tokenResponse = response;
|
||||
logger.LogInformation("Refreshing Bearer Token to {BaseAddress}", BaseAddress);
|
||||
this.SetBearerToken(tokenResponse.AccessToken);
|
||||
}
|
||||
|
||||
public new async Task<HttpResponseMessage> GetAsync(string requestUri)
|
||||
{
|
||||
var response = await base.GetAsync(requestUri);
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await RefreshBearerToken();
|
||||
response = await base.GetAsync(requestUri);
|
||||
}
|
||||
logger.LogInformation(
|
||||
"Repository GET to {BaseUrl}{RequestUrl} \nResponse: {Response}\n",
|
||||
BaseAddress,
|
||||
requestUri,
|
||||
await response.Content.ReadAsStringAsync());
|
||||
return response;
|
||||
}
|
||||
public new async Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
|
||||
{
|
||||
var response = await base.PostAsync(requestUri, content);
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await RefreshBearerToken();
|
||||
response = await base.PostAsync(requestUri, content);
|
||||
}
|
||||
logger.LogInformation(
|
||||
"Repository POST to {BaseUrl}{RequestUrl} \n\tRespCode: {RespCode} \n\tRequest: {Request}\n\tResponse: {Response}\n",
|
||||
BaseAddress,
|
||||
requestUri,
|
||||
response.StatusCode,
|
||||
await content.ReadAsStringAsync(),
|
||||
await response.Content.ReadAsStringAsync());
|
||||
return response;
|
||||
}
|
||||
public new async Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content)
|
||||
{
|
||||
var response = await base.PutAsync(requestUri, content);
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await RefreshBearerToken();
|
||||
response = await base.PutAsync(requestUri, content);
|
||||
}
|
||||
logger.LogInformation(
|
||||
"Repository PUT to {BaseUrl}{RequestUrl} \n\tRespCode: {RespCode} \n\tRequest: {Request}\n\tResponse: {Response}\n",
|
||||
BaseAddress,
|
||||
requestUri,
|
||||
response.StatusCode,
|
||||
await content.ReadAsStringAsync(),
|
||||
await response.Content.ReadAsStringAsync());
|
||||
return response;
|
||||
}
|
||||
public new async Task<HttpResponseMessage> DeleteAsync(string requestUri)
|
||||
{
|
||||
var response = await base.DeleteAsync(requestUri);
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await RefreshBearerToken();
|
||||
response = await base.DeleteAsync(requestUri);
|
||||
}
|
||||
logger.LogInformation("Repository DELETE to {BaseUrl}{RequestUrl}", BaseAddress, requestUri);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user