checkpoint

This commit is contained in:
2022-06-12 12:37:32 -05:00
parent 2dcc6ca417
commit 4ca0b63564
43 changed files with 563 additions and 2128 deletions

View File

@@ -1,65 +1,52 @@
using Gameboard.ShogiUI.Sockets.Utilities;
using Shogi.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class BoardStateDocument : CouchDocument
{
public string Name { get; set; }
public class BoardStateDocument : CouchDocument
{
public string Name { get; set; }
/// <summary>
/// A dictionary where the key is a board-notation position, like D3.
/// </summary>
public Dictionary<string, Piece?> Board { get; set; }
/// <summary>
/// A dictionary where the key is a board-notation position, like D3.
/// </summary>
public Dictionary<string, Piece?> Board { get; set; }
public Piece[] Player1Hand { get; set; }
public Piece[] Player1Hand { get; set; }
public Piece[] Player2Hand { get; set; }
public Piece[] Player2Hand { get; set; }
/// <summary>
/// Move is null for first BoardState of a session - before anybody has made moves.
/// </summary>
public Move? Move { get; set; }
/// <summary>
/// Move is null for first BoardState of a session - before anybody has made moves.
/// </summary>
public Move? Move { get; set; }
/// <summary>
/// Default constructor and setters are for deserialization.
/// </summary>
public BoardStateDocument() : base(WhichDocumentType.BoardState)
{
Name = string.Empty;
Board = new Dictionary<string, Piece?>(81, StringComparer.OrdinalIgnoreCase);
Player1Hand = Array.Empty<Piece>();
Player2Hand = Array.Empty<Piece>();
}
/// <summary>
/// Default constructor and setters are for deserialization.
/// </summary>
public BoardStateDocument() : base(WhichDocumentType.BoardState)
{
Name = string.Empty;
Board = new Dictionary<string, Piece?>(81, StringComparer.OrdinalIgnoreCase);
Player1Hand = Array.Empty<Piece>();
Player2Hand = Array.Empty<Piece>();
}
public BoardStateDocument(string sessionName, Models.Shogi shogi)
: base($"{sessionName}-{DateTime.Now:O}", WhichDocumentType.BoardState)
{
Name = sessionName;
Board = new Dictionary<string, Piece?>(81, StringComparer.OrdinalIgnoreCase);
public BoardStateDocument(string sessionName, Session shogi)
: base($"{sessionName}-{DateTime.Now:O}", WhichDocumentType.BoardState)
{
static Piece MapPiece(Shogi.Domain.Pieces.Piece piece)
{
return new Piece { IsPromoted = piece.IsPromoted, Owner = piece.Owner, WhichPiece = piece.WhichPiece };
}
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
{
var position = new Vector2(x, y);
var piece = shogi.Board[y, x];
Name = sessionName;
Board = shogi.BoardState.ToDictionary(kvp => kvp.Key, kvp => kvp.Value == null ? null : MapPiece(kvp.Value));
if (piece != null)
{
var positionNotation = NotationHelper.ToBoardNotation(position);
Board[positionNotation] = new Piece(piece);
}
}
Player1Hand = shogi.Player1Hand.Select(model => new Piece(model)).ToArray();
Player2Hand = shogi.Player2Hand.Select(model => new Piece(model)).ToArray();
if (shogi.MoveHistory.Count > 0)
{
Move = new Move(shogi.MoveHistory[^1]);
}
}
}
Player1Hand = shogi.Player1Hand.Select(piece => MapPiece(piece)).ToArray();
Player2Hand = shogi.Player2Hand.Select(piece => MapPiece(piece)).ToArray();
}
}
}