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 BattlePropertiesObject GetBattleProps(GameProperties props) { BattlePropertiesObject propertiesInDb = new BattlePropertiesObject { GameMode = props.GameMode, GameId = props.Id, Player1Name = props.Player1Name, Player2Name = props.Player2Name, FieldSize = JsonSerializer.Serialize(props.FieldSize), Player1FieldArray = JsonSerializer.Serialize(props.Player1FieldArray), Player2FieldArray = JsonSerializer.Serialize(props.Player2FieldArray), CurrentPlayer = props.CurrentPlayer, Round = props.Round, SelectableRowCount = props.SelectableRowCount, BattleHistory = JsonSerializer.Serialize(props.BattleHistory), MenuOptions = JsonSerializer.Serialize(props.MenuOptions) }; return(propertiesInDb); }
public static GameProperties GetGameProps(BattlePropertiesObject propsInDb, Flotilla flotilla1, Flotilla flotilla2) { GameProperties properties = new GameProperties { Id = propsInDb.GameId, GameMode = propsInDb.GameMode, Player1Name = propsInDb.Player1Name, Player2Name = propsInDb.Player2Name, Player1Flotilla = flotilla1, Player2Flotilla = flotilla2, FieldSize = JsonSerializer.Deserialize <int[]>(propsInDb.FieldSize), Player1FieldArray = JsonSerializer.Deserialize <string[]>(propsInDb.Player1FieldArray), Player2FieldArray = JsonSerializer.Deserialize <string[]>(propsInDb.Player2FieldArray), CurrentPlayer = propsInDb.CurrentPlayer, Round = propsInDb.Round, SelectableRowCount = propsInDb.SelectableRowCount, BattleHistory = JsonSerializer.Deserialize <List <string> >(propsInDb.BattleHistory), MenuOptions = JsonSerializer.Deserialize <List <string> >(propsInDb.MenuOptions) }; return(properties); }