private static void AddGameToDb
     (AppDbContext context, GameBoardEngine gameBoardEngine, GameBoard gameBoard, string saveGameName)
 {
     while (true)
     {
         if (context.GameBoards.Any(e => e.SaveGameName == saveGameName))
         {
             Console.WriteLine($"{saveGameName} name already exists. Please pick a new name.");
             Console.WriteLine(">");
             saveGameName = Console.ReadLine();
         }
         else
         {
             gameBoard.SaveGameName = saveGameName;
             break;
         }
     }
     gameBoardEngine.AddGameBoardToDb(gameBoard);
 }
Beispiel #2
0
        public IActionResult OnPost()
        {
            GameBoard !.MineCount = 10;

            var gameBoardEngine = new GameBoardEngine(_appDbContext);
            var gameBoard       = gameBoardEngine.CreateNewGameBoard(GameBoard.Height, GameBoard.Width, GameBoard.MineCount);

            gameBoard.SaveGameName = GameBoard.SaveGameName;

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (_appDbContext.GameBoards.Any(e => e.SaveGameName == GameBoard !.SaveGameName))
            {
                ModelState.AddModelError("GameBoard.SaveGameName", "A save game with this name already exists.");
                return(Page());
            }
            gameBoardEngine.AddGameBoardToDb(gameBoard);
            return(RedirectToPage("/PlayGame/Index", new { id = gameBoard.Id }));
        }