private static void GetReceivingShips(TradeOffer tradeOffer, List <Ship> ships, List <Ship> ShipsToReceiveGoods, int commNodeId) { var AmountOffered = 0; AmountOffered = tradeOffer.offered.Where(good => Core.Instance.Goods[good.goodsId].goodsType == 1).Sum(e => e.amount); //Fetch Goods that are modules, get the module data, sum up all needed ressoures for these modules. AmountOffered += tradeOffer.offered.Where(good => Core.Instance.Goods[good.goodsId].goodsType == 2).Select(good => new { ModuleDate = Core.Instance.Modules.First(Module => Module != null && Module.goodsId == good.goodsId), Amount = good.amount }).Sum(Modules => Modules.ModuleDate.CargoCost() * Modules.Amount); foreach (var ship in ships) { if (AmountOffered <= 0) { return; } CommunicationNode ShipNode = ship.GetCommNode(); if (ShipNode == null || ShipNode.id != commNodeId) { continue; } AmountOffered -= (ship.CalcStorage() - ship.AmountOnStock()); ShipsToReceiveGoods.Add(ship); } }
private static bool CheckStorageCapacity(TradeOffer tradeOffer, List <Ship> ships, int commNodeId) { List <shipStock> OnStockAtTradeStations = new List <shipStock>(); int RemainigFreeSpace = 0; foreach (var ship in ships) { CommunicationNode ShipNode = ship.GetCommNode(); if (ShipNode == null || ShipNode.id != commNodeId) { continue; } RemainigFreeSpace += ship.CalcStorage() - ship.AmountOnStock(); } var AmountOffered = 0; AmountOffered = tradeOffer.offered.Where(good => Core.Instance.Goods[good.goodsId].goodsType == 1).Sum(e => e.amount); //Fetch Goods that are modules, get the module data, sum up all needed ressoures for these modules. AmountOffered += tradeOffer.offered.Where(good => Core.Instance.Goods[good.goodsId].goodsType == 2).Select(good => new { ModuleDate = Core.Instance.Modules.First(Module => Module != null && Module.goodsId == good.goodsId), Amount = good.amount }).Sum(Modules => Modules.ModuleDate.CargoCost() * Modules.Amount); return(RemainigFreeSpace >= AmountOffered); }
public static bool createAlliance(User user, string name) { Core core = Core.Instance; List <Lockable> elementsToLock = new List <Lockable>(1); elementsToLock.Add(user); if (!LockingManager.lockAllOrSleep(elementsToLock)) { return(false); } try { //Prüfen ob der Nutzer noch Mitlgied einer ALlianz ist if (user.allianceId != 0 || user.group != null) { LockingManager.unlockAll(elementsToLock); return(false); } //create alliance int newId = (int)Core.Instance.identities.allianceId.getNext(); Alliance alliance = new Alliance(newId, name, "", "", user.id, 1000, null); core.alliances.TryAdd(newId, alliance); //add owner AllianceMember fullRights = AllianceMember.fullRights(user.id, newId); alliance.join(user, fullRights); //All relations from and towards the user are now concerning his new alliance: List <DiplomaticRelation> relations = new List <DiplomaticRelation>(); foreach (var relation in core.userRelations.getDiplomatics(user, 1)) { core.userRelations.setDiplomaticEntityState(alliance, relation.target, (Relation)relation.relationSenderProposal); relations.Add(new DiplomaticRelation(alliance.GetHashCode(), relation.target.GetHashCode(), relation.relationSenderProposal)); core.userRelations.setDiplomaticEntityState(relation.target, alliance, (Relation)relation.relationTargetProposal); relations.Add(new DiplomaticRelation(relation.target.GetHashCode(), alliance.GetHashCode(), relation.relationTargetProposal)); } //save alliance: core.dataConnection.saveAlliances(alliance); // new relations: Core.Instance.dataConnection.saveDiplomaticEntities(relations); //members: Core.Instance.dataConnection.saveAllianceMembers(alliance); CommunicationNode.CreateAllianceNode(alliance); } catch (Exception ex) { core.writeExceptionToLog(ex); } finally { LockingManager.unlockAll(elementsToLock); } return(true); }
public static void createCommNodeBuilding(ColonyBuilding building) { if (building.buildingId != 8) { return; } CommunicationNode node = new CommunicationNode((int)Core.Instance.identities.commNode.getNext()); node.userId = building.userId; node.name = building.colony.name; node.unformattedName = building.colony.name; node.positionX = building.colony.X; node.positionY = building.colony.Y; node.systemX = building.colony.SystemX; node.systemY = building.colony.SystemY; node.connectionType = 1; node.connectionId = building.id; node.activ = true; var instance = Core.Instance; instance.commNodes.TryAdd(node.id, node); //add to tree SpacegameServer.Core.NodeQuadTree.Field commNodeField2 = new SpacegameServer.Core.NodeQuadTree.Field(node.positionX, node.positionY); instance.nodeQuadTree.insertNode(commNodeField2, node.id); //add CommNodeUser CommNodeUser user = new CommNodeUser(building.userId, node.id); node.commNodeUsers.TryAdd(building.userId, user); instance.users[building.userId].commNodeRights.Add(node.id, user); instance.dataConnection.saveCommNode(node); building.node = node; }
public static void CreateAllianceNode(Alliance alliance) { CommunicationNode node = new CommunicationNode((int)Core.Instance.identities.commNode.getNext()); node.userId = alliance.allianceowner; node.name = alliance.NAME; node.unformattedName = alliance.NAME; node.connectionType = 4; node.connectionId = alliance.id; node.activ = true; var instance = Core.Instance; instance.commNodes.TryAdd(node.id, node); //add CommNodeUser CommNodeUser user = new CommNodeUser((int)alliance.allianceowner, node.id); node.commNodeUsers.TryAdd((int)alliance.allianceowner, user); instance.users[(int)alliance.allianceowner].commNodeRights.Add(node.id, user); instance.dataConnection.saveCommNode(node); }
private void leave(User user) { this.members.Remove(user); this.memberRights.RemoveAll(e => e.userId == user.id); user.group = null; user.allianceId = 0; var allianceCommNode = CommunicationNode.GetAllianceNode(this); if (allianceCommNode != null) { allianceCommNode.commNodeUsers.TryRemove(user.id); Core.Instance.dataConnection.DeleteCommNodeUsers(allianceCommNode, user); } //remove his secondary trait from other members this.RemoveSecondarySpecificationGain(user); //remove ALL secondary traits from this user List <UserResearch> AllNewUserResearchs = new List <UserResearch>(); foreach (var SpecGroup in Core.Instance.SpecializationGroups.Where(specGroup => specGroup.SpecializationResearches.Any(SpecResearch => SpecResearch.SecondaryResearchId != null))) { foreach (var SpecResearch in SpecGroup.SpecializationResearches) { if (!user.PlayerResearch.Any(e => e.isCompleted == 1 && e.researchId == SpecResearch.SecondaryResearchId)) { continue; } var PlayerResearch = user.PlayerResearch.First(e => e.researchId == SpecResearch.SecondaryResearchId && e.isCompleted == 1); PlayerResearch.isCompleted = 0; AllNewUserResearchs.Add(PlayerResearch); } } if (AllNewUserResearchs.Count > 0) { try { Core.Instance.dataConnection.SaveResearch(AllNewUserResearchs); } catch (Exception ex) { SpacegameServer.Core.Core.Instance.writeExceptionToLog(ex); } } foreach (System.Collections.DictionaryEntry HashEntry in GeometryIndex.allFields) { Field fieldToSet = (Field)HashEntry.Value; if (fieldToSet.Influence.Count == 0) { continue; } if (fieldToSet.Owner == user) { fieldToSet.Entity = user.GetEntity(); } } }
public void saveCommNode(SpacegameServer.Core.CommunicationNode node) { }
public void DeleteCommNodeUsers(SpacegameServer.Core.CommunicationNode node, SpacegameServer.Core.User user) { }
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); }