예제 #1
0
 public ShipFactory(IGalaxyRegistrationManager rm, WarpManager wm, ILocalIDManager galaxyIdManager, LocatorService ls, IDatabaseManager dbm)
 {
     _galaxyRegistrationManager = rm;
     _warpManager     = wm;
     _galaxyIdManager = galaxyIdManager;
     _locatorService  = ls;
     _databaseManager = dbm;
 }
예제 #2
0
 public LocatorService(IGalaxyRegistrationManager grm,
                       IPlayerLocator pl,
                       IAreaLocator al,
                       IShipLocator sl,
                       IAccountLocator acl,
                       ITeamLocator tl,
                       ITeamManager tm,
                       IMessageManager mm,
                       IObjectLocator <IStructure> structureManager,
                       ISlaveIDProvider slaveIDProvider)
 {
     PlayerLocator       = pl;
     ShipLocator         = sl;
     AccountLocator      = acl;
     TeamLocator         = tl;
     TeamManager         = tm;
     AreaLocator         = al;
     MessageManager      = mm;
     RegistrationManager = grm;
     StructureManager    = structureManager;
     SlaveIDProvider     = slaveIDProvider;
 }
예제 #3
0
        public static IShip DeserializeShip(ShipModel s, LocatorService ls, IGalaxyRegistrationManager rm)
        {
            IShip retShip;

            if (s.PilotType == PilotTypes.Player)
            {
                retShip = new PlayerShip((PlayerShipModel)s, ls);
                rm.RegisterObject(retShip);
            }
            else if (s.PilotType == PilotTypes.NPC)
            {
                retShip = new NPCShip((NPCShipModel)s, ls);
                rm.RegisterObject(retShip);
            }
            else
            {
                throw new Exception("Deserialization not implemented for PilotTypes " + s.PilotType + ".");
            }


            return(retShip);
        }
예제 #4
0
        // Need to find a better place for this...
        public static IStructure InstantiateStructure(IStructureModel sm, IPlayerLocator pl, IGalaxyRegistrationManager gm)
        {
            IStructure s;

            switch (sm.StructureType)
            {
            case (StructureTypes.LaserTurret):
                s = new Turret((TurretModel)sm, pl);
                break;

            case (StructureTypes.Biodome):
                s = new Biodome((BiodomeModel)sm);
                break;

            case (StructureTypes.PowerPlant):
                s = new PowerPlant((PowerPlantModel)sm);
                break;

            case (StructureTypes.Silo):
                s = new Silo((SiloModel)sm);
                break;

            case (StructureTypes.CommandCenter):
                return(new CommandCenter((CommandCenterModel)sm));

            case (StructureTypes.Factory):
                s = new Factory((FactoryModel)sm);
                break;

            case StructureTypes.Refinery:
                s = new Refinery((RefineryModel)sm);
                break;

            case StructureTypes.Mine:
                s = new MineStructure((MineModel)sm);
                break;

            case StructureTypes.DefensiveMine:
                s = new DefensiveMine((DefensiveMineModel)sm, pl);
                break;

            case StructureTypes.ConstructionBuilding:
                s = new ConstructionBuilding((ConstructionBuildingModel)sm);
                break;

            default:
                throw new Exception("CreateStructure not implemented for structure type " + sm.StructureType.ToString());
            }
            gm.RegisterObject(s);


            return(s);
        }
예제 #5
0
        /// <summary>
        /// Loads the structures associated with the given object
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static async Task <List <IStructure> > LoadStructures(IHasStructures loadMine, IDatabaseManager dbm, IGalaxyRegistrationManager rm, IPlayerLocator pl)
        {
            var loadedStructureModels = await dbm.GetStructuresAsync(loadMine.GetStructureIDs());

            List <IStructure> loadedStructures = new List <IStructure>();

            foreach (var ls in loadedStructureModels)
            {
                IStructure s = StructureHelper.InstantiateStructure(ls, pl, rm);
                loadMine.AddStructure(s);
                loadedStructures.Add(s);
            }

            return(loadedStructures);
        }
