static string SaveGame(BattleShips game) { var saveName = AskUserForSaveGameName(1, 30); BattleShipsSaveJson battleShips = new BattleShipsSaveJson() { Height = game.Height, Width = game.Width, Player1 = game.Player1, Player1Turn = game.Player1Turn, Player2 = game.Player2, SaveName = saveName, GameType = game.GameType }; SaveTool.SaveGameToFile(battleShips); using var ctx = new AppDbContext(); var battleShipsSave = game.CreateBattleShipsSave(saveName); if (ctx.BattleShipsSaves.Any(e => e.SaveName == saveName)) { var save = ctx.BattleShipsSaves.FirstOrDefault(e => e.SaveName == saveName); game.UpdateBattleShipsSave(save !); ctx.Update(save); } else { ctx.Add(battleShipsSave); } ctx.SaveChanges(); return(""); }
public static void SaveGameToFile(BattleShipsSaveJson battleShipSaveJson) { using StreamWriter file = new StreamWriter(TextFile, true); string jsonString = JsonSerializer.Serialize(battleShipSaveJson); file.WriteLine(jsonString); }
public BattleShips(BattleShipsSaveJson battleShipsSave) { Player1 = battleShipsSave.Player1 !; Player2 = battleShipsSave.Player2 !; Width = battleShipsSave.Width; Height = battleShipsSave.Height; GameType = battleShipsSave.GameType; Player1Turn = battleShipsSave.Player1Turn; }
private static void DeserializeJsonFromJsonList(ICollection <string> jsonStrings, ICollection <BattleShipsSaveJson> battleShipsSaves) { if (jsonStrings.Count <= 0 || jsonStrings.First().Equals("")) { return; } foreach (var jsonString in jsonStrings) { BattleShipsSaveJson battleShipsSaveJson = JsonSerializer.Deserialize <BattleShipsSaveJson>(jsonString); battleShipsSaves.Add(battleShipsSaveJson); } }