70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
namespace Shogi.Api.Repositories.Dto.SessionState;
|
|
|
|
public class SessionStateDocument
|
|
{
|
|
public long Id { get; set; }
|
|
|
|
public Dictionary<string, Piece?> Board { get; set; }
|
|
|
|
public WhichPiece[] Player1Hand { get; set; }
|
|
|
|
public WhichPiece[] Player2Hand { get; set; }
|
|
|
|
public WhichPlayer? PlayerInCheck { get; set; }
|
|
|
|
public WhichPlayer WhoseTurn { get; set; }
|
|
|
|
public bool IsGameOver { get; set; }
|
|
|
|
public string DocumentVersion { get; set; } = "1";
|
|
|
|
public SessionStateDocument() { }
|
|
public SessionStateDocument(Domain.ValueObjects.BoardState boardState)
|
|
{
|
|
this.Board = boardState.State.ToDictionary(
|
|
kvp => kvp.Key,
|
|
kvp => kvp.Value == null ? null : new Piece(kvp.Value));
|
|
|
|
this.Player1Hand = boardState.Player1Hand
|
|
.Select(piece => Map(piece.WhichPiece))
|
|
.ToArray();
|
|
|
|
this.Player2Hand = boardState.Player2Hand
|
|
.Select(piece => Map(piece.WhichPiece))
|
|
.ToArray();
|
|
|
|
this.PlayerInCheck = boardState.InCheck.HasValue
|
|
? Map(boardState.InCheck.Value)
|
|
: null;
|
|
|
|
this.IsGameOver = boardState.IsCheckmate;
|
|
}
|
|
|
|
|
|
static WhichPiece Map(Domain.ValueObjects.WhichPiece whichPiece)
|
|
{
|
|
return whichPiece switch
|
|
{
|
|
Domain.ValueObjects.WhichPiece.Bishop => WhichPiece.Bishop,
|
|
Domain.ValueObjects.WhichPiece.GoldGeneral => WhichPiece.GoldGeneral,
|
|
Domain.ValueObjects.WhichPiece.King => WhichPiece.King,
|
|
Domain.ValueObjects.WhichPiece.SilverGeneral => WhichPiece.SilverGeneral,
|
|
Domain.ValueObjects.WhichPiece.Rook => WhichPiece.Rook,
|
|
Domain.ValueObjects.WhichPiece.Knight => WhichPiece.Knight,
|
|
Domain.ValueObjects.WhichPiece.Lance => WhichPiece.Lance,
|
|
Domain.ValueObjects.WhichPiece.Pawn => WhichPiece.Pawn,
|
|
_ => throw new NotImplementedException()
|
|
};
|
|
}
|
|
|
|
static WhichPlayer Map(Domain.ValueObjects.WhichPlayer whichPlayer)
|
|
{
|
|
return whichPlayer switch
|
|
{
|
|
Domain.ValueObjects.WhichPlayer.Player1 => WhichPlayer.Player1,
|
|
Domain.ValueObjects.WhichPlayer.Player2 => WhichPlayer.Player2,
|
|
_ => throw new NotImplementedException()
|
|
};
|
|
}
|
|
}
|