public ActionResult <Response> RemoveUnverifiedPlayer([FromForm] int playerID, [FromForm] int playerIDToRemove) { try { //Create the player submitting the request Player playerMakingRequest = new Player(playerID); //Create the player to remove Player playerToRemove = new Player(playerIDToRemove); //Remove the unverified player Response <Player> response = new PlayerDAL().RemoveUnverifiedPlayer(playerMakingRequest, playerToRemove); //if the response was successful call the hub interface to update the clients if (response.IsSuccessful()) { HubInterface hubInterface = new HubInterface(_hubContext); hubInterface.UpdatePlayerLeftGame(response.Data); } return(new Response(response.ErrorMessage, response.ErrorCode)); } //Catch any error associated with invalid model data catch (InvalidModelException e) { return(new Response <int>(e.Msg, e.Code)); } //Catch any unhandled / unexpected server errrors catch { return(StatusCode(500)); } }
public ActionResult <Response> LeaveGame([FromHeader] int playerID) { try { Player player = new Player(playerID); //Get the player who is leaving the game PlayerDAL playerDAL = new PlayerDAL(); Response <Player> getPlayerResponse = playerDAL.GetPlayerByID(playerID); if (!getPlayerResponse.IsSuccessful()) { return(new Response(getPlayerResponse.ErrorMessage, getPlayerResponse.ErrorCode)); } player = getPlayerResponse.Data; //Create the hub interface which will be used to send live updates to clients HubInterface hubInterface = new HubInterface(_hubContext); //If the player leaving the game is the host and the game is currently in the lobby kick all other players from the game //because only the host player can begin the game if (player.IsHost && player.Game.IsInLobby()) { Response endLobbyResponse = new GameDAL().EndLobby(player.Game); //If successfully kicked all players from the game send live updates to clients that they have been removed from the lobby if (endLobbyResponse.IsSuccessful()) { hubInterface.UpdateLobbyEnded(player.Game); } //Return and leave the method because there is nothing else to process at this point return(endLobbyResponse); } //Call the data access layer to remove the player from the game. bool isGameComplete = false; bool isPhotosComplete = false; Response <List <Photo> > leaveGameResponse = playerDAL.LeaveGame(player, ref isGameComplete, ref isPhotosComplete); //Return the error response if an error occurred if (!leaveGameResponse.IsSuccessful()) { return(new Response(leaveGameResponse.ErrorMessage, leaveGameResponse.ErrorCode)); } //Call the hub method to send out notifications to players that the game is now complete if (isGameComplete) { hubInterface.UpdateGameCompleted(player.Game, true); } //Otherwise, if the photo list is not empty then photos have been completed and need to send out updates else if (isPhotosComplete) { foreach (var photo in leaveGameResponse.Data) { hubInterface.UpdatePhotoVotingCompleted(photo); } } //If the game is not completed send out the player left notification if (!isGameComplete) { hubInterface.UpdatePlayerLeftGame(player); } return(new Response(leaveGameResponse.ErrorMessage, leaveGameResponse.ErrorCode)); } //Catch any error associated with invalid model data catch (InvalidModelException e) { return(new Response(e.Msg, e.Code)); } //Catch any unhandled / unexpected server errrors catch { return(StatusCode(500)); } }