private Dictionary <long, PurchaseCollection> FindFilteredBuyables(PurchaseMode mode, bool filterRandom = true)
        {
            var buyables = FindBuyables();

            if (mode == PurchaseMode.RANDOM)
            {
                if (filterRandom)
                {
                    return(FilterBuyablesForNpcRatio(buyables, mode));
                }
                else
                {
                    return(buyables);
                }
            }

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

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

                if (!modes.Contains(mode))
                {
                    continue;
                }

                filtered.Add(identityId, modes);
            }

            return(FilterBuyablesForNpcRatio(filtered, mode));
        }
        private Dictionary <long, PurchaseCollection> FindBuyables()
        {
            var factionCollection = MySession.Static.Factions;
            var playerCollection  = MySession.Static.Players;
            var identities        = playerCollection.GetAllIdentities();
            var result            = new Dictionary <long, PurchaseCollection>();

            var config = Plugin.Config;

            foreach (var identity in identities)
            {
                long identityId = identity.IdentityId;

                MyFaction faction = factionCollection.GetPlayerFaction(identityId);

                /* If we dont want to include factionless players we have to filter here now */
                if (!config.IncludePlayersWithoutFaction && faction == null)
                {
                    continue;
                }

                bool isNpc        = playerCollection.IdentityIsNpc(identityId);
                bool mustCheckPcu = !isNpc || config.MinPCUAlsoForNPC;

                /* If we dont want to find players below a certain PCU limit filter here now */
                if (mustCheckPcu && config.MinPCUToBeFound > identity.BlockLimits.PCUBuilt)
                {
                    continue;
                }

                var modes = new PurchaseCollection();

                if (isNpc)
                {
                    bool validFaction = faction != null && faction.IsEveryoneNpc() && faction.Stations.Count > 0;

                    if (validFaction)
                    {
                        modes.Add(PurchaseMode.NPC);
                    }
                }
                else
                {
                    if (playerCollection.IsPlayerOnline(identityId))
                    {
                        modes.Add(PurchaseMode.ONLINE);
                    }
                    else
                    {
                        var lastSeen = PlayerUtils.GetLastSeenDate(identity);

                        /* How many minutes ago does still count as online? */
                        var maxOnlineDate = DateTime.Now.AddMinutes(-config.LastOnlineMinutes);

                        if (lastSeen >= maxOnlineDate)
                        {
                            modes.Add(PurchaseMode.ONLINE);
                        }

                        if (config.OfflineLongerThanHours > 0)
                        {
                            /* How many hours ago does mean its inactive? */
                            var minOnlineInactiveDate = DateTime.Now.AddHours(-config.OfflineLongerThanHours);

                            if (lastSeen < minOnlineInactiveDate)
                            {
                                modes.Add(PurchaseMode.INACTIVE);
                            }
                        }
                    }
                }

                if (modes.Count > 0)
                {
                    result.Add(identityId, modes);
                }
            }

            return(result);
        }