Ejemplo n.º 1
0
        public async Task <GameBoardState> ResetGameAsync(ResetGameRequest request)
        {
            await Task.Delay(500).ConfigureAwait(false);

            string    userId      = ExtractUserId();
            GameState gameState   = null;
            bool      gameExists  = serverState.Exists(request.GameId);
            bool      validUserId = false;

            if (gameExists)
            {
                gameState   = serverState.GetGameState(request.GameId);
                validUserId = gameState.Player1.Id == userId || gameState.Player2.Id == userId;
            }
            if (!gameExists || !validUserId)
            {
                throw new ArgumentException($"Game with id {request.GameId} not found");
            }

            if (!GameLogicUtils.GameCanBeReset(gameState))
            {
                throw new ArgumentException("Game cannot be reset, only games vs computer and finished games can be reset");
            }

            gameState.BoardState = GameLogicUtils.InitializeGameBoard(request.UserTurn);
            //return Task.FromResult(gameState.BoardState);
            return(gameState.BoardState);
        }
Ejemplo n.º 2
0
    void OnMouseEnter()
    {
        startColor = theRenderer.material.color;

        if (GameLogicUtils.canHeroMoveToTile(logic.getSelectedObject(), this))
        {
            theRenderer.material.color = Color.green;
        }
        else
        {
            theRenderer.material.color = Color.red;
        }
    }
Ejemplo n.º 3
0
        private GameState InitializeGameVsComputer(Player player, InitGameRequest request)
        {
            var gameState = new GameState
            {
                Id           = Guid.NewGuid().ToString("N"),
                Type         = GameType.VS_COMPUTER,
                StartTimeUtc = DateTime.UtcNow,
                Stage        = GameStage.PLAYING,
                Player1      = player,
                Player2      = serverState.ComputerPlayer,
                BoardState   = GameLogicUtils.InitializeGameBoard(request.UserTurn),
                Difficulty   = request.Difficulty ?? serverState.DefaultGameDifficulty
            };

            serverState.AddNewGame(gameState, player);
            return(gameState);
        }
Ejemplo n.º 4
0
        public async Task <GameNotification> SendGameActionAsync(GameActionRequest actionRequest)
        {
            GameState gameState = serverState.GetGameState(actionRequest.GameId);

            if (!gameLogic.ValidateGameAction(actionRequest, gameState))
            {
                // TODO: make error messages clearer
                throw new ArgumentException("Invalid move");
            }
            GameState newState = gameLogic.ApplyAction(gameState, actionRequest.Action);

            Player player = GameLogicUtils.GetCurrentPlayer(newState);

            if (player.Type == PlayerType.COMPUTER)
            {
                if (newState.Stage == GameStage.GAME_OVER)
                {
                    return(new GameNotification {
                        NewGameState = newState
                    });
                }
                // Get computer's move and return new state to player
                GameAction computerAction    = gameAI.CalculateComputerMove(newState.BoardState, newState.Difficulty);
                GameState  afterComputerMove = gameLogic.ApplyAction(newState, computerAction);

                serverState.UpdateGameState(afterComputerMove);
                return(new GameNotification {
                    LastAction = computerAction,
                    NewGameState = afterComputerMove
                });
            }
            else
            {
                serverState.UpdateGameState(newState);
                // Notify opponent
                var notification = new GameNotification
                {
                    NewGameState = newState,
                    LastAction   = actionRequest.Action
                };
                await Clients.Client(player.Id).ReceiveGameStateUpdate(notification).ConfigureAwait(false);

                return(notification);
            }
        }
Ejemplo n.º 5
0
    public void CmdMoveUnit(int aX, int aZ, int aH, int bX, int bZ, int bH)
    {
        Debug.Log("cmdMove Unit");
        Debug.Log("connection to client: " + connectionToClient.connectionId);
        GameObject     go = connectionToClient.playerControllers [0].gameObject;
        PlayerIdentity pi = go.GetComponent <PlayerIdentity> ();

        Debug.Log("player id:" + pi.PlayerNumber);
        if (logic == null)
        {
            logic = (GameLogic)FindObjectOfType(typeof(GameLogic));
        }

        HeroInfo heroInfo = logic.getUnitOnTile(aX, aZ, aH).GetComponent <HeroInfo> () as HeroInfo;
        TileInfo tileInfo = logic.getTile(bX, bZ, bH).GetComponentInChildren <TileInfo> () as TileInfo;

        if (heroInfo == null || tileInfo == null)
        {
            Debug.Log("Something was null");
            return;
        }

        if (!logic.isPlayersTurn(getPlayerIdByConnection(connectionToClient)))
        {
            return;
        }

        if (heroInfo.player != getPlayerIdByConnection(connectionToClient))
        {
            Debug.Log("You don't own that player you fool");
            return;
        }

        if (GameLogicUtils.canHeroMoveToTile(heroInfo, tileInfo))
        {
            // get the server to do this
            if (isServer && !isClient)
            {
                MoveUnit(aX, aZ, aH, bX, bZ, bH);
            }
            // get the clients to do this
            RpcMoveUnit(aX, aZ, aH, bX, bZ, bH);
        }
    }
Ejemplo n.º 6
0
    public void CmdAttackUnit(int aX, int aZ, int aH, int bX, int bZ, int bH)
    {
        if (logic == null)
        {
            logic = (GameLogic)FindObjectOfType(typeof(GameLogic));
        }
        if (!logic.isPlayersTurn(getPlayerIdByConnection(connectionToClient)))
        {
            return;
        }

        GameObject heroAttacker = logic.getUnitOnTile(aX, aZ, aH);

        if (heroAttacker == null)
        {
            Debug.LogError("hero was null in rpcMoveUnit");
            return;
        }
        GameObject heroDeffender = logic.getUnitOnTile(bX, bZ, bH);

        if (heroDeffender == null)
        {
            Debug.LogError("hero was null in rpcMoveUnit");
            return;
        }

        HeroInfo heroInfoA = heroAttacker.GetComponent <HeroInfo> ();
        HeroInfo heroInfoB = heroDeffender.GetComponent <HeroInfo> ();

        if (heroInfoA == null)
        {
            Debug.LogError("Hero Attacker was null");
            return;
        }

        if (heroInfoB == null)
        {
            Debug.LogError("Hero Deffender was null");
            return;
        }

        if (!logic.isPlayersTurn(getPlayerIdByConnection(connectionToClient)))
        {
            return;
        }

        if (heroInfoA.player != getPlayerIdByConnection(connectionToClient))
        {
            Debug.Log("You don't own that player you fool");
            return;
        }

        if (GameLogicUtils.canHeroAttackUnit(heroInfoA, heroInfoB))
        {
            // get the server to do this
            if (isServer && !isClient)
            {
                AttackUnit(aX, aZ, aH, bX, bZ, bH);
            }
            // get the client to do this..
            RpcAttackUnit(aX, aZ, aH, bX, bZ, bH);
            logic.AttackFinished();
        }
    }