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 })); }
public ActionResult GameDetails(int id) { //validating the id to make sure its not null if (id == 0) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewGameViewModel model = new ViewGameViewModel(); //getting current logged in user information in case they want to join game string email = User.Identity.GetUserName(); Contact contact = _contactService.GetContactByEmail(email); Game game = _gameService.GetGameById(id); if (contact == null) { // Check if is venue owner. If not, they did not initalize their profile VenueOwner venueOwner = _venueOwnerService.GetVenueOwnerByEmail(email); model.IsCreatorOfGame = false; if (venueOwner.VenueId == game.VenueId) { model.IsVenueOwner = true; } } else if (_gameService.IsCreatorOfGame(contact.ContactId, game)) { model.IsCreatorOfGame = true; model.IsVenueOwner = false; } else { // Check if logged in user is already a part of this game var allPickUpGames = _gameService.GetPickUpGamesByContactId(contact.ContactId); if (allPickUpGames != null) { var existingPickUpGame = allPickUpGames.FirstOrDefault(x => x.GameId == id); if (existingPickUpGame == null) { model.IsAlreadyJoined = false; } else { model.IsAlreadyJoined = true; } } else { model.IsAlreadyJoined = false; } } model.EndDate = game.EndTime.ToString(); model.GameId = game.GameId; model.Status = Enum.GetName(typeof(GameStatusEnum), game.GameStatusId); model.Sport = _gameService.GetSportNameById(game.SportId); model.StartDate = game.StartTime.ToString(); model.Venue = _venueService.GetVenueNameById(game.VenueId); if (game.ContactId != null) { model.ContactId = game.ContactId; model.ContactName = _contactService.GetContactById(game.ContactId).Username; } //returning model to the view return(View(model)); }