public void RestoreFaction(string factionTag, string gridNameOrEntityId, int backupNumber = 1, bool keepOriginalPosition = false, bool force = false)
        {
            IMyFaction faction = FactionUtils.GetIdentityByTag(factionTag);

            if (faction == null)
            {
                Context.Respond("Player not found!");
                return;
            }

            var factionMembers = faction.Members;

            var relevantGrids = Utilities.FindRelevantGrids(Plugin, factionMembers.Keys);

            Utilities.FindPathToRestore(Plugin, relevantGrids, gridNameOrEntityId, backupNumber,
                                        out string path, out bool gridFound, out bool outOfBounds);

            if (outOfBounds)
            {
                Context.Respond("Backup not found! Check if the number is in range!");
                return;
            }

            if (!gridFound)
            {
                Context.Respond("Grid not found!");
                return;
            }

            ImportPath(path, keepOriginalPosition, force);
        }
        private KeyValuePair <long, List <MyCubeGrid> > CheckGroupsDays(HashSetReader <MyGroups <MyCubeGrid, MyGridMechanicalGroupData> .Node> nodes, bool checkFaction, DateTime today)
        {
            List <MyCubeGrid> gridsList = new List <MyCubeGrid>();

            foreach (var groupNodes in nodes)
            {
                MyCubeGrid cubeGrid = groupNodes.NodeData;

                if (cubeGrid.Physics == null)
                {
                    continue;
                }

                gridsList.Add(cubeGrid);
            }

            if (gridsList.Count == 0)
            {
                return(new KeyValuePair <long, List <MyCubeGrid> >(0, gridsList));
            }

            MyCubeGrid biggestGrid = GridUtils.GetBiggestGridInGroup(gridsList);

            if (biggestGrid == null)
            {
                return(new KeyValuePair <long, List <MyCubeGrid> >(0, new List <MyCubeGrid>()));
            }

            long ownerId      = OwnershipUtils.GetOwner(biggestGrid);
            long daysInactive = 0;

            if (ownerId != 0L && !PlayerUtils.IsNpc(ownerId))
            {
                var identity     = PlayerUtils.GetIdentityById(ownerId);
                var lastSeenDate = PlayerUtils.GetLastSeenDate(identity);

                daysInactive = (today - lastSeenDate).Days;

                if (checkFaction)
                {
                    var faction = FactionUtils.GetPlayerFaction(ownerId);

                    if (faction != null)
                    {
                        foreach (long member in faction.Members.Keys)
                        {
                            identity     = PlayerUtils.GetIdentityById(member);
                            lastSeenDate = PlayerUtils.GetLastSeenDate(identity);

                            daysInactive = Math.Min(daysInactive, (today - lastSeenDate).Days);
                        }
                    }
                }
            }

            return(new KeyValuePair <long, List <MyCubeGrid> >(daysInactive, gridsList));
        }
        public static bool OwnershipCorrect(MyCubeGrid grid, long playerId, bool checkFactions) {

            /* If Player is owner we are totally fine and can allow it */
            if(grid.BigOwners.Contains(playerId))
                return true;

            /* If he is not owner and we dont want to allow checks for faction members... then prohibit */
            if (!checkFactions)
                return false;

            /* If checks for faction are allowed grab owner and see if factions are equal */
            long gridOwner = OwnershipUtils.GetOwner(grid);

            return FactionUtils.HavePlayersSameFaction(playerId, gridOwner);
        }
        private void ListInternal(PurchaseMode mode, bool filterRandom = true)
        {
            var buyables = FindFilteredBuyables(mode, filterRandom);

            List <string> lines = new List <string>();

            foreach (var entry in buyables)
            {
                long identityId = entry.Key;

                List <PurchaseMode> modes = entry.Value.ToList();
                modes.Sort();

                IMyFaction faction = FactionUtils.GetPlayerFaction(identityId);

                string factionString = "";
                if (faction != null)
                {
                    factionString = "[" + faction.Tag + "]";
                }

                string name     = PlayerUtils.GetPlayerNameById(identityId) + " " + factionString;
                string modesStr = string.Join(", ", modes);

                lines.Add(name + " -- " + modesStr);
            }

            lines.Sort();

            StringBuilder sb = new StringBuilder();

            foreach (var line in lines)
            {
                sb.AppendLine(line);
            }

            if (Context.Player == null)
            {
                Context.Respond($"Buyable GPS for " + mode);
                Context.Respond(sb.ToString());
            }
            else
            {
                ModCommunication.SendMessageTo(new DialogMessage("Buyable GPS", "For " + mode, sb.ToString()), Context.Player.SteamUserId);
            }
        }
        private long GetAdjustedPriceForPlayer(long price, MyIdentity identity)
        {
            if (!Plugin.Config.UseDynamicPrices)
            {
                return(price);
            }

            var faction = FactionUtils.GetPlayerFaction(identity.IdentityId);

            if (faction == null)
            {
                return(price);
            }

            int numberOfMembers = faction.Members.Count;

            float priceMultiplier = Plugin.Config.DynamicPriceMultiplier;

            return(price + (long)Math.Round(price * (numberOfMembers - 1) * priceMultiplier));
        }
        private Dictionary <long, PurchaseCollection> FindFilteredBuyablesForPlayer(PurchaseMode mode)
        {
            /* We dont want to have filtering for random in here to not filter twice. */
            var buyables = FindFilteredBuyables(mode, false);

            var filtered = new Dictionary <long, PurchaseCollection>();

            var player     = Context.Player;
            var identityId = player.IdentityId;

            HashSet <long> factionMembers = new HashSet <long>();

            if (Plugin.Config.FilterFactionMembers)
            {
                try {
                    var faction = FactionUtils.GetPlayerFaction(identityId);

                    factionMembers = new HashSet <long>(faction.Members.Keys);
                } catch (Exception e) {
                    Log.Error(e, "Faction could not be checked, it potentially has no founder!");
                }
            }

            factionMembers.Add(identityId);

            foreach (var entry in buyables)
            {
                long id = entry.Key;

                if (factionMembers.Contains(id))
                {
                    continue;
                }

                filtered.Add(entry.Key, entry.Value);
            }

            /* After removing Faction Members and the player himself we may have to check again if NPCs needs to go */
            return(FilterBuyablesForNpcRatio(filtered, mode));
        }
        private void BuyInternal(long price, PurchaseMode mode)
        {
            if (Context.Player == null)
            {
                Context.Respond("Only an actual player can buy GPS!");
                return;
            }

            if (price < 0)
            {
                Context.Respond("This command was disabled in the settings. Use '!gps list commands' to see which commands are active!");
                return;
            }

            var player   = Context.Player;
            var identity = PlayerUtils.GetIdentityById(player.IdentityId);

            price = GetAdjustedPriceForPlayer(price, identity);

            if (Plugin.Config.MustBeInFactionToBuy)
            {
                var faction = FactionUtils.GetPlayerFaction(identity.IdentityId);

                if (faction == null)
                {
                    Context.Respond("You must be part of a Faction to buy GPS!");
                    return;
                }
            }

            if (mode == PurchaseMode.ONLINE || mode == PurchaseMode.RANDOM)
            {
                var minPlayersOnline = Plugin.Config.MinPlayerOnlineToBuy;

                int onlineCount = MySession.Static.Players.GetOnlinePlayerCount();

                if (onlineCount < minPlayersOnline)
                {
                    Context.Respond("For Online/Random gps at least " + minPlayersOnline + " players must be online!");
                    return;
                }
            }

            var minOnlineTime = Plugin.Config.MinOnlineMinutesToBuy;

            if (minOnlineTime > 0)
            {
                var lastSeen      = Plugin.getLastLoginDate(identity.IdentityId);
                var minOnlineDate = DateTime.Now.AddMinutes(-Plugin.Config.MinOnlineMinutesToBuy);

                if (lastSeen > minOnlineDate)
                {
                    int differenceSeconds = (int)lastSeen.Subtract(minOnlineDate).TotalSeconds;

                    int minutes = (differenceSeconds / 60);
                    int seconds = differenceSeconds % 60;

                    Context.Respond("You are not online for long enough! You must be online for at least " + minutes.ToString("00") + ":" + seconds.ToString("00") + " more minutes!");

                    return;
                }
            }

            var minPCUToBuy = Plugin.Config.MinPCUToBuy;

            if (minPCUToBuy > 0)
            {
                var pcuBuilt  = identity.BlockLimits.PCUBuilt;
                var neededPcu = Plugin.Config.MinPCUToBuy;

                if (neededPcu > pcuBuilt)
                {
                    Context.Respond("You dont have enough PCU to buy! You need at least " + (neededPcu - pcuBuilt) + " more!");

                    return;
                }
            }

            var cooldownManager        = Plugin.CooldownManager;
            var cooldownManagerFaction = Plugin.CooldownManagerFactionChange;
            var steamId = new SteamIdCooldownKey(player.SteamUserId);

            long currentBalance = MyBankingSystem.GetBalance(player.IdentityId);

            if (currentBalance < price)
            {
                Context.Respond("You dont have enough credits to effort a GPS! You need at least " + price.ToString("#,##0") + " SC.");
                return;
            }

            if (!cooldownManagerFaction.CheckCooldown(steamId, GpsRoulettePlugin.COOLDOWN_COMMAND, out long remainingSecondsFaction))
            {
                Log.Info("Faction Cooldown for Player " + player.DisplayName + " still running! " + remainingSecondsFaction + " seconds remaining!");
                Context.Respond("Command is still on cooldown after you changed Factions for " + remainingSecondsFaction + " seconds.");
                return;
            }

            if (!cooldownManager.CheckCooldown(steamId, GpsRoulettePlugin.COOLDOWN_COMMAND, out long remainingSeconds))
            {
                Log.Info("Cooldown for Player " + player.DisplayName + " still running! " + remainingSeconds + " seconds remaining!");
                Context.Respond("Command is still on cooldown for " + remainingSeconds + " seconds.");
                return;
            }

            var buyables = FindFilteredBuyablesForPlayer(mode);

            if (buyables.Count == 0)
            {
                Context.Respond("Currently there is no GPS available for purchase. Please try again later!");
                return;
            }

            if (!CheckConformation(steamId, mode, price))
            {
                return;
            }

            if (BuyRandomFromDict(buyables))
            {
                var config     = Plugin.Config;
                var cooldownMs = config.CooldownMinutes * 60 * 1000L;

                MyBankingSystem.ChangeBalance(player.IdentityId, -price);

                cooldownManager.StartCooldown(steamId, GpsRoulettePlugin.COOLDOWN_COMMAND, cooldownMs);

                Context.Respond("Purchase successful!");
            }
            else
            {
                Context.Respond("The location of the selected player could not be retrieved. The purchase was cancelled. Please try again.");
            }
        }
        private void AddCommandsToSb(StringBuilder sb, string prefix = "")
        {
            bool       shouldShowAdjustedPrices = false;
            MyIdentity identity = null;

            if (Plugin.Config.UseDynamicPrices)
            {
                var player = Context.Player;

                if (player != null)
                {
                    identity = PlayerUtils.GetIdentityById(player.IdentityId);

                    if (FactionUtils.GetPlayerFaction(identity.IdentityId) != null)
                    {
                        shouldShowAdjustedPrices = true;
                    }
                }

                sb.AppendLine();

                sb.AppendLine("Prices change dynamically relative to the number of members your faction has.");
                sb.AppendLine("Actual Price = baseprice + baseprice * multiplier * (Number of Factionmembers - 1)");
                sb.AppendLine("Current Multiplier is " + Plugin.Config.DynamicPriceMultiplier.ToString("#,##0.00"));

                sb.AppendLine();
            }

            bool atLeastOneActiveCommand = false;

            var price = Plugin.Config.PriceCreditsRandom;

            if (price >= 0)
            {
                atLeastOneActiveCommand = true;

                sb.AppendLine(prefix + "!gps buy random -- for " + price.ToString("#,##0") + " SC (Baseprice)");

                if (shouldShowAdjustedPrices)
                {
                    sb.AppendLine(prefix + "   You would pay " + GetAdjustedPriceForPlayer(price, identity).ToString("#,##0") + " SC");
                }
            }

            price = Plugin.Config.PriceCreditsOnline;
            if (price >= 0)
            {
                atLeastOneActiveCommand = true;

                sb.AppendLine(prefix + "!gps buy online -- for " + price.ToString("#,##0") + " SC (Baseprice)");

                if (shouldShowAdjustedPrices)
                {
                    sb.AppendLine(prefix + "   You would pay " + GetAdjustedPriceForPlayer(price, identity).ToString("#,##0") + " SC");
                }
            }

            price = Plugin.Config.PriceCreditsInactive;
            if (price >= 0)
            {
                atLeastOneActiveCommand = true;

                sb.AppendLine(prefix + "!gps buy inactive -- for " + price.ToString("#,##0") + " SC (Baseprice)");

                if (shouldShowAdjustedPrices)
                {
                    sb.AppendLine(prefix + "   You would pay " + GetAdjustedPriceForPlayer(price, identity).ToString("#,##0") + " SC");
                }
            }

            price = Plugin.Config.PriceCreditsNPC;
            if (price >= 0)
            {
                atLeastOneActiveCommand = true;

                sb.AppendLine(prefix + "!gps buy npc -- for " + price.ToString("#,##0") + " SC (Baseprice)");

                if (shouldShowAdjustedPrices)
                {
                    sb.AppendLine(prefix + "   You would pay " + GetAdjustedPriceForPlayer(price, identity).ToString("#,##0") + " SC");
                }
            }

            if (!atLeastOneActiveCommand)
            {
                sb.AppendLine(prefix + "Buying is currently NOT enabled!");
            }

            if (Plugin.Config.UseDynamicPrices)
            {
                sb.AppendLine();
                sb.AppendLine("Keep in mind: These prices change relative to the number of members your faction has.");
            }
        }
