private static string MarkLocation(string gameId, string playerId, int row, int column)
        {
            var    game = GetGame(gameId);
            bool   operationResult;
            string error;

            if (game == null)
            {
                operationResult = false;
                error           = "Game not found (" + gameId + ")";
            }
            else if (game.CurrentPlayer.Id != playerId)
            {
                operationResult = false;
                error           = "Not player's turn (" + playerId + ")";
            }
            else
            {
                operationResult = true;
                error           = null;
                game.MarkLocation(row, column);
                SaveGame(game);
            }

            return(ContractResult.NewContractResult(operationResult, error, game).Serialize().AsString());
        }
        private static string JoinGame(string playerTwoJson)
        {
            var    game      = FindOpenGame();
            var    playerTwo = CastPlayer(playerTwoJson);
            bool   operationResult;
            string error;

            if (game == null)
            {
                operationResult = false;
                error           = "No games available";
            }
            else if (playerTwo == null)
            {
                operationResult = false;
                error           = "Invalid Player JSON (" + playerTwo + ")";
            }
            else
            {
                if (game.JoinGame(playerTwo))
                {
                    operationResult = true;
                    error           = null;
                    CloseGame(game);
                }
                else
                {
                    operationResult = false;
                    error           = "Game full (" + game.Id + ")";
                }
            }

            return(ContractResult.NewContractResult(operationResult, error, game).Serialize().AsString());
        }
        public static string Main(string operation, params object[] args)
        {
            string error = "null";

            if (Runtime.Trigger == TriggerType.Application)
            {
                if (operation == "creategame")
                {
                    if (args.Length == 1)
                    {
                        return(CreateGame((string)args[0]));
                    }

                    error = "Incorrect number of args (" + args.Length + ") for operation (" + operation + ")";
                }
                else if (operation == "joingame")
                {
                    if (args.Length == 1)
                    {
                        return(JoinGame((string)args[1]));
                    }

                    error = "Incorrect number of args (" + args.Length + ") for operation (" + operation + ")";
                }
                else if (operation == "marklocation")
                {
                    if (args.Length == 4)
                    {
                        return(MarkLocation((string)args[0], (string)args[1], (int)args[2], (int)args[3]));
                    }

                    error = "Incorrect number of args (" + args.Length + ") for operation (" + operation + ")";
                }
                else if (operation == "fetchgame")
                {
                    if (args.Length == 1)
                    {
                        return(FetchGame((string)args[0]));
                    }

                    error = "Incorrect number of args (" + args.Length + ") for operation (" + operation + ")";
                }
                else
                {
                    error = "Invalid operation (" + operation + ")";
                }
            }

            return(ContractResult.NewContractResult(false, error).Serialize().AsString());
        }
        private static string FetchGame(string gameId)
        {
            var    game = GetGame(gameId);
            bool   operationResult;
            string error;

            if (game == null)
            {
                operationResult = false;
                error           = "Game not found (" + gameId + ")";
            }
            else
            {
                operationResult = true;
                error           = null;
            }

            return(ContractResult.NewContractResult(operationResult, error, game).Serialize().AsString());
        }
        private static string CreateGame(string playerOneJson)
        {
            var           playerOne = CastPlayer(playerOneJson);
            bool          operationResult;
            string        error;
            TicTacToeGame game;

            if (playerOne == null)
            {
                operationResult = false;
                error           = "Invalid Player JSON (" + playerOneJson + ")";
                game            = null;
            }
            else
            {
                operationResult = true;
                error           = null;
                game            = TicTacToeGame.NewTicTacToeGame(GetNextGameId(), playerOne);
                OpenGame(game);
            }

            return(ContractResult.NewContractResult(operationResult, error, game).Serialize().AsString());
        }