Exemplo n.º 1
0
        /*
         * If a game was once rejected, only the venue owner can re-accept it
         */
        public ActionResult AcceptGame(int id)
        {
            // Email creator game that game is being reinstated by owner
            var game  = _gameService.GetGameById(id);
            var owner = _venueOwnerService.GetVenueOwnerByVenueId(game.VenueId);

            if (game.ContactId != null)
            {
                var creator        = _contactService.GetContactById(game.ContactId);
                var venueOwnerLink = "<a href='"
                                     + Request.Url.Scheme + "://"
                                     + Request.Url.Authority
                                     + @Url.Action("Detail", "VenueOwner", new { id = owner.VenueOwnerId })
                                     + "'>Visit this page for owner's contact information</a>";

                var venueName = _venueService.GetVenueNameById(game.VenueId);
                var sport     = _gameService.GetSportNameById(game.SportId);

                var email = new MailMessage();
                email.To.Add(creator.Email);
                email.From    = new MailAddress(_gMailer.GetEmailAddress());
                email.Subject = $"Good news! The owner of {venueName} has reinstated your game.";
                email.Body    = $"<center>The owner of {venueName} has reinstated your previously rejected "
                                + $"{sport.ToLower()} game that you scheduled on {game.StartTime.ToShortDateString()}."
                                + $"<p>{venueOwnerLink}</p></center>";
                email.IsBodyHtml = true;

                _gMailer.Send(email);
            }

            _gameService.AcceptGame(id);

            return(RedirectToAction("Detail"));
        }
