89 lines
3.2 KiB
C#
89 lines
3.2 KiB
C#
using Gameboard.ShogiUI.BoardState;
|
|
using Microsoft.FSharp.Core;
|
|
using System;
|
|
using System.Numerics;
|
|
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 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 Move(ShogiApi.Move move)
|
|
{
|
|
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;
|
|
}
|
|
public ServiceModels.Socket.Types.Move ToServiceModel() => new()
|
|
{
|
|
From = From.ToBoardNotation(),
|
|
IsPromotion = IsPromotion,
|
|
PieceFromCaptured = PieceFromCaptured,
|
|
To = To.ToBoardNotation()
|
|
};
|
|
public ShogiApi.Move ToApiModel()
|
|
{
|
|
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 BoardState.Move ToBoardModel()
|
|
{
|
|
return new BoardState.Move
|
|
{
|
|
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)
|
|
};
|
|
}
|
|
}
|
|
}
|