before deleting Rules

This commit is contained in:
2021-05-08 10:26:04 -05:00
parent 05a9c71499
commit f8f779e84c
80 changed files with 1109 additions and 832 deletions

View File

@@ -0,0 +1,75 @@
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,15 @@
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class CouchCreateResult
{
public string Id { get; set; }
public bool Ok { get; set; }
public string Rev { get; set; }
public CouchCreateResult()
{
Id = string.Empty;
Rev = string.Empty;
}
}
}

View File

@@ -0,0 +1,25 @@
using Newtonsoft.Json;
using System;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public abstract class CouchDocument
{
[JsonProperty("_id")]
public string Id { get; set; }
public string Type { get; set; }
public DateTimeOffset CreatedDate { get; set; }
public CouchDocument()
{
Id = string.Empty;
Type = string.Empty;
CreatedDate = DateTimeOffset.UtcNow;
}
public CouchDocument(string id, string type)
{
Id = id;
Type = type;
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
internal class CouchFindResult<T>
{
public T[] docs;
public CouchFindResult()
{
docs = Array.Empty<T>();
}
}
}

View File

@@ -0,0 +1,45 @@
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class Move
{
/// <summary>
/// A board coordinate, like A3 or G6. When null, look for PieceFromHand to exist.
/// </summary>
public string? From { get; set; }
public bool IsPromotion { get; set; }
/// <summary>
/// The piece placed from the player's hand.
/// </summary>
public WhichPiece? PieceFromHand { get; set; }
/// <summary>
/// A board coordinate, like A3 or G6.
/// </summary>
public string To { get; set; }
/// <summary>
/// Default constructor and setters are for deserialization.
/// </summary>
public Move()
{
To = string.Empty;
}
public Move(Models.Move move)
{
From = move.From?.ToBoardNotation();
IsPromotion = move.IsPromotion;
To = move.To.ToBoardNotation();
PieceFromHand = move.PieceFromHand;
}
public Models.Move ToDomainModel() => PieceFromHand.HasValue
? new((ServiceModels.Socket.Types.WhichPiece)PieceFromHand, Coords.FromBoardNotation(To))
: new(Coords.FromBoardNotation(From!), Coords.FromBoardNotation(To), IsPromotion);
}
}

View File

@@ -0,0 +1,27 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class Piece
{
public bool IsPromoted { get; set; }
public WhichPlayer Owner { get; set; }
public WhichPiece WhichPiece { get; set; }
/// <summary>
/// Default constructor and setters are for deserialization.
/// </summary>
public Piece()
{
}
public Piece(Models.Piece piece)
{
IsPromoted = piece.IsPromoted;
Owner = piece.Owner;
WhichPiece = piece.WhichPiece;
}
public Models.Piece ToDomainModel() => new(IsPromoted, Owner, WhichPiece);
}
}

View File

@@ -0,0 +1,4 @@
### 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

@@ -0,0 +1,30 @@
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,23 @@
using System;
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
{
public class User : CouchDocument
{
public static string GetDocumentId(string userName) => $"org.couchdb.user:{userName}";
public enum LoginPlatform
{
Microsoft,
Guest
}
public string Name { get; set; }
public LoginPlatform Platform { get; set; }
public User(string name, LoginPlatform platform) : base($"org.couchdb.user:{name}", nameof(User))
{
Name = name;
Platform = platform;
}
}
}