private void AutoHangarWorker(Hangar Plugin) { //Significant performance increase if (MySession.Static.Ready) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); List <MyCubeGrid> ExportedGrids = new List <MyCubeGrid>(); List <MyIdentity> ExportPlayerIdentities = new List <MyIdentity>(); Hangar.Debug("AutoHangar: Getting Players!"); var PlayerIdentities = MySession.Static.Players.GetAllIdentities().OfType <MyIdentity>(); foreach (MyIdentity player in PlayerIdentities) { if (player == null) { continue; } DateTime LastLogin; LastLogin = player.LastLoginTime; ulong SteamID = MySession.Static.Players.TryGetSteamId(player.IdentityId); if (LastLogin.AddDays(Plugin.Config.AutoHangarDayAmount) < DateTime.Now) { //AutoHangarBlacklist if (!Plugin.Config.AutoHangarPlayerBlacklist.Any(x => x.SteamID == SteamID)) { ExportPlayerIdentities.Add(player); } } } Hangar.Debug("AutoHangar: Total players to check-" + ExportPlayerIdentities.Count()); int GridCounter = 0; //This gets all the grids foreach (MyIdentity player in ExportPlayerIdentities) { ulong id = 0; try { id = MySession.Static.Players.TryGetSteamId(player.IdentityId); } catch { Hangar.Debug("Identitiy doesnt have a SteamID! Shipping!"); continue; } if (id == 0) { //Sanity check continue; } GridMethods methods = new GridMethods(id, Plugin.Config.FolderDirectory); //string path = GridMethods.CreatePathForPlayer(Config.FolderDirectory, id); if (!methods.LoadInfoFile(out PlayerInfo Data)) { return; } ConcurrentBag <List <MyCubeGrid> > gridGroups = GridFinder.FindGridList(player.IdentityId, false); if (gridGroups.Count == 0) { continue; } long LargestGridID = 0; if (Plugin.Config.KeepPlayersLargestGrid) { //First need to find their largets grid int BlocksCount = 0; foreach (List <MyCubeGrid> grids in gridGroups) { int GridBlockCounts = 0; int LargestSingleGridCount = 0; MyCubeGrid LargetsGrid = grids[0]; foreach (MyCubeGrid grid in grids) { if (grid.BlocksCount > LargestSingleGridCount) { LargestSingleGridCount = grid.BlocksCount; LargetsGrid = grid; } } GridBlockCounts = LargetsGrid.BlocksCount; if (GridBlockCounts > BlocksCount) { BlocksCount = GridBlockCounts; LargestGridID = LargetsGrid.EntityId; } } } foreach (List <MyCubeGrid> grids in gridGroups) { if (grids.Count == 0) { continue; } if (grids[0].IsRespawnGrid && Plugin.Config.DeleteRespawnPods) { grids[0].Close(); continue; } Result result = new Result(); result.grids = grids; var BiggestGrid = grids[0]; foreach (MyCubeGrid grid in grids) { if (grid.BlocksCount > BiggestGrid.BlocksCount) { BiggestGrid = grid; } } if (Plugin.Config.KeepPlayersLargestGrid) { if (BiggestGrid.EntityId == LargestGridID) { //Skip players largest grid continue; } } //Grid Size Checks if (BiggestGrid.GridSizeEnum == MyCubeSize.Large) { if (BiggestGrid.IsStatic && !Plugin.Config.AutoHangarStaticGrids) { continue; } else if (!BiggestGrid.IsStatic && !Plugin.Config.AutoHangarLargeGrids) { continue; } } else if (BiggestGrid.GridSizeEnum == MyCubeSize.Small && !Plugin.Config.AutoHangarSmallGrids) { continue; } result.biggestGrid = BiggestGrid; result.GetGrids = true; //Check for existing grid names Utils.FormatGridName(Data, result); if (methods.SaveGrids(result.grids, result.GridName, Plugin)) { //Load player file and update! //Fill out grid info and store in file HangarChecks.GetBPDetails(result, Plugin.Config, out GridStamp Grid); Grid.GridName = result.GridName; Data.Grids.Add(Grid); GridCounter++; Hangar.Debug(result.biggestGrid.DisplayName + " was sent to Hangar due to inactivity!"); } else { Hangar.Debug(result.biggestGrid.DisplayName + " FAILED to Hangar due to inactivity!"); } } //Save players file! methods.SaveInfoFile(Data); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Hangar.Debug("AutoHangar: Finished Hangaring -" + GridCounter + " grids! Action took: " + ts.ToString()); } }
private static void AutoHangarWorker() { //Significant performance increase Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); List <MyCubeGrid> ExportedGrids = new List <MyCubeGrid>(); List <long> ExportPlayerIdentities = new List <long>(); Log.Warn("AutoHangar: Getting Players!"); foreach (MyIdentity player in MySession.Static.Players.GetAllIdentities()) { if (player == null) { continue; } DateTime LastLogin; LastLogin = player.LastLoginTime; ulong SteamID = MySession.Static.Players.TryGetSteamId(player.IdentityId); if (SteamID == 0) { continue; } if (LastLogin.AddDays(Config.AutoHangarDayAmount) < DateTime.Now) { //AutoHangarBlacklist if (!Config.AutoHangarPlayerBlacklist.Any(x => x.SteamID == SteamID)) { ExportPlayerIdentities.Add(player.IdentityId); } } } Log.Warn("AutoHangar: Total players to check:" + ExportPlayerIdentities.Count()); int GridCounter = 0; //This gets all the grids foreach (long player in ExportPlayerIdentities) { ulong id = MySession.Static.Players.TryGetSteamId(player); //string path = GridMethods.CreatePathForPlayer(Config.FolderDirectory, id); ConcurrentBag <List <MyCubeGrid> > gridGroups = GridUtilities.FindGridList(player, false); //Log.Warn(player + ":" + gridGroups.Count); if (gridGroups.Count == 0) { continue; } long LargestGridID = 0; if (Config.KeepPlayersLargestGrid) { //First need to find their largets grid int BlocksCount = 0; foreach (List <MyCubeGrid> grids in gridGroups) { int GridBlockCounts = 0; int LargestSingleGridCount = 0; MyCubeGrid LargetsGrid = grids[0]; foreach (MyCubeGrid grid in grids) { if (grid.BlocksCount > LargestSingleGridCount) { LargestSingleGridCount = grid.BlocksCount; LargetsGrid = grid; } } GridBlockCounts = LargetsGrid.BlocksCount; if (GridBlockCounts > BlocksCount) { BlocksCount = GridBlockCounts; LargestGridID = LargetsGrid.EntityId; } } } if (gridGroups.Count == 0) { continue; } PlayerHangar PlayersHangar = new PlayerHangar(id, null); foreach (List <MyCubeGrid> grids in gridGroups) { if (grids.Count == 0) { continue; } if (grids[0].IsRespawnGrid && Config.DeleteRespawnPods) { grids[0].Close(); continue; } GridResult Result = new GridResult(); Result.Grids = grids; var BiggestGrid = grids[0]; foreach (MyCubeGrid grid in grids) { if (grid.BlocksCount > BiggestGrid.BlocksCount) { BiggestGrid = grid; } } Result.BiggestGrid = BiggestGrid; if (Config.KeepPlayersLargestGrid) { if (BiggestGrid.EntityId == LargestGridID) { //Skip players largest grid continue; } } //Grid Size Checks if (BiggestGrid.GridSizeEnum == MyCubeSize.Large) { if (BiggestGrid.IsStatic && !Config.AutoHangarStaticGrids) { continue; } else if (!BiggestGrid.IsStatic && !Config.AutoHangarLargeGrids) { continue; } } else if (BiggestGrid.GridSizeEnum == MyCubeSize.Small && !Config.AutoHangarSmallGrids) { continue; } GridStamp Stamp = Result.GenerateGridStamp(); GridUtilities.FormatGridName(PlayersHangar, Stamp); if (PlayersHangar.SaveGridsToFile(Result, Stamp.GridName)) { //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 due to inactivity!"); } } //Save players file! PlayersHangar.SavePlayerFile(); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Log.Warn("Finished Hangaring: " + GridCounter + " grids! Action took: " + ts.ToString()); }
//AutoSellsGrids (HotPile of s***e) private void AutoSellWorker(Hangar Plugin) { String[] subdirectoryEntries = Directory.GetDirectories(Plugin.Config.FolderDirectory); foreach (string subdir in subdirectoryEntries) { string FolderName = new DirectoryInfo(subdir).Name; //Path.GetDirectoryName(subdir+"\\"); if (FolderName == "PublicOffers") { continue; } //Main.Debug(FolderName); ulong SteamID; try { SteamID = Convert.ToUInt64(FolderName); } catch { continue; //Not a valid steam dir; } //Check playerlast logon //MyPlayer.PlayerId CurrentPlayer = MySession.Static.Players.GetAllPlayers().First(x => x.SteamId == SteamID); MyIdentity identity; DateTime LastLogin; try { string playername = MySession.Static.Players.TryGetIdentityNameFromSteamId(SteamID); identity = MySession.Static.Players.GetAllIdentities().First(x => x.DisplayName == playername); //Main.Debug(identity.DisplayName); //MyPlayer.PlayerId PlayerID = MySession.Static.Players.GetAllPlayers().First(x => x.SteamId == SteamID); //CurrentPlayer = MySession.Static.Players.GetPlayerById(0); LastLogin = identity.LastLoginTime; if (LastLogin.AddDays(Plugin.Config.SellAFKDayAmount) < DateTime.Now) { Hangar.Debug("Grids will be auto sold by auction!"); } else { //Main.Debug(LastLogin.AddDays(MaxDayCount).ToString()); continue; } } catch { //Perhaps players was removed? Should we delete thy folder? Nah. WE SHALL SELL continue; } PlayerInfo Data = new PlayerInfo(); try { Data = JsonConvert.DeserializeObject <PlayerInfo>(File.ReadAllText(Path.Combine(subdir, "PlayerInfo.json"))); if (Data == null || Data.Grids == null || Data.Grids.Count == 0) { //Delete folder Directory.Delete(subdir); continue; } } catch (Exception p) { //Main.Debug("Unable File IO exception!", e, Main.ErrorType.Warn); //File is prob null/missing continue; } foreach (GridStamp grid in Data.Grids) { // if (grid.GridForSale == true) { //This grid could be for sale somewhere else. We need to remove it try { int index = GridMarket.GridList.FindIndex(x => x.Name == grid.GridName); CrossServerMessage message = new CrossServerMessage(); message.Type = CrossServer.MessageType.RemoveItem; message.List.Add(GridMarket.GridList[index]); //Send all servers that we removed an item Servers.Update(message); GridMarket.GridList.RemoveAt(index); grid.GridForSale = false; } catch { //Could be a bugged grid? grid.GridForSale = false; } } string Description = "Sold by server due to inactivity at a discounted price! Originial owner: " + identity.DisplayName; long Price = (long)grid.MarketValue; if (grid.MarketValue == 0) { Price = grid.NumberofBlocks * grid.GridPCU; } grid.MarketValue = Price; Price = Price / 2; grid.GridForSale = true; if (!SellOnMarket(subdir, grid, Data, identity, Price, Description)) { Hangar.Debug("Unkown error on grid sell! Grid doesnt exist! (Dont manually delete files!)"); return; } } FileSaver.Save(Path.Combine(subdir, "PlayerInfo.json"), Data); //File.WriteAllText(Path.Combine(subdir, "PlayerInfo.json"), JsonConvert.SerializeObject(Data)); } }