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 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);
        }