private static List <string> GetListOfSavedGames() { using var context = new LudoContext(); var boardsList = context.Board.Select(x => x.BoardName).ToList(); // Return all saved games return(boardsList); }
public static void AddBoard(Board board) { using var context = new LudoContext(); context.Board.Add(board); context.SaveChanges(); board.PlayerIDLastMadeMove = board.LastMadeMove.Id; context.SaveChanges(); }
public static void RemoveBoard(string boardName) { using var context = new LudoContext(); var board = context.Board.FirstOrDefault(b => b.BoardName == boardName); if (board != null) { context.Board.Remove(board); } context.SaveChanges(); }
public static void LoadSavedGame() // Load a saved game by getting the boardId and all entites connected to that board. { Console.Clear(); var boardsList = GetListOfSavedGames(); // Return all saved boards if (boardsList.Count == 0) { Console.WriteLine("There are no saved games"); Console.ReadKey(); } else { var selectedOption = Menu.ShowMenu("Select board to load game from", boardsList); var selectedGameName = boardsList[selectedOption]; using var context = new LudoContext(); var board = context.Board // load selected game from DB .Include(b => b.Players) .ThenInclude(p => p.Tokens) .First(b => b.BoardName == selectedGameName); board.LastMadeMove = board.Players.Single(p => p.Id == board.PlayerIDLastMadeMove); board.Players = NewOrder(board.Players, board.LastMadeMove); board.Squares = GameFactory.CreateSquares(); foreach (var player in board.Players) { TokenColor color = player.Tokens[0].Color; int[] route = GameFactory.GetRoute(color); // Set the route by finding the color of the loaded token(s). foreach (var token in player.Tokens) { token.Route = route; // Assign route to each token if (token.IsActive) { var SquareId = route[token.Steps]; Square square = board.Squares.Single(s => s.Id == SquareId); square.Occupants.Add(token); } } } Console.Clear(); Console.WriteLine("Summary, press any key to start game.\n"); foreach (var p in board.Players) // Summary { Console.WriteLine($"{p.Name} - Team {p.Tokens[0].Color}"); Console.WriteLine(); foreach (var t in p.Tokens) { if (t.IsActive) { Console.WriteLine($"Token {t.Color}, steps: {t.Steps}, square: {t.Route[t.Steps]}"); } } int tokensInPlay = p.Tokens.Count(x => x.IsActive == true); int tokensOnBase = p.Tokens.Count(x => x.IsActive == false); int tokensAtFinish = 4 - tokensOnBase - tokensInPlay; Console.WriteLine($"Numb" + $"er of tokens on base: {tokensOnBase}"); Console.WriteLine($"Number of tokens at the finish: {tokensAtFinish}"); Console.WriteLine("\n"); } Console.ReadKey(); Action.StartGame(board); } }