示例#1
0
        public static void AddListEntriesToSb(GridBackupPlugin plugin, StringBuilder sb,
                                              long playerIdentity, int startIndex, bool outputPlayerName,
                                              out int nextIndex)
        {
            nextIndex = startIndex;

            string path = plugin.CreatePath();

            path = plugin.CreatePathForPlayer(path, playerIdentity);

            DirectoryInfo gridDir = new DirectoryInfo(path);

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

            if (outputPlayerName && dirList.Length > 0)
            {
                var identity = PlayerUtils.GetIdentityById(playerIdentity);

                sb.AppendLine(identity.DisplayName);
            }

            foreach (var file in dirList)
            {
                string dateString = GenerateDateString(file);

                sb.AppendLine((nextIndex++) + "      " + file.Name + " - " + dateString);
            }

            if (outputPlayerName && dirList.Length > 0)
            {
                sb.AppendLine();
            }
        }
        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));
        }
        private CheckResult FixGroups(ConcurrentBag<MyGroups<MyCubeGrid, MyGridPhysicalGroupData>.Group> groups, long playerId) {

            var result = CheckGroups(groups, out MyGroups<MyCubeGrid, MyGridPhysicalGroupData>.Group group, playerId, FactionFixEnabled);

            if (result != CheckResult.OK)
                return result;

            MyIdentity executingPlayer = null;

            if (playerId != 0)
                executingPlayer = PlayerUtils.GetIdentityById(playerId);

            return FixGroup(group, executingPlayer);
        }
        private MyGps FindPositionOfPlayer(long foundIdentity)
        {
            var identity = PlayerUtils.GetIdentityById(foundIdentity);

            if (identity == null)
            {
                Log.Error("Could not find Identity to ID #" + foundIdentity);
                return(null);
            }

            var factionCollection = MySession.Static.Factions;
            var playerCollection  = MySession.Static.Players;

            if (playerCollection.IdentityIsNpc(foundIdentity))
            {
                var faction = factionCollection.GetPlayerFaction(foundIdentity);

                if (faction == null)
                {
                    Log.Error("NPC Identity " + identity.DisplayName + " has no faction!");
                    return(null);
                }

                var stations = faction.Stations;

                if (stations.Count == 0)
                {
                    Log.Error("NPC Faction " + faction.Tag + " has no stations!");
                    return(null);
                }

                var stationsList = new List <MyStation>(stations);
                stationsList.ShuffleList();

                var station = stations.First();

                return(CreateGps(station.Position, identity));
            }

            var character = identity.Character;

            if (character == null)
            {
                if (!playerCollection.IsPlayerOnline(foundIdentity))
                {
                    /* If player is not online look for him in beds cryopods etc. */
                    foreach (var grid in MyEntities.GetEntities().OfType <MyCubeGrid>().ToList())
                    {
                        foreach (var controller in grid.GetFatBlocks <MyShipController>())
                        {
                            var pilot = controller.Pilot;

                            if (pilot != null && pilot.GetPlayerIdentityId() == foundIdentity)
                            {
                                return(CreateGps(controller.PositionComp.GetPosition(), identity));
                            }
                        }
                    }
                }

                if (identity.LastDeathPosition != null)
                {
                    return(CreateGps(identity.LastDeathPosition.Value, identity));
                }

                Log.Error("Player " + identity.DisplayName + " has no last death location!");

                return(null);
            }

            return(CreateGps(character.PositionComp.GetPosition(), identity));
        }
        private bool BuyRandomFromDict(Dictionary <long, PurchaseCollection> buyables)
        {
            List <long> buyablesAsList = new List <long>(buyables.Keys);

            var   config   = Plugin.Config;
            MyGps position = null;

            long identityId    = Context.Player.IdentityId;
            long foundIdentity = 0L;

            /* Try 5 times to make sure the player gets something. If we wont find anything after 5 times we stop */
            for (int i = 0; i < 5; i++)
            {
                buyablesAsList.ShuffleList();

                foundIdentity = buyablesAsList.First();

                position = FindPositionOfPlayer(foundIdentity);

                if (position != null)
                {
                    break;
                }
            }

            if (position == null)
            {
                return(false);
            }

            SendGps(position, identityId);

            var location    = position.Coords;
            var otherPlayer = PlayerUtils.GetIdentityById(foundIdentity);

            if (otherPlayer != null)
            {
                Log.Info("Player " + Context.Player.DisplayName + " bought GPS Location X: " + location.X + " Y: " + location.Y + " Z: " + location.Z + " of Player " + otherPlayer.DisplayName);
            }

            if (config.NotifySoldPlayer)
            {
                if (config.NotifyDelaySeconds <= 0)
                {
                    Plugin.NotifyPlayer(foundIdentity);
                }
                else
                {
                    /* If anything goes wrong we notify immediately. Cannot risk the player getting no notification. */
                    try {
                        Plugin.AddIdentityToNotifiticationQueue(foundIdentity);
                    } catch (Exception e) {
                        Log.Error(e, "Error on adding to notify queue!");

                        Plugin.NotifyPlayer(foundIdentity);
                    }
                }
            }

            return(true);
        }
        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.");
            }
        }
        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);
                }
            }
        }