Fixed accidentally building the board from player2 perspective.
This commit is contained in:
@@ -31,7 +31,7 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
|
||||
var isPlayer1 = await manager.IsPlayer1(request.SessionName, userName);
|
||||
if (isPlayer1)
|
||||
{
|
||||
var code = (await repository.PostJoinCode(request.SessionName, userName)).JoinCode;
|
||||
var code = await repository.PostJoinCode(request.SessionName, userName);
|
||||
return new CreatedResult("", new PostGameInvitationResponse(code));
|
||||
}
|
||||
else
|
||||
@@ -49,7 +49,7 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
|
||||
var isPlayer1 = manager.IsPlayer1(request.SessionName, request.GuestId);
|
||||
if (isGuest && await isPlayer1)
|
||||
{
|
||||
var code = (await repository.PostJoinCode(request.SessionName, request.GuestId)).JoinCode;
|
||||
var code = await repository.PostJoinCode(request.SessionName, request.GuestId);
|
||||
return new CreatedResult("", new PostGameInvitationResponse(code));
|
||||
}
|
||||
else
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Gameboard.ShogiUI.Sockets.Managers;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -15,16 +14,13 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
|
||||
public class SocketController : ControllerBase
|
||||
{
|
||||
private readonly ISocketTokenManager tokenManager;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly IGameboardRepositoryManager gameboardManager;
|
||||
|
||||
public SocketController(
|
||||
ISocketTokenManager tokenManager,
|
||||
IGameboardRepository gameboardRepository,
|
||||
IGameboardRepositoryManager gameboardManager)
|
||||
{
|
||||
this.tokenManager = tokenManager;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.gameboardManager = gameboardManager;
|
||||
}
|
||||
|
||||
@@ -48,11 +44,10 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
|
||||
}
|
||||
else
|
||||
{
|
||||
var response = await gameboardRepository.GetPlayer(request.ClientId);
|
||||
if (response != null && response.Player != null)
|
||||
if (await gameboardManager.PlayerExists(request.ClientId))
|
||||
{
|
||||
var token = tokenManager.GenerateToken(response.Player.Name);
|
||||
return new JsonResult(new GetGuestTokenResponse(response.Player.Name, token));
|
||||
var token = tokenManager.GenerateToken(request.ClientId);
|
||||
return new JsonResult(new GetGuestTokenResponse(request.ClientId, token));
|
||||
}
|
||||
}
|
||||
return new UnauthorizedResult();
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.BoardState.csproj" />
|
||||
<ProjectReference Include="..\Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.Rules.csproj" />
|
||||
<ProjectReference Include="..\Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Gameboard.ShogiUI.BoardState;
|
||||
using Gameboard.ShogiUI.Rules;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
@@ -26,10 +26,5 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
return board;
|
||||
return null;
|
||||
}
|
||||
|
||||
public string GetBoardState()
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,16 +12,13 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
// It can be an API route and still tell socket connections about the new session.
|
||||
public class CreateGameHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<CreateGameHandler> logger;
|
||||
private readonly IGameboardRepository repository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
|
||||
public CreateGameHandler(
|
||||
ILogger<CreateGameHandler> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository repository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.repository = repository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
@@ -29,7 +26,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<CreateGameRequest>(json);
|
||||
var postSessionResponse = await repository.PostSession(new PostSession
|
||||
var sessionName = await repository.PostSession(new PostSession
|
||||
{
|
||||
SessionName = request.GameName,
|
||||
PlayerName = userName,
|
||||
@@ -41,12 +38,12 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
PlayerName = userName,
|
||||
Game = new Game
|
||||
{
|
||||
GameName = postSessionResponse.SessionName,
|
||||
GameName = sessionName,
|
||||
Players = new[] { userName }
|
||||
}
|
||||
};
|
||||
|
||||
if (string.IsNullOrWhiteSpace(postSessionResponse.SessionName))
|
||||
if (string.IsNullOrWhiteSpace(sessionName))
|
||||
{
|
||||
response.Error = "Game already exists.";
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -10,16 +9,13 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public class JoinByCodeHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<JoinByCodeHandler> logger;
|
||||
private readonly IGameboardRepository repository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
|
||||
public JoinByCodeHandler(
|
||||
ILogger<JoinByCodeHandler> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository repository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.repository = repository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
@@ -27,38 +23,38 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<JoinByCode>(json);
|
||||
var joinGameResponse = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
|
||||
var sessionName = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
|
||||
{
|
||||
PlayerName = userName,
|
||||
JoinCode = request.JoinCode
|
||||
});
|
||||
|
||||
if (joinGameResponse.JoinSucceeded)
|
||||
if (sessionName == null)
|
||||
{
|
||||
// Other members of the game see a regular JoinGame occur.
|
||||
var response = new JoinGameResponse(ClientAction.JoinGame)
|
||||
var response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = joinGameResponse.SessionName
|
||||
};
|
||||
// At this time, userName hasn't subscribed and won't receive this message.
|
||||
await communicationManager.BroadcastToGame(joinGameResponse.SessionName, response);
|
||||
|
||||
// The player joining sees the JoinByCode occur.
|
||||
response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = joinGameResponse.SessionName
|
||||
GameName = sessionName,
|
||||
Error = "Error joining game."
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
else
|
||||
{
|
||||
var response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
// Other members of the game see a regular JoinGame occur.
|
||||
var response = new JoinGameResponse(ClientAction.JoinGame)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = joinGameResponse.SessionName,
|
||||
Error = "Error joining game."
|
||||
GameName = sessionName
|
||||
};
|
||||
// At this time, userName hasn't subscribed and won't receive this message.
|
||||
await communicationManager.BroadcastToGame(sessionName, response);
|
||||
|
||||
// The player joining sees the JoinByCode occur.
|
||||
response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = sessionName
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
|
||||
|
||||
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
|
||||
var joinSucceeded = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
|
||||
{
|
||||
PlayerName = userName,
|
||||
SessionName = request.GameName
|
||||
@@ -34,7 +34,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
PlayerName = userName,
|
||||
GameName = request.GameName
|
||||
};
|
||||
if (joinGameResponse.JoinSucceeded)
|
||||
if (joinSucceeded)
|
||||
{
|
||||
await communicationManager.BroadcastToAll(response);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Gameboard.ShogiUI.BoardState;
|
||||
using Gameboard.ShogiUI.Rules;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
@@ -37,9 +37,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
var gameTask = gameboardRepository.GetGame(request.GameName);
|
||||
var moveTask = gameboardRepository.GetMoves(request.GameName);
|
||||
|
||||
var getGameResponse = await gameTask;
|
||||
var getMovesResponse = await moveTask;
|
||||
if (getGameResponse == null || getMovesResponse == null)
|
||||
var sessionModel = await gameTask;
|
||||
if (sessionModel == null)
|
||||
{
|
||||
logger.LogWarning("{action} - {user} was unable to load session named {session}.", ClientAction.LoadGame, userName, request.GameName);
|
||||
var response = new LoadGameResponse(ClientAction.LoadGame) { Error = "Game not found." };
|
||||
@@ -47,17 +46,17 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
}
|
||||
else
|
||||
{
|
||||
var sessionModel = new Models.Session(getGameResponse.Session);
|
||||
var moveModels = getMovesResponse.Moves.Select(_ => new Models.Move(_)).ToList();
|
||||
var moveModels = await moveTask;
|
||||
|
||||
communicationManager.SubscribeToGame(sessionModel, userName);
|
||||
var boardMoves = moveModels.Select(_ => _.ToBoardModel()).ToList();
|
||||
boardManager.Add(getGameResponse.Session.Name, new ShogiBoard(boardMoves));
|
||||
var shogiBoard = new ShogiBoard(boardMoves);
|
||||
boardManager.Add(sessionModel.Name, shogiBoard);
|
||||
|
||||
var response = new LoadGameResponse(ClientAction.LoadGame)
|
||||
{
|
||||
Game = sessionModel.ToServiceModel(),
|
||||
Moves = moveModels.Select(_ => _.ToServiceModel()).ToList(),
|
||||
BoardState = new Models.BoardState(shogiBoard).ToServiceModel()
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using Newtonsoft.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public class MoveHandler : IActionHandler
|
||||
@@ -25,31 +26,40 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json);
|
||||
// Basic move validation
|
||||
if (request.Move.To.Equals(request.Move.From))
|
||||
{
|
||||
var error = new Service.Messages.ErrorResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
Error = "Error: moving piece from tile to the same tile."
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(error, userName);
|
||||
return;
|
||||
}
|
||||
|
||||
var moveModel = new Move(request.Move);
|
||||
var board = boardManager.Get(request.GameName);
|
||||
var boardMove = moveModel.ToBoardModel();
|
||||
//board.Move()
|
||||
await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
|
||||
|
||||
|
||||
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
|
||||
if (board == null)
|
||||
{
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName,
|
||||
Move = moveModel.ToServiceModel()
|
||||
};
|
||||
await communicationManager.BroadcastToGame(request.GameName, response);
|
||||
// TODO: Find a flow for this
|
||||
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
Error = $"Game isn't loaded. Send a message with the {Service.Types.ClientAction.LoadGame} action first."
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
|
||||
}
|
||||
var boardMove = moveModel.ToBoardModel();
|
||||
var moveSuccess = board.Move(boardMove);
|
||||
if (moveSuccess)
|
||||
{
|
||||
await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
|
||||
var boardState = new BoardState(board);
|
||||
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName,
|
||||
BoardState = boardState.ToServiceModel()
|
||||
};
|
||||
await communicationManager.BroadcastToGame(request.GameName, response);
|
||||
}
|
||||
else
|
||||
{
|
||||
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
Error = "Invalid move."
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
40
Gameboard.ShogiUI.Sockets/Models/BoardState.cs
Normal file
40
Gameboard.ShogiUI.Sockets/Models/BoardState.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Gameboard.ShogiUI.Rules;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ServiceTypes = Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class BoardState
|
||||
{
|
||||
public Piece[,] Board { get; set; }
|
||||
public IReadOnlyCollection<Piece> Player1Hand { get; set; }
|
||||
public IReadOnlyCollection<Piece> Player2Hand { get; set; }
|
||||
|
||||
public BoardState(ShogiBoard shogi)
|
||||
{
|
||||
Board = new Piece[9, 9];
|
||||
for (var x = 0; x < 9; x++)
|
||||
for (var y = 0; y < 9; y++)
|
||||
Board[x, y] = new Piece(shogi.Board[x, y]);
|
||||
|
||||
Player1Hand = shogi.Hands[WhichPlayer.Player1].Select(_ => new Piece(_)).ToList();
|
||||
Player2Hand = shogi.Hands[WhichPlayer.Player2].Select(_ => new Piece(_)).ToList();
|
||||
}
|
||||
|
||||
public ServiceTypes.BoardState ToServiceModel()
|
||||
{
|
||||
var board = new ServiceTypes.Piece[9, 9];
|
||||
Board = new Piece[9, 9];
|
||||
for (var x = 0; x < 9; x++)
|
||||
for (var y = 0; y < 9; y++)
|
||||
board[x, y] = Board[x, y].ToServiceModel();
|
||||
return new ServiceTypes.BoardState
|
||||
{
|
||||
Board = board,
|
||||
Player1Hand = Player1Hand.Select(_ => _.ToServiceModel()).ToList(),
|
||||
Player2Hand = Player2Hand.Select(_ => _.ToServiceModel()).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
using Gameboard.ShogiUI.BoardState;
|
||||
using Gameboard.ShogiUI.Rules;
|
||||
using Microsoft.FSharp.Core;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using BoardStateMove = Gameboard.ShogiUI.Rules.Move;
|
||||
using ShogiApi = Gameboard.Shogi.Api.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
@@ -13,7 +14,6 @@ namespace Gameboard.ShogiUI.Sockets.Models
|
||||
public Coords To { get; set; }
|
||||
public bool IsPromotion { get; set; }
|
||||
|
||||
public Move() { }
|
||||
public Move(ServiceModels.Socket.Types.Move move)
|
||||
{
|
||||
From = Coords.FromBoardNotation(move.From);
|
||||
@@ -74,9 +74,9 @@ namespace Gameboard.ShogiUI.Sockets.Models
|
||||
};
|
||||
return target;
|
||||
}
|
||||
public BoardState.Move ToBoardModel()
|
||||
public BoardStateMove ToBoardModel()
|
||||
{
|
||||
return new BoardState.Move
|
||||
return new BoardStateMove
|
||||
{
|
||||
From = new Vector2(From.X, From.Y),
|
||||
IsPromotion = IsPromotion,
|
||||
|
||||
27
Gameboard.ShogiUI.Sockets/Models/Piece.cs
Normal file
27
Gameboard.ShogiUI.Sockets/Models/Piece.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using BoardStatePiece = Gameboard.ShogiUI.Rules.Pieces.Piece;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class Piece
|
||||
{
|
||||
public WhichPiece WhichPiece { get; set; }
|
||||
|
||||
public bool IsPromoted { get; set; }
|
||||
|
||||
public Piece(BoardStatePiece piece)
|
||||
{
|
||||
WhichPiece = (WhichPiece)piece.WhichPiece;
|
||||
IsPromoted = piece.IsPromoted;
|
||||
}
|
||||
|
||||
public ServiceModels.Socket.Types.Piece ToServiceModel()
|
||||
{
|
||||
return new ServiceModels.Socket.Types.Piece
|
||||
{
|
||||
IsPromoted = IsPromoted,
|
||||
WhichPiece = WhichPiece
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Gameboard.ShogiUI.Sockets/Models/Player.cs
Normal file
12
Gameboard.ShogiUI.Sockets/Models/Player.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class Player
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
public Player(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"AspShogiSockets": {
|
||||
"Kestrel": {
|
||||
"commandName": "Project",
|
||||
"launchUrl": "Socket/Token",
|
||||
"environmentVariables": {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories.Utility;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -11,17 +14,17 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
public interface IGameboardRepository
|
||||
{
|
||||
Task DeleteGame(string gameName);
|
||||
Task<GetSessionResponse> GetGame(string gameName);
|
||||
Task<Session> GetGame(string gameName);
|
||||
Task<GetSessionsResponse> GetGames();
|
||||
Task<GetSessionsResponse> GetGames(string playerName);
|
||||
Task<GetMovesResponse> GetMoves(string gameName);
|
||||
Task<PostSessionResponse> PostSession(PostSession request);
|
||||
Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request);
|
||||
Task<PutJoinPublicSessionResponse> PutJoinPublicSession(PutJoinPublicSession request);
|
||||
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<PostJoinCodeResponse> PostJoinCode(string gameName, string userName);
|
||||
Task<GetPlayerResponse> GetPlayer(string userName);
|
||||
Task<HttpResponseMessage> PostPlayer(PostPlayer request);
|
||||
Task<string> PostJoinCode(string gameName, string userName);
|
||||
Task<Player> GetPlayer(string userName);
|
||||
Task<bool> PostPlayer(PostPlayer request);
|
||||
}
|
||||
|
||||
public class GameboardRepository : IGameboardRepository
|
||||
@@ -52,12 +55,16 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
return JsonConvert.DeserializeObject<GetSessionsResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetSessionResponse> GetGame(string gameName)
|
||||
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();
|
||||
return JsonConvert.DeserializeObject<GetSessionResponse>(json);
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Session(JsonConvert.DeserializeObject<GetSessionResponse>(json).Session);
|
||||
}
|
||||
|
||||
public async Task DeleteGame(string gameName)
|
||||
@@ -66,36 +73,46 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
await client.DeleteAsync(Uri.EscapeUriString(uri));
|
||||
}
|
||||
|
||||
public async Task<PostSessionResponse> PostSession(PostSession request)
|
||||
public async Task<string> PostSession(PostSession request)
|
||||
{
|
||||
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);
|
||||
return JsonConvert.DeserializeObject<PostSessionResponse>(json).SessionName;
|
||||
}
|
||||
|
||||
public async Task<PutJoinPublicSessionResponse> PutJoinPublicSession(PutJoinPublicSession request)
|
||||
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);
|
||||
return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json).JoinSucceeded;
|
||||
}
|
||||
|
||||
public async Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request)
|
||||
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();
|
||||
return JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
|
||||
var deserialized = JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
|
||||
if (deserialized.JoinSucceeded)
|
||||
{
|
||||
return deserialized.SessionName;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<GetMovesResponse> GetMoves(string gameName)
|
||||
public async Task<List<Move>> GetMoves(string gameName)
|
||||
{
|
||||
var uri = $"Session/{gameName}/Moves";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetMovesResponse>(json);
|
||||
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)
|
||||
@@ -105,27 +122,33 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
}
|
||||
|
||||
public async Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName)
|
||||
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);
|
||||
return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json).JoinCode;
|
||||
}
|
||||
|
||||
public async Task<GetPlayerResponse> GetPlayer(string playerName)
|
||||
public async Task<Player> GetPlayer(string playerName)
|
||||
{
|
||||
var uri = $"Player/{playerName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetPlayerResponse>(json);
|
||||
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<HttpResponseMessage> PostPlayer(PostPlayer request)
|
||||
public async Task<bool> PostPlayer(PostPlayer request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
return await client.PostAsync(PlayerRoute, content);
|
||||
var response = await client.PostAsync(PlayerRoute, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +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);
|
||||
}
|
||||
|
||||
public class GameboardRepositoryManager : IGameboardRepositoryManager
|
||||
@@ -33,8 +34,8 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
|
||||
{
|
||||
PlayerName = clientId
|
||||
};
|
||||
var response = await repository.PostPlayer(request);
|
||||
if (response.IsSuccessStatusCode)
|
||||
var isCreated = await repository.PostPlayer(request);
|
||||
if (isCreated)
|
||||
{
|
||||
return clientId;
|
||||
}
|
||||
@@ -45,19 +46,21 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
|
||||
public async Task<bool> IsPlayer1(string sessionName, string playerName)
|
||||
{
|
||||
var session = await repository.GetGame(sessionName);
|
||||
return session?.Session.Player1 == playerName;
|
||||
return session?.Player1 == playerName;
|
||||
}
|
||||
|
||||
public async Task<string> CreateJoinCode(string sessionName, string playerName)
|
||||
{
|
||||
var getGameResponse = await repository.GetGame(sessionName);
|
||||
if (playerName == getGameResponse?.Session.Player1)
|
||||
var session = await repository.GetGame(sessionName);
|
||||
if (playerName == session?.Player1)
|
||||
{
|
||||
return (await repository.PostJoinCode(sessionName, playerName)).JoinCode;
|
||||
return await repository.PostJoinCode(sessionName, playerName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsGuest(string playerName) => playerName.StartsWith(GuestPrefix);
|
||||
|
||||
public async Task<bool> PlayerExists(string playerName) => await repository.GetPlayer(playerName) != null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user