// [HttpPost("create_movie_user_join")] public void CreateMovieUserJoin(int GameOutcome) { int?playerId = HttpContext.Session.GetInt32("id"); User retrievedUser = _context.Users.SingleOrDefault(p => p.UserId == playerId); var sessionMovieId = HttpContext.Session.GetInt32("SessionMovieId"); // check if a join of player and movie already exists var playerJoins = _context.MovieUserJoin.Where(p => p.MovieId == sessionMovieId).Where(t => t.UserId == playerId); var joinsList = playerJoins.ToList(); List <int> maxes = new List <int> (); // check the game outcome - 1 is a win, 0 is a loss int oneGameOutcome = GameOutcome; // the player won, go this way if (oneGameOutcome == 1) { // since the player won, check if any joins with player/movie combo already exists int?newPoints = HttpContext.Session.GetInt32("NewPoints"); if (joinsList.Count > 0) { // not first time the player attempted this movie Console.WriteLine("MPJ_A -- WIN _____ NOT FIRST JOIN"); foreach (var item in joinsList) { // get current attempt count and count of joins int currAttemptCount = item.AttemptCount; maxes.Add(currAttemptCount); } // get current max attempt of player/movie combo var newAttemptsMax = maxes.Max() + 1; // MPJ --> create new many-to-many of player and movie MovieUserJoin MPJ_A = new MovieUserJoin { UserId = (int)playerId, MovieId = (int)sessionMovieId, AttemptCount = newAttemptsMax, PointsReceived = (int)newPoints, WinFlag = true, }; _context.Add(MPJ_A); } // first time player attempted this movie else { Console.WriteLine("MPJ_B -- WIN _____ FIRST JOIN"); MovieUserJoin MPJ_B = new MovieUserJoin { UserId = (int)playerId, MovieId = (int)sessionMovieId, AttemptCount = 1, PointsReceived = (int)newPoints, WinFlag = true, }; _context.Add(MPJ_B); } } // the player lost if (oneGameOutcome == 0) { // this is not the first time the player has attempted to guess this movie if (joinsList.Count > 0) { Console.WriteLine("MPJ_C -- LOSS _____ NOT FIRST JOIN"); foreach (var item in joinsList) { // get current attempt count and count of joins int currAttemptCount = item.AttemptCount; maxes.Add(currAttemptCount); } var newAttemptsMax = maxes.Max() + 1; // MPJ --> create new many-to-many of player and movie MovieUserJoin MPJ_C = new MovieUserJoin { UserId = (int)playerId, MovieId = (int)sessionMovieId, AttemptCount = newAttemptsMax, PointsReceived = 0, WinFlag = false, }; _context.Add(MPJ_C); } else { Console.WriteLine("MPJ_D -- LOSS _____ FIRST JOIN"); // MPJ --> create new many-to-many of player and movie MovieUserJoin MPJ_D = new MovieUserJoin { UserId = (int)playerId, MovieId = (int)sessionMovieId, AttemptCount = 1, PointsReceived = 0, WinFlag = false, }; _context.Add(MPJ_D); } } _context.SaveChanges(); // return Json (playerJoins); }
public async Task AddAndSaveChangesAsync(object obj) { _h.StartMethod(); _context.Add(obj); await _context.SaveChangesAsync(); }