private void removeGoods(Colony colony, ShipTemplate template, bool fastBuild) { Core core = Core.Instance; //remove shipHullCosts foreach (var shipHullsCost in Core.Instance.ShipHulls[template.hullid].ShipHullsCosts) { colony.addGood(shipHullsCost.goodsId, -shipHullsCost.amount); } //remove modules if (fastBuild) { foreach (var module in template.shipModules) { foreach (var modulesCost in Core.Instance.Modules[module.moduleId].ModulesCosts) { colony.addGood(modulesCost.goodsId, -modulesCost.amount); } } } else { foreach (var module in template.shipModules) { colony.addGood(core.Modules[module.moduleId].goodsId, -1); } } }
private static bool checkGoodsAvailability(Colony colony, SpacegameServer.Core.Transfer transfer) { Core core = Core.Instance; Colony dummy = new Colony(colony.id); //add foreach (var good in colony.goods) { dummy.goods.Add(good.clone()); } dummy.goods.RemoveAll(good => good.amount < 0); //remove TemplateModules foreach (var moduleLine in transfer.Goods) { Module toBuild = core.Modules.First(e => e != null && e.goodsId == moduleLine.Id); foreach (var costs in toBuild.ModulesCosts) { dummy.addGood(costs.goodsId, -costs.amount * moduleLine.Qty, false); } } //check if (dummy.goods.Any(good => good.amount < 0)) { return(false); } return(true); }
private bool checkGoodsAvailability(Colony colony, ShipTemplate template, bool fastBuild, ref ShipBuildErrorCode errorCode, ref int errorValue) { Colony dummy = new Colony(colony.id); //add foreach (var good in colony.goods) { dummy.goods.Add(good.clone()); } dummy.goods.RemoveAll(good => good.amount < 0); //remove TemplateModules foreach (var module in template.shipModules) { if (!fastBuild) { dummy.addGood(core.Modules[module.moduleId].goodsId, -1, false); } else { foreach (var costs in core.Modules[module.moduleId].ModulesCosts) { dummy.addGood(costs.goodsId, -costs.amount, false); } } } //and remove shipHullCosts foreach (var good in Core.Instance.ShipHulls[template.hullid].ShipHullsCosts) { dummy.addGood(good.goodsId, -good.amount, false); } //check if (dummy.goods.Any(good => good.amount < 0)) { errorCode = ShipBuildErrorCode.GoodsAvailability; return(false); } return(true); }
public static bool BuildModules(int userId, SpacegameServer.Core.Transfer transfer, ref string xml) { SpacegameServer.Core.Core core = SpacegameServer.Core.Core.Instance; if (!core.colonies.ContainsKey(transfer.Sender)) { return(false); } Colony colony = core.colonies[transfer.Sender]; if (colony.userId != userId) { return(false); } User user = Core.Instance.users[userId]; //check if modules are researched: foreach (var moduleLine in transfer.Goods) { if (moduleLine.Qty < 0) { return(false); } //check that the Module exists if (!Core.Instance.Modules.Any(e => e != null && e.goodsId == moduleLine.Id)) { return(false); } Module toBuild = Core.Instance.Modules.First(e => e != null && e.goodsId == moduleLine.Id); if (!user.hasModuleResearch(toBuild)) { return(false); } } if (!Modules.checkGoodsAvailability(colony, transfer)) { return(false); } List <Lockable> elementsToLock = new List <Lockable>(3); elementsToLock.Add(colony); if (!LockingManager.lockAllOrSleep(elementsToLock)) { return(false); } try { if (!Modules.checkGoodsAvailability(colony, transfer)) { return(false); } //remove TemplateModules foreach (var moduleLine in transfer.Goods) { Module toBuild = core.Modules.First(e => e != null && e.goodsId == moduleLine.Id); foreach (var costs in toBuild.ModulesCosts) { colony.addGood(costs.goodsId, -costs.amount * moduleLine.Qty, false); } colony.addGood(toBuild.goodsId, moduleLine.Qty, false); } Core.Instance.dataConnection.saveColonyGoods(colony); } catch (Exception ex) { SpacegameServer.Core.Core.Instance.writeExceptionToLog(ex); } finally { //release the ressources and return true LockingManager.unlockAll(elementsToLock); } return(true); }
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); }