Ejemplo n.º 9
0
        private void FactionCreated(long factionId)
        {
            var faction = FactionUtils.GetIdentityById(factionId);

            ChangeCooldownOnFactionChange(faction.FounderId);
        }
        public void ListFaction(string factionTag, string gridNameOrEntityId = null)
        {
            IMyFaction faction = FactionUtils.GetIdentityByTag(factionTag);

            if (faction == null)
            {
                Context.Respond("Player not found!");
                return;
            }

            StringBuilder sb       = new StringBuilder();
            string        gridname = null;
            int           i        = 1;

            var factionMembers = faction.Members;

            if (gridNameOrEntityId == null)
            {
                foreach (long playerIdentity in factionMembers.Keys)
                {
                    MyIdentity identity = PlayerUtils.GetIdentityById(playerIdentity);

                    Utilities.AddListEntriesToSb(Plugin, sb, playerIdentity, i, true, out i);
                }
            }
            else
            {
                var relevantGrids = Utilities.FindRelevantGrids(Plugin, factionMembers.Keys);

                Utilities.AddListEntriesToSb(relevantGrids, sb, gridNameOrEntityId, out gridname);

                if (gridname == null)
                {
                    Context.Respond("Grid not found!");
                    return;
                }
            }

            if (Context.Player == null)
            {
                Context.Respond($"Backed up Grids for Faction {faction.Name} [{faction.Tag}]");

                if (gridname != null)
                {
                    Context.Respond($"Grid {gridname}");
                }

                Context.Respond(sb.ToString());
            }
            else
            {
                if (gridname != null)
                {
                    ModCommunication.SendMessageTo(new DialogMessage("Backed up Grids", $"Grid {gridname}", sb.ToString()), Context.Player.SteamUserId);
                }
                else
                {
                    ModCommunication.SendMessageTo(new DialogMessage("Backed up Grids", $"Faction {faction.Name} [{faction.Tag}]", sb.ToString()), Context.Player.SteamUserId);
                }
            }
        }
        public void Find(string gridNameOrEntityId = null)
        {
            string basePath = Plugin.CreatePath();

            var identities = MySession.Static.Players.GetAllIdentities();

            StringBuilder sb = new StringBuilder();
            int           i  = 1;

            foreach (var identitiy in identities)
            {
                long playerId = identitiy.IdentityId;

                string path = Plugin.CreatePathForPlayer(basePath, playerId);

                DirectoryInfo   gridDir = new DirectoryInfo(path);
                DirectoryInfo[] dirList = gridDir.GetDirectories("*", SearchOption.TopDirectoryOnly);

                List <DirectoryInfo> filteredGrids = new List <DirectoryInfo>();

                foreach (DirectoryInfo grid in dirList)
                {
                    if (!Utilities.Matches(grid, gridNameOrEntityId))
                    {
                        continue;
                    }

                    filteredGrids.Add(grid);
                }

                if (filteredGrids.Count == 0)
                {
                    continue;
                }

                string factionTag = FactionUtils.GetPlayerFactionTag(playerId);

                if (factionTag != "")
                {
                    factionTag = " [" + factionTag + "]";
                }

                sb.AppendLine(identitiy.DisplayName + factionTag);

                foreach (DirectoryInfo grid in filteredGrids)
                {
                    string dateString = Utilities.GenerateDateString(grid);

                    sb.AppendLine((i++) + "      " + grid + " - " + dateString);
                }
            }

            if (Context.Player == null)
            {
                Context.Respond($"Find results");
                Context.Respond($"for grids matching {gridNameOrEntityId}");

                Context.Respond(sb.ToString());
            }
            else
            {
                ModCommunication.SendMessageTo(new DialogMessage("Find results", $"for grids matching {gridNameOrEntityId}", sb.ToString()), Context.Player.SteamUserId);
            }
        }