/// <summary> /// The code which will run in a new thread after the game start time has passed in order /// to updated a started game to a PLAYING game state. /// </summary> /// <param name="game">The Game being updated</param> /// <param name="hubInterface">The HubInterface used to to send live updates to users.</param> /// <param name="timeToWait">The number of milliseconds to sleep before the thread starts.</param> private static void Run_GameInPlayingState(Game game, int timeToWait, HubInterface hubInterface) { //Sleep the thread until the game has reached the playing state Thread.Sleep(timeToWait); //Get the updated game record GameDAL gameDAL = new GameDAL(); Response <Game> gameResponse = gameDAL.GetGameByID(game.GameID); if (!gameResponse.IsSuccessful()) { return; } //Confirm the game is in a starting state and the start time has elapsed if (gameResponse.Data.GameState != "STARTING") { return; } if (gameResponse.Data.StartTime.Value > DateTime.Now) { return; } Response response = gameDAL.SetGameState(game.GameID, "PLAYING"); //Send updates to clients that the game has now officially begun if (response.IsSuccessful()) { hubInterface.UpdateGameNowPlaying(game); } }
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 <Game> > BeginGame([FromHeader] int playerID) { try { //Create the player object and begin the game. Player hostPlayer = new Player(playerID); Response <Game> response = new GameDAL().BeginGame(hostPlayer); //If the response is successful schedule code to run to update the GameState after the time periods have passed if (response.IsSuccessful()) { HubInterface hubInterface = new HubInterface(_hubContext); ScheduledTasks.ScheduleGameInPlayingState(response.Data, hubInterface); ScheduledTasks.ScheduleCompleteGame(response.Data, hubInterface); //Update all clients that the game is now in a starting state and the game will be playing soon hubInterface.UpdateGameInStartingState(response.Data); } return(response); } //Catch any error associated with invalid model data catch (InvalidModelException e) { return(new Response <Game>(e.Msg, e.Code)); } //Catch any unhandled / unexpected server errrors catch { return(StatusCode(500)); } }
/// <summary> /// Schedules a player to be re-enabled after a certain period of time after they have been disabled /// after being disabled for taking a photo outside of the playing zone in a BR game. /// </summary> /// <param name="player">The player being re-enabled</param> /// <param name="totalMillisecondsDisabled">The number of milliseconds to wait for the thread to start.</param> /// <param name="hubInterface">The Hub interface which will be used to send notifications / updates</param> public static void BR_SchedulePlayerReEnabled(Player player, HubInterface hubInterface, int totalMillisecondsDisabled) { int timeToWait = totalMillisecondsDisabled; //Start the Method in a new thread Thread SchedulePlayerReEnabled = new Thread( () => Run_RenablePlayer(player, timeToWait, hubInterface) ); SchedulePlayerReEnabled.Start(); }
/// <summary> /// Schedules code to run in a new thread which will run after the FinishVotingTime has passed. /// The method checks to see if all players have voted on the image and if not, will /// update the image to be successful and make all votes a success. Then send out notifications /// to the affected players. /// </summary> /// <param name="uploadedPhoto">The photo which was uploaded and being checked if voting has been completed.</param> /// <param name="hubInterface">The Hub interface which will be used to send notifications / updates</param> public static void ScheduleCheckPhotoVotingCompleted(Photo uploadedPhoto, HubInterface hubInterface) { //Get how long to wait for the task to run int timeToWait = GetTimeToWait(uploadedPhoto.VotingFinishTime); //Start the Method in a new thread Thread ScheduleReplenishAmmoThread = new Thread( () => Run_CheckPhotoVotingCompleted(uploadedPhoto, timeToWait, hubInterface) ); ScheduleReplenishAmmoThread.Start(); }
/// <summary> /// Schedules code to run in a new thread after the ammo replenish time has passed in order /// to increment the players ammo count. /// </summary> /// <param name="playerID">The ID of the player being updated.</param> /// <param name="hubInterface">The HubInterface used to to send live updates to users.</param> public static void ScheduleReplenishAmmo(Player player, HubInterface hubInterface) { //Get how long to wait for the task to run int timeToWait = GetTimeToWait(DateTime.Now.AddMilliseconds(player.Game.ReplenishAmmoDelay)); //Start the Method in a new thread Thread ScheduleReplenishAmmoThread = new Thread( () => Run_ReplenishAmmo(player, timeToWait, hubInterface) ); ScheduleReplenishAmmoThread.Start(); }
/// <summary> /// Schedules code to run in a new thread after the game finish time has passed in order /// to updated the game record to COMPLETED. /// </summary> /// <param name="game">The Game being updated</param> /// <param name="hubInterface">The HubInterface used to to send live updates to users.</param> public static void ScheduleCompleteGame(Game game, HubInterface hubInterface) { //Get how long to wait for the task to run int timeToWait = GetTimeToWait(game.EndTime.Value); //Start the Method in a new thread Thread ScheduleCompleteGameThread = new Thread( () => Run_CompleteGame(game, timeToWait, hubInterface) ); ScheduleCompleteGameThread.Start(); }
/// <summary> /// The code which will run in a new thread after the ammo replenish time has passed in order /// to increment the players ammo count. /// </summary> /// <param name="playerID">The ID of the player being updated.</param> /// <param name="hubInterface">The HubInterface used to to send live updates to users.</param> /// <param name="timeToWait">The number of milliseconds to sleep before the thread starts.</param> private static void Run_ReplenishAmmo(Player player, int timeToWait, HubInterface hubInterface) { Thread.Sleep(timeToWait); //Call the DataAccessLayer to increment the ammo count in the database PlayerDAL playerDAL = new PlayerDAL(); Response <Player> response = playerDAL.ReplenishAmmo(player); if (response.IsSuccessful()) { //As the ammo has replenished, send out a notification and update client. hubInterface.UpdateAmmoReplenished(response.Data); } }
/// <summary> /// The code which will run in a new thread after the game finish time has passed in order /// to updated the game record to COMPLETED. /// </summary> /// <param name="game">The Game being updated</param> /// <param name="hubInterface">The HubInterface used to to send live updates to users.</param> /// <param name="timeToWait">The number of milliseconds to sleep before the thread starts.</param> private static void Run_CompleteGame(Game game, int timeToWait, HubInterface hubInterface) { Thread.Sleep(timeToWait); //Call the DataAccessLayer to complete the game in the DB GameDAL gameDAL = new GameDAL(); Response response = gameDAL.CompleteGame(game.GameID); //If the response was successful send out the game completed messages to players if (response.IsSuccessful()) { hubInterface.UpdateGameCompleted(game, false); } }
public ActionResult <Response> Upload(PhotoUploadRequest request) { try { //Build the photo object Photo uploadedPhoto = new Photo(request.latitude, request.longitude, request.imgUrl, request.takenByID, request.photoOfID); //Get the player object from the database Response <Player> getPlayerResponse = new PlayerDAL().GetPlayerByID(uploadedPhoto.TakenByPlayerID); if (!getPlayerResponse.IsSuccessful()) { return(new Response(getPlayerResponse.ErrorMessage, getPlayerResponse.ErrorCode)); } Response <Photo> response; //Call the BR Upload business logic if the player is a BR player if (getPlayerResponse.Data.IsBRPlayer()) { response = new PhotoDAL().SavePhoto(uploadedPhoto, true); } //Otherwise, call the CORE business logic else { response = new PhotoDAL().SavePhoto(uploadedPhoto, false); } //If the response is successful we want to send live updates to clients and //email or text message notifications to not connected players if (response.IsSuccessful()) { HubInterface hubInterface = new HubInterface(_hubContext); hubInterface.UpdatePhotoUploaded(response.Data); ScheduledTasks.ScheduleCheckPhotoVotingCompleted(response.Data, hubInterface); } return(new Response(response.ErrorMessage, response.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)); } }
/// <summary> /// Helper method for the UseAmmo request, processes the CORE gamemode business logic for using a players ammo. /// </summary> /// <param name="player">The player who is using the ammo</param> /// <returns>The updated player object outlining the new ammo count.</returns> private Response <Player> CORE_UseAmmoLogic(Player player) { Response <Player> response = new PlayerDAL().UseAmmo(player); //If the response was successful schedule code to run in order to replenish the players ammo if (response.IsSuccessful()) { HubInterface hubInterface = new HubInterface(_hubContext); ScheduledTasks.ScheduleReplenishAmmo(response.Data, hubInterface); //Compress the player object before sending back over the network response.Data.Compress(true, true, true); } return(response); }
/// <summary> /// The code which will run after the disabled time has elapsed and the player will be re-enabled. /// </summary> /// <param name="player"></param> /// <param name="timeToWait"></param> /// <param name="hubInterface"></param> private static void Run_RenablePlayer(Player player, int timeToWait, HubInterface hubInterface) { //Wait for the specified time Thread.Sleep(timeToWait); //Renable the player Response <Player> response = new PlayerDAL().BR_DisableOrRenablePlayer(player, 0); if (!response.IsSuccessful()) { return; } //Call the hub to send an update that the player has now been re-enabled hubInterface.BR_UpdatePlayerReEnabled(player); }
/// <summary> /// Helper method for the UseAmmo request, processes the BR gamemode businesss logic. /// Will confirm the player is within the playing radius, if not will disable the player. /// </summary> /// <param name="player">The player who is using the ammo</param> /// <param name="latitude">The latitude of the player.</param> /// <param name="longitude">The longitude of the player.</param> /// <returns></returns> private ActionResult <Response <Player> > BR_UseAmmoLogic(Player player, double latitude, double longitude) { //Decrement the players ammo Response <Player> response = new PlayerDAL().UseAmmo(player); if (!response.IsSuccessful()) { return(response); } player = response.Data; HubInterface hubInterface = new HubInterface(_hubContext); //If the player is not within the zone disable the player if (!player.Game.IsInZone(latitude, longitude)) { //Calculate the the number of minutes the player will be disabled for int totalMinutesDisabled = player.Game.CalculateDisabledTime(); int totalMillisecondsDisabled = totalMinutesDisabled * 60 * 1000; //Disable the player response = new PlayerDAL().BR_DisableOrRenablePlayer(player, totalMinutesDisabled); if (!response.IsSuccessful()) { return(response); } player = response.Data; //Call the hub to update the client that they are now disabled hubInterface.BR_UpdatePlayerDisabled(player, totalMinutesDisabled); //Schedule the player to be re-enabled ScheduledTasks.BR_SchedulePlayerReEnabled(player, hubInterface, totalMillisecondsDisabled); //Set the error code and message to indicate to the client that the player is now disabled. response.ErrorMessage = "Not inside the zone."; response.ErrorCode = ErrorCodes.BR_NOTINZONE; } //Schedule the ammo to be replenished ScheduledTasks.ScheduleReplenishAmmo(player, hubInterface); //Compress the player object before sending back over the network response.Data.Compress(true, true, true); return(response); }
public ActionResult <Response> VoteOnPhoto([FromHeader] int playerID, [FromHeader] int voteID, [FromForm] string decision) { try { //Get the player object from the database Response <Player> getPlayerResponse = new PlayerDAL().GetPlayerByID(playerID); if (!getPlayerResponse.IsSuccessful()) { return(new Response(getPlayerResponse.ErrorMessage, getPlayerResponse.ErrorCode)); } Vote vote = new Vote(voteID, decision, playerID); Response <Vote> response; //Vote on the photo response = new PhotoDAL().VoteOnPhoto(vote, getPlayerResponse.Data.IsBRPlayer()); if (response.IsSuccessful()) { //If the response's data is NULL that means the game is now completed for a BR game. Send live updates to complete the game if (response.Data == null) { HubInterface hubInterface = new HubInterface(_hubContext); hubInterface.UpdateGameCompleted(getPlayerResponse.Data.Game, false); } //If the Photo's voting has now been completed send the notifications / updates else if (response.Data.Photo.IsVotingComplete) { HubInterface hubInterface = new HubInterface(_hubContext); hubInterface.UpdatePhotoVotingCompleted(response.Data.Photo); } } return(new Response(response.ErrorMessage, response.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)); } }
public ActionResult <Response <Player> > JoinGame(JoinGameRequest request) { try { //Create the player object who will be joining the game Player playerToJoin = new Player(request.nickname, request.imgUrl, request.contact); Game gameToJoin = new Game(request.gameCode); //Generate a verification code int verificationCode = Player.GenerateVerificationCode(); //Call the data access layer to add the player to the database Response <Player> response = new PlayerDAL().JoinGame(gameToJoin, playerToJoin, verificationCode); //If the response was successful, send the verification code to the player and update the lobby list if (response.IsSuccessful()) { string message = "Your CamTag verification code is: " + verificationCode; string subject = "CamTag Verification Code"; response.Data.ReceiveMessage(message, subject); //Call the hub interface to invoke client methods to update the clients that another player has joined HubInterface hubInterface = new HubInterface(_hubContext); hubInterface.UpdatePlayerJoinedGame(response.Data); //Compress the player data before sending back over the network response.Data.Compress(true, true, true); } return(response); } //Catch any error associated with invalid model data catch (InvalidModelException e) { return(new Response <Player>(e.Msg, e.Code)); } //Catch any unhandled / unexpected server errrors catch { return(StatusCode(500)); } }
public ActionResult <Response> VerifyPlayer([FromForm] string verificationCode, [FromHeader] int playerID) { try { Player playerToVerify = new Player(playerID); //Confirm the verification code is valid and return an error response if the verification code is invalid int code = Player.ValidateVerificationCode(verificationCode); if (code == -1) { return(new Response("The verification code is invalid. Must be an INT between 10000 and 99999.", ErrorCodes.DATA_INVALID)); } //Call the data access layer to confirm the verification code is correct. Response <Player> response = new PlayerDAL().ValidateVerificationCode(code, playerToVerify); //If the player was successfully verified, updated all the clients about a joined player. if (response.IsSuccessful()) { HubInterface hubInterface = new HubInterface(_hubContext); hubInterface.UpdatePlayerJoinedGame(response.Data); } return(response); } //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)); } }
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)); } }
/// <summary> /// The code which will run in a new thread after the FinishVotingTime has passed. /// The method checks to see if all players have voted on the image and if not, will /// update the image to be successful and make all votes a success. Then send out notifications /// to the affected players. /// </summary> /// <param name="uploadedPhoto">The photo which was uploaded and being checked if voting has been completed.</param> /// <param name="timeToWait">The number of milliseconds to wait for the thread to start.</param> /// <param name="hubInterface">The Hub interface which will be used to send notifications / updates</param> private static void Run_CheckPhotoVotingCompleted(Photo uploadedPhoto, int timeToWait, HubInterface hubContext) { //Wait for the specified time Thread.Sleep(timeToWait); //Get the updated photo record from the database Photo photo = new PhotoDAL().GetPhotoByID(uploadedPhoto.PhotoID); if (photo == null) { return; } //Confirm the game the photo is apart of is not completed, if completed leave the method if (photo.Game.IsCompleted()) { return; } //Check to see if the voting has been completed for the photo. //If the voting has been completed exit the method if (photo.IsVotingComplete) { return; } //Otherwise, the game is not completed and the photo has not been successfully voted on by all players //Call the Data Access Layer to update the photo record to now be completed. PhotoDAL photoDAL = new PhotoDAL(); Response <Photo> response = photoDAL.VotingTimeExpired(photo.PhotoID, photo.TakenByPlayer.IsBRPlayer()); //If the update was successful then send out the notifications to the affected players //Will send out in game notifications and text/email notifications if (response.IsSuccessful()) { //If the response's data is NULL that means the game is now completed. Send live updates to complete the game if (response.Data == null) { hubContext.UpdateGameCompleted(photo.Game, false); } //Otherwise, update the players that voting has been completed else { hubContext.UpdatePhotoVotingCompleted(response.Data); } } }