checkpoint

This commit is contained in:
2021-11-21 10:07:35 -06:00
parent cf3fbbbc1d
commit 433ab2772a
23 changed files with 1556 additions and 1487 deletions

View File

@@ -6,7 +6,10 @@ namespace Gameboard.ShogiUI.Sockets.ServiceModels.Api
public class GetSessionResponse
{
public Game Game { get; set; }
public WhichPlayer PlayerPerspective { get; set; }
/// <summary>
/// The perspective on the game of the requesting user.
/// </summary>
public WhichPerspective PlayerPerspective { get; set; }
public BoardState BoardState { get; set; }
public IList<Move> MoveHistory { get; set; }
}

View File

@@ -1,5 +1,4 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Api;
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket
{

View File

@@ -8,7 +8,7 @@ namespace Gameboard.ShogiUI.Sockets.ServiceModels.Types
public Dictionary<string, Piece?> Board { get; set; } = new Dictionary<string, Piece?>();
public IReadOnlyCollection<Piece> Player1Hand { get; set; } = Array.Empty<Piece>();
public IReadOnlyCollection<Piece> Player2Hand { get; set; } = Array.Empty<Piece>();
public WhichPlayer? PlayerInCheck { get; set; }
public WhichPlayer WhoseTurn { get; set; }
public WhichPerspective? PlayerInCheck { get; set; }
public WhichPerspective WhoseTurn { get; set; }
}
}

View File

@@ -4,9 +4,10 @@ namespace Gameboard.ShogiUI.Sockets.ServiceModels.Types
{
public class Game
{
public string Player1 { get; set; } = string.Empty;
public string? Player2 { get; set; } = string.Empty;
public string Player1 { get; set; }
public string? Player2 { get; set; }
public string GameName { get; set; } = string.Empty;
/// <summary>
/// Players[0] is the session owner, Players[1] is the other person.
/// </summary>
@@ -20,9 +21,13 @@ namespace Gameboard.ShogiUI.Sockets.ServiceModels.Types
}
}
/// <summary>
/// Constructor for serialization.
/// </summary>
public Game()
{
}
public Game(string gameName, string player1, string? player2 = null)
{
GameName = gameName;

View File

@@ -4,6 +4,6 @@
{
public bool IsPromoted { get; set; }
public WhichPiece WhichPiece { get; set; }
public WhichPlayer Owner { get; set; }
public WhichPerspective Owner { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace Gameboard.ShogiUI.Sockets.ServiceModels.Types
{
public class User
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
}
}

View File

@@ -0,0 +1,9 @@
namespace Gameboard.ShogiUI.Sockets.ServiceModels.Types
{
public enum WhichPerspective
{
Player1,
Player2,
Spectator
}
}

View File

@@ -1,8 +0,0 @@
namespace Gameboard.ShogiUI.Sockets.ServiceModels.Types
{
public enum WhichPlayer
{
Player1,
Player2
}
}

View File

@@ -136,17 +136,20 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
if (success)
{
try
{
await communicationManager.BroadcastToAll(new CreateGameResponse
{
Game = session.ToServiceModel(),
PlayerName = user.Id
}).ContinueWith(cont =>
{
if (cont.Exception != null)
{
Console.Error.WriteLine("Yep");
}
});
}
catch (Exception e)
{
Console.Error.WriteLine("Error broadcasting during PostSession");
}
return Ok();
}
return Conflict();
@@ -166,13 +169,23 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
return NotFound();
}
var playerPerspective = WhichPerspective.Spectator;
if (session.Player1.Id == user.Id)
{
playerPerspective = WhichPerspective.Player1;
}
else if (session.Player2?.Id == user.Id)
{
playerPerspective = WhichPerspective.Player2;
}
communicationManager.SubscribeToGame(session, user!.Id);
var response = new GetSessionResponse()
{
Game = new Models.SessionMetadata(session).ToServiceModel(user),
Game = new Models.SessionMetadata(session).ToServiceModel(),
BoardState = session.Shogi.ToServiceModel(),
MoveHistory = session.Shogi.MoveHistory.Select(_ => _.ToServiceModel()).ToList(),
PlayerPerspective = user.Id == session.Player1.Id ? WhichPlayer.Player1 : WhichPlayer.Player2
PlayerPerspective = playerPerspective
};
return new JsonResult(response);
}

View File

