public void AddModulesToShip(IShip ship, int numMods, CargoSynchronizer cargoSynchronizer, LocalIDManager galaxyIDManager) { for (int i = 0; i < numMods; i++) { Module m = null; int moduleID = galaxyIDManager.PopFreeID(); byte level = (byte)Rand.Random.Next(1, 4); int modnum = Rand.Random.Next(1, 11); { switch (modnum) { case 1: m = new EnergyRegenModule(moduleID, level); break; case 2: m = new ThrustModule(moduleID, level); break; case 3: m = new MaxShieldModule(moduleID, level); break; case 4: m = new ShieldRegenModule(moduleID, level); break; case 5: m = new MaxEnergyModule(moduleID, level); break; case 6: m = new DamageModule(moduleID, level); break; case 7: m = new DefenseModule(moduleID, level); break; case 8: m = new TurnRateModule(moduleID, level); break; case 9: m = new LateralThrustModule(moduleID, level); break; case 10: m = new TopSpeedModule(moduleID, level); break; } } TransactionAddStatefulCargo str = new TransactionAddStatefulCargo(ship, m, true); str.OnCompletion += ship.CargoAdded; cargoSynchronizer.RequestTransaction(str); } }
public EconomyManager(LocalIDManager transactionIDManager, IPlayerLocator pl, IAreaLocator al, CargoSynchronizer cs, ShipManager sm, IDatabaseManager databaseManager, ISlaveIDProvider slaveIDProvider) { _shipManager = sm; _playerLocator = pl; _areaLocator = al; _cargoSynchronizer = cs; _tradeIDManager = transactionIDManager; _databaseManager = databaseManager; _slaveIDProvider = slaveIDProvider; _shipShipTrades = new ConcurrentDictionary <int, ShipShipTrade>(); _shipIDToTradeID = new ConcurrentDictionary <int, int>(); _pendingTradeRequests = new ConcurrentDictionary <int, TradeRequest>(); _tradingPlayerIDs = new HashSet <int>(); }
public MinimalMockServer(GalacticProperties gp, IDatabaseManager databaseManager, IDbIdIoService dbIdIoService) { Logger.Initialize(); DatabaseManager = databaseManager; DbIdIoService = dbIdIoService; //Minimal initializations GalaxyIDManager = new LocalIDManager(null, IDTypes.GalaxyID); TeamIDManager = new LocalIDManager(null, IDTypes.TeamID); accountIDManager = new LocalIDManager(null, IDTypes.AccountID); InitializeIdData(gp); globalGalaxyIDManager = new GlobalGalaxyIDManager(dbIdIoService, gp); GenerateIDsForLocalIDManager(globalGalaxyIDManager, GalaxyIDManager, gp.IdProperties[IDTypes.GalaxyID].LastIDAdded); globalTeamIDManager = new GlobalTeamIDManager(dbIdIoService, gp); GenerateIDsForLocalIDManager(globalTeamIDManager, TeamIDManager, gp.IdProperties[IDTypes.TeamID].LastIDAdded); globalAccountIDManager = new GlobalAccountIDManager(dbIdIoService, gp); GenerateIDsForLocalIDManager(globalAccountIDManager, accountIDManager, gp.IdProperties[IDTypes.AccountID].LastIDAdded); _redisServer = new RedisServer(Logger.LogRedisError, Logger.LogRedisInfo, new RedisConfig().Address); TeamManager = new GlobalTeamManager(TeamIDManager, null, null, null, DatabaseManager); PlayerManager = new PlayerManager(DatabaseManager, null, _redisServer, GalaxyIDManager, null); GalaxyManager = new GalaxyManager(gp.SolID, TeamManager); shipManager = new ShipManager(null, GalaxyManager, null, null, DatabaseManager); CollisionManager = new CollisionManager(GalaxyManager, null, null, null); GalaxyGenerator = new DebugGalaxyGenerator(PlayerManager, GalaxyManager, shipManager); AccountManager = new AccountManager_MasterServer(accountIDManager, DatabaseManager, false); CargoSynchronizer = new Server.Managers.CargoSynchronizer(); StructureManager = new StructureManager(DatabaseManager, GalaxyManager, GalaxyIDManager, CargoSynchronizer); RegistrationManger = new GalaxyRegistrationManager(GalaxyManager, shipManager, CollisionManager, GalaxyIDManager, PlayerManager, null, CargoSynchronizer, StructureManager); ColonyFactory.Initialize(GalaxyIDManager, RegistrationManger); StructureFactory.Initialize(GalaxyIDManager, RegistrationManger); LocatorService = new LocatorService(RegistrationManger, PlayerManager, GalaxyManager, shipManager, AccountManager, TeamManager, TeamManager, null, StructureManager, null); WarpManager = new WarpManager(GalaxyManager, null, null, _redisServer, AccountManager, null); CargoSynchronizer.Start(10, 4); }
/// <summary> /// Returns created NPCPlayers /// </summary> /// <param name="galaxyManager"></param> /// <param name="IDManager"></param> /// <param name="rm"></param> /// <param name="pm"></param> /// <param name="npcShips"></param> /// <returns></returns> async Task <IEnumerable <NPCPlayer> > CreateNPCs(GalaxyManager galaxyManager, LocalIDManager IDManager, GalaxyRegistrationManager rm, PlayerManager pm, LocatorService locatorService, CargoSynchronizer cargoSynchronizer, GlobalTeamManager teamManager, LocalIDManager galaxyIDManager) { Random r = new Random(666); var players = new List <NPCPlayer>(); var systems = galaxyManager.Systems; int npcCount = 0; foreach (PSystem s in systems) { List <Player> team1 = new List <Player>(); List <Player> team2 = new List <Player>(); List <Player> team3 = new List <Player>(); for (int i = 0; i < _config.NumNPCsPerSystem; i++) { List <WeaponTypes> weapons = new List <WeaponTypes>(); ShipTypes shipType = ShipTypes.Barge; switch (npcCount % 3) { case 0: shipType = ShipTypes.Penguin; break; case 1: shipType = ShipTypes.Barge; break; case 2: shipType = ShipTypes.Reaper; break; } if (shipType == ShipTypes.Reaper) { weapons.Add(WeaponTypes.LaserWave); weapons.Add(WeaponTypes.PlasmaCannon); } else { weapons.Add(WeaponTypes.AltLaser); weapons.Add(WeaponTypes.LaserWave); } ShipCreationProperties props = new ShipCreationProperties(r.Next(-20, 20), r.Next(-20, 20), (int)galaxyManager.SolAreaID, PilotTypes.NPC, shipType, weapons); IShip tempShip = _dbFillerUtils.ShipFactory.CreateShip(props); tempShip.ShipStats.ShieldType = ShieldTypes.QuickRegen; NPCPlayer p = pm.CreateNPCPlayer(locatorService); pm.RegisterPlayer(p); players.Add(p); tempShip.SetPlayer(p); p.SetActiveShip(tempShip, MockServer.WarpManager); TransactionAddStatelessCargo tr = new TransactionAddStatelessCargo(tempShip, StatelessCargoTypes.AmbassadorMissile, 666666, true); cargoSynchronizer.RequestTransaction(tr); await tr.ResultTask; tr = new TransactionAddStatelessCargo(tempShip, StatelessCargoTypes.Biodome, 666666, true); cargoSynchronizer.RequestTransaction(tr); await tr.ResultTask; Helpers.DebugWarp(s, p, tempShip); ships.Add(tempShip); //Random team assignment switch (npcCount % 2) { case 0: team1.Add(p); break; case 1: team2.Add(p); break; case 2: team3.Add(p); break; } //Give the guy some turrets for (int j = 0; j < _config.NumTurretsPerNPC; j++) { var t = StructureFactory.CreateStructure(StructureTypes.LaserTurret, r.Next(-20, 20), r.Next(-20, 20), p, null, (int)p.CurrentAreaID, locatorService.PlayerLocator, true, dbm); p.GetArea().AddStructure(t); } AddModulesToShip(tempShip, 5, cargoSynchronizer, galaxyIDManager); npcCount++; } foreach (Planet pl in s.GetPlanets()) { npcCount = 0; for (int i = 0; i < _config.NumNpcsPerPlanet; i++) { ShipTypes shipType = ShipTypes.Barge; switch (npcCount % 3) { case 0: shipType = ShipTypes.Penguin; break; case 1: shipType = ShipTypes.Barge; break; case 2: shipType = ShipTypes.Reaper; break; } NPCPlayer p = pm.CreateNPCPlayer(locatorService); pm.RegisterPlayer(p); players.Add(p); IShip tempShip = new NPCShip(ShipStatManager.TypeToStats[shipType], locatorService); tempShip.ShipStats.ShieldType = ShieldTypes.QuickRegen; tempShip.Id = IDManager.PopFreeID(); rm.RegisterObject(tempShip); p.SetActiveShip(tempShip, MockServer.WarpManager); tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.MissileLauncher)); if (shipType == ShipTypes.Reaper) { tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.LaserWave)); tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.PlasmaCannon)); } else { tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.AltLaser)); tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.LaserWave)); } TransactionAddStatelessCargo tr = new TransactionAddStatelessCargo(tempShip, StatelessCargoTypes.AmbassadorMissile, 666666, true); cargoSynchronizer.RequestTransaction(tr); await tr.ResultTask; tr = new TransactionAddStatelessCargo(tempShip, StatelessCargoTypes.Biodome, 666666, true); cargoSynchronizer.RequestTransaction(tr); await tr.ResultTask; tempShip.PosX = r.Next(-20, 20); tempShip.PosY = r.Next(-20, 20); Helpers.DebugWarp(pl, p, tempShip); ships.Add(tempShip); //Random team assignment switch (npcCount % 2) { case 0: team1.Add(p); break; case 1: team2.Add(p); break; case 2: team3.Add(p); break; } AddModulesToShip(tempShip, 5, cargoSynchronizer, galaxyIDManager); npcCount++; } teamManager.DebugCreateNewTeam(team1); teamManager.DebugCreateNewTeam(team2); teamManager.DebugCreateNewTeam(team3); } } return(players); }
async Task AddCargoToPlayerShips(IEnumerable <IShip> ships, ILocalIDManager galaxyIDManager, GalaxyRegistrationManager registrationManager, CargoSynchronizer cargoSynchronizer) { //Making this into a grand test of transaction sequences, there's really no reason to put this all into one sequence CargoTransactionSequence cs = new CargoTransactionSequence(); foreach (var s in ships) { for (int i = 0; i < _config.CARGO_NumTurrets; i++)//Sending ship state over a network might be painful while this is here... { CargoLaserTurret c = new CargoLaserTurret(galaxyIDManager.PopFreeID(), 666, new LaserWeaponStats()); TransactionAddStatefulCargo t = new TransactionAddStatefulCargo(s, c, true); cs.Add(t); registrationManager.RegisterObject(c); } TransactionAddStatelessCargo tr = new TransactionAddStatelessCargo(s, StatelessCargoTypes.AmbassadorMissile, _config.CARGO_NumMissiles, true); cs.Add(tr); tr = new TransactionAddStatelessCargo(s, StatelessCargoTypes.HellHoundMissile, _config.CARGO_NumMissiles, true); cs.Add(tr); tr = new TransactionAddStatelessCargo(s, StatelessCargoTypes.MissileType1, _config.CARGO_NumMissiles, true); cs.Add(tr); tr = new TransactionAddStatelessCargo(s, StatelessCargoTypes.MissileType2, _config.CARGO_NumMissiles, true); cs.Add(tr); tr = new TransactionAddStatelessCargo(s, StatelessCargoTypes.MissileType3, _config.CARGO_NumMissiles, true); cs.Add(tr); tr = new TransactionAddStatelessCargo(s, StatelessCargoTypes.MissileType4, _config.CARGO_NumMissiles, true); cs.Add(tr); tr = new TransactionAddStatelessCargo(s, StatelessCargoTypes.Biodome, _config.CARGO_NumBiodomes, true); cs.Add(tr); } cargoSynchronizer.RequestAtomicTransactionSequence(cs); await cs.ResultTask; if (cs.ResultTask.Result != CargoResult.Success) { ConsoleManager.WriteLine(cs.ResultTask.Result.ToString()); } return; }
/// <summary> /// receivingPlayers get mines in cargo, owningPlayers get the mines added to the receivingSystems (deployed in space) /// </summary> /// <param name="receivingPlayers"></param> /// <param name="receivingSystems"></param> /// <param name="owningPlayers"></param> void AddMines(IEnumerable <Player> receivingPlayers, IEnumerable <PSystem> receivingSystems, IEnumerable <Player> owningPlayers, CargoSynchronizer cargoSynchronizer, GalaxyManager galaxyManager, LocalIDManager galaxyIDManager, StructureManager structureManager, LocatorService locatorService) { foreach (Player p in receivingPlayers) { for (int i = 0; i < _config.CARGO_NumMines; i++) { TransactionAddStatefulCargo t = new TransactionAddStatefulCargo(p.GetActiveShip(), new StatefulCargo(galaxyIDManager.PopFreeID(), StatefulCargoTypes.DefensiveMine), true); cargoSynchronizer.RequestTransaction(t); } } if (owningPlayers.Count() < 2) { return; } var itr = new CyclicalIterator <Player>(owningPlayers); itr.MoveNext(); foreach (var system in galaxyManager.Systems) { for (int i = 0; i < _config.NumMinesPerSystem; i++) { int ownerID = itr.Current.Id; var mine = new DefensiveMine(Rand.Random.Next(-system.AreaSize / 100, system.AreaSize / 100), Rand.Random.Next(-system.AreaSize / 100, system.AreaSize / 100), galaxyIDManager.PopFreeID(), ownerID, system.Id, locatorService.PlayerLocator); structureManager.RegisterObject(mine); system.AddStructure(mine); itr.MoveNext(); } } }
async Task FillPorts(GalaxyManager galaxyManager, LocalIDManager galaxyIDManager, CargoSynchronizer cargoSynchronizer) { var ports = galaxyManager.GetAllAreas().Where(a => a.AreaType == AreaTypes.Port); CargoTransaction lastTransaction = null; foreach (var p in ports) { var port = p as Port; foreach (var s in _config.PortConfig.StatefulCargoCounts) { StatefulCargo sc; for (int i = 0; i < s.Value; i++)//Yes, this loop is lazy, but it's 11:30PM... { //TODO: make a StatefulCargoFactory switch (s.Key) { case StatefulCargoTypes.Barge: { sc = new CargoShip(galaxyIDManager.PopFreeID(), 666, ShipStats[ShipTypes.Barge]); break; } case StatefulCargoTypes.BattleCruiser: { sc = new CargoShip(galaxyIDManager.PopFreeID(), 666, ShipStats[ShipTypes.BattleCruiser]); break; } case StatefulCargoTypes.Penguin: { sc = new CargoShip(galaxyIDManager.PopFreeID(), 666, ShipStats[ShipTypes.Penguin]); break; } case StatefulCargoTypes.Reaper: { sc = new CargoShip(galaxyIDManager.PopFreeID(), 666, ShipStats[ShipTypes.Reaper]); break; } case StatefulCargoTypes.LaserTurret: { sc = new CargoLaserTurret(galaxyIDManager.PopFreeID(), 666, new LaserWeaponStats()); break; } default: { sc = new StatefulCargo(galaxyIDManager.PopFreeID(), s.Key); break; } } CargoTransaction tr = new TransactionAddStatefulCargo(port, sc, true); cargoSynchronizer.RequestTransaction(tr); lastTransaction = tr; } } foreach (var s in _config.PortConfig.StatelessCargoCounts) { var tr = new TransactionAddStatelessCargo(port, s.Key, s.Value, true); cargoSynchronizer.RequestTransaction(tr); lastTransaction = tr; } foreach (var s in _config.PortConfig.ModuleCounts) { Module m = ModuleFactory.CreateModule(s.Key, galaxyIDManager.PopFreeID(), 1); var tr = new TransactionAddStatefulCargo(port, m, true); cargoSynchronizer.RequestTransaction(tr); lastTransaction = tr; } } if (lastTransaction != null) { await lastTransaction.ResultTask; } }
/// <summary> /// Initializes the entire server /// </summary> private void Initialize() { for (int i = 0; i < 20; i++) { msgAvgQue.Enqueue(0); } var rand = new Random(3); // Logging Enabled Logger.Initialize(); _synchronizers = new List <ISynchronizer>(); //Load configs ConnectionManagerConfig connectionManagerConfig = new ConnectionManagerConfig(new CoreNetworkConfig()); GalacticProperties galacticProperties = new GalacticProperties(); _consoleManager = new ConsoleManager(); _projectileManager = new ProjectileManager(); ConsoleManager.WriteLine("Starting database...", ConsoleMessageType.Startup); _databaseManager = new MongoDatabaseManager(); ShipStatManager.ReadShipsFromDBSList(_databaseManager.GetStatsFromDBAsync().Result); ConsoleManager.WriteLine("Ship types loaded.", ConsoleMessageType.Startup); RedisConfig rc = new RedisConfig(); _redisServer = new RedisServer(LogRedisError, LogRedisInfo, rc.Address); var slaveId = SlaveServerConfigService.GetFreeSlaveID(_redisServer).Result; _slaveServerConfigService = new SlaveServerConfigService(_redisServer, slaveId); _masterServerManager = new MasterServerManager(new MasterServerConfig(), new GalacticProperties(), _databaseManager, _databaseManager, _redisServer, _slaveServerConfigService); _cargoSynchronizer = new CargoSynchronizer(); _synchronizers.Add(_cargoSynchronizer); connectionManagerConfig.MyConfig.Port = ConnectionManager.GetFreePort(28002, 28010); _connectionManager = new ConnectionManager(); _connectionManager.Initialize(connectionManagerConfig); //Poll to listen to Lidgren until it is ready //LidgrenMessagePoller_Init initializationPoller = new LidgrenMessagePoller_Init(_connectionManager.Server, this); //initializationPoller.Poll(); _galaxyIDManager = new LocalIDManager(_masterServerManager, IDTypes.GalaxyID); _teamIDManager = new LocalIDManager(_masterServerManager, IDTypes.TeamID); _accountIDManager = new LocalIDManager(_masterServerManager, IDTypes.AccountID); _transactionIDManager = new LocalIDManager(_masterServerManager, IDTypes.TransactionID); _accountManager = new AccountManager(_accountIDManager, _databaseManager); _messageManager = new MessageManager(_connectionManager); _clientUpdateManager = new ClientUpdateManager(_playerManager); _playerManager = new PlayerManager(_databaseManager, _connectionManager, _redisServer, _galaxyIDManager, _clientUpdateManager); var chatCommands = new List <IChatCommand>() { new HelpCommand(), new ShoutCommand(_redisServer), new RadioCommand(), new TellCommand(_playerManager) }; var asyncChatCommands = new List <IAsyncChatCommand>() { new AdminWarpCommand(_databaseManager, _redisServer, new Random()) }; _teamManager = new GlobalTeamManager(_teamIDManager, _connectionManager, _redisServer, _playerManager, _databaseManager); _galaxyManager = new GalaxyManager(galacticProperties.SolID, _teamManager); _chatManager = new ChatManager(chatCommands, asyncChatCommands, _playerManager, _messageManager, _redisServer); _warpManager = new WarpManager(_galaxyManager, _messageManager, _chatManager, _redisServer, _accountManager, _databaseManager); _shipManager = new ShipManager(_messageManager, _galaxyManager, _warpManager, _connectionManager, _databaseManager); _structureManager = new StructureManager(_databaseManager, _galaxyManager, _galaxyIDManager, _cargoSynchronizer); loginManager = new LoginManager(_accountManager, _playerManager, _connectionManager, _redisServer); // Todo: Convert everything over to ServerNetworkMessage to propogate full request context. _simulatorManager = new SimulatorManager(new SimulatorConfig(), _redisServer, (sender, container) => ProcessMessage(sender, new ServerNetworkMessage(container, null))); StructureStatManager.Initialize(); ConsoleManager.WriteLine("Completed Initialization", ConsoleMessageType.Startup); _economyManager = new EconomyManager(_transactionIDManager, _playerManager, _galaxyManager, _cargoSynchronizer, _shipManager, _databaseManager, _masterServerManager); _killManager = new KillManager(_cargoSynchronizer, _playerManager, _galaxyManager, _messageManager, _connectionManager, _warpManager, _chatManager, _economyManager); _collisionManager = new CollisionManager(_galaxyManager, _messageManager, _killManager, _projectileManager); _registrationManager = new GalaxyRegistrationManager(_galaxyManager, _shipManager, _collisionManager, _galaxyIDManager, _playerManager, _accountManager, _cargoSynchronizer, _structureManager); _warpManager.SetRegistrationManager(_registrationManager);//Gross, I know. _locatorService = new LocatorService(_registrationManager, _playerManager, _galaxyManager, _shipManager, _accountManager, _teamManager, _teamManager, _messageManager, _structureManager, _masterServerManager); _msMessageHandler = new MasterServerMessageHandler((int)_masterServerManager.SlaveID, _redisServer, _connectionManager, _locatorService, _accountManager, _accountIDManager, _databaseManager, _galaxyManager, _galaxyIDManager, _playerManager, _shipManager, _registrationManager, _teamIDManager, _messageManager, _teamManager, _warpManager, _transactionIDManager, ProcessRoutedMessage); StructureFactory.Initialize(_galaxyIDManager, _registrationManager); ColonyFactory.Initialize(_galaxyIDManager, _registrationManager); dbSyncer = new DBSyncer(_databaseManager, _galaxyManager, _shipManager, _playerManager, _accountManager, _structureManager); #if DEBUG _typesToExcludeIgnore.Add(MessageTypes.PositionUpdateData); _typesToExcludeIgnore.Add(MessageTypes.ShipFireRequest); _typesToExcludeIgnore.Add(MessageTypes.ProjectileCollisionReport); _typesToExcludeIgnore.Add(MessageTypes.StructureFireRequest); _typesToExcludeIgnore.Add(MessageTypes.ObjectPickupRequest); #endif }