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 leaveCheck(User user, User userToRemove, 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(userToRemove);
            elementsToLock.Add(alliance);

            if (user != userToRemove)
            {
                //check that both are in the same alliance
                if (user.allianceId != userToRemove.allianceId)
                {
                    return(false);
                }
                //check that user may fire a member
                if (!alliance.memberRights.Any(e => e.userId == user.id && e.mayFire))
                {
                    return(false);
                }
            }

            //Test ob @userId Teil der ALlianz ist
            if (!alliance.members.Any(e => e.id == user.id))
            {
                return(false);
            }


            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(false);
            }
            try
            {
                if (user != userToRemove)
                {
                    //check that both are in the same alliance
                    if (user.allianceId != userToRemove.allianceId)
                    {
                        LockingManager.unlockAll(elementsToLock);
                        return(false);
                    }
                    //check that user may fire a member
                    if (!alliance.memberRights.Any(e => e.userId == user.id && e.mayFire))
                    {
                        LockingManager.unlockAll(elementsToLock);
                        return(false);
                    }
                }

                //Test ob @userId Teil der ALlianz ist
                if (!alliance.members.Any(e => e.id == user.id))
                {
                    LockingManager.unlockAll(elementsToLock);
                    return(false);
                }

                //remove User
                alliance.leave(userToRemove);



                //members:
                Core.Instance.dataConnection.saveAllianceMembers(alliance);

                //delete alliance if the need exists:
                if (alliance.members.Count == 0)
                {
                    int allianceHash = alliance.GetHashCode();
                    core.alliances.TryRemove(alliance.id);

                    //delete all relations:
                    core.userRelations.removeEntity(alliance);
                    core.dataConnection.deleteAllianceRelations(allianceHash);

                    //delete alliance in DB
                    core.dataConnection.deleteAlliance(allianceId);
                }
                else
                {
                    //check if alliance owner has left, if yes, choose a new one
                    if (alliance.allianceowner == userToRemove.id)
                    {
                        alliance.allianceowner = 0;

                        //first fullAdmin
                        if (alliance.memberRights.Any(e => e.fullAdmin))
                        {
                            alliance.allianceowner = alliance.memberRights.First(e => e.fullAdmin).userId;
                        }

                        //then first diplomaticAdmin
                        if (alliance.allianceowner == 0 && alliance.memberRights.Any(e => e.diplomaticAdmin))
                        {
                            alliance.allianceowner = alliance.memberRights.First(e => e.diplomaticAdmin).userId;
                        }

                        //then first diplomaticAdmin
                        if (alliance.allianceowner == 0 && alliance.memberRights.Any(e => e.mayMakeDiplomaticProposals))
                        {
                            alliance.allianceowner = alliance.memberRights.First(e => e.mayMakeDiplomaticProposals).userId;
                        }

                        //at last just anyone
                        if (alliance.allianceowner == 0)
                        {
                            alliance.allianceowner = alliance.memberRights.First().userId;
                        }

                        //update alliance in DB
                        core.dataConnection.saveAlliances(alliance);
                    }
                }
            }
            catch (Exception ex)
            {
                core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }
            return(true);
        }