Exemple #1
0
        public static BaseResponse messageFromBytes(byte[] buffer)
        {
            VRage.ByteStream stream = new VRage.ByteStream(buffer, buffer.Length);
            TYPE             t      = (TYPE)stream.getUShort();

            stream.Seek(0, SeekOrigin.Begin);

            BaseResponse msg = null;

            switch (t)
            {
            case TYPE.NOTIFICATION:
                msg = new NotificationResponse();
                break;

            case TYPE.DIALOG:
                msg = new DialogResponse();
                break;

            case TYPE.SETTINGS:
                msg = new SettingsResponse();
                break;

            case TYPE.FLEET:
                msg = new FleetResponse();
                break;
            }

            if (msg != null)
            {
                msg.deserialize(stream);
            }
            return(msg);
        }
        public void TestGenericDeserialize()
        {
            NotificationResponse nr = new NotificationResponse();
            nr.DestType = BaseResponse.DEST_TYPE.FACTION;
            nr.Destination = new List<long>() { 2194 };
            nr.NotificationText = "Test String";
            nr.Time = 2000;
            nr.Font = Sandbox.Common.MyFontEnum.Red;

            byte[] buffer = nr.serialize();

            BaseResponse msg = BaseResponse.messageFromBytes(buffer);

            Assert.IsInstanceOfType(msg, typeof(NotificationResponse));
            Assert.AreEqual(nr.MsgType, msg.MsgType);
            Assert.AreEqual(nr.DestType, msg.DestType);
            CollectionAssert.AreEqual(nr.Destination, msg.Destination);
            Assert.AreEqual(nr.NotificationText, (msg as NotificationResponse).NotificationText);
            Assert.AreEqual(nr.Time, (msg as NotificationResponse).Time);
            Assert.AreEqual(nr.Font, (msg as NotificationResponse).Font);
        }
Exemple #3
0
        public void eventCleanupTimerEnd(GridEnforcer ge, DerelictTimer.COMPLETION c)
        {
            //log("start", "eventCleanupTimerEnd", Logger.severity.TRACE);
            if (ge == null)
                return;

            //log("grid exists, getting owner", "eventCleanupTimerEnd", Logger.severity.TRACE);
            GridOwner owner = ge.Owner;
            //log("grid exists, getting owner type", "eventCleanupTimerEnd", Logger.severity.TRACE);
            GridOwner.OWNER_TYPE owner_type = owner.OwnerType;
            //log("grid exists, getting faction", "eventCleanupTimerEnd", Logger.severity.TRACE);
            long gridFactionID = ge.Owner.FactionID;

            //log("determining destinations", "eventCleanupTimerEnd", Logger.severity.TRACE);
            BaseResponse.DEST_TYPE destType = BaseResponse.DEST_TYPE.NONE;
            List<long> Destinations = new List<long>();
            string message = "";

            if (owner_type == GridOwner.OWNER_TYPE.FACTION) {
                destType = BaseResponse.DEST_TYPE.FACTION;
                Destinations.Add(gridFactionID);
                message += "Your faction's ";
            }
            else if (owner_type == GridOwner.OWNER_TYPE.PLAYER) {
                destType = BaseResponse.DEST_TYPE.PLAYER;
                Destinations.Add(ge.Owner.PlayerID);
                message += "Your ";
            }
            else {
                List<long> nearbyPlayers = ge.Grid.getPlayerIDsWithinPlacementRadius();
                if (nearbyPlayers.Count > 0) {
                    destType = BaseResponse.DEST_TYPE.PLAYER;
                    Destinations = nearbyPlayers;
                    message += "Nearby ";
                }
                else {
                    return;
                }
            }

            log("building message", "eventCleanupTimerEnd", Logger.severity.TRACE);
            MyFontEnum font = MyFontEnum.Red;
            if (c == DerelictTimer.COMPLETION.CANCELLED) {
                message += "grid " + ge.Grid.DisplayName + " is now within limits.";
                font = MyFontEnum.Green;
            } else if (c == DerelictTimer.COMPLETION.ELAPSED) {
                message += "grid " + ge.Grid.DisplayName +
                    " had some of its offending blocks removed.";
                font = MyFontEnum.Red;
            }

            log("Sending message", "eventDerelictEnd");
            NotificationResponse noti = new NotificationResponse() {
                NotificationText = message,
                Time = Constants.NotificationMillis,
                Font = font,
                Destination = Destinations,
                DestType = destType
            };
            m_MailMan.send(noti);
        }
