Better communication
This commit is contained in:
6
Gameboard.ShogiUI.Sockets/Managers/BoardManager.cs
Normal file
6
Gameboard.ShogiUI.Sockets/Managers/BoardManager.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
{
|
||||
public class BoardManager
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -41,13 +41,11 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
}
|
||||
else
|
||||
{
|
||||
var session = new Session(getGameResponse.Session);
|
||||
communicationManager.SubscribeToGame(socket, session, userName);
|
||||
var sessionModel = new Session(getGameResponse.Session);
|
||||
communicationManager.SubscribeToGame(socket, sessionModel, userName);
|
||||
|
||||
response.Game = session.ToServiceModel();
|
||||
response.Moves = userName.Equals(session.Player1)
|
||||
? getMovesResponse.Moves.Select(_ => Mapper.Map(_))
|
||||
: getMovesResponse.Moves.Select(_ => Move.ConvertPerspective(Mapper.Map(_)));
|
||||
response.Game = sessionModel.ToServiceModel();
|
||||
response.Moves = getMovesResponse.Moves.Select(_ => Mapper.Map(_).ToServiceModel());
|
||||
}
|
||||
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
@@ -24,12 +24,12 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<MoveRequest>(json);
|
||||
var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json);
|
||||
// Basic move validation
|
||||
if (request.Move.To.Equals(request.Move.From))
|
||||
{
|
||||
var serialized = JsonConvert.SerializeObject(
|
||||
new ErrorResponse(ClientAction.Move)
|
||||
new Service.Messages.ErrorResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
Error = "Error: moving piece from tile to the same tile."
|
||||
});
|
||||
@@ -37,25 +37,17 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
return;
|
||||
}
|
||||
|
||||
var moveModel = new Move(request.Move);
|
||||
var session = (await gameboardRepository.GetGame(request.GameName)).Session;
|
||||
var isPlayer2 = userName == session.Player2;
|
||||
// Shogi.Api expects the move coordinates from the perspective of player 1.
|
||||
var move = isPlayer2 ? Move.ConvertPerspective(request.Move) : request.Move;
|
||||
await gameboardRepository.PostMove(request.GameName, new PostMove(Mapper.Map(move)));
|
||||
await gameboardRepository.PostMove(request.GameName, new PostMove(Mapper.Map(moveModel)));
|
||||
|
||||
var responseForPlayer1 = new MoveResponse(ClientAction.Move)
|
||||
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName,
|
||||
Move = isPlayer2 ? Move.ConvertPerspective(request.Move) : request.Move
|
||||
Move = moveModel.ToServiceModel()
|
||||
};
|
||||
var responseForPlayer2 = new MoveResponse(ClientAction.Move)
|
||||
{
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName,
|
||||
Move = isPlayer2 ? request.Move : Move.ConvertPerspective(request.Move)
|
||||
};
|
||||
await communicationManager.BroadcastToGame(session.Name, responseForPlayer1, responseForPlayer2);
|
||||
await communicationManager.BroadcastToGame(session.Name, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Microsoft.FSharp.Core;
|
||||
using ShogiApi = Gameboard.Shogi.Api.ServiceModels.Types;
|
||||
|
||||
@@ -55,8 +55,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.Utility
|
||||
|
||||
var target = new Move
|
||||
{
|
||||
From = new Coords { X = origin.X, Y = origin.Y },
|
||||
To = new Coords { X = destination.X, Y = destination.Y },
|
||||
From = new Coords(origin.X, origin.Y),
|
||||
To = new Coords(destination.X, destination.Y),
|
||||
IsPromotion = source.IsPromotion,
|
||||
PieceFromCaptured = pieceFromCaptured
|
||||
};
|
||||
|
||||
41
Gameboard.ShogiUI.Sockets/Models/Coords.cs
Normal file
41
Gameboard.ShogiUI.Sockets/Models/Coords.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class Coords
|
||||
{
|
||||
private const string BoardNotationRegex = @"(?<file>[A-I])(?<rank>[1-9])";
|
||||
private const char A = 'A';
|
||||
public int X { get; }
|
||||
public int Y { get; }
|
||||
public Coords(int x, int y)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
public string ToBoardNotation()
|
||||
{
|
||||
var file = (char)(X + A);
|
||||
var rank = Y + 1;
|
||||
return $"{file}{rank}";
|
||||
}
|
||||
|
||||
public static Coords FromBoardNotation(string notation)
|
||||
{
|
||||
if (string.IsNullOrEmpty(notation))
|
||||
{
|
||||
if (Regex.IsMatch(notation, BoardNotationRegex))
|
||||
{
|
||||
var match = Regex.Match(notation, BoardNotationRegex);
|
||||
char file = match.Groups["file"].Value[0];
|
||||
int rank = int.Parse(match.Groups["rank"].Value);
|
||||
return new Coords(file - A, rank);
|
||||
}
|
||||
throw new ArgumentException("Board notation not recognized."); // TODO: Move this error handling to the service layer.
|
||||
}
|
||||
return new Coords(-1, -1); // Temporarily this is how I tell Gameboard.API that a piece came from the hand.
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Gameboard.ShogiUI.Sockets/Models/Move.cs
Normal file
27
Gameboard.ShogiUI.Sockets/Models/Move.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class Move
|
||||
{
|
||||
public string PieceFromCaptured { get; set; }
|
||||
public Coords From { get; set; }
|
||||
public Coords To { get; set; }
|
||||
public bool IsPromotion { get; set; }
|
||||
|
||||
public Move() { }
|
||||
public Move(ServiceModels.Socket.Types.Move move)
|
||||
{
|
||||
From = Coords.FromBoardNotation(move.From);
|
||||
To = Coords.FromBoardNotation(move.To);
|
||||
PieceFromCaptured = move.PieceFromCaptured;
|
||||
IsPromotion = move.IsPromotion;
|
||||
}
|
||||
|
||||
public ServiceModels.Socket.Types.Move ToServiceModel() => new ServiceModels.Socket.Types.Move
|
||||
{
|
||||
From = From.ToBoardNotation(),
|
||||
IsPromotion = IsPromotion,
|
||||
PieceFromCaptured = PieceFromCaptured,
|
||||
To = To.ToBoardNotation()
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user