Before changing Piece[,] to Dictionary<string,Piece>

This commit is contained in:
2021-07-26 06:28:56 -05:00
parent f8f779e84c
commit 178cb00253
73 changed files with 1537 additions and 1418 deletions

View File

@@ -1,75 +0,0 @@
using System;
using System.Linq;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class BoardState : CouchDocument
{
public string Name { get; set; }
public Piece?[,] Board { get; set; }
public Piece[] Player1Hand { 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>
/// Default constructor and setters are for deserialization.
/// </summary>
public BoardState() : base()
{
Name = string.Empty;
Board = new Piece[9, 9];
Player1Hand = Array.Empty<Piece>();
Player2Hand = Array.Empty<Piece>();
}
public BoardState(string sessionName, Models.BoardState boardState) : base($"{sessionName}-{DateTime.Now:O}", nameof(BoardState))
{
Name = sessionName;
Board = new Piece[9, 9];
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
{
var piece = boardState.Board[x, y];
if (piece != null)
{
Board[x, y] = new Piece(piece);
}
}
Player1Hand = boardState.Player1Hand.Select(model => new Piece(model)).ToArray();
Player2Hand = boardState.Player2Hand.Select(model => new Piece(model)).ToArray();
if (boardState.Move != null)
{
Move = new Move(boardState.Move);
}
}
public Models.BoardState ToDomainModel()
{
/*
* Board = new Piece[9, 9];
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
{
var piece = boardState.Board[x, y];
if (piece != null)
{
Board[x, y] = new Piece(piece);
}
}
Player1Hand = boardState.Player1Hand.Select(_ => new Piece(_)).ToList();
Player2Hand = boardState.Player2Hand.Select(_ => new Piece(_)).ToList();
if (boardState.Move != null)
{
Move = new Move(boardState.Move);
}
*/
return null;
}
}
}

View File

@@ -0,0 +1,57 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using System;
using System.Linq;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class BoardStateDocument : CouchDocument
{
public string Name { get; set; }
public Piece?[,] Board { get; set; }
public Piece[] Player1Hand { 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>
/// Default constructor and setters are for deserialization.
/// </summary>
public BoardStateDocument() : base(WhichDocumentType.BoardState)
{
Name = string.Empty;
Board = new Piece[9, 9];
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 Piece[9, 9];
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
{
var piece = shogi.Board[y, x];
if (piece != null)
{
Board[y, x] = new Piece(piece);
}
}
Player1Hand = shogi.Hands[WhichPlayer.Player1].Select(model => new Piece(model)).ToArray();
Player2Hand = shogi.Hands[WhichPlayer.Player2].Select(model => new Piece(model)).ToArray();
if (shogi.MoveHistory.Count > 0)
{
Move = new Move(shogi.MoveHistory[^1]);
}
}
}
}

View File

@@ -5,21 +5,21 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public abstract class CouchDocument
{
[JsonProperty("_id")]
public string Id { get; set; }
public string Type { get; set; }
[JsonProperty("_id")] public string Id { get; set; }
public WhichDocumentType DocumentType { get; }
public DateTimeOffset CreatedDate { get; set; }
public CouchDocument()
{
Id = string.Empty;
Type = string.Empty;
CreatedDate = DateTimeOffset.UtcNow;
}
public CouchDocument(string id, string type)
public CouchDocument(WhichDocumentType documentType)
: this(string.Empty, documentType, DateTimeOffset.UtcNow) { }
public CouchDocument(string id, WhichDocumentType documentType)
: this(id, documentType, DateTimeOffset.UtcNow) { }
public CouchDocument(string id, WhichDocumentType documentType, DateTimeOffset createdDate)
{
Id = id;
Type = type;
DocumentType = documentType;
CreatedDate = createdDate;
}
}
}

View File