Exemple #4
0
        public void eventPlacementViolation(GridEnforcer ge, GridEnforcer.VIOLATION_TYPE v)
        {
            log("hit", "eventGridViolation");

            // Check for players within the vicinity of the grid, since there's no
            // built-in way to tell who just placed the block
            List<long> players = ge.Grid.getPlayerIDsWithinPlacementRadius();

            if (players.Count <= 0)
                return;

            string message = "";
            if (v == GridEnforcer.VIOLATION_TYPE.TOTAL_BLOCKS)
                message = "No more blocks allowed for this Class";
            else if (v == GridEnforcer.VIOLATION_TYPE.BLOCK_TYPE)
                message = "No more blocks of this type allowed for this Class";
            else if (v == GridEnforcer.VIOLATION_TYPE.TOO_MANY_CLASSIFIERS)
                message = "Only one Hull Classifier allowed";
            else if (v == GridEnforcer.VIOLATION_TYPE.SHOULD_BE_STATIC)
                message = "This classifier is only allowed on Stations";
            else if (v == GridEnforcer.VIOLATION_TYPE.TOO_MANY_OF_CLASS) {
                GridOwner.OWNER_TYPE owner_type = ge.Owner.OwnerType;
                if (owner_type == GridOwner.OWNER_TYPE.UNOWNED) {
                    message = "Take ownership of this grid or it will eventually be removed.";
                }
                else if (owner_type == GridOwner.OWNER_TYPE.PLAYER) {
                    message = "No more ships of this class allowed in this player's fleet. " +
                        "Try joining a faction.";
                } else if (owner_type == GridOwner.OWNER_TYPE.FACTION) {
                    message = "No more ships of this class allowed in this faction's fleet. ";
                }
            }

            log("Sending message", "eventPlacementViolation");
            NotificationResponse noti = new NotificationResponse() {
                NotificationText = message,
                Time = Constants.NotificationMillis,
                Font = MyFontEnum.Red,
                Destination = players,
                DestType = BaseResponse.DEST_TYPE.PLAYER
            };
            m_MailMan.send(noti);
        }
Exemple #5
0
        public void eventCleanupViolation(GridEnforcer ge, List<GridEnforcer.VIOLATION> violations)
        {
            log("Start", "eventCleanupViolation");
            if (ge == null)
                return;

            log("Determine destination", "eventCleanupViolation");
            GridOwner owner = ge.Owner;
            GridOwner.OWNER_TYPE owner_type = owner.OwnerType;
            long gridFactionID = ge.Owner.FactionID;

            BaseResponse.DEST_TYPE destType = BaseResponse.DEST_TYPE.NONE;
            List<long> Destinations = new List<long>();
            string message = "";

            if (owner_type == GridOwner.OWNER_TYPE.FACTION) {
                destType = BaseResponse.DEST_TYPE.FACTION;
                Destinations.Add(gridFactionID);
                message += "Your faction's ";
            } else if (owner_type == GridOwner.OWNER_TYPE.PLAYER) {
                destType = BaseResponse.DEST_TYPE.PLAYER;
                Destinations.Add(ge.Owner.PlayerID);
                message += "Your ";
            } else {
                List<long> nearbyPlayers = ge.Grid.getPlayerIDsWithinPlacementRadius();
                if (nearbyPlayers.Count > 0) {
                    destType = BaseResponse.DEST_TYPE.PLAYER;
                    Destinations = nearbyPlayers;
                    message += "Nearby unowned ";
                } else {
                    return;
                }
            }

            message += "grid '" + ge.Grid.DisplayName + "' ";

            log("Build violations message", "eventCleanupViolation");
            if (violations != null) {
                message += "is violating: ";

                foreach (GridEnforcer.VIOLATION violation in violations)
                    message += violation.Name + ": " + violation.Count + "/" +
                        violation.Limit + " ";

                message += " and ";
            }

            log("Build time message", "eventCleanupViolation");
            int secondsUntilCleanup = ge.TimeUntilCleanup;

            message += "will have some blocks removed in " +
                Utility.prettySeconds(secondsUntilCleanup);

            // send
            log("Sending message", "eventDerelictStart");
            NotificationResponse noti = new NotificationResponse() {
                NotificationText = message,
                Time = Constants.NotificationMillis,
                Font = MyFontEnum.Red,
                Destination = Destinations,
                DestType = destType
            };
            m_MailMan.send(noti);
        }
