public static bool build(int userId, int colonyId, ref string xml) { SpacegameServer.Core.Core core = SpacegameServer.Core.Core.Instance; // lock colony Colony colony = core.colonies[colonyId]; if (colony == null) { return(false); } for (int i = 0; i < 10; i++) { bool colonyLocked = false; colonyLocked = colony.setLock(); if (colonyLocked) { //userId may have been changed by another thread, so check it again after locking if (colony.userId != userId) { colony.removeLock(); return(false); } Core.Instance.dataConnection.buildModules(userId, colonyId, ref xml); // get colony data (goods, later population (colony ships)) Core.Instance.dataConnection.getColonyStock(core, colony); //release the ressources and return true colony.removeLock(); BC.XMLGroups.Colonize bCCol = new BC.XMLGroups.Colonize(); bCCol.Colony = colony; BC.BusinessConnector.Serialize <BC.XMLGroups.Colonize>(bCCol, ref xml); return(true); } else { Thread.Sleep(Lockable.rnd.Next(0, 50)); } } return(true); }
public bool build(int _shipTemplateId, int _userId, int _colonyId, bool _fastBuild, ref string _xml) { // lock colony Colony colony = core.colonies[_colonyId]; if (colony == null) { return(false); } for (int i = 0; i < 10; i++) { bool colonyLocked = false; colonyLocked = colony.setLock(); if (colonyLocked) { //userId may have been changed by another thread, so check it again after locking if (colony.userId != _userId) { colony.removeLock(); return(false); } int newShipId = 0; Core.Instance.dataConnection.buildShip(_shipTemplateId, _userId, _colonyId, _fastBuild, ref _xml, ref newShipId); //get new ship data Core.Instance.dataConnection.getShips(core, newShipId, null); Core.Instance.dataConnection.getShipModules(core, core.ships[newShipId], false); // get colony data (goods, later population (colony ships)) Core.Instance.dataConnection.getColonyStock(core, colony); //release the ressources and return true colony.removeLock(); return(true); } else { Thread.Sleep(Lockable.rnd.Next(0, 50)); } } return(true); }
public void TestLockable() { SpacegameServer.Core.Core Instance = SpacegameServer.Core.Core.Instance; SpacegameServer.Core.User x = Mock.MockUserAndAdd(Instance); int shipId1 = (int)Instance.identities.shipLock.getNext(); int shipId2 = (int)Instance.identities.shipLock.getNext(); SpacegameServer.Core.Ship Ship1, Ship2; Ship1 = new SpacegameServer.Core.Ship(shipId1); Ship2 = new SpacegameServer.Core.Ship(shipId2); SpacegameServer.Core.Colony Colony1; Colony1 = new SpacegameServer.Core.Colony(1); //Object can only be locked once Assert.IsTrue(Ship1.setLock()); Assert.IsFalse(Ship1.setLock()); //LockAll of Ships can only be set once Assert.IsTrue(SpacegameServer.Core.Ship.setLockAll()); Assert.IsFalse(SpacegameServer.Core.Ship.setLockAll()); //ships can't be lcoked anymore, colonies can Assert.IsFalse(Ship1.setLock()); Assert.IsFalse(Ship2.setLock()); Assert.IsTrue(Colony1.setLock()); Ship1.removeLock(); Colony1.removeLock(); //after removing LockAll, all is lockable again SpacegameServer.Core.Ship.removeLockAll(); Assert.IsTrue(Ship1.setLock()); Assert.IsTrue(Ship2.setLock()); Assert.IsTrue(Colony1.setLock()); }
public static bool build(int userId, int colonyId, int tileNr, short buildingId, ref int newBuildingId) { SpacegameServer.Core.Core core = SpacegameServer.Core.Core.Instance; //check against research if (!Core.Instance.users.ContainsKey(userId)) { return(false); } var User = Core.Instance.users[userId]; if (!User.hasGameObjectEnabled(3, buildingId)) { return(false); } Colony colony = core.colonies[colonyId]; if (colony == null) { return(false); } Building template = core.Buildings[buildingId]; if (template == null) { return(false); } //check on building count in case of mines, hydrocarbon, rare chemicals if (buildingId == 2 || buildingId == 6) { if (ColonyBuildingActions.countBuildings(colony, buildingId) >= ColonyBuildingActions.allowedBuildings(colony, buildingId)) { return(false); } } //check tile position if (colony.colonyBuildings.Any(e => e.planetSurfaceId == tileNr)) { return(false); } // lock colony List <Lockable> elementsToLock = new List <Lockable>(1); elementsToLock.Add(colony); if (!LockingManager.lockAllOrSleep(elementsToLock)) { return(false); } try { //userId may have been changed by another thread, so check it again after locking if (colony.userId != userId) { colony.removeLock(); return(false); } //test ressources on colony var costOK = true; foreach (var cost in template.BuildingCosts) { if (!colony.goods.Any(e => e.goodsId == cost.goodsId)) { costOK = false; break; } if (colony.goods.Find(e => e.goodsId == cost.goodsId).amount < cost.amount) { costOK = false; break; } } if (!costOK) { colony.removeLock(); return(false); } if (template.oncePerColony) { if (colony.colonyBuildings.Any(e => e.buildingId == template.id)) { colony.removeLock(); return(false); } } //Special Ressourcen /* * if (template.id > 1029 && template.id < 1035) * { * var star = Core.Instance.stars[colony.starId]; * if (star.ressourceid != template.id - 1030) * { * colony.removeLock(); * return false; * } * } */ //Create Building var newId = (int)core.identities.colonyBuildingId.getNext(); newBuildingId = newId; var building = new ColonyBuilding(core, newId, colony, template, tileNr, userId); /* * var building = new ColonyBuilding(newId); * building.colonyId = colony.id; * building.colony = colony; * building.planetSurfaceId = tileNr; * building.userId = userId; * building.buildingId = buildingId; * building.isActive = true; * building.underConstruction = false; * building.remainingHitpoint = 100; * building.building = template; * colony.colonyBuildings.Add(building); * Core.Instance.colonyBuildings.Add(newId, building); */ foreach (var cost in template.BuildingCosts) { colony.addGood(cost.goodsId, -cost.amount); } /*check auf: * TODO - Feld frei? * TODO - Forschung */ //Todo: Scanrange, CommNode if (building.buildingId == 51) { building.colony.scanRange = Math.Max(building.colony.scanRange, (byte)7); core.dataConnection.saveSingleColony(building.colony); } if (building.buildingId == 64) { building.colony.scanRange = Math.Max(building.colony.scanRange, (byte)9); core.dataConnection.saveSingleColony(building.colony); } core.dataConnection.saveColonyBuildings(building); core.dataConnection.saveColonyGoods(colony); CommunicationNode.createCommNodeBuilding(building); //Core.Instance.dataConnection.buildBuilding(userId, colonyId, tileNr, buildingId, ref xml); // get colony data (goods, later population (colony ships)) //Core.Instance.dataConnection.getColonyStock(core, colony); //Core.Instance.dataConnection.getColonyBuildings(core, colony); } catch (Exception ex) { core.writeExceptionToLog(ex); } finally { LockingManager.unlockAll(elementsToLock); } return(true); }