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 bool joinCheck(User user, int allianceId) { Core core = Core.Instance; if (!core.alliances.ContainsKey(allianceId)) { return(false); } Alliance alliance = core.alliances[allianceId]; List <Lockable> elementsToLock = new List <Lockable>(2); elementsToLock.Add(user); elementsToLock.Add(alliance); 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); } //check that user is invited: if (!core.invitesPerAlliance.ContainsKey(allianceId) || !core.invitesPerAlliance[allianceId].Any(e => e == user.id)) { LockingManager.unlockAll(elementsToLock); return(false); } alliance.join(user); //save alliance: core.dataConnection.saveAlliances(alliance); //members: Core.Instance.dataConnection.saveAllianceMembers(alliance); //remove invitation: Core.Instance.invitesPerAlliance[alliance.id].RemoveAll(userId => userId == user.id); if (Core.Instance.invitesPerUser.ContainsKey(user.id)) { Core.Instance.invitesPerUser[user.id].RemoveAll(allyId => allyId == alliance.id); } Core.Instance.dataConnection.removeInvite(alliance.id, user.id); //add to alliance communication if (Core.Instance.commNodes.Any(e => e.Value.connectionType == 4 && e.Value.connectionId == alliance.id)) { Core.Instance.commNodes.First(e => e.Value.connectionType == 4 && e.Value.connectionId == alliance.id).Value.addUserAndSave(user); } } catch (Exception ex) { core.writeExceptionToLog(ex); } finally { LockingManager.unlockAll(elementsToLock); } return(true); }