Exemple #6
0
        public void eventCleanupTimerStart(GridEnforcer ge, int secondsRemaining)
        {
            if (ge == null)
                return;

            GridOwner owner = ge.Owner;
            GridOwner.OWNER_TYPE owner_type = owner.OwnerType;
            long gridFactionID = ge.Owner.FactionID;

            BaseResponse.DEST_TYPE destType = BaseResponse.DEST_TYPE.NONE;
            List<long> Destinations = new List<long>();
            string message = "";

            if (owner_type == GridOwner.OWNER_TYPE.FACTION) {
                destType = BaseResponse.DEST_TYPE.FACTION;
                Destinations.Add(gridFactionID);
                message += "Your faction's ";
            }
            else if (owner_type == GridOwner.OWNER_TYPE.PLAYER) {
                destType = BaseResponse.DEST_TYPE.PLAYER;
                Destinations.Add(ge.Owner.PlayerID);
                message += "Your ";
            }
            else {
                List<long> nearbyPlayers = ge.Grid.getPlayerIDsWithinPlacementRadius();
                if (nearbyPlayers.Count > 0) {
                    destType = BaseResponse.DEST_TYPE.PLAYER;
                    Destinations = nearbyPlayers;
                    message += "Nearby ";
                }
                else {
                    return;
                }
            }
            log("msg details built", "eventCleanupTimerStart", Logger.severity.TRACE);

            // build notification
            message += "grid " + ge.Grid.DisplayName +
                " will have some of its offending blocks removed in " +
                Utility.prettySeconds(secondsRemaining);

            log("msg built, building noti", "eventDerelictStart");
            NotificationResponse noti = new NotificationResponse() {
                NotificationText = message,
                Time = Constants.NotificationMillis,
                Font = MyFontEnum.Red,
                Destination = Destinations,
                DestType = destType
            };
            log("notification built, sending message", "eventDerelictStart");
            m_MailMan.send(noti);
            log("Msg sent", "eventDerelictStart");
        }
 private void processNotificationResponse(NotificationResponse noti)
 {
     log("Hit", "processNotificationResponse");
     MyAPIGateway.Utilities.ShowNotification(noti.NotificationText, noti.Time, noti.Font);
 }
        public void TestNotificationResponse()
        {
            NotificationResponse nr = new NotificationResponse();
            nr.DestType = BaseResponse.DEST_TYPE.FACTION;
            nr.Destination = new List<long>() { 1, 2 };
            nr.NotificationText = "Test String";
            nr.Time = 2000;
            nr.Font = Sandbox.Common.MyFontEnum.Red;

            byte[] buffer = nr.serialize();

            NotificationResponse nr2 = new NotificationResponse();
            nr2.deserialize(new VRage.ByteStream(buffer, buffer.Length));

            Assert.AreEqual(nr.MsgType, nr2.MsgType);
            Assert.AreEqual(nr.DestType, nr2.DestType);
            CollectionAssert.AreEqual(nr.Destination, nr2.Destination);
            Assert.AreEqual(nr.NotificationText, nr2.NotificationText);
            Assert.AreEqual(nr.Time, nr2.Time);
            Assert.AreEqual(nr.Font, nr2.Font);
        }
        public static BaseResponse messageFromBytes(byte[] buffer)
        {
            VRage.ByteStream stream = new VRage.ByteStream(buffer, buffer.Length);
            TYPE t = (TYPE)stream.getUShort();
            stream.Seek(0, SeekOrigin.Begin);

            BaseResponse msg = null;
            switch (t) {
                case TYPE.NOTIFICATION:
                    msg = new NotificationResponse();
                    break;
                case TYPE.DIALOG:
                    msg = new DialogResponse();
                    break;
                case TYPE.SETTINGS:
                    msg = new SettingsResponse();
                    break;
                case TYPE.FLEET:
                    msg = new FleetResponse();
                    break;
            }

            if (msg != null)
                msg.deserialize(stream);
            return msg;
        }
        private void processStopGridRequest(StopGridRequest req)
        {
            log("", "processStopGridRequest");
            IMyCubeGrid gridToStop = MyAPIGateway.Entities.GetEntityById(req.EntityID) as IMyCubeGrid;
            List<IMySlimBlock> fatBlocks = new List<IMySlimBlock>();

            // Get all thrusters, spaceballs, artificial masses, and cockpits
            Func<IMySlimBlock, bool> selectBlocks = b =>
                b.FatBlock != null && (b.FatBlock is IMyThrust || b.FatBlock is IMySpaceBall || b.FatBlock is InGame.IMyVirtualMass || b.FatBlock is InGame.IMyShipController);
            gridToStop.GetBlocks(fatBlocks, selectBlocks);

            // Can the player interact with this grid? If they can, stop the ship by enabling dampeners, turning off
            // space balls and artificial masses, and disable thruster override
            if (gridToStop.canInteractWith(req.ReturnAddress)) {
                foreach (IMySlimBlock block in fatBlocks) {
                    // Thruster
                    if (block.FatBlock is IMyThrust) {
                        Interfaces.TerminalPropertyExtensions.SetValueFloat(block.FatBlock as IMyTerminalBlock, "Override", 0);
                    }
                    // Spaceball
                    else if (block.FatBlock is IMySpaceBall) {
                        (block.FatBlock as InGame.IMyFunctionalBlock).RequestEnable(false);
                    }
                    // Artificial Mass
                    else if (block.FatBlock is InGame.IMyVirtualMass) {
                        (block.FatBlock as InGame.IMyFunctionalBlock).RequestEnable(false);
                    }
                    // Cockpit
                    else if (block.FatBlock is InGame.IMyShipController) {
                        Interfaces.TerminalPropertyExtensions.SetValueBool(block.FatBlock as InGame.IMyShipController, "DampenersOverride", true);
                    }
                }
                gridToStop.Physics.ClearSpeed();
            }
            // Player can't interact with grid, send error message
            else {
                GridOwner.OWNER owner = GridOwner.ownerFromPlayerID(req.ReturnAddress);
                string errorMessage = "";

                // Build text based on whether or not player is in faction
                switch (owner.OwnerType) {
                    case GridOwner.OWNER_TYPE.FACTION:
                        errorMessage = "Your faction does not have control of that ship's Main Cockpit!";
                        break;
                    case GridOwner.OWNER_TYPE.PLAYER:
                        errorMessage = "You do not have control of that ship's Main Cockpit!";
                        break;
                }

                NotificationResponse noti = new NotificationResponse() {
                    NotificationText = errorMessage,
                    Time = Constants.NotificationMillis,
                    Font = MyFontEnum.Red,
                    Destination = new List<long>() { req.ReturnAddress },
                    DestType = BaseResponse.DEST_TYPE.PLAYER
                };

                send(noti);
            }
        }
