/// <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);
                }
            }
        }
Beispiel #2
0
        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));
            }
        }
Beispiel #3
0
        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));
            }
        }