@@ -24,18 +24,22 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
private readonly ISocketTokenCache tokenCache;
private readonly IGameboardManager gameboardManager;
private readonly IGameboardRepository gameboardRepository;
private readonly ISocketConnectionManager connectionManager;
private readonly AuthenticationProperties authenticationProps;
public SocketController(
ILogger<SocketController> logger,
ISocketTokenCache tokenCache,
IGameboardManager gameboardManager,
IGameboardRepository gameboardRepository)
IGameboardRepository gameboardRepository,
ISocketConnectionManager connectionManager)
{
this.logger = logger;
this.tokenCache = tokenCache;
this.gameboardManager = gameboardManager;
this.gameboardRepository = gameboardRepository;
this.connectionManager = connectionManager;
authenticationProps = new AuthenticationProperties
{
AllowRefresh = true,
@@ -47,7 +51,15 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
[AllowAnonymous]
public async Task<IActionResult> GuestLogout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
var signoutTask = HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
var userId = User?.UserId();
if (!string.IsNullOrEmpty(userId))
{
connectionManager.UnsubscribeFromBroadcastAndGames(userId);
}
await signoutTask;
return Ok();
}

View File

@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Extensions
@@ -10,6 +12,7 @@ namespace Gameboard.ShogiUI.Sockets.Extensions
private readonly RequestDelegate next;
private readonly ILogger logger;
public LogMiddleware(RequestDelegate next, ILoggerFactory factory)
{
this.next = next;
@@ -24,10 +27,14 @@ namespace Gameboard.ShogiUI.Sockets.Extensions
}
finally
{
logger.LogInformation("Request {method} {url} => {statusCode}",
using var stream = new MemoryStream();
context.Request?.Body.CopyToAsync(stream);
logger.LogInformation("Request {method} {url} => {statusCode} \n Body: {body}",
context.Request?.Method,
context.Request?.Path.Value,
context.Response?.StatusCode);
context.Response?.StatusCode,
Encoding.UTF8.GetString(stream.ToArray()));
}
}
}

View File

@@ -1,5 +1,4 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
using System;
using System.Text;
using System.Text.RegularExpressions;
@@ -21,7 +20,7 @@ namespace Gameboard.ShogiUI.Sockets.Extensions
WhichPiece.Pawn => self.IsPromoted ? "^P " : " P ",
_ => " ? ",
};
if (self.Owner == WhichPlayer.Player2)
if (self.Owner == WhichPerspective.Player2)
name = Regex.Replace(name, @"([^\s]+)\s", "$1.");
return name;
}

View File

@@ -43,6 +43,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
public void SubscribeToBroadcast(WebSocket socket, string playerName)
{
connections.TryRemove(playerName, out var _);
connections.TryAdd(playerName, socket);
}

View File

@@ -8,11 +8,11 @@ namespace Gameboard.ShogiUI.Sockets.Models
public class Piece : IPlanarElement
{
public WhichPiece WhichPiece { get; }
public WhichPlayer Owner { get; private set; }
public WhichPerspective Owner { get; private set; }
public bool IsPromoted { get; private set; }
public bool IsUpsideDown => Owner == WhichPlayer.Player2;
public bool IsUpsideDown => Owner == WhichPerspective.Player2;
public Piece(WhichPiece piece, WhichPlayer owner, bool isPromoted = false)
public Piece(WhichPiece piece, WhichPerspective owner, bool isPromoted = false)
{
WhichPiece = piece;
Owner = owner;
@@ -28,9 +28,9 @@ namespace Gameboard.ShogiUI.Sockets.Models
public void ToggleOwnership()
{
Owner = Owner == WhichPlayer.Player1
? WhichPlayer.Player2
: WhichPlayer.Player1;
Owner = Owner == WhichPerspective.Player1
? WhichPerspective.Player2
: WhichPerspective.Player1;
}
public void Promote() => IsPromoted = CanPromote;

View File

@@ -33,6 +33,6 @@ namespace Gameboard.ShogiUI.Sockets.Models
Player2 = user;
}
public Game ToServiceModel() => new() { GameName = Name, Player1 = Player1.DisplayName, Player2 = Player2?.DisplayName };
public Game ToServiceModel() => new(Name, Player1.DisplayName, Player2?.DisplayName);
}
}

View File

@@ -32,20 +32,6 @@
public bool IsSeated(User user) => user.Id == Player1.Id || user.Id == Player2?.Id;
public ServiceModels.Types.Game ToServiceModel(User? user = null)
{
// TODO: Find a better way for the UI to know whether or not they are seated at a given game than client-side ID matching.
var player1 = Player1.DisplayName;
var player2 = Player2?.DisplayName;
if (user != null)
{
if (user.Id == Player1.Id) player1 = Player1.Id;
if (Player2 != null && user.Id == Player2.Id)
{
player2 = Player2.DisplayName;
}
}
return new(Name, player1, player2);
}
public ServiceModels.Types.Game ToServiceModel() => new(Name, Player1.DisplayName, Player2?.DisplayName);
}
}

View File

@@ -20,13 +20,13 @@ namespace Gameboard.ShogiUI.Sockets.Models
private Shogi? validationBoard;
private Vector2 player1King;
private Vector2 player2King;
private List<Piece> Hand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
private List<Piece> Hand => WhoseTurn == WhichPerspective.Player1 ? Player1Hand : Player2Hand;
public List<Piece> Player1Hand { get; }
public List<Piece> Player2Hand { get; }
public CoordsToNotationCollection Board { get; } //TODO: Hide this being a getter method
public List<Move> MoveHistory { get; }
public WhichPlayer WhoseTurn => MoveHistory.Count % 2 == 0 ? WhichPlayer.Player1 : WhichPlayer.Player2;
public WhichPlayer? InCheck { get; private set; }
public WhichPerspective WhoseTurn => MoveHistory.Count % 2 == 0 ? WhichPerspective.Player1 : WhichPerspective.Player2;
public WhichPerspective? InCheck { get; private set; }
public bool IsCheckmate { get; private set; }
public string Error { get; private set; }
@@ -76,7 +76,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
public bool Move(Move move)
{
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
var otherPlayer = WhoseTurn == WhichPerspective.Player1 ? WhichPerspective.Player2 : WhichPerspective.Player1;
var moveSuccess = TryMove(move);
if (!moveSuccess)
@@ -90,6 +90,10 @@ namespace Gameboard.ShogiUI.Sockets.Models
InCheck = otherPlayer;
IsCheckmate = EvaluateCheckmate();
}
else
{
InCheck = null;
}
return true;
}
/// <summary>
@@ -149,8 +153,8 @@ namespace Gameboard.ShogiUI.Sockets.Models
case WhichPiece.Knight:
{
// Knight cannot be placed onto the farthest two ranks from the hand.
if ((WhoseTurn == WhichPlayer.Player1 && move.To.Y > 6)
|| (WhoseTurn == WhichPlayer.Player2 && move.To.Y < 2))
if ((WhoseTurn == WhichPerspective.Player1 && move.To.Y > 6)
|| (WhoseTurn == WhichPerspective.Player2 && move.To.Y < 2))
{
Error = $"Knight has no valid moves after placed.";
return false;
@@ -161,8 +165,8 @@ namespace Gameboard.ShogiUI.Sockets.Models
case WhichPiece.Pawn:
{
// Lance and Pawn cannot be placed onto the farthest rank from the hand.
if ((WhoseTurn == WhichPlayer.Player1 && move.To.Y == 8)
|| (WhoseTurn == WhichPlayer.Player2 && move.To.Y == 0))
if ((WhoseTurn == WhichPerspective.Player1 && move.To.Y == 8)
|| (WhoseTurn == WhichPerspective.Player2 && move.To.Y == 0))
{
Error = $"{move.PieceFromHand} has no valid moves after placed.";
return false;
@@ -174,6 +178,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
// Mutate the board.
Board[move.To] = Hand[index];
Hand.RemoveAt(index);
MoveHistory.Add(move);
return true;
}
@@ -208,11 +213,11 @@ namespace Gameboard.ShogiUI.Sockets.Models
//Mutate the board.
if (move.IsPromotion)
{
if (WhoseTurn == WhichPlayer.Player1 && (move.To.Y > 5 || move.From.Value.Y > 5))
if (WhoseTurn == WhichPerspective.Player1 && (move.To.Y > 5 || move.From.Value.Y > 5))
{
fromPiece.Promote();
}
else if (WhoseTurn == WhichPlayer.Player2 && (move.To.Y < 3 || move.From.Value.Y < 3))
else if (WhoseTurn == WhichPerspective.Player2 && (move.To.Y < 3 || move.From.Value.Y < 3))
{
fromPiece.Promote();
}
@@ -221,12 +226,12 @@ namespace Gameboard.ShogiUI.Sockets.Models
Board[move.From!.Value] = null;
if (fromPiece.WhichPiece == WhichPiece.King)
{
if (fromPiece.Owner == WhichPlayer.Player1)
if (fromPiece.Owner == WhichPerspective.Player1)
{
player1King.X = move.To.X;
player1King.Y = move.To.Y;
}
else if (fromPiece.Owner == WhichPlayer.Player2)
else if (fromPiece.Owner == WhichPerspective.Player2)
{
player2King.X = move.To.X;
player2King.Y = move.To.Y;
@@ -250,12 +255,12 @@ namespace Gameboard.ShogiUI.Sockets.Models
}
#region Rules Validation
private bool EvaluateCheckAfterMove(Move move, WhichPlayer whichPlayer)
private bool EvaluateCheckAfterMove(Move move, WhichPerspective WhichPerspective)
{
if (whichPlayer == InCheck) return true; // If we already know the player is in check, don't bother.
if (WhichPerspective == InCheck) return true; // If we already know the player is in check, don't bother.
var isCheck = false;
var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
var kingPosition = WhichPerspective == WhichPerspective.Player1 ? player1King : player2King;
// Check if the move put the king in check.
if (pathFinder.PathTo(move.To, kingPosition)) return true;
@@ -273,7 +278,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
// if slope of the move is also infinity...can skip this?
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
if (piece.Owner != whichPlayer)
if (piece.Owner != WhichPerspective)
{
switch (piece.WhichPiece)
{
@@ -291,7 +296,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
{
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Bishop)
if (piece.Owner != WhichPerspective && piece.WhichPiece == WhichPiece.Bishop)
{
isCheck = true;
}
@@ -301,7 +306,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
{
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Rook)
if (piece.Owner != WhichPerspective && piece.WhichPiece == WhichPiece.Rook)
{
isCheck = true;
}
@@ -352,35 +357,35 @@ namespace Gameboard.ShogiUI.Sockets.Models
private void InitializeBoardState()
{
Board["A1"] = new Piece(WhichPiece.Lance, WhichPlayer.Player1);
Board["B1"] = new Piece(WhichPiece.Knight, WhichPlayer.Player1);
Board["C1"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player1);
Board["D1"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player1);
Board["E1"] = new Piece(WhichPiece.King, WhichPlayer.Player1);
Board["F1"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player1);
Board["G1"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player1);
Board["H1"] = new Piece(WhichPiece.Knight, WhichPlayer.Player1);
Board["I1"] = new Piece(WhichPiece.Lance, WhichPlayer.Player1);
Board["A1"] = new Piece(WhichPiece.Lance, WhichPerspective.Player1);
Board["B1"] = new Piece(WhichPiece.Knight, WhichPerspective.Player1);
Board["C1"] = new Piece(WhichPiece.SilverGeneral, WhichPerspective.Player1);
Board["D1"] = new Piece(WhichPiece.GoldGeneral, WhichPerspective.Player1);
Board["E1"] = new Piece(WhichPiece.King, WhichPerspective.Player1);
Board["F1"] = new Piece(WhichPiece.GoldGeneral, WhichPerspective.Player1);
Board["G1"] = new Piece(WhichPiece.SilverGeneral, WhichPerspective.Player1);
Board["H1"] = new Piece(WhichPiece.Knight, WhichPerspective.Player1);
Board["I1"] = new Piece(WhichPiece.Lance, WhichPerspective.Player1);
Board["A2"] = null;
Board["B2"] = new Piece(WhichPiece.Bishop, WhichPlayer.Player1);
Board["B2"] = new Piece(WhichPiece.Bishop, WhichPerspective.Player1);
Board["C2"] = null;
Board["D2"] = null;
Board["E2"] = null;
Board["F2"] = null;
Board["G2"] = null;
Board["H2"] = new Piece(WhichPiece.Rook, WhichPlayer.Player1);
Board["H2"] = new Piece(WhichPiece.Rook, WhichPerspective.Player1);
Board["I2"] = null;
Board["A3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["B3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["C3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["D3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["E3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["F3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["G3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["H3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["I3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["A3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["B3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["C3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["D3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["E3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["F3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["G3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["H3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["I3"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player1);
Board["A4"] = null;
Board["B4"] = null;
@@ -412,35 +417,35 @@ namespace Gameboard.ShogiUI.Sockets.Models
Board["H6"] = null;
Board["I6"] = null;
Board["A7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["B7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["C7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["D7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["E7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["F7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["G7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["H7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["I7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["A7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["B7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["C7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["D7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["E7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["F7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["G7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["H7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["I7"] = new Piece(WhichPiece.Pawn, WhichPerspective.Player2);
Board["A8"] = null;
Board["B8"] = new Piece(WhichPiece.Rook, WhichPlayer.Player2);
Board["B8"] = new Piece(WhichPiece.Rook, WhichPerspective.Player2);
Board["C8"] = null;
Board["D8"] = null;
Board["E8"] = null;
Board["F8"] = null;
Board["G8"] = null;
Board["H8"] = new Piece(WhichPiece.Bishop, WhichPlayer.Player2);
Board["H8"] = new Piece(WhichPiece.Bishop, WhichPerspective.Player2);
Board["I8"] = null;
Board["A9"] = new Piece(WhichPiece.Lance, WhichPlayer.Player2);
Board["B9"] = new Piece(WhichPiece.Knight, WhichPlayer.Player2);
Board["C9"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player2);
Board["D9"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player2);
Board["E9"] = new Piece(WhichPiece.King, WhichPlayer.Player2);
Board["F9"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player2);
Board["G9"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player2);
Board["H9"] = new Piece(WhichPiece.Knight, WhichPlayer.Player2);
Board["I9"] = new Piece(WhichPiece.Lance, WhichPlayer.Player2);
Board["A9"] = new Piece(WhichPiece.Lance, WhichPerspective.Player2);
Board["B9"] = new Piece(WhichPiece.Knight, WhichPerspective.Player2);
Board["C9"] = new Piece(WhichPiece.SilverGeneral, WhichPerspective.Player2);
Board["D9"] = new Piece(WhichPiece.GoldGeneral, WhichPerspective.Player2);
Board["E9"] = new Piece(WhichPiece.King, WhichPerspective.Player2);
Board["F9"] = new Piece(WhichPiece.GoldGeneral, WhichPerspective.Player2);
Board["G9"] = new Piece(WhichPiece.SilverGeneral, WhichPerspective.Player2);
Board["H9"] = new Piece(WhichPiece.Knight, WhichPerspective.Player2);
Board["I9"] = new Piece(WhichPiece.Lance, WhichPerspective.Player2);
}
public BoardState ToServiceModel()

View File

@@ -14,7 +14,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
"Fortuitous", "Retractable", "Happy", "Habbitable", "Creative", "Fluffy", "Impervious", "Kingly"
});
public static readonly ReadOnlyCollection<string> Subjects = new(new[] {
"Hippo", "Basil", "Mouse", "Walnut", "Prince", "Lima Bean", "Coala", "Potato"
"Hippo", "Basil", "Mouse", "Walnut", "Prince", "Lima Bean", "Coala", "Potato", "Penguin"
});
public static User CreateMsalUser(string id) => new(id, id, WhichLoginPlatform.Microsoft);
public static User CreateGuestUser(string id)
@@ -75,5 +75,11 @@ namespace Gameboard.ShogiUI.Sockets.Models
return new ClaimsIdentity(claims, JwtBearerDefaults.AuthenticationScheme);
}
}
public ServiceModels.Types.User ToServiceModel() => new()
{
Id = Id,
Name = DisplayName
};
}
}

View File

@@ -5,7 +5,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
public class Piece
{
public bool IsPromoted { get; set; }
public WhichPlayer Owner { get; set; }
public WhichPerspective Owner { get; set; }
public WhichPiece WhichPiece { get; set; }
/// <summary>

View File

@@ -1,8 +1,5 @@
using Gameboard.ShogiUI.Sockets.Repositories;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

View File

@@ -3,7 +3,7 @@ using Gameboard.ShogiUI.Sockets.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Numerics;
using WhichPlayer = Gameboard.ShogiUI.Sockets.ServiceModels.Types.WhichPlayer;
using WhichPerspective = Gameboard.ShogiUI.Sockets.ServiceModels.Types.WhichPerspective;
using WhichPiece = Gameboard.ShogiUI.Sockets.ServiceModels.Types.WhichPiece;
namespace Gameboard.ShogiUI.UnitTests.Rules
{

View File

@@ -6,7 +6,7 @@ using System.Linq;
using Xunit;
using Xunit.Abstractions;
using WhichPiece = Gameboard.ShogiUI.Sockets.ServiceModels.Types.WhichPiece;
using WhichPlayer = Gameboard.ShogiUI.Sockets.ServiceModels.Types.WhichPlayer;
using WhichPerspective = Gameboard.ShogiUI.Sockets.ServiceModels.Types.WhichPerspective;
namespace Gameboard.ShogiUI.xUnitTests
{
@@ -26,36 +26,36 @@ namespace Gameboard.ShogiUI.xUnitTests
// Assert
board["A1"].WhichPiece.Should().Be(WhichPiece.Lance);
board["A1"].Owner.Should().Be(WhichPlayer.Player1);
board["A1"].Owner.Should().Be(WhichPerspective.Player1);
board["A1"].IsPromoted.Should().Be(false);
board["B1"].WhichPiece.Should().Be(WhichPiece.Knight);
board["B1"].Owner.Should().Be(WhichPlayer.Player1);
board["B1"].Owner.Should().Be(WhichPerspective.Player1);
board["B1"].IsPromoted.Should().Be(false);
board["C1"].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board["C1"].Owner.Should().Be(WhichPlayer.Player1);
board["C1"].Owner.Should().Be(WhichPerspective.Player1);
board["C1"].IsPromoted.Should().Be(false);
board["D1"].WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board["D1"].Owner.Should().Be(WhichPlayer.Player1);
board["D1"].Owner.Should().Be(WhichPerspective.Player1);
board["D1"].IsPromoted.Should().Be(false);
board["E1"].WhichPiece.Should().Be(WhichPiece.King);
board["E1"].Owner.Should().Be(WhichPlayer.Player1);
board["E1"].Owner.Should().Be(WhichPerspective.Player1);
board["E1"].IsPromoted.Should().Be(false);
board["F1"].WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board["F1"].Owner.Should().Be(WhichPlayer.Player1);
board["F1"].Owner.Should().Be(WhichPerspective.Player1);
board["F1"].IsPromoted.Should().Be(false);
board["G1"].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board["G1"].Owner.Should().Be(WhichPlayer.Player1);
board["G1"].Owner.Should().Be(WhichPerspective.Player1);
board["G1"].IsPromoted.Should().Be(false);
board["H1"].WhichPiece.Should().Be(WhichPiece.Knight);
board["H1"].Owner.Should().Be(WhichPlayer.Player1);
board["H1"].Owner.Should().Be(WhichPerspective.Player1);
board["H1"].IsPromoted.Should().Be(false);
board["I1"].WhichPiece.Should().Be(WhichPiece.Lance);
board["I1"].Owner.Should().Be(WhichPlayer.Player1);
board["I1"].Owner.Should().Be(WhichPerspective.Player1);
board["I1"].IsPromoted.Should().Be(false);
board["A2"].Should().BeNull();
board["B2"].WhichPiece.Should().Be(WhichPiece.Bishop);
board["B2"].Owner.Should().Be(WhichPlayer.Player1);
board["B2"].Owner.Should().Be(WhichPerspective.Player1);
board["B2"].IsPromoted.Should().Be(false);
board["C2"].Should().BeNull();
board["D2"].Should().BeNull();
@@ -63,36 +63,36 @@ namespace Gameboard.ShogiUI.xUnitTests
board["F2"].Should().BeNull();
board["G2"].Should().BeNull();
board["H2"].WhichPiece.Should().Be(WhichPiece.Rook);
board["H2"].Owner.Should().Be(WhichPlayer.Player1);
board["H2"].Owner.Should().Be(WhichPerspective.Player1);
board["H2"].IsPromoted.Should().Be(false);
board["I2"].Should().BeNull();
board["A3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["A3"].Owner.Should().Be(WhichPlayer.Player1);
board["A3"].Owner.Should().Be(WhichPerspective.Player1);
board["A3"].IsPromoted.Should().Be(false);
board["B3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["B3"].Owner.Should().Be(WhichPlayer.Player1);
board["B3"].Owner.Should().Be(WhichPerspective.Player1);
board["B3"].IsPromoted.Should().Be(false);
board["C3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["C3"].Owner.Should().Be(WhichPlayer.Player1);
board["C3"].Owner.Should().Be(WhichPerspective.Player1);
board["C3"].IsPromoted.Should().Be(false);
board["D3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["D3"].Owner.Should().Be(WhichPlayer.Player1);
board["D3"].Owner.Should().Be(WhichPerspective.Player1);
board["D3"].IsPromoted.Should().Be(false);
board["E3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["E3"].Owner.Should().Be(WhichPlayer.Player1);
board["E3"].Owner.Should().Be(WhichPerspective.Player1);
board["E3"].IsPromoted.Should().Be(false);
board["F3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["F3"].Owner.Should().Be(WhichPlayer.Player1);
board["F3"].Owner.Should().Be(WhichPerspective.Player1);
board["F3"].IsPromoted.Should().Be(false);
board["G3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["G3"].Owner.Should().Be(WhichPlayer.Player1);
board["G3"].Owner.Should().Be(WhichPerspective.Player1);
board["G3"].IsPromoted.Should().Be(false);
board["H3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["H3"].Owner.Should().Be(WhichPlayer.Player1);
board["H3"].Owner.Should().Be(WhichPerspective.Player1);
board["H3"].IsPromoted.Should().Be(false);
board["I3"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["I3"].Owner.Should().Be(WhichPlayer.Player1);
board["I3"].Owner.Should().Be(WhichPerspective.Player1);
board["I3"].IsPromoted.Should().Be(false);
board["A4"].Should().BeNull();
@@ -126,36 +126,36 @@ namespace Gameboard.ShogiUI.xUnitTests
board["I6"].Should().BeNull();
board["A7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["A7"].Owner.Should().Be(WhichPlayer.Player2);
board["A7"].Owner.Should().Be(WhichPerspective.Player2);
board["A7"].IsPromoted.Should().Be(false);
board["B7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["B7"].Owner.Should().Be(WhichPlayer.Player2);
board["B7"].Owner.Should().Be(WhichPerspective.Player2);
board["B7"].IsPromoted.Should().Be(false);
board["C7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["C7"].Owner.Should().Be(WhichPlayer.Player2);
board["C7"].Owner.Should().Be(WhichPerspective.Player2);
board["C7"].IsPromoted.Should().Be(false);
board["D7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["D7"].Owner.Should().Be(WhichPlayer.Player2);
board["D7"].Owner.Should().Be(WhichPerspective.Player2);
board["D7"].IsPromoted.Should().Be(false);
board["E7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["E7"].Owner.Should().Be(WhichPlayer.Player2);
board["E7"].Owner.Should().Be(WhichPerspective.Player2);
board["E7"].IsPromoted.Should().Be(false);
board["F7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["F7"].Owner.Should().Be(WhichPlayer.Player2);
board["F7"].Owner.Should().Be(WhichPerspective.Player2);
board["F7"].IsPromoted.Should().Be(false);
board["G7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["G7"].Owner.Should().Be(WhichPlayer.Player2);
board["G7"].Owner.Should().Be(WhichPerspective.Player2);
board["G7"].IsPromoted.Should().Be(false);
board["H7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["H7"].Owner.Should().Be(WhichPlayer.Player2);
board["H7"].Owner.Should().Be(WhichPerspective.Player2);
board["H7"].IsPromoted.Should().Be(false);
board["I7"].WhichPiece.Should().Be(WhichPiece.Pawn);
board["I7"].Owner.Should().Be(WhichPlayer.Player2);
board["I7"].Owner.Should().Be(WhichPerspective.Player2);
board["I7"].IsPromoted.Should().Be(false);
board["A8"].Should().BeNull();
board["B8"].WhichPiece.Should().Be(WhichPiece.Rook);
board["B8"].Owner.Should().Be(WhichPlayer.Player2);
board["B8"].Owner.Should().Be(WhichPerspective.Player2);
board["B8"].IsPromoted.Should().Be(false);
board["C8"].Should().BeNull();
board["D8"].Should().BeNull();
@@ -163,36 +163,36 @@ namespace Gameboard.ShogiUI.xUnitTests
board["F8"].Should().BeNull();
board["G8"].Should().BeNull();
board["H8"].WhichPiece.Should().Be(WhichPiece.Bishop);
board["H8"].Owner.Should().Be(WhichPlayer.Player2);
board["H8"].Owner.Should().Be(WhichPerspective.Player2);
board["H8"].IsPromoted.Should().Be(false);
board["I8"].Should().BeNull();
board["A9"].WhichPiece.Should().Be(WhichPiece.Lance);
board["A9"].Owner.Should().Be(WhichPlayer.Player2);
board["A9"].Owner.Should().Be(WhichPerspective.Player2);
board["A9"].IsPromoted.Should().Be(false);
board["B9"].WhichPiece.Should().Be(WhichPiece.Knight);
board["B9"].Owner.Should().Be(WhichPlayer.Player2);
board["B9"].Owner.Should().Be(WhichPerspective.Player2);
board["B9"].IsPromoted.Should().Be(false);
board["C9"].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board["C9"].Owner.Should().Be(WhichPlayer.Player2);
board["C9"].Owner.Should().Be(WhichPerspective.Player2);
board["C9"].IsPromoted.Should().Be(false);
board["D9"].WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board["D9"].Owner.Should().Be(WhichPlayer.Player2);
board["D9"].Owner.Should().Be(WhichPerspective.Player2);
board["D9"].IsPromoted.Should().Be(false);
board["E9"].WhichPiece.Should().Be(WhichPiece.King);
board["E9"].Owner.Should().Be(WhichPlayer.Player2);
board["E9"].Owner.Should().Be(WhichPerspective.Player2);
board["E9"].IsPromoted.Should().Be(false);
board["F9"].WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board["F9"].Owner.Should().Be(WhichPlayer.Player2);
board["F9"].Owner.Should().Be(WhichPerspective.Player2);
board["F9"].IsPromoted.Should().Be(false);
board["G9"].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board["G9"].Owner.Should().Be(WhichPlayer.Player2);
board["G9"].Owner.Should().Be(WhichPerspective.Player2);
board["G9"].IsPromoted.Should().Be(false);
board["H9"].WhichPiece.Should().Be(WhichPiece.Knight);
board["H9"].Owner.Should().Be(WhichPlayer.Player2);
board["H9"].Owner.Should().Be(WhichPerspective.Player2);
board["H9"].IsPromoted.Should().Be(false);
board["I9"].WhichPiece.Should().Be(WhichPiece.Lance);
board["I9"].Owner.Should().Be(WhichPlayer.Player2);
board["I9"].Owner.Should().Be(WhichPerspective.Player2);
board["I9"].IsPromoted.Should().Be(false);
}
@@ -209,6 +209,32 @@ namespace Gameboard.ShogiUI.xUnitTests
shogi.Board["A4"].WhichPiece.Should().Be(WhichPiece.Pawn);
}
[Fact]
public void AllowValidMoves_AfterCheck()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move("C3", "C4"),
// P2 Pawn
new Move("G7", "G6"),
// P1 Bishop puts P2 in check
new Move("B2", "G7"),
};
var shogi = new Shogi(moves);
shogi.InCheck.Should().Be(WhichPerspective.Player2);
// Act - P2 is able to un-check theirself.
/// P2 King moves out of check
var moveSuccess = shogi.Move(new Move("E9", "E8"));
// Assert
using var _ = new AssertionScope();
moveSuccess.Should().BeTrue();
shogi.InCheck.Should().BeNull();
}
[Fact]
public void PreventInvalidMoves_MoveFromEmptyPosition()
{
@@ -263,8 +289,8 @@ namespace Gameboard.ShogiUI.xUnitTests
{
// Arrange
var shogi = new Shogi();
shogi.WhoseTurn.Should().Be(WhichPlayer.Player1);
shogi.Board["A7"].Owner.Should().Be(WhichPlayer.Player2);
shogi.WhoseTurn.Should().Be(WhichPerspective.Player1);
shogi.Board["A7"].Owner.Should().Be(WhichPerspective.Player2);
// Act - Move Player2 Pawn when it is Player1 turn.
var moveSuccess = shogi.Move(new Move("A7", "A6"));
@@ -322,14 +348,14 @@ namespace Gameboard.ShogiUI.xUnitTests
new Move("B2", "G7")
};
var shogi = new Shogi(moves);
shogi.InCheck.Should().Be(WhichPlayer.Player2);
shogi.InCheck.Should().Be(WhichPerspective.Player2);
// Act - P2 moves Lance while in check.
var moveSuccess = shogi.Move(new Move("I9", "I8"));
// Assert
moveSuccess.Should().BeFalse();
shogi.InCheck.Should().Be(WhichPlayer.Player2);
shogi.InCheck.Should().Be(WhichPerspective.Player2);
shogi.Board["I9"].WhichPiece.Should().Be(WhichPiece.Lance);
shogi.Board["I8"].Should().BeNull();
}
@@ -367,7 +393,7 @@ namespace Gameboard.ShogiUI.xUnitTests
shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.WhoseTurn.Should().Be(WhichPlayer.Player1);
shogi.WhoseTurn.Should().Be(WhichPerspective.Player1);
// Act | Assert - Illegally placing Knight from the hand in farthest row.
shogi.Board["H9"].Should().BeNull();
@@ -423,7 +449,7 @@ namespace Gameboard.ShogiUI.xUnitTests
new Move(WhichPiece.Bishop, "G7")
};
var shogi = new Shogi(moves);
shogi.InCheck.Should().Be(WhichPlayer.Player2);
shogi.InCheck.Should().Be(WhichPerspective.Player2);
shogi.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.Board["E5"].Should().BeNull();
@@ -433,7 +459,7 @@ namespace Gameboard.ShogiUI.xUnitTests
// Assert
dropSuccess.Should().BeFalse();
shogi.Board["E5"].Should().BeNull();
shogi.InCheck.Should().Be(WhichPlayer.Player2);
shogi.InCheck.Should().Be(WhichPerspective.Player2);
shogi.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
}
@@ -458,7 +484,7 @@ namespace Gameboard.ShogiUI.xUnitTests
shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.Board["I9"].Should().NotBeNull();
shogi.Board["I9"].WhichPiece.Should().Be(WhichPiece.Lance);
shogi.Board["I9"].Owner.Should().Be(WhichPlayer.Player2);
shogi.Board["I9"].Owner.Should().Be(WhichPerspective.Player2);
}
// Act - P1 tries to place a piece where an opponent's piece resides.
@@ -471,7 +497,7 @@ namespace Gameboard.ShogiUI.xUnitTests
shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.Board["I9"].Should().NotBeNull();
shogi.Board["I9"].WhichPiece.Should().Be(WhichPiece.Lance);
shogi.Board["I9"].Owner.Should().Be(WhichPlayer.Player2);
shogi.Board["I9"].Owner.Should().Be(WhichPerspective.Player2);
}
}
@@ -492,7 +518,7 @@ namespace Gameboard.ShogiUI.xUnitTests
shogi.Move(new Move("B2", "G7"));
// Assert
shogi.InCheck.Should().Be(WhichPlayer.Player2);
shogi.InCheck.Should().Be(WhichPerspective.Player2);
}
[Fact]
@@ -518,7 +544,7 @@ namespace Gameboard.ShogiUI.xUnitTests
shogi.Board["B2"].Should().BeNull();
shogi.Board["G7"].Should().NotBeNull();
shogi.Board["G7"].WhichPiece.Should().Be(WhichPiece.Bishop);
shogi.Board["G7"].Owner.Should().Be(WhichPlayer.Player1);
shogi.Board["G7"].Owner.Should().Be(WhichPerspective.Player1);
shogi.Board["G7"].IsPromoted.Should().BeTrue();
}
}
@@ -560,7 +586,7 @@ namespace Gameboard.ShogiUI.xUnitTests
// Assert - checkmate
moveSuccess.Should().BeTrue();
shogi.IsCheckmate.Should().BeTrue();
shogi.InCheck.Should().Be(WhichPlayer.Player2);
shogi.InCheck.Should().Be(WhichPerspective.Player2);
}
[Fact]
@@ -581,14 +607,14 @@ namespace Gameboard.ShogiUI.xUnitTests
moveSuccess.Should().BeTrue();
shogi.Board["B2"].Should().BeNull();
shogi.Board["H8"].WhichPiece.Should().Be(WhichPiece.Bishop);
shogi.Board["H8"].Owner.Should().Be(WhichPlayer.Player1);
shogi.Board["H8"].Owner.Should().Be(WhichPerspective.Player1);
shogi.Board.Values
.Where(p => p != null)
.Should().ContainSingle(piece => piece.WhichPiece == WhichPiece.Bishop);
shogi.Player1Hand
.Should()
.ContainSingle(p => p.WhichPiece == WhichPiece.Bishop && p.Owner == WhichPlayer.Player1);
.ContainSingle(p => p.WhichPiece == WhichPiece.Bishop && p.Owner == WhichPerspective.Player1);
}
}
}