Exemplo n.º 2
0
        public ActionResult GameDetails(ViewGameViewModel model, string button)
        {
            //Find all the players that are currently signed up for the game
            List <PickUpGame> checkGames = _gameService.GetPickUpGameListByGameId(model.GameId);

            Game game = _gameService.GetGameById(model.GameId);

            //find the current logged-on user
            string  email           = User.Identity.GetUserName();
            Contact currContactUser = _contactService.GetContactByEmail(email);

            ViewGameViewModel returnModel = new ViewGameViewModel()
            {
                EndDate   = game.EndTime.ToString(),
                GameId    = game.GameId,
                Status    = Enum.GetName(typeof(GameStatusEnum), game.GameStatusId),
                Sport     = _gameService.GetSportNameById(game.SportId),
                StartDate = game.StartTime.ToString(),
                Venue     = _venueService.GetVenueNameById(game.VenueId)
            };

            //make the email more readable
            var fileContents = System.IO.File.ReadAllText(Server.MapPath("~/Content/EmailFormat.html"));
            //add game link to the email
            var directUrl = Url.Action("GameDetails", "Game", new { id = game.GameId }, protocol: Request.Url.Scheme);

            fileContents = fileContents.Replace("{URL}", directUrl);
            fileContents = fileContents.Replace("{URLNAME}", "CHECK");
            fileContents = fileContents.Replace("{VENUE}", returnModel.Venue);
            fileContents = fileContents.Replace("{SPORT}", _gameService.GetSportNameById(game.SportId));
            fileContents = fileContents.Replace("{STARTTIME}", returnModel.StartDate);
            fileContents = fileContents.Replace("{ENDTIME}", returnModel.EndDate);

            //If the Reject button was pressed
            if (button.Equals("Reject Game"))
            {
                returnModel.Status = Enum.GetName(typeof(GameStatusEnum), 4);
                if (game.ContactId != null)
                {
                    returnModel.ContactId   = game.ContactId;
                    returnModel.ContactName = _contactService.GetContactById(game.ContactId).Username;
                }

                //replace the html contents to the game details
                fileContents = fileContents.Replace("{TITLE}", "Game Rejected");
                fileContents = fileContents.Replace("{INFO}", "Sorry the venue owner reject your game based on the confliction of the venue arrangement");
                var subject = "Game Status";
                SendMessage(game, (int)returnModel.ContactId, fileContents, subject);

                //save it
                _gameService.RejectGame(game.GameId);
            }
            //If the Reject button was pressed
            if (button.Equals("Accept Game"))
            {
                returnModel.Status = Enum.GetName(typeof(GameStatusEnum), 3);

                if (game.ContactId != null)
                {
                    returnModel.ContactId   = game.ContactId;
                    returnModel.ContactName = _contactService.GetContactById(game.ContactId).Username;
                }

                //replace the html contents to the game details
                fileContents = fileContents.Replace("{TITLE}", "Game Accepted");
                fileContents = fileContents.Replace("{INFO}", "The venue owner accept your game!");
                var subject = "Game Status";
                SendMessage(game, (int)returnModel.ContactId, fileContents, subject);

                //save it
                _gameService.AcceptGame(game.GameId);
            }

            //If the Join Game button was pressed
            if (button.Equals("Join Game"))
            {
                if (game.ContactId != null)
                {
                    returnModel.ContactId   = game.ContactId;
                    returnModel.ContactName = _contactService.GetContactById(game.ContactId).Username;
                }

                //if the game is cancelled, users are prevent to join this game
                if (game.GameStatusId == 2)
                {
                    //error message
                    ViewData.ModelState.AddModelError("SignedUp", "Sorry, this game is canceled, you can not join this game");
                    return(View(returnModel));
                }
                //check if the person is already signed up for the game
                if (!_gameService.IsNotSignedUpForGame(currContactUser.ContactId, checkGames))
                {
                    //error message
                    ViewData.ModelState.AddModelError("SignedUp", "You are already signed up for this game");

                    return(View(returnModel));
                }

                //add new person to the pickupgame table
                PickUpGame newPickUpGame = new PickUpGame()
                {
                    ContactId = currContactUser.ContactId,
                    GameId    = model.GameId,
                };
                //save it
                _gameService.AddPlayerToGame(newPickUpGame);

                //replace the html contents to the game details
                fileContents = fileContents.Replace("{TITLE}", "New Player!!!");
                fileContents = fileContents.Replace("{INFO}", currContactUser.Username + " has just joined the game." + "The current number of players on this game is: " + _gameService.GetPickUpGameListByGameId(game.GameId).Count());
                var subject = "Game Information Update";
                SendMessage(game, (int)returnModel.ContactId, fileContents, subject);
            }

            //If the Leave Game button was pressed
            if (button.Equals("Leave Game"))
            {
                //check if the person is already signed up for the game
                if (_gameService.IsNotSignedUpForGame(currContactUser.ContactId, checkGames))
                {
                    //error message
                    ViewData.ModelState.AddModelError("SignedUp", "You have not signed up for this game");

                    if (game.ContactId != null)
                    {
                        returnModel.ContactId   = game.ContactId;
                        returnModel.ContactName = _contactService.GetContactById(game.ContactId).Username;
                    }

                    return(View(returnModel));
                }

                Debug.Write(model);

                //Remove the Player from the Game
                var usersGames = _gameService.GetPickUpGamesByContactId(currContactUser.ContactId);
                var pickUpGame = usersGames.FirstOrDefault(x => x.GameId == model.GameId);
                _gameService.RemovePlayerFromGame(pickUpGame);

                //count the number of current users in the game to send notification to the creator
                int playerCount = 0;

                if (_gameService.GetPickUpGameListByGameId(game.GameId) == null)
                {
                    playerCount = 0;
                }
                else
                {
                    playerCount = _gameService.GetPickUpGameListByGameId(game.GameId).Count();
                }

                //replace the html contents to the game details
                fileContents = fileContents.Replace("{TITLE}", "Somebody Leaves");
                fileContents = fileContents.Replace("{INFO}", currContactUser.Username + " has just left the game. " + "The current number of players on this game is: " + playerCount);
                var subject = "Game Information Update";
                SendMessage(game, (int)game.ContactId, fileContents, subject);
            }

            return(RedirectToAction("GameDetails", new { id = model.GameId }));
        }