Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public static bool delete(SpacegameServer.Core.ShipTemplate template)
        {
            Core            core           = Core.Instance;
            List <Lockable> elementsToLock = new List <Lockable>(1);

            elementsToLock.Add(template);
            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(false);
            }
            try
            {
                template.obsolete        = true;
                template.isConstructable = true;
                List <AsyncSaveable> elementsToSave = new List <AsyncSaveable>();
                elementsToSave.Add(template);
                core.dataConnection.saveAsync(elementsToSave);
            }
            catch (Exception ex)
            {
                core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }
            return(true);
        }
Ejemplo n.º 3
0
        public static TradeOffer createTrade(ClientTrade trade, int senderId)
        {
            Core core = Core.Instance;

            int        newId         = (int)core.identities.trades.getNext();
            TradeOffer NewTradeOffer = new TradeOffer(newId, trade);

            NewTradeOffer.FetchUser();


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

            elementsToLock.Add(NewTradeOffer.Sender.Owner);


            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(null);
            }
            try
            {
                if (!NewTradeOffer.Check(senderId))
                {
                    return(null);
                }

                try
                {
                    Core.Instance.dataConnection.SaveTradeOffer(NewTradeOffer);
                    core.tradeOffer.TryAdd(NewTradeOffer.tradeOfferId, NewTradeOffer);
                }
                catch (Exception ex)
                {
                    SpacegameServer.Core.Core.Instance.writeExceptionToLog(ex);
                }
            }
            catch (Exception ex)
            {
                core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }

            if (NewTradeOffer != null)
            {
                NewTradeOffer.TradingShip = Core.Instance.ships[NewTradeOffer.spaceObjectId];
                NewTradeOffer.TradingShip.TradeOffers.Add(NewTradeOffer);

                var offerSerialized = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(NewTradeOffer);
                Core.Instance.SendNewTrade(new { NewTradeOffer = offerSerialized });
            }

            return(NewTradeOffer);
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        public bool Abandon(int previousOwnerId, ref List <Ship> ships)
        {
            Core Core = Core.Instance;

            if (!Core.users.Any(e => e.Value.AiId == 3))
            {
                return(false);
            }
            User Separatist = Core.users.First(e => e.Value.AiId == 3).Value;

            User oldUser = Core.users[this.userId];

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

            elementsToLock.Add(this);
            List <Ship> shipsChangingOwnership = new List <Ship>();

            foreach (var ship in this.field.ships.Where(e => e.userid == oldUser.id && e.systemX == this.SystemX && e.systemY == this.SystemY))
            {
                shipsChangingOwnership.Add(ship);
                elementsToLock.Add(ship);
            }

            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(false);
            }
            try
            {
                if (this.userId != previousOwnerId)
                {
                    return(false);
                }
                this.userId = Separatist.id;
                foreach (var colBuilding in this.colonyBuildings)
                {
                    colBuilding.userId = this.userId;
                }

                foreach (var ship in shipsChangingOwnership)
                {
                    ship.userid = Separatist.id;
                    ships.Add(ship);
                }

                //save colony and buildings
                Core.dataConnection.saveColonyFull(this.planet, this, false);

                //save Ships
                Core.dataConnection.saveShips(shipsChangingOwnership);

                GalacticEvents.AddNewEvent(GalacticEventType.ColonyAbandoned, int1: previousOwnerId, int2: this.id, string1: this.name);
            }
            catch (Exception ex)
            {
                Core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }


            return(true);
        }
Ejemplo n.º 9
0
        public static bool createUpdate(string xml, int userId, ref SpacegameServer.Core.ShipTemplate template, ref SpacegameServer.Core.ShipTemplate oldTemplate)
        {
            /*
             * <?xml version="1.0" encoding="utf-8" ?>
             * <ShipTemplate>
             * <ShipTemplateId>1</ShipTemplateId>
             * <ShipTemplateHullId>1</ShipTemplateHullId>
             * <name>testName</name>
             * <gif>scout.png</gif>
             * <shipHullsImage>1</shipHullsImage>
             * <modulePositions>
             * <modulePosition>
             *  <posX>3</posX>
             *  <posY>3</posY>
             *  <moduleId>1</moduleId>
             * </modulePosition>
             * <modulePosition>
             *  <posX>2</posX>
             *  <posY>3</posY>
             *  <moduleId>2</moduleId>
             * </modulePosition>
             * </modulePositions>
             * </ShipTemplate>
             */

            Core            core           = Core.Instance;
            List <Lockable> elementsToLock = new List <Lockable>(2);


            SpacegameServer.Core.User user = core.users[userId];

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            string templateIdString = doc.DocumentElement.SelectSingleNode("/ShipTemplate/ShipTemplateId").InnerText;
            int    templateId;

            if (!Int32.TryParse(templateIdString, out templateId))
            {
                return(false);
            }

            string templateHullIdString = doc.DocumentElement.SelectSingleNode("/ShipTemplate/ShipTemplateHullId").InnerText;
            byte   templateHullId;

            if (!System.Byte.TryParse(templateHullIdString, out templateHullId))
            {
                return(false);
            }

            string shipHullsImageString = doc.DocumentElement.SelectSingleNode("/ShipTemplate/shipHullsImage").InnerText;
            int    shipHullsImage;

            if (!Int32.TryParse(shipHullsImageString, out shipHullsImage))
            {
                return(false);
            }


            if (templateId != -1)
            {
                template = core.shipTemplate[templateId];

                if (template.userId != userId)
                {
                    return(false);
                }
                if (template.amountbuilt > 0)
                {
                    oldTemplate = template;
                    int newTemplateId = (int)SpacegameServer.Core.Core.Instance.identities.templateLock.getNext();
                    template = new SpacegameServer.Core.ShipTemplate(newTemplateId);
                }
            }
            else
            {
                int newTemplateId = (int)SpacegameServer.Core.Core.Instance.identities.templateLock.getNext();
                template        = new SpacegameServer.Core.ShipTemplate(newTemplateId);
                template.userId = userId;
                template.gif    = "Dummy";
            }


            template.name           = doc.DocumentElement.SelectSingleNode("/ShipTemplate/name").InnerText;
            template.hullid         = templateHullId;
            template.shipHullsImage = shipHullsImage;

            elementsToLock.Add(template);
            if (oldTemplate != null)
            {
                elementsToLock.Add(oldTemplate);
            }
            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(false);
            }
            try
            {
                template.shipModules.Clear();

                System.Xml.XmlNodeList modules = doc.DocumentElement.SelectNodes("/ShipTemplate/modulePositions/modulePosition");
                foreach (System.Xml.XmlNode node in modules)
                {
                    string posXS     = node.SelectSingleNode("posX").InnerText;     //or loop through its children as well
                    string posYS     = node.SelectSingleNode("posY").InnerText;     //or loop through its children as well
                    string moduleIdS = node.SelectSingleNode("moduleId").InnerText; //or loop through its children as well

                    byte  posX, posY;
                    short moduleId;
                    if (!(Byte.TryParse(posXS, out posX) && Byte.TryParse(posYS, out posY) && Int16.TryParse(moduleIdS, out moduleId)))
                    {
                        LockingManager.unlockAll(elementsToLock);
                        return(false);
                    }

                    ShipTemplateModules newModule = new ShipTemplateModules();
                    newModule.posX           = posX;
                    newModule.posY           = posY;
                    newModule.moduleId       = moduleId;
                    newModule.shiptemplateid = template.id;
                    template.shipModules.Add(newModule);
                }

                StatisticsCalculator.calc(template, Core.Instance);

                template.isConstructable = template.energy >= 0 && template.crew >= 0;

                if (!core.shipTemplate.ContainsKey(template.id))
                {
                    core.shipTemplate[template.id] = template;
                }

                //save newTemplate and oldTemplate
                List <AsyncSaveable> elementsToSave = new List <AsyncSaveable>();
                elementsToSave.Add(template);
                if (oldTemplate != null)
                {
                    oldTemplate.obsolete = true;
                    elementsToSave.Add(oldTemplate);
                }
                core.dataConnection.saveAsync(elementsToSave);
            }
            catch (Exception ex)
            {
                core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }
            return(true);
        }