/// <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); } } } }
/// <summary> /// Sends a notification to the players in the game that a new player has joined the game. /// /// If the game is currently in the lobby or starting (GameState = IN LOBBY, STARTING) /// then no notification will be sent, the lobby list will be updated. /// /// If the game is currently playing (GameState = PLAYING) then a in game notification will be sent to /// the players connect to the hub / have the web app open. /// /// An out of game notification will be sent to the players contact (Email or phone) if the player is /// not currently connected to the application hub when GameState = STARTING, PLAYING. /// </summary> /// <param name="joinedPlayer">The player who joined the game.</param> public async void UpdatePlayerJoinedGame(Player joinedPlayer) { Console.WriteLine("HubInterface - UpdatePlayerJoinedGame"); //Get all the players currently in the game which are updateable GameDAL gameDAL = new GameDAL(); Response <Game> gameResponse = gameDAL.GetAllPlayersInGame(joinedPlayer.PlayerID, true, "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 new player joining, if the game is playing and the player joining is verified if (game.IsPlaying() && joinedPlayer.IsVerified) { gameDAL.CreateJoinNotification(joinedPlayer); } //Loop through each of the players and update any player currently in the game. foreach (var player in game.Players) { //If the PlayerID is the playerID who joined skip this iteration if (player.PlayerID == joinedPlayer.PlayerID) { continue; } //Update players in game when connected to the hub / have the web app open if (player.IsConnected) { //If the game is IN LOBBY or STARTING update the lobby list for both verfied and unverified players joining if (game.IsInLobby() || game.IsStarting()) { Console.WriteLine("Invoking UpdateGameLobbyList on :" + player.Nickname); await _hubContext.Clients.Client(player.ConnectionID).SendAsync("UpdateGameLobbyList"); } //Otherwise, update the players notifications and the scoreboard because they are currently playing the game else if (game.IsPlaying() && joinedPlayer.IsVerified) { 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 out of the app and not a BR player who is eliminated, send them a text message or email. else if (!player.IsEliminated) { //Only send a notification when the game is playing or starting string message = joinedPlayer.Nickname + " has joined your game of CamTag."; string subject = "New Player Joined Your Game"; if ((game.IsPlaying() || game.IsStarting()) && joinedPlayer.IsVerified) { Console.WriteLine("Sending new player joined SMS or Email to:" + player.Nickname); player.ReceiveMessage(message, subject); } } } }