@@ -1,5 +1,5 @@
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using System.Numerics;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
@@ -32,14 +32,25 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
public Move(Models.Move move)
{
From = move.From?.ToBoardNotation();
if (move.From.HasValue)
{
From = ToBoardNotation(move.From.Value);
}
IsPromotion = move.IsPromotion;
To = move.To.ToBoardNotation();
To = ToBoardNotation(move.To);
PieceFromHand = move.PieceFromHand;
}
private static readonly char A = 'A';
private static string ToBoardNotation(Vector2 vector)
{
var file = (char)(vector.X + A);
var rank = vector.Y + 1;
return $"{file}{rank}";
}
public Models.Move ToDomainModel() => PieceFromHand.HasValue
? new((ServiceModels.Socket.Types.WhichPiece)PieceFromHand, Coords.FromBoardNotation(To))
: new(Coords.FromBoardNotation(From!), Coords.FromBoardNotation(To), IsPromotion);
? new(PieceFromHand.Value, To)
: new(From!, To, IsPromotion);
}
}

View File

@@ -22,6 +22,6 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
WhichPiece = piece.WhichPiece;
}
public Models.Piece ToDomainModel() => new(IsPromoted, Owner, WhichPiece);
public Models.Piece ToDomainModel() => new(WhichPiece, Owner, IsPromoted);
}
}

View File

@@ -1,4 +0,0 @@
### Couch Models
Couch models should accept domain models during construction and offer a ToDomainModel method which constructs a domain model.
In this way, domain models have the freedom to define their valid states.

View File

@@ -1,30 +0,0 @@
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class Session : CouchDocument
{
public string Name { get; set; }
public string Player1 { get; set; }
public string? Player2 { get; set; }
public bool IsPrivate { get; set; }
/// <summary>
/// Default constructor and setters are for deserialization.
/// </summary>
public Session() : base()
{
Name = string.Empty;
Player1 = string.Empty;
Player2 = string.Empty;
}
public Session(string id, Models.Session session) : base(id, nameof(Session))
{
Name = session.Name;
Player1 = session.Player1;
Player2 = session.Player2;
IsPrivate = session.IsPrivate;
}
public Models.Session ToDomainModel() => new(Name, IsPrivate, Player1, Player2);
}
}

View File

@@ -0,0 +1,48 @@
using System.Collections.Generic;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class SessionDocument : CouchDocument
{
public string Name { get; set; }
public string Player1 { get; set; }
public string? Player2 { get; set; }
public bool IsPrivate { get; set; }
public IList<BoardStateDocument> History { get; set; }
/// <summary>
/// Default constructor and setters are for deserialization.
/// </summary>
public SessionDocument() : base(WhichDocumentType.Session)
{
Name = string.Empty;
Player1 = string.Empty;
Player2 = string.Empty;
History = new List<BoardStateDocument>(0);
}
public SessionDocument(Models.Session session)
: base(session.Name, WhichDocumentType.Session)
{
Name = session.Name;
Player1 = session.Player1;
Player2 = session.Player2;
IsPrivate = session.IsPrivate;
History = new List<BoardStateDocument>(0);
}
public SessionDocument(Models.SessionMetadata sessionMetaData)
: base(sessionMetaData.Name, WhichDocumentType.Session)
{
Name = sessionMetaData.Name;
Player1 = sessionMetaData.Player1;
Player2 = sessionMetaData.Player2;
IsPrivate = sessionMetaData.IsPrivate;
History = new List<BoardStateDocument>(0);
}
public Models.Session ToDomainModel(Models.Shogi shogi) => new(Name, IsPrivate, shogi, Player1, Player2);
public Models.SessionMetadata ToDomainModel() => new(Name, IsPrivate, Player1, Player2);
}
}

View File

@@ -1,8 +1,6 @@
using System;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class User : CouchDocument
public class UserDocument : CouchDocument
{
public static string GetDocumentId(string userName) => $"org.couchdb.user:{userName}";
@@ -14,7 +12,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
public string Name { get; set; }
public LoginPlatform Platform { get; set; }
public User(string name, LoginPlatform platform) : base($"org.couchdb.user:{name}", nameof(User))
public UserDocument(string name, LoginPlatform platform) : base($"org.couchdb.user:{name}", WhichDocumentType.User)
{
Name = name;
Platform = platform;

View File

@@ -0,0 +1,9 @@
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public enum WhichDocumentType
{
User,
Session,
BoardState
}
}