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 BattleFlotillasObject GetBattleFlotilla(Flotilla flotilla) { BattleFlotillasObject flotillaObject = new BattleFlotillasObject { Size = flotilla.Size, FlotillaHealth = flotilla.FlotillaHealth, ShipCount = flotilla.ShipCount }; return(flotillaObject); }
public static Flotilla GetFlotilla(BattleFlotillasObject flotillaInDb) { Flotilla flotilla = new Flotilla { Destroyed = false, FlotillaHealth = flotillaInDb.FlotillaHealth, ShipCount = flotillaInDb.ShipCount, Ships = new List <Ship>(), Size = flotillaInDb.Size }; return(flotilla); }
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); }