Example #1
0
        public void Save(Common.Game game)
        {
            var doc    = game.ToBsonDocument();
            var filter = Builders <BsonDocument> .Filter.Eq("_id", game.Id);

            Collection.FindOneAndReplace(filter, doc, new FindOneAndReplaceOptions <BsonDocument, BsonDocument> {
                IsUpsert = true
            });
        }
Example #2
0
        public static Common.Game ToGame(this BsonDocument doc)
        {
            var id          = doc["_id"].AsString;
            var currentTurn = (Common.Color)doc["currentTurn"].AsInt32;
            var board       = Common.Board.Parse(doc["board"].AsString);
            var moves       = doc["moves"].ToMoves();

            var game = new Common.Game(id, currentTurn, board, moves);

            return(game);
        }
Example #3
0
 public static BsonDocument ToBsonDocument(this Common.Game game)
 {
     return(new BsonDocument
     {
         { "_id", game.Id },
         { "currentTurn", game.CurrentTurn },
         { "board", game.Board.Dump() },
         { "moves", new BsonArray(game.Moves
                                  .Select(m => new BsonDocument
             {
                 { "moveTimeUtc", m.MoveTimeUtc },
                 { "piece", new BsonDocument {
                       { "color", m.Piece.Color }, { "type", m.Piece.PieceType }
                   } },
                 { "start", m.Start.ToString() },
                 { "end", m.End.ToString() },
                 { "captured", new BsonDocument {
                       { "color", m.CapturedPiece.Color }, { "type", m.CapturedPiece.PieceType }
                   } }
             })
                                  ) }
     });
 }
Example #4
0
 public void Save(Common.Game game)
 {
     _games[game.Id] = game;
 }