private static int SaveToDb(Save save) { using AppDbContext ctx = new AppDbContext(); GameProperties props = save.Properties; Flotilla flotilla1 = props.Player1Flotilla; BattleFlotillasObject flotillaInDb1 = GetBattleFlotilla(flotilla1); Flotilla flotilla2 = save.Properties.Player2Flotilla; BattleFlotillasObject flotillaInDb2 = GetBattleFlotilla(flotilla2); ctx.Flotillas.Add(flotillaInDb1); ctx.Flotillas.Add(flotillaInDb2); ctx.SaveChanges(); foreach (Ship ship in flotilla1.Ships) { BattleShipsObject shipInDb = GetBattleShip(flotillaInDb1, ship); ctx.Add(shipInDb); } foreach (Ship ship in flotilla2.Ships) { BattleShipsObject shipInDb = GetBattleShip(flotillaInDb2, ship); ctx.Add(shipInDb); } ctx.SaveChanges(); BattlePropertiesObject propertiesInDb = GetBattleProps(props); ctx.Add(propertiesInDb); ctx.SaveChanges(); var propertiesFlotillasInDb1 = new PropertiesFlotillasObject { BattleId = propertiesInDb.BattlePropertiesObjectId, FlotillaId = flotillaInDb1.BattleFlotillasObjectId }; ctx.Add(propertiesFlotillasInDb1); var propertiesFlotillasInDb2 = new PropertiesFlotillasObject { BattleId = propertiesInDb.BattlePropertiesObjectId, FlotillaId = flotillaInDb2.BattleFlotillasObjectId }; ctx.Add(propertiesFlotillasInDb2); ctx.SaveChanges(); var saveInDb = new SaveObject { SaveName = save.SaveName, BattlePropertiesObjectId = propertiesInDb.BattlePropertiesObjectId }; ctx.Add(saveInDb); ctx.SaveChanges(); return(saveInDb.SaveObjectId); }
private static BattleShipsObject GetBattleShip(BattleFlotillasObject flotillaInDb, Ship ship) { BattleShipsObject shipInDb = new BattleShipsObject { FlotillaId = flotillaInDb.BattleFlotillasObjectId, Name = ship.Name, Size = ship.Size, Health = ship.Health, ShipCellsArray = JsonSerializer.Serialize(ship.ShipCellsArray), }; return(shipInDb); }
private void UpdateShips(Flotilla flotilla, IReadOnlyList <BattleShipsObject> ships) { int index = 0; while (index < flotilla.ShipCount) { BattleShipsObject shipFromDb = ships[index]; Ship shipFromGame = flotilla.Ships[index++]; shipFromDb.Name = shipFromGame.Name; shipFromDb.Size = shipFromGame.Size; shipFromDb.Health = shipFromGame.Health; shipFromDb.ShipCellsArray = JsonSerializer.Serialize(shipFromGame.ShipCellsArray); } }