public static void RunAutoHangar(bool SaveAll, bool hangarStatic = true, bool hangarLarge = true, bool hangarSmall = true, bool hangarLargest = true) { if (!Hangar.ServerRunning || !MySession.Static.Ready || MySandboxGame.IsPaused) { return; } List <long> ExportPlayerIdentities = new List <long>(); try { //Scan all the identities we need foreach (MyIdentity Identity in MySession.Static.Players.GetAllIdentities()) { //No need to check this identity. Its an NPC if (string.IsNullOrEmpty(Identity.DisplayName) || MySession.Static.Players.IdentityIsNpc(Identity.IdentityId)) { continue; } //Funcky SteamID check DateTime LastLogin = Identity.LastLoginTime; ulong SteamID = MySession.Static.Players.TryGetSteamId(Identity.IdentityId); if (SteamID == 0) { continue; } //Need to see if we need to check this identity if (SaveAll) { ExportPlayerIdentities.Add(Identity.IdentityId); } else if (!SaveAll && LastLogin.AddDays(Config.AutoHangarDayAmount) < DateTime.Now && !Config.AutoHangarPlayerBlacklist.Any(x => x.SteamID == SteamID)) { ExportPlayerIdentities.Add(Identity.IdentityId); } } Log.Warn($"AutoHangar Running! Total players to check {ExportPlayerIdentities.Count()}"); //Dictionary for a list of all the grids we need to remove Dictionary <long, List <AutoHangarItem> > scannedGrids = new Dictionary <long, List <AutoHangarItem> >(); foreach (var group in MyCubeGridGroups.Static.Physical.Groups.ToList()) { if (group.Nodes.Count == 0) { continue; } int totalBlocks = 0; List <MyCubeGrid> grids = new List <MyCubeGrid>(); foreach (MyGroups <MyCubeGrid, MyGridPhysicalGroupData> .Node groupNodes in group.Nodes) { MyCubeGrid grid = groupNodes.NodeData; if (grid == null || grid.MarkedForClose || grid.MarkedAsTrash) { continue; } totalBlocks += grid.BlocksCount; grids.Add(grid); } //Dont add if this grid group is null/empty if (grids.Count == 0) { continue; } //Get biggest grid owner, and see if they are an identity that we need to export grids.BiggestGrid(out MyCubeGrid LargestGrid); long biggestOwner = LargestGrid.GetBiggestOwner(); //No need to autohangar if the largest owner is an NPC if (MySession.Static.Players.IdentityIsNpc(biggestOwner)) { continue; } //Now see if we should hangar this shit if (LargestGrid.GridSizeEnum == MyCubeSize.Large && LargestGrid.IsStatic && !hangarStatic) { continue; } else if (LargestGrid.GridSizeEnum == MyCubeSize.Large && !LargestGrid.IsStatic && !hangarLarge) { continue; } else if (LargestGrid.GridSizeEnum == MyCubeSize.Small && !hangarSmall) { continue; } //Add this new grid into our planned export queue if (ExportPlayerIdentities.Contains(biggestOwner)) { AutoHangarItem hangar = new AutoHangarItem(totalBlocks, grids, LargestGrid); if (!scannedGrids.ContainsKey(biggestOwner)) { scannedGrids.Add(biggestOwner, new List <AutoHangarItem>() { hangar }); } else { scannedGrids[biggestOwner].Add(hangar); } } } Task.Run(() => SaveAutohangarGrids(scannedGrids)); } catch (Exception ex) { Log.Error(ex); } finally { ExportPlayerIdentities.Clear(); Watcher.Stop(); } }
public static async Task <int> SaveAutohangarGrids(Dictionary <long, List <AutoHangarItem> > scannedGrids) { int GridCounter = 0; try { Watcher.Reset(); Watcher.Start(); //Now that we have our complete collection, lets loop through the grids foreach (KeyValuePair <long, List <AutoHangarItem> > kvp in scannedGrids) { List <AutoHangarItem> allGrids = kvp.Value; if (Config.KeepPlayersLargestGrid) { AutoHangarItem largest = allGrids.Aggregate((i1, i2) => i1.blocksCount > i2.blocksCount ? i1 : i2); allGrids.Remove(largest); } //No sense in running everything if this list is empty if (allGrids.Count == 0) { continue; } //Grab Players Hangar ulong id = MySession.Static.Players.TryGetSteamId(kvp.Key); PlayerHangar PlayersHangar = new PlayerHangar(id, null); foreach (AutoHangarItem item in allGrids) { //remove respawn grids if (item.largestGrid.IsRespawnGrid && Config.DeleteRespawnPods) { //Close all grids item.grids.Close("Autohangar deleted respawn pod"); continue; } GridResult Result = new GridResult(); Result.Grids = item.grids; Result.BiggestGrid = item.largestGrid; GridStamp Stamp = Result.GenerateGridStamp(); PlayersHangar.SelectedPlayerFile.FormatGridName(Stamp); bool val = await PlayersHangar.SaveGridsToFile(Result, Stamp.GridName); if (val) { //Load player file and update! //Fill out grid info and store in file PlayersHangar.SaveGridStamp(Stamp, false, true); GridCounter++; Log.Info(Result.BiggestGrid.DisplayName + " was sent to Hangar due to inactivity!"); } else { Log.Info(Result.BiggestGrid.DisplayName + " FAILED to Hangar! Check read error above for more info!"); } } PlayersHangar.SavePlayerFile(); } Watcher.Stop(); Log.Warn($"Finished Hangaring: {GridCounter} grids! Action took: {Watcher.Elapsed}"); return(GridCounter); } catch (Exception ex) { Log.Error(ex); } Watcher.Stop(); return(GridCounter); }