Exemple #11
0
        /// <summary>
        /// Called at the end of a round.  Distributes rewards to winning factions.
        /// </summary>
        private void distributeRewards()
        {
            log("Timer triggered", "roundEnd");

            try {
                if (!m_Initialized)
                    return;

                // Check each CP in turn
                Dictionary<long, int> totalTokens = new Dictionary<long, int>();
                foreach (ControlPoint cp in s_Settings.ControlPoints) {
                    log("Processing control point " + cp.Name, "roundEnd");

                    // Get a list of all grids within this CPs sphere of influence
                    List<IMyCubeGrid> gridsInSOI = getGridsInCPRadius(cp);
                    log("Found " + gridsInSOI.Count + " grids in CP SOI", "roundEnd");

                    // Group all of the grids in the SOI into their factions
                    // This will only return grids which conform to the rules which make them valid
                    // for counting.  All other grids discarded.
                    Dictionary<long, List<FACGRID>> allFactionGrids =
                        groupFactionGrids(gridsInSOI, cp.Position);
                    log("After aggregation there are " + allFactionGrids.Count + " factions present", "roundEnd");
                    foreach (KeyValuePair<long, List<FACGRID>> entry in allFactionGrids) {
                        log("Grids for faction " + entry.Key, "roundEnd");
                        foreach (FACGRID grid in entry.Value) {
                            log("\t" + grid.grid.Name, "roundEnd");
                        }
                    }

                    // Now that we have an aggregation of grids for factions
                    // in the SOI, we can decide who wins
                    long greatestFaction = -1;
                    int greatestTotal = -1;
                    bool tie = false;
                    foreach (KeyValuePair<long, List<FACGRID>> entry in allFactionGrids) {
                        int weightedTotal = 0;
                        foreach (FACGRID fg in entry.Value) {
                            weightedTotal +=
                                s_Settings.HullRules[(int)fg.hullClass].CaptureMultiplier;
                        }

                        if (weightedTotal >= greatestTotal) {
                            tie = weightedTotal == greatestTotal;

                            greatestFaction = entry.Key;
                            greatestTotal = weightedTotal;
                        }
                    }

                    log("Faction with most grids: " + greatestFaction, "roundEnd");
                    log("Number of (weighted) grids: " + greatestTotal, "roundEnd");
                    log("Tie? " + tie, "roundEnd");

                    // If we have a tie, nobody gets the tokens
                    // If we don't, award tokens to the faction with the most ships in the SOI
                    if (greatestFaction != -1 && !tie) {
                        // Deposit order:
                        // 1. Largest station (by block count)
                        // 2. If no stations, largest (by block count) large ship with cargo
                        // 3. Otherwise largest (by block count) small ship with cargo

                        // Sort the list by these rules ^
                        log("Sorting list of grids", "roundEnd");
                        List<FACGRID> grids = allFactionGrids[greatestFaction];
                        grids.Sort(s_Sorter);

                        //foreach (FACGRID g in grids) {
                        //	log(g.grid.EntityId + " " + g.gtype + " " + g.blockCount);
                        //}

                        // Go through the sorted list and find the first ship with a cargo container
                        // with space.  If the faction has no free cargo container they are S.O.L.
                        log("Looking for valid container", "roundEnd");
                        InGame.IMyCargoContainer container = null;
                        foreach (FACGRID grid in grids) {
                            container = grid.grid.getAvailableCargo(s_TokenDef, cp.TokensPerPeriod);
                            if (container != null)
                                break;
                        }

                        if (container != null) {
                            // Award the tokens
                            log("Found a ship to put tokens in", "roundEnd");
                            ((container as Interfaces.IMyInventoryOwner).GetInventory(0)
                                as IMyInventory).AddItems(
                                cp.TokensPerPeriod,
                                s_TokenBuilder);

                            // Track totals
                            if (totalTokens.ContainsKey(greatestFaction)) {
                                totalTokens[greatestFaction] += cp.TokensPerPeriod;
                            } else {
                                totalTokens.Add(greatestFaction, cp.TokensPerPeriod);
                            }
                        }
                    }
                }

                // Anounce round ended
                log("Sending message", "roundEnd");
                NotificationResponse endedMessage = new NotificationResponse() {
                    NotificationText = "Conquest Round Ended",
                    Time = Constants.NotificationMillis,
                    Font = MyFontEnum.White,
                    Destination = null,
                    DestType = BaseResponse.DEST_TYPE.EVERYONE
                };
                m_MailMan.send(endedMessage);

                // Report round results
                // For every faction that got rewards, tell them
                NotificationResponse rewardMessage = new NotificationResponse() {
                    NotificationText = "",
                    Time = Constants.NotificationMillis,
                    Font = MyFontEnum.White,
                    Destination = new List<long>() { 0 },
                    DestType = BaseResponse.DEST_TYPE.FACTION
                };
                foreach (KeyValuePair<long, int> entry in totalTokens) {
                    rewardMessage.NotificationText = "Your faction has been awarded " +
                        entry.Value + " licenses";
                    rewardMessage.Destination[0] = entry.Key;
                    m_MailMan.send(endedMessage);
                }

            } catch (Exception e) {
                log("An exception occured: " + e, "roundEnd", Logger.severity.ERROR);
            }
        }
