before deleting Rules
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Gameboard.ShogiUI.Rules;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using ServiceTypes = Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
|
||||
@@ -7,28 +8,53 @@ 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; }
|
||||
// TODO: Create a custom 2D array implementation which removes the (x,y) or (y,x) ambiguity.
|
||||
public Piece?[,] Board { get; }
|
||||
public IReadOnlyCollection<Piece> Player1Hand { get; }
|
||||
public IReadOnlyCollection<Piece> Player2Hand { get; }
|
||||
/// <summary>
|
||||
/// Move is null in the first BoardState of a Session, before any moves have been made.
|
||||
/// </summary>
|
||||
public Move? Move { get; }
|
||||
|
||||
public BoardState() : this(new ShogiBoard()) { }
|
||||
|
||||
public BoardState(Piece?[,] board, IList<Piece> player1Hand, ICollection<Piece> player2Hand, Move move)
|
||||
{
|
||||
Board = board;
|
||||
Player1Hand = new ReadOnlyCollection<Piece>(player1Hand);
|
||||
}
|
||||
|
||||
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]);
|
||||
{
|
||||
var piece = shogi.Board[x, y];
|
||||
if (piece != null)
|
||||
{
|
||||
Board[x, y] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
|
||||
Player1Hand = shogi.Hands[WhichPlayer.Player1].Select(_ => new Piece(_)).ToList();
|
||||
Player2Hand = shogi.Hands[WhichPlayer.Player2].Select(_ => new Piece(_)).ToList();
|
||||
Move = new Move(shogi.MoveHistory[^1]);
|
||||
}
|
||||
|
||||
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();
|
||||
{
|
||||
var piece = Board[x, y];
|
||||
if (piece != null)
|
||||
{
|
||||
board[x, y] = piece.ToServiceModel();
|
||||
}
|
||||
}
|
||||
return new ServiceTypes.BoardState
|
||||
{
|
||||
Board = board,
|
||||
|
||||
@@ -1,88 +1,41 @@
|
||||
using Gameboard.ShogiUI.Rules;
|
||||
using Microsoft.FSharp.Core;
|
||||
using System;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using System.Numerics;
|
||||
using BoardStateMove = Gameboard.ShogiUI.Rules.Move;
|
||||
using ShogiApi = Gameboard.Shogi.Api.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class Move
|
||||
{
|
||||
public string PieceFromCaptured { get; set; }
|
||||
public Coords From { get; set; }
|
||||
public Coords To { get; set; }
|
||||
public Coords? From { get; set; }
|
||||
public bool IsPromotion { get; set; }
|
||||
public WhichPiece? PieceFromHand { get; set; }
|
||||
public Coords To { get; set; }
|
||||
|
||||
public Move(ServiceModels.Socket.Types.Move move)
|
||||
public Move(Coords from, Coords to, bool isPromotion)
|
||||
{
|
||||
From = Coords.FromBoardNotation(move.From);
|
||||
To = Coords.FromBoardNotation(move.To);
|
||||
PieceFromCaptured = move.PieceFromCaptured;
|
||||
IsPromotion = move.IsPromotion;
|
||||
From = from;
|
||||
To = to;
|
||||
IsPromotion = isPromotion;
|
||||
}
|
||||
public Move(ShogiApi.Move move)
|
||||
|
||||
public Move(WhichPiece pieceFromHand, Coords to)
|
||||
{
|
||||
string pieceFromCaptured = null;
|
||||
if (move.PieceFromCaptured != null)
|
||||
{
|
||||
pieceFromCaptured = move.PieceFromCaptured.Value switch
|
||||
{
|
||||
ShogiApi.WhichPieceName.Bishop => "",
|
||||
ShogiApi.WhichPieceName.GoldenGeneral => "G",
|
||||
ShogiApi.WhichPieceName.King => "K",
|
||||
ShogiApi.WhichPieceName.Knight => "k",
|
||||
ShogiApi.WhichPieceName.Lance => "L",
|
||||
ShogiApi.WhichPieceName.Pawn => "P",
|
||||
ShogiApi.WhichPieceName.Rook => "R",
|
||||
ShogiApi.WhichPieceName.SilverGeneral => "S",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
From = new Coords(move.Origin.X, move.Origin.Y);
|
||||
To = new Coords(move.Destination.X, move.Destination.Y);
|
||||
IsPromotion = move.IsPromotion;
|
||||
PieceFromCaptured = pieceFromCaptured;
|
||||
PieceFromHand = pieceFromHand;
|
||||
To = to;
|
||||
}
|
||||
|
||||
public ServiceModels.Socket.Types.Move ToServiceModel() => new()
|
||||
{
|
||||
From = From.ToBoardNotation(),
|
||||
From = From?.ToBoardNotation(),
|
||||
IsPromotion = IsPromotion,
|
||||
PieceFromCaptured = PieceFromCaptured,
|
||||
To = To.ToBoardNotation()
|
||||
To = To.ToBoardNotation(),
|
||||
PieceFromCaptured = PieceFromHand
|
||||
};
|
||||
public ShogiApi.Move ToApiModel()
|
||||
|
||||
public Rules.Move ToRulesModel()
|
||||
{
|
||||
var pieceFromCaptured = PieceFromCaptured switch
|
||||
{
|
||||
"B" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Bishop),
|
||||
"G" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.GoldenGeneral),
|
||||
"K" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.King),
|
||||
"k" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Knight),
|
||||
"L" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Lance),
|
||||
"P" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Pawn),
|
||||
"R" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Rook),
|
||||
"S" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.SilverGeneral),
|
||||
_ => null
|
||||
};
|
||||
var target = new ShogiApi.Move
|
||||
{
|
||||
Origin = new ShogiApi.BoardLocation { X = From.X, Y = From.Y },
|
||||
Destination = new ShogiApi.BoardLocation { X = To.X, Y = To.Y },
|
||||
IsPromotion = IsPromotion,
|
||||
PieceFromCaptured = pieceFromCaptured
|
||||
};
|
||||
return target;
|
||||
}
|
||||
public BoardStateMove ToBoardModel()
|
||||
{
|
||||
return new BoardStateMove
|
||||
{
|
||||
From = new Vector2(From.X, From.Y),
|
||||
IsPromotion = IsPromotion,
|
||||
PieceFromCaptured = Enum.TryParse<WhichPiece>(PieceFromCaptured, out var whichPiece) ? whichPiece : null,
|
||||
To = new Vector2(To.X, To.Y)
|
||||
};
|
||||
return PieceFromHand != null
|
||||
? new Rules.Move((Rules.WhichPiece)PieceFromHand, new Vector2(To.X, To.Y))
|
||||
: new Rules.Move(new Vector2(From!.X, From.Y), new Vector2(To.X, To.Y), IsPromotion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,25 @@
|
||||
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; }
|
||||
public WhichPlayer Owner { get; }
|
||||
public WhichPiece WhichPiece { get; }
|
||||
|
||||
public bool IsPromoted { get; set; }
|
||||
|
||||
public Piece(BoardStatePiece piece)
|
||||
public Piece(bool isPromoted, WhichPlayer owner, WhichPiece whichPiece)
|
||||
{
|
||||
IsPromoted = isPromoted;
|
||||
Owner = owner;
|
||||
WhichPiece = whichPiece;
|
||||
}
|
||||
|
||||
public Piece(Rules.Pieces.Piece piece)
|
||||
{
|
||||
WhichPiece = (WhichPiece)piece.WhichPiece;
|
||||
IsPromoted = piece.IsPromoted;
|
||||
Owner = (WhichPlayer)piece.Owner;
|
||||
WhichPiece = (WhichPiece)piece.WhichPiece;
|
||||
}
|
||||
|
||||
public ServiceModels.Socket.Types.Piece ToServiceModel()
|
||||
@@ -20,6 +27,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
|
||||
return new ServiceModels.Socket.Types.Piece
|
||||
{
|
||||
IsPromoted = IsPromoted,
|
||||
Owner = Owner,
|
||||
WhichPiece = WhichPiece
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.WebSockets;
|
||||
@@ -9,18 +10,20 @@ namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class Session
|
||||
{
|
||||
[JsonIgnore] public ConcurrentDictionary<string, WebSocket> Subscriptions { get; }
|
||||
public string Name { get; }
|
||||
public string Player1 { get; }
|
||||
public string Player2 { get; }
|
||||
public string? Player2 { get; }
|
||||
public bool IsPrivate { get; }
|
||||
|
||||
public ConcurrentDictionary<string, WebSocket> Subscriptions { get; }
|
||||
|
||||
public Session(Shogi.Api.ServiceModels.Types.Session session)
|
||||
public Session(string name, bool isPrivate, string player1, string? player2 = null)
|
||||
{
|
||||
Name = session.Name;
|
||||
Player1 = session.Player1;
|
||||
Player2 = session.Player2;
|
||||
Subscriptions = new ConcurrentDictionary<string, WebSocket>();
|
||||
|
||||
Name = name;
|
||||
Player1 = player1;
|
||||
Player2 = player2;
|
||||
IsPrivate = isPrivate;
|
||||
}
|
||||
|
||||
public bool Subscribe(string playerName, WebSocket socket) => Subscriptions.TryAdd(playerName, socket);
|
||||
@@ -47,7 +50,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
|
||||
|
||||
public Task SendToPlayer2(string message)
|
||||
{
|
||||
if (Subscriptions.TryGetValue(Player2, out var socket))
|
||||
if (Player2 != null && Subscriptions.TryGetValue(Player2, out var socket))
|
||||
{
|
||||
return socket.SendTextAsync(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user