コード例 #1
0
        private async Task <dynamic> GameInfo(dynamic args, CancellationToken cancellation_token)
        {
            string             game_id = Request.Query["gameId"];
            QuantumChessEngine engine  = await WebAppManagers.DatabaseManager.RequestEngine(game_id);

            var response = new Model.InfoResponse();

            response.ActivePlayer = PlayerUtils.ToString(engine.ActivePlayer);
            response.GameState    = GameStateUtils.ToString(engine.GameState);
            response.Squares      = new Dictionary <String, Model.InfoResponse.SquareEncoded>();
            for (int i = 0; i < 64; i++)
            {
                Position     pos    = Position.FromIndex(i);
                QuantumPiece qpiece = engine.QuantumChessboard.GetQuantumPiece(pos);
                if (qpiece.Piece.HasValue)
                {
                    response.Squares[pos.ToString()] = new Model.InfoResponse.SquareEncoded()
                    {
                        Player      = PlayerUtils.ToString(qpiece.Piece.Value.Player),
                        Piece       = PieceTypeUtils.ToString(qpiece.Piece.Value.PieceType),
                        Probability = qpiece.Probability
                    };
                }
                else
                {
                    response.Squares[pos.ToString()] = null;
                }
            }

            response.LastMovePositions = engine.LastMovePositions.Select((pos) => pos.ToString().ToLower()).ToArray();

            return(Response.AsJson(response));
        }
コード例 #2
0
        public static BsonDocument Serialize(Game game)
        {
            var document = new BsonDocument();

            document.Set("_id", new BsonObjectId(game.Id));
            document.Set("active_player", PlayerUtils.ToString(game.Engine.ActivePlayer));
            document.Set("game_state", GameStateUtils.ToString(game.Engine.QuantumChessboard.GameState));
            document.Set("creation_time", game.Engine.CreationTime);
            document.Set("last_modification_time", DateTime.Now);

            var bson_harmonics = new BsonArray();

            foreach (QuantumHarmonic harmonic in game.Engine.QuantumChessboard.Harmonics)
            {
                var bson_harmonic = new BsonDocument();
                bson_harmonic.Set("harmonic_state", GameStateUtils.ToString(harmonic.Board.GameState));
                bson_harmonic.Set("degeneracy", Convert.ToInt64(harmonic.Degeneracy));

                var bson_chessboard = new BsonArray();
                for (int i = 0; i < 64; i++)
                {
                    Piece?piece = harmonic.Board[i];
                    if (piece.HasValue)
                    {
                        var bson_piece = new BsonDocument();
                        bson_piece.Set("player", PlayerUtils.ToString(piece.Value.Player));
                        bson_piece.Set("piece", PieceTypeUtils.ToString(piece.Value.PieceType));
                        bson_chessboard.Add(bson_piece);
                    }
                    else
                    {
                        bson_chessboard.Add(BsonNull.Value);
                    }
                }

                bson_harmonic.Set("chessboard", bson_chessboard);
                bson_harmonics.Add(bson_harmonic);
            }
            document.Set("harmonics", bson_harmonics);

            var last_move_positions = new BsonArray();

            foreach (Position pos in game.Engine.LastMovePositions)
            {
                last_move_positions.Add(pos.ToString());
            }
            document.Set("last_move_positions", last_move_positions);

            return(document);
        }