Beispiel #1
0
        public async Task PlacePiece(int row, int col)
        {
            Player     playerMakingTurn = GameState.GetPlayer(playerId: Context.ConnectionId);
            Player     opponent;
            Game       game       = GameState.GetGame(playerMakingTurn, out opponent);
            GameStatus gameStatus = game.MakeMove(playerMakingTurn, row, col);
            await Clients.Group(game.Id).SendAsync("PiecePlaced", row, col, playerMakingTurn.Id == game.Player1.Id);

            if (gameStatus == GameStatus.Win)
            {
                await Clients.Group(game.Id).SendAsync("Winner", playerMakingTurn);

                GameState.RemoveGame(game.Id);
            }
            else if (gameStatus == GameStatus.Tie)
            {
                await Clients.Group(game.Id).SendAsync("Tie");

                GameState.RemoveGame(game.Id);
            }
            else
            {
                await Clients.Group(game.Id).SendAsync("UpdateTurn");
            }
        }
Beispiel #2
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            Player leavingPlayer = GameState.GetPlayer(playerId: Context.ConnectionId);

            // Only handle cases where user was a player in a game or waiting for an opponent
            if (leavingPlayer != null)
            {
                Player opponent;
                Game   ongoingGame = GameState.GetGame(leavingPlayer, out opponent);
                if (ongoingGame != null)
                {
                    await Clients.Group(ongoingGame.Id).SendAsync("OpponentLeft");

                    GameState.RemoveGame(ongoingGame.Id);
                }
            }

            await base.OnDisconnectedAsync(exception);
        }