Beispiel #1
0
        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);
        }
Beispiel #2
0
        public static void removeInvitation(User sender, User receiver)
        {
            Core core = Core.Instance;

            //sender removes the invitation invites the receiver to his alliance:
            if (!(sender.allianceId != 0 && Core.Instance.alliances.ContainsKey(sender.allianceId)))
            {
                return;
            }
            Alliance alliance = Core.Instance.alliances[sender.allianceId];

            //teste ob user erlaubt ist invites zu geben
            if (!alliance.getMemberRight(sender).mayInvite)
            {
                return;
            }

            //teste das kein invite vorhanden ist
            if (!(Core.Instance.invitesPerAlliance.ContainsKey(alliance.id) &&
                  Core.Instance.invitesPerAlliance[alliance.id].Any(user => user == receiver.id)))
            {
                return;
            }


            List <Lockable> elementsToLock = new List <Lockable>(2);

            elementsToLock.Add(alliance);
            elementsToLock.Add(receiver);
            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return;
            }
            try
            {
                //teste das kein invite vorhanden ist
                if (!(Core.Instance.invitesPerAlliance.ContainsKey(alliance.id) &&
                      Core.Instance.invitesPerAlliance[alliance.id].Any(userId => userId == receiver.id)))
                {
                    LockingManager.unlockAll(elementsToLock);
                    return;
                }

                Core.Instance.invitesPerAlliance[alliance.id].RemoveAll(userId => userId == receiver.id);

                if (Core.Instance.invitesPerUser.ContainsKey(receiver.id))
                {
                    Core.Instance.invitesPerUser[receiver.id].RemoveAll(allianceId => allianceId == alliance.id);
                }

                Core.Instance.dataConnection.removeInvite(alliance.id, receiver.id);
            }
            catch (Exception ex)
            {
                core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }
        }
Beispiel #3
0
        public static void inviteTo(User sender, User receiver)
        {
            Core core = Core.Instance;

            //sender invites the receiver to his alliance:
            if (!(sender.allianceId != 0 && Core.Instance.alliances.ContainsKey(sender.allianceId)))
            {
                return;
            }
            Alliance alliance = Core.Instance.alliances[sender.allianceId];

            //teste ob sich beide user kennen:
            //if (!core.userRelations.hasContact(sender, receiver)) return;

            //teste ob user erlaubt ist invites zu geben
            if (!alliance.getMemberRight(sender).mayInvite)
            {
                return;
            }

            //teste ob schon ein invite vorhanden ist
            if (core.invitesPerAlliance.ContainsKey(alliance.id) &&
                core.invitesPerAlliance[alliance.id].Any(userId => userId == receiver.id))
            {
                return;
            }

            List <Lockable> elementsToLock = new List <Lockable>(2);

            elementsToLock.Add(alliance);
            elementsToLock.Add(receiver);
            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return;
            }
            try
            {
                //teste ob schon ein invite vorhanden ist
                if (core.invitesPerAlliance.ContainsKey(alliance.id) &&
                    core.invitesPerAlliance[alliance.id].Any(userId => userId == receiver.id))
                {
                    LockingManager.unlockAll(elementsToLock);
                    return;
                }

                //add into both dicts
                if (core.invitesPerAlliance.ContainsKey(alliance.id))
                {
                    core.invitesPerAlliance[alliance.id].Add(receiver.id);
                }
                else
                {
                    core.invitesPerAlliance.TryAdd(alliance.id, new List <int>());
                    core.invitesPerAlliance[alliance.id].Add(receiver.id);
                }

                if (core.invitesPerUser.ContainsKey(receiver.id))
                {
                    core.invitesPerUser[receiver.id].Add(alliance.id);
                }
                else
                {
                    core.invitesPerUser.TryAdd(receiver.id, new List <int>());
                    core.invitesPerUser[receiver.id].Add(alliance.id);
                }

                core.dataConnection.insertInvite(alliance.id, receiver.id);
            }
            catch (Exception ex)
            {
                core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
 public void saveAllianceMembers(SpacegameServer.Core.Alliance alliance)
 {
 }
        //only alliance2alliance relations
        public List <FullDiplomaticRelationProposals> getDiplomatics(Alliance alliance, bool includeProposals = true)
        {
            List <FullDiplomaticRelationProposals> relations = new List <FullDiplomaticRelationProposals>();

            //id user is part of an alliance, only alliance diplomatics are used.
            //but the user-to-user-Contacts are also needed to give the current state between users
            //users are fetched first, but then overwritten by alliance to user data if needed:
            //if (!DiplomaticEntityState.ContainsKey(alliance)) return relations;

            //DiplomaticEntityState[alliance] contains state (if that is changed) and offers from alliance towards other alliances
            if (DiplomaticEntityState.ContainsKey(alliance))
            {
                foreach (var targetUser in DiplomaticEntityState[alliance])
                {
                    //only fetch alliances
                    if (targetUser.Key.diplomaticType != 2)
                    {
                        continue;
                    }

                    //overallRelation is the Minimum of user relations towards target and target relation towards user
                    var myRelation      = targetUser.Value;
                    var overallRelation = Relation.Neutral;
                    var hisRelation     = Relation.Neutral;
                    if (DiplomaticEntityState.ContainsKey(targetUser.Key) &&
                        DiplomaticEntityState[targetUser.Key].ContainsKey(alliance))
                    {
                        overallRelation = Min(myRelation, DiplomaticEntityState[targetUser.Key][alliance]);
                        hisRelation     = DiplomaticEntityState[targetUser.Key][alliance];
                    }

                    relations.Add(new FullDiplomaticRelationProposals(
                                      alliance,
                                      targetUser.Key,
                                      (int)myRelation,
                                      (int)hisRelation,
                                      (int)overallRelation
                                      )
                                  );
                }
            }
            //fetch offers from other alliances towards this alliance
            if (includeProposals)
            {
                foreach (var AllianceRelations in DiplomaticEntityState)
                {
                    if (AllianceRelations.Key.diplomaticType != 2)
                    {
                        continue;
                    }
                    if (AllianceRelations.Key == alliance)
                    {
                        continue;
                    }

                    //if other alliance is contained in own relations, continue, since it is no offer
                    if (DiplomaticEntityState.ContainsKey(alliance) && DiplomaticEntityState[alliance].ContainsKey(AllianceRelations.Key))
                    {
                        continue;
                    }

                    foreach (var OtherRelation in AllianceRelations.Value)
                    {
                        //only offers towards alliance are interesting
                        if (OtherRelation.Key.diplomaticType != 2)
                        {
                            continue;
                        }

                        //offers towards other alliances are not interesting
                        if (OtherRelation.Key != alliance)
                        {
                            continue;
                        }

                        relations.Add(new FullDiplomaticRelationProposals(
                                          alliance,
                                          AllianceRelations.Key,
                                          (int)Relation.Neutral,
                                          (int)OtherRelation.Value,
                                          (int)Relation.Neutral
                                          )
                                      );
                    }
                }
            }

            return(relations);
        }
        //sender asks information about target alliance
        //only non neutral relations
        public List <SpacegameServer.BC.XMLGroups.AllianceRelation> getAllianceDiplomatics(Alliance target)
        {
            List <SpacegameServer.BC.XMLGroups.AllianceRelation> relations = new List <SpacegameServer.BC.XMLGroups.AllianceRelation>();

            foreach (var targetRelation in this.getDiplomatics(target, false))
            {
                if (targetRelation.relation != 2)
                {
                    relations.Add(new SpacegameServer.BC.XMLGroups.AllianceRelation(
                                      targetRelation.target.id,
                                      targetRelation.relation));
                }
            }
            return(relations);
        }