private GameField CreateGame() { //creates a new game and enters it into a queue to wait for an opponent MatrixViewModel mvm = new MatrixViewModel(7); GameField currGame = new GameField(); currGame.Id = new Guid(); currGame.PlayerOneId = User.FindFirst(ClaimTypes.NameIdentifier).Value; currGame.Field = JsonConvert.SerializeObject(mvm.Field); byte[] startingTurn = new byte[2]; startingTurn[0] = 0; startingTurn[1] = 2; currGame.CurrentPlayerAndTurn = startingTurn[rnd.Next(0, 2)]; _context.Games.Add(currGame); _context.SaveChanges(); UserInQueue newUser = new UserInQueue(); newUser.UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; newUser.GameId = currGame.Id.ToString(); ViewData["PlayerOneOrTwo"] = "Your are player one , indicated on the field by - P1"; HttpContext.Session.SetString("GameId", currGame.Id.ToString()); _context.Queue.Add(newUser); _context.SaveChanges(); return(currGame); }
public IActionResult Rematch() { //rematches the current players if both of them agree GameField game = _context.Games.Where(g => g.Id.ToString() == HttpContext.Session.GetString("GameId")).FirstOrDefault(); UserInQueue queuedUser = _context.Queue.Where(g => g.UserId == getOppenentId(game)).FirstOrDefault(); if (queuedUser == null) { GameField rematchedGame = CreateGame(); //rematchedGame.PreviousPlayerOneId = game.PlayerOneId; //rematchedGame.PreviousPlayerTwoId = game.PlayerTwoId; //rematchedGame.IsRematch = true; _context.Games.Where(g => g.Id == rematchedGame.Id).FirstOrDefault().IsRematch = true; _context.Games.Where(g => g.Id == rematchedGame.Id).FirstOrDefault().PreviousPlayerOneId = game.PlayerOneId; _context.Games.Where(g => g.Id == rematchedGame.Id).FirstOrDefault().PreviousPlayerTwoId = game.PlayerTwoId; _context.SaveChanges(); } else { string yourId = User.FindFirst(ClaimTypes.NameIdentifier).Value; GameField rematchedGame = _context.Games.Where(g => g.PreviousPlayerOneId == yourId && g.PlayerTwoId == null || g.PreviousPlayerTwoId == yourId && g.PlayerTwoId == null).FirstOrDefault(); rematchedGame.PlayerTwoId = yourId; UserInQueue rematchedPlayer = _context.Queue.Where(u => u.GameId == rematchedGame.Id.ToString()).FirstOrDefault(); ViewData["PlayerOneOrTwo"] = "Your are player two , indicated on the field by - P2"; HttpContext.Session.SetString("GameId", rematchedGame.Id.ToString()); _context.Queue.Remove(rematchedPlayer); _context.SaveChanges(); } return(View()); }