Пример #1
0
        /// <summary>
        /// Sends a notification to the Players in the game that a player has left the game.
        /// If the game is currently in the Lobby (GameState = STARTING) then no notification will be sent, the lobby list will be updated.
        /// If the game is currently playing (GameState = PLAYING) then a notification will be sent to the players in the game.
        /// A notification will be sent to the players contact (Email or phone) if the player is not currently connected to the application hub.
        /// Otherwise, if the player is currently in the app and connected to the hub an InGame notification will be sent to the client.
        /// </summary>
        /// <param name="leftPlayer">The player who left the game</param>
        public async void UpdatePlayerLeftGame(Player leftPlayer)
        {
            Console.WriteLine("HubInterface - UpdatePlayerLeftGame");

            //Get all the players currently in the game which are updateable
            GameDAL         gameDAL      = new GameDAL();
            Response <Game> gameResponse = gameDAL.GetAllPlayersInGame(leftPlayer.GameID, false, "UPDATEABLE", "AZ");

            //If an error occurred while trying to get the list of players exit the method
            if (!gameResponse.IsSuccessful())
            {
                return;
            }

            Game game = gameResponse.Data;

            //Create notifications of player leaving, if the game is playing
            if (game.IsPlaying())
            {
                gameDAL.CreateLeaveNotification(leftPlayer);
            }

            //Loop through each of the players and update any player currently connected to the hub
            foreach (var player in game.Players)
            {
                //The player is connected to the hub, send live updates
                if (player.IsConnected)
                {
                    //If the game state is IN LOBBY or STARTING then the players are in the Lobby, update the lobby list
                    if (game.IsInLobby() || game.IsStarting())
                    {
                        Console.WriteLine("Invoking UpdateGameLobbyList on :" + player.Nickname);
                        await _hubContext.Clients.Client(player.ConnectionID).SendAsync("UpdateGameLobbyList");
                    }

                    //If the game state is PLAYING - Send a notification to the players in the game.
                    else
                    {
                        Console.WriteLine("Invoking UpdateNotifications and UpdateScoreboard on :" + player.Nickname);
                        await _hubContext.Clients.Client(player.ConnectionID).SendAsync("UpdateNotifications");

                        await _hubContext.Clients.Client(player.ConnectionID).SendAsync("UpdateScoreboard");
                    }
                }

                //Otherwise, the player is not connected to the Hub, send a notification via the contact information
                //only if the player is not a BR player which is eliminated
                else if (!player.IsEliminated)
                {
                    //Don't send a notification when the game is IN LOBBY or STARTING state
                    string message = leftPlayer.Nickname + " has left your game of CamTag.";
                    string subject = "A Player Left Your Game";
                    if (game.IsPlaying() || game.IsStarting())
                    {
                        Console.WriteLine("Sending player left game SMS or Email to :" + player.Nickname);
                        player.ReceiveMessage(message, subject);
                    }
                }
            }
        }