예제 #6
0
 static async Task InstantiateStructures(IEnumerable <IArea> loadedAreas, Dictionary <int, IStructureModel> loadedStructureModels, IGalaxyRegistrationManager rm, IPlayerLocator pl)
 {
     foreach (IArea a in loadedAreas)
     {
         var structureIDs = a.GetStructureIDs();
         foreach (int id in structureIDs)
         {
             IStructure s = StructureHelper.InstantiateStructure(loadedStructureModels[id], pl, rm);
             a.AddStructure(s);
         }
     }
 }
예제 #7
0
        /// <summary>
        /// Instantiates and registers all objects associated with a PSystem (planets, players, ships, etc)
        /// </summary>
        public static async Task <PSystem> DeserializePSystemAsync(PSystemModel system, RedisServer redisServer, LocatorService ls, IGalaxyRegistrationManager rm, IDatabaseManager dbm)
        {
            PSystem           retSys            = new PSystem(system, ls);
            List <IShip>      deserializedShips = new List <IShip>();
            List <IStructure> loadedStructures  = new List <IStructure>();
            List <Player>     loadedPlayers     = new List <Player>();

            //For now we just run the whole lambda synchronously in its own thread. Marking it async makes this method return complete before the task is complete, need to investigate...
            await Task.Factory.StartNew(() =>
            {
                HashSet <int> colonyIDsToLoad = new HashSet <int>();
                HashSet <int> shipIDsToLoad   = new HashSet <int>();
                HashSet <int> areaIDsToLoad   = new HashSet <int>();
                List <int> structureIDsToLoad = new List <int>();
                HashSet <int> layoutIDsToLoad = new HashSet <int>();

                foreach (int id in system.ShipIDs)
                {
                    if (shipIDsToLoad.Contains(id))
                    {
                        throw new CorruptStateException("Multiple areas contain the same shipID.");
                    }
                    shipIDsToLoad.Add(id);
                }
                foreach (var id in retSys.MoonIDs)
                {
                    areaIDsToLoad.Add(id);
                }

                foreach (var id in retSys.PlanetIDs)
                {
                    areaIDsToLoad.Add(id);
                }

                foreach (var id in retSys.PortIDs)
                {
                    areaIDsToLoad.Add(id);
                }

                IEnumerable <AreaModel> loadedAreaModels = dbm.GetAreasAsync(areaIDsToLoad).Result;

                foreach (var am in loadedAreaModels)
                {
                    switch (am.AreaType)
                    {
                    case AreaTypes.Planet:
                        layoutIDsToLoad.Add(((PlanetModel)am).LayoutId);
                        break;
                    }
                }


                IEnumerable <PlanetLayout> loadedLayouts = dbm.GetLayoutsAsync(layoutIDsToLoad).Result.Select(s => (PlanetLayout)s);
                Dictionary <int, PlanetLayout> layouts   = new Dictionary <int, PlanetLayout>();
                foreach (var l in loadedLayouts)
                {
                    layouts.Add(l.Id, l);
                }


                var loadedAreas = new List <IArea>();
                // Instantiate all areas
                foreach (AreaModel am in loadedAreaModels)
                {
                    IArea loadedArea = null;
                    structureIDsToLoad.AddRange(am.StructureIDs);

                    // Planets
                    if (am.AreaType == AreaTypes.Planet)
                    {
                        loadedArea = new Planet((PlanetModel)am, layouts[((PlanetModel)am).LayoutId], ls);
                        var p      = loadedArea as Planet;
                        //rm.RegisterObject(p);
                        if (p.ColonyID != null)
                        {
                            colonyIDsToLoad.Add((int)p.ColonyID);
                        }
                    }

                    // Ports
                    else if (am.AreaType == AreaTypes.Port)
                    {
                        loadedArea = new Port((PortModel)am, ls);
                    }
                    else
                    {
                        throw new Exception("Error: Loaded area not handled in DeserializePSystem()");
                    }



                    foreach (var id in am.ShipIDs)
                    {
                        if (shipIDsToLoad.Contains(id))
                        {
                            throw new CorruptStateException("Multiple areas contain the same shipID.");
                        }
                        shipIDsToLoad.Add(id);
                    }
                    loadedAreas.Add(loadedArea);
                    rm.RegisterObject(loadedArea);
                }

                // Colonies
                IEnumerable <AreaModel> LoadedColonies = dbm.GetAreasAsync(colonyIDsToLoad).Result;
                List <Colony> deserializedColonies     = new List <Colony>();
                foreach (AreaModel am in LoadedColonies)
                {
                    if (am.AreaType == AreaTypes.Colony)
                    {
                        Colony c         = new Colony((ColonyModel)am, ls);
                        c.DisableUpdates = true;
                        rm.RegisterObject(c);
                        deserializedColonies.Add(c);
                    }
                    else
                    {
                        throw new Exception("AreaID query resulted in an AreaModel which was not a ColonyModel in DeserializePSystem()");
                    }
                    foreach (var id in am.ShipIDs)
                    {
                        if (shipIDsToLoad.Contains(id))
                        {
                            throw new CorruptStateException("Multiple areas contain the same shipID.");
                        }
                        shipIDsToLoad.Add(id);
                    }
                }

                // Structures
                loadedStructures.AddRange(LoadStructures(retSys, dbm, rm, ls.PlayerLocator).Result);

                foreach (IArea loadedArea in loadedAreas)
                {
                    if (loadedArea is IHasStructures)
                    {
                        loadedStructures.AddRange(LoadStructures((IHasStructures)loadedArea, dbm, rm, ls.PlayerLocator).Result);
                    }
                }


                // Ships
                IEnumerable <ShipModel> loadedShipModels = dbm.GetShipsAsync(shipIDsToLoad).Result;
                HashSet <int> playerIDsToLoad            = new HashSet <int>();
                foreach (var s in loadedShipModels)
                {
                    var loadedShip = DeserializeShip(s, ls, rm);
                    deserializedShips.Add(loadedShip);
                    if (loadedShip.PlayerID != null)
                    {
                        playerIDsToLoad.Add((int)s.PlayerID);
                    }
                }


                // Players
                IEnumerable <PlayerModel> loadedPlayerModels = dbm.GetPlayersAsync(playerIDsToLoad).Result;

                HashSet <int> accountIDsToLoad = new HashSet <int>();
                foreach (var p in loadedPlayerModels)
                {
                    if (p.PlayerType == PlayerTypes.Human)
                    {
                        HumanPlayer hp = new HumanPlayer(p, ls);
                        rm.RegisterObject(hp);
                        loadedPlayers.Add(hp);
                    }
                    else
                    {
                        NPCPlayer np      = new NPCPlayer(p, ls);
                        np.MessageService = new RedisOutgoingMessageService(redisServer, np);
                        rm.RegisterObject(np);
                        loadedPlayers.Add(np);
                    }
                    if (p.AccountID != null)
                    {
                        accountIDsToLoad.Add((int)p.AccountID);
                    }
                }


                //Accounts
                IEnumerable <AccountModel> loadedAccounts = dbm.GetAccountsAsync(accountIDsToLoad).Result;

                foreach (var a in loadedAccounts)
                {
                    rm.RegisterObject(new Account(a));
                }

                foreach (var c in deserializedColonies)
                {
                    c.DisableUpdates = false;
                }
            }
                                        );

            rm.RegisterObject(retSys);

            foreach (Player p in loadedPlayers)
            {
                if (p.PlayerType == PlayerTypes.NPC)
                {
                    p.GetArea().MovePlayerHere(p, false);
                }
            }

            foreach (var s in deserializedShips)
            {
                IArea a = s.GetArea();

                if (a == null)//Corrupt state, the ship's CurrentAreaId doesn't match the list is for an area which hasn't been loaded yet. Abort the ship read.
                {
                    //I'm not sure that there's a way to gracefully handle this without a major rewrite and a bunch of overhead, so it'll be an exception for now.
                    throw new CorruptStateException("Ship's CurrentAreaId does not match the area from which it was loaded. Expect exceptions.");
                }
                else
                {
                    a.AddShip(s, true);
                }
            }


            return(retSys);
        }
예제 #8
0
        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));
        }
예제 #9
0
        /// <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);
        }