public void UnRegisterSubGrid(MyCubeGrid grid, bool clean = false) { if (!SubGridsRegistered.Contains(grid)) { Log.Line($"sub Grid Already UnRegistered: [Main]:{grid == MyGrid}"); } if (!clean) { SubGrids.Remove(grid); } SubGridsRegistered.Remove(grid); grid.OnFatBlockAdded -= FatBlockAdded; grid.OnFatBlockRemoved -= FatBlockRemoved; foreach (var cube in grid.GetFatBlocks()) { var battery = cube as MyBatteryBlock; if (InventoryMonitor.ContainsKey(cube) || battery != null && Batteries.Contains(battery)) { FatBlockRemoved(cube); } } GridAi removeAi; if (!Session.GridTargetingAIs.ContainsKey(grid)) { Session.GridToMasterAi.TryRemove(grid, out removeAi); } }
internal void FatBlockAdded(MyCubeBlock cube) { try { var battery = cube as MyBatteryBlock; var weaponType = (cube is MyConveyorSorter || cube is IMyUserControllableGun); var isWeaponBase = weaponType && cube.BlockDefinition != null && (Session.ReplaceVanilla && Session.VanillaIds.ContainsKey(cube.BlockDefinition.Id) || Session.WeaponPlatforms.ContainsKey(cube.BlockDefinition.Id)); if (!isWeaponBase && (cube is MyConveyor || cube is IMyConveyorTube || cube is MyConveyorSorter || cube is MyCargoContainer || cube is MyCockpit || cube is IMyAssembler || cube is IMyShipConnector) && cube.CubeGrid.IsSameConstructAs(MyGrid)) { MyInventory inventory; if (cube.HasInventory && cube.TryGetInventory(out inventory) && InventoryMonitor.TryAdd(cube, inventory)) { inventory.InventoryContentChanged += CheckAmmoInventory; Construct.RootAi.Construct.NewInventoryDetected = true; int monitors; if (!Session.InventoryMonitors.TryGetValue(inventory, out monitors)) { Session.InventoryMonitors[inventory] = 0; Session.InventoryItems[inventory] = Session.PhysicalItemListPool.Get(); Session.AmmoThreadItemList[inventory] = Session.BetterItemsListPool.Get(); } else { Session.InventoryMonitors[inventory] = monitors + 1; } } } else if (battery != null) { if (Batteries.Add(battery)) { SourceCount++; } UpdatePowerSources = true; } else if (isWeaponBase) { MyOrientedBoundingBoxD b; BoundingSphereD s; MyOrientedBoundingBoxD blockBox; SUtils.GetBlockOrientedBoundingBox(cube, out blockBox); if (Session.IsWeaponAreaRestricted(cube.BlockDefinition.Id.SubtypeId, blockBox, cube.CubeGrid, cube.EntityId, null, out b, out s)) { if (Session.IsServer) { cube.CubeGrid.RemoveBlock(cube.SlimBlock); } } } } catch (Exception ex) { Log.Line($"Exception in Controller FatBlockAdded: {ex} - {cube?.BlockDefinition == null} - RootAiNull: {Construct.RootAi == null}"); } }
private void ForceCloseAiInventories() { foreach (var pair in InventoryMonitor) { InventoryRemove(pair.Key, pair.Value); } if (InventoryMonitor.Count > 0) { Log.Line($"Found stale inventories during AI close - failedToRemove:{InventoryMonitor.Count}"); InventoryMonitor.Clear(); } }
private bool InventoryRemove(MyCubeBlock cube, MyInventory inventory) { try { MyInventory oldInventory; if (InventoryMonitor.TryRemove(cube, out oldInventory)) { inventory.InventoryContentChanged -= CheckAmmoInventory; int monitors; if (Session.InventoryMonitors.TryGetValue(inventory, out monitors)) { if (--monitors < 0) { MyConcurrentList <MyPhysicalInventoryItem> removedPhysical; MyConcurrentList <BetterInventoryItem> removedBetter; if (Session.InventoryItems.TryRemove(inventory, out removedPhysical)) { Session.PhysicalItemListPool.Return(removedPhysical); } if (Session.AmmoThreadItemList.TryRemove(inventory, out removedBetter)) { Session.BetterItemsListPool.Return(removedBetter); } Session.InventoryMonitors.Remove(inventory); } else { Session.InventoryMonitors[inventory] = monitors; } } else { return(false); } } } catch (Exception ex) { Log.Line($"Exception in InventoryRemove: {ex}"); } return(true); }
public async Task <bool> StartAsync() { if (!_serviceIsStarted && !_serviceIsStarting) { _serviceIsStarting = true; if (!ConfigService.Instance.ConfigIsInitialized) { if (!ConfigService.Instance.LoadConfig()) { WriteLog(LogType.Warning, "Failed loading config"); return(false); } } var config = ConfigService.Instance.Configuration; if (config.TelegramActive) { _telegram.Start(); LoggerHelper.TelegramHelper = _telegram; _telegramConfigUpdater = new TelegramConfigUpdater(_telegram); _telegramConfigUpdater.Start(); } WriteLog(LogType.Information, "Starting Service"); ConfigService.Instance.OnConfigUpdated += OnConfigUpdated; _service = new Service(config.Key); if (!await _service.Init()) { WriteLog(LogType.Error, "Failed initialising MarketAPI. Is the ApiKey correct?"); return(false); } var mainSteamID = await _service.GetMySteamIDAsync(); _steamID64 = mainSteamID.SteamID64; _inventoryMonitor.Add(_steamID64, InventoryMonitor.StartNewService(_steamID64)); foreach (var steamID in config.Entries.SelectMany(s => s.AltAccounts).GroupBy(a => a.SteamID64)) { _inventoryMonitor.Add(steamID.Key, InventoryMonitor.StartNewService(steamID.Key)); } if (ConfigService.GetConfig().Entries.Any(c => c.Mode != BuyMode.IgnoreAveragePrice)) { WriteLog(LogType.Information, "Prefilling average item prices"); await _timers.AddEntry(new TimerHelper(config.AveragePriceCheckInterval * 60 * 1000, UpdateAverageItemPriceListAsync, true)).RunActionAsync(); // Get average prices every 5 minutes } _timers.Add(new TimerHelper(ConfigService.GetConfig().CheckInterval, BuyItemsAsync, true)); // Buy items every n milliseconds _timers.Add(new TimerHelper(1 * 60 * 1000, UpdateBalanceAsync, true)); // Get current balance every minute if (ConfigService.GetConfig().EnablePing) { WriteLog(LogType.Information, "Activating Selling/Autopurchase"); _timers.Add(new TimerHelper(3 * 61 * 1000, HandlePingAsync, true)); } _serviceIsStarting = false; return(_serviceIsStarted = true); } return(false); }
private async Task BuyItemAsync(ItemConfiguration itemConfig, List <ItemData> currentInfo) { if (currentInfo?.Count > 0) { var buyFunction = _buyModeFunctions[itemConfig.Mode]; var itemsToBuy = buyFunction(itemConfig, currentInfo); foreach (var itemToBuy in itemsToBuy) { InventoryMonitor inventoryMonitor = null; SteamAccount altAccount = null; if (itemConfig.AltAccounts?.Count > 0) { altAccount = itemConfig.AltAccounts.FirstOrDefault(c => { var monitor = _inventoryMonitor[c.SteamID64]; if ((monitor.InventorySize + itemsToBuy.Count()) < InventoryMonitor.MaxCSGOInventorySize) { inventoryMonitor = monitor; return(true); } return(false); }); if (altAccount == null) { WriteLog(LogType.Warning, "Skipping purchase, as no alt account with enough space was found.", false); return; } } else { inventoryMonitor = _inventoryMonitor[_steamID64]; } if (inventoryMonitor.InventorySize >= InventoryMonitor.MaxCSGOInventorySize) { WriteLog(LogType.Warning, "Skipping purchase, as the main account has not enough space.", false); return; } if (_marketBalance < itemToBuy.Price) { WriteLog(LogType.Warning, "Skipping purchase, as the balance is not sufficient.", false); continue; } BuyItemResponse response; if (altAccount == null) { response = await _service.BuyItemAsync(itemToBuy.ID, itemToBuy.Price); } else { response = await _service.BuyItemForAsync(itemToBuy.ID, itemToBuy.Price, altAccount.SteamID32, altAccount.Token); } if (response?.IsSuccessfully ?? false) { string quantityLeftString = ""; if (itemConfig?.MaxQuantity > 0) { itemConfig.MaxQuantity = itemConfig.MaxQuantity - 1; quantityLeftString = ". (" + itemConfig.MaxQuantity + " left)"; if (itemConfig.MaxQuantity == 0) { itemConfig.IsActive = false; } // Update new Quantity/IsActive settings to config file ConfigService.Instance.SaveConfig(); } inventoryMonitor.InventorySize++; WriteLog(LogType.Information, "Bought '" + itemConfig.HashName + "' at " + (response?.Price ?? 0) + " " + _service.Currency + quantityLeftString, false); } } } }