Exemple #12
0
 private void processNotificationResponse(NotificationResponse noti)
 {
     log("Hit", "processNotificationResponse");
     MyAPIGateway.Utilities.ShowNotification(noti.NotificationText, noti.Time, noti.Font);
 }
Exemple #13
0
        private void processStopGridRequest(StopGridRequest req)
        {
            log("", "processStopGridRequest");
            IMyCubeGrid gridToStop = MyAPIGateway.Entities.GetEntityById(req.EntityID) as IMyCubeGrid;

            // @TODO: make it easy to find enforcers by entityId so we can provide
            // greater accuracy to our canInteractWith check
            //GridEnforcer enforcer = StateTracker.getInstance().

            // Can the player interact with this grid? If they can, stop the ship by enabling dampeners, turning off
            // space balls and artificial masses, and disable thruster override
            if (gridToStop.canInteractWith(req.ReturnAddress))
            {
                // Get all thrusters, spaceballs, artificial masses, and cockpits
                List <IMySlimBlock>       fatBlocks    = new List <IMySlimBlock>();
                Func <IMySlimBlock, bool> selectBlocks = b =>
                                                         b.FatBlock != null && (b.FatBlock is IMyThrust || b.FatBlock is IMySpaceBall || b.FatBlock is InGame.IMyVirtualMass || b.FatBlock is InGame.IMyShipController);
                gridToStop.GetBlocks(fatBlocks, selectBlocks);

                foreach (IMySlimBlock block in fatBlocks)
                {
                    // Thruster
                    if (block.FatBlock is IMyThrust)
                    {
                        Interfaces.TerminalPropertyExtensions.SetValueFloat(block.FatBlock as IMyTerminalBlock, "Override", 0);
                    }
                    // Spaceball
                    else if (block.FatBlock is IMySpaceBall)
                    {
                        (block.FatBlock as InGame.IMyFunctionalBlock).RequestEnable(false);
                    }
                    // Artificial Mass
                    else if (block.FatBlock is InGame.IMyVirtualMass)
                    {
                        (block.FatBlock as InGame.IMyFunctionalBlock).RequestEnable(false);
                    }
                    // Cockpit
                    else if (block.FatBlock is InGame.IMyShipController)
                    {
                        Interfaces.TerminalPropertyExtensions.SetValueBool(block.FatBlock as InGame.IMyShipController, "DampenersOverride", true);
                    }
                }
                gridToStop.Physics.ClearSpeed();
            }
            // Player can't interact with grid, send error message
            else
            {
                GridOwner.OWNER owner        = GridOwner.ownerFromPlayerID(req.ReturnAddress);
                string          errorMessage = "";

                // Build text based on whether or not player is in faction
                switch (owner.OwnerType)
                {
                case GridOwner.OWNER_TYPE.FACTION:
                    errorMessage = "Your faction does not have control of that ship's Main Cockpit!";
                    break;

                case GridOwner.OWNER_TYPE.PLAYER:
                    errorMessage = "You do not have control of that ship's Main Cockpit!";
                    break;
                }

                NotificationResponse noti = new NotificationResponse()
                {
                    NotificationText = errorMessage,
                    Time             = Constants.NotificationMillis,
                    Font             = MyFontEnum.Red,
                    Destination      = new List <long>()
                    {
                        req.ReturnAddress
                    },
                    DestType = BaseResponse.DEST_TYPE.PLAYER
                };

                send(noti);
            }
        }