public void DeletePickUpGame(PickUpGame pickUpGame) { PickUpGame existing = _context.PickUpGames.Find(pickUpGame.PickUpGameId); if (existing == null) { throw new ArgumentNullException($"Could not find existing pick up game by ID {pickUpGame.PickUpGameId}"); } _context.PickUpGames.Remove(existing); _context.SaveChanges(); }
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 })); }
/* * Method to remove a player from a game that they previously joined */ public void RemovePlayerFromGame(PickUpGame pickUpGame) { _pickUpGameRepository.DeletePickUpGame(pickUpGame); }
/* * Method to add a player to an existing game */ public PickUpGame AddPlayerToGame(PickUpGame pickUpGame) { return(_pickUpGameRepository.AddPickUpGame(pickUpGame)); }
public PickUpGame AddPickUpGame(PickUpGame pickUpGame) { _context.PickUpGames.Add(pickUpGame); _context.SaveChanges(); return(pickUpGame); }