/// <summary>
        /// We should probably remove deleteExistingDB before release. Implemented for DBFiller.
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="deleteExistingDB"></param>
        public DatabaseManager(SimpleLogger logger = null, bool deleteExistingDB = false)
        {
            _galacticObjectContext = new GalacticObjectContext(deleteExistingDB);

            _registerDBSets();

            Logger = logger;
        }
        public void Test()
        {
            CommandCenterModel cm = new CommandCenterModel();

            cm.OwnerID     = 5;
            cm.Id          = 231214;
            cm.OwnerTeamID = 66;

            _galacticObjectContext.CommandCenters.Add(cm);
            _galacticObjectContext = new GalacticObjectContext();
        }
        public IEnumerable <IEFSerializable> Add(IEnumerable <IEFSerializable> objects)
        {
            using (GalacticObjectContext c = new GalacticObjectContext())
            {
                c.Configuration.AutoDetectChangesEnabled = false;

                foreach (var o in objects)
                {
                    Add(o, c, false);
                }
                c.SaveChanges();
            }


            return(objects);
        }
        /// <summary>
        /// Returns null if area is not found. Will probably be depreciated soon, because of excessive querying.
        /// Note that this method does not load nested references if they are stored as Ids (e.g. planets in systems, which are stored only by IDs: PSystem.PlanetIDs)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Area GetArea(int id, IPlayerLocator pl, IAreaLocator al, IShipLocator sl, IMessageManager mm, IGalaxyRegistrationManager rm, ITeamLocator tl)
        {
            //Apparently, include is retardedly slow, so we have to use a manual workaround.
            //DBArea a = _galacticObjectContext.Areas.Include(e=>e.Warpholes).Include(se=>se.Structures).First(ee=>ee.Id == id);
            Area retArea;

            using (GalacticObjectContext context = new GalacticObjectContext())
            {
                context.SetLogger(Logger);

                if (context.Systems.Any(e => e.Id == id))
                {
                    retArea = GetSystem(context, id, pl, al, sl, mm);
                }
                else if (context.Moons.Any(e => e.Id == id))
                {
                    retArea = GetMoon(context, id, pl, al, sl, mm);
                }
                else if (context.Planets.Any(e => e.Id == id))
                {
                    retArea = GetPlanet(context, id, pl, al, sl, mm, rm, tl);
                }
                else if (context.Ports.Any(e => e.Id == id))
                {
                    retArea = GetPort(context, id, pl, al, sl, mm);
                }
                else if (context.Colonies.Any(e => e.Id == id))
                {
                    retArea = GetColony(context, id, pl, al, sl, mm);
                }
                else
                {
                    throw new Exception("Error: Area not found. Has deserialization been imlemented for this type?");
                }


                List <IHasGalaxyID> l = new List <IHasGalaxyID>();
                retArea.GetRegisterableNestedObjects(l);
                foreach (IHasGalaxyID obj in l)
                {
                    rm.RegisterObject(obj);
                }
            }
            return(retArea);
        }
        public Planet GetPlanet(GalacticObjectContext context, int id, IPlayerLocator pl, IAreaLocator al, IShipLocator sl, IMessageManager mm, IGalaxyRegistrationManager rm, ITeamLocator tl)
        {
            var a = context.Planets.Include(e0 => e0.Warpholes).Include(e1 => e1.Structures).Include(e2 => e2.Colony).First(e3 => e3.Id == id);

            if (a != null)
            {
                Planet retArea = new Planet(a, pl, al, sl, mm, tl);
                if (a.Colony != null)
                {
                    Colony col = new Colony(a.Colony, pl, al, sl, mm);
                    rm.RegisterObject(col);

                    foreach (var s in retArea.GetStructures())
                    {
                        col.RegisterStructure(s.Value);
                    }
                }
            }


            return(a == null ? null : new Planet(a, pl, al, sl, mm, tl));
        }
 public void ResetContext()
 {
     _galacticObjectContext = new GalacticObjectContext();
     _galacticObjectContext.SetLogger(Logger);
 }
        IEFSerializable Add(IEFSerializable obj, GalacticObjectContext c, bool saveChanges = true)
        {
            IDBObject saveme = obj.GetDBObject();
            //_getDBSet(saveme).Add(saveme);
            Type t = saveme.GetType();

            //This could use a convenient workaround...

            if (t == typeof(DBPSystem))
            {
                c.Systems.AddOrUpdate((DBPSystem)saveme);
            }
            else if (t == typeof(DBMoon))
            {
                c.Moons.AddOrUpdate((DBMoon)saveme);
            }
            else if (t == typeof(DBPort))
            {
                c.Ports.AddOrUpdate((DBPort)saveme);
            }
            else if (t == typeof(DBPlanet))
            {
                c.Planets.AddOrUpdate((DBPlanet)saveme);
            }
            else if (t == typeof(DBColony))
            {
                c.Colonies.AddOrUpdate((DBColony)saveme);
            }
            else if (t == typeof(DBShip) || t.IsSubclassOf(typeof(DBShip)))
            {
                c.Ships.AddOrUpdate((DBShip)saveme);
            }
            else if (t == typeof(DBPlayer) || t.IsSubclassOf(typeof(DBPlayer)))
            {
                c.Players.AddOrUpdate((DBPlayer)saveme);
            }
            else if (t == typeof(DBAccount) || t.IsSubclassOf(typeof(DBAccount)))
            {
                c.Accounts.AddOrUpdate((DBAccount)saveme);
            }
            else if (t == typeof(PlanetLayout) || t.IsSubclassOf(typeof(PlanetLayout)))
            {
                c.Layouts.AddOrUpdate((PlanetLayout)saveme);
            }
            else if (t == typeof(ShipStats) || t.IsSubclassOf(typeof(ShipStats)))
            {
                c.ShipStats.AddOrUpdate((ShipStats)saveme);
            }
            else if (t == typeof(DBTeam))
            {
                c.Teams.AddOrUpdate((DBTeam)saveme);
            }
            else if (t == typeof(StructureModel) || t.IsSubclassOf(typeof(StructureModel)))
            {
                c.Structures.Add((StructureModel)saveme);
            }
            else
            {
                throw new Exception("Error: Entity set not available for objects of type " + t.ToString());
            }



            if (saveChanges)
            {
                c.SaveChanges();
            }

            return(obj);
        }
        public Colony GetColony(GalacticObjectContext context, int id, IPlayerLocator pl, IAreaLocator al, IShipLocator sl, IMessageManager mm)
        {
            var a = context.Colonies.First(e3 => e3.Id == id);

            return(a == null ? null : new Colony(a, pl, al, sl, mm));
        }
        public Port GetPort(GalacticObjectContext context, int id, IPlayerLocator pl, IAreaLocator al, IShipLocator sl, IMessageManager mm)
        {
            var a = context.Ports.Find(id);

            return(a == null ? null : new Port(a, pl, al, sl, mm));
        }
        public PSystem GetSystem(GalacticObjectContext context, int id, IPlayerLocator pl, IAreaLocator al, IShipLocator sl, IMessageManager mm)
        {
            var a = context.Systems.Include(e0 => e0.Warpholes).First(e2 => e2.Id == id);

            return(a == null ? null : new PSystem(a, pl, al, sl, mm));
        }
        public Moon GetMoon(GalacticObjectContext context, int id, IPlayerLocator pl, IAreaLocator al, IShipLocator sl, IMessageManager mm)
        {
            var a = context.Moons.Include(e0 => e0.Warpholes).Include(e1 => e1.Structures).First(e2 => e2.Id == id);

            return(a == null ? null : new Moon(a, pl, al, sl, mm));
        }