private static void ProcessSell(InventoryDataObject_SHOP selected) { if (selected == null) { Control.LogDebug(DInfo.ShopActions, "-- nothing to sell, return"); return; } var price = selected.GetCBillValue(); if (selected.quantity == 1 || !Control.Settings.AllowMultiSell) { Control.LogDebug(DInfo.ShopActions, $"-- selling 1x{selected.GetName()}"); if (Control.Settings.ShowConfirm && price > Control.Settings.ConfirmLowLimit) { GenericPopupBuilder.Create("Confirm?", $"Sell {selected.GetName()} for {price}?") .AddButton("Cancel", null, true, null) .AddButton("Accept", () => OnSellItems(1), true, null) .CancelOnEscape() .AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true) .Render(); } else { OnSellItems(1); } } else { Control.LogDebug(DInfo.ShopActions, $"-- show multisell dialog"); SG_Stores_MultiPurchasePopup_Handler.StartDialog("Sell", selected.shopDefItem, selected.GetName(), selected.quantity, price, OnSellItems, null); } }
public BuyHelper(SG_Shop_Screen shopScreen, InventoryDataObject_SHOP selectedController, SimGameState simGameState) { this.shopScreen = shopScreen; this.simGameState = simGameState; shopIDO = selectedController; Traverse traverse = Traverse.Create(shopScreen).Field("inventoryWidget"); inventoryWidget = (MechLabInventoryWidget_ListView)traverse.GetValue(); }
// Clone of SoldMultipleItems public void BuyMultipleItems(int quantityBought) { Mod.Log.Debug("BH:BMI entered."); //bool isNotStoreOrSalvage = shopIDO.GetItemType() != MechLabDraggableItemType.StorePart && // shopIDO.GetItemType() != MechLabDraggableItemType.SalvagePart; //if (simGameState.InMechLabStore() || !isNotStoreOrSalvage) { // Mod.Logger.Info($"BH:BMI - in the mech lab store, or the part isn't a store part or salvage (is {shopIDO.GetItemType()}). Aborting!"); // return; //} ShopDefItem toBuySDI = shopIDO.shopDefItem; // Look for the item in the store inventory Shop itemShop = shopIDO.GetShop(); List <ShopDefItem> activeInventory = itemShop.ActiveInventory; Shop.PurchaseType storePT = toBuySDI.IsInfinite ? Shop.PurchaseType.Normal : Shop.PurchaseType.Special; ShopDefItem storeSDI = activeInventory.Find((ShopDefItem cachedItem) => cachedItem.ID == toBuySDI.ID && cachedItem.Type == shopIDO.shopDefItem.Type); if (storeSDI == null) { Mod.Log.Info("BH:BMI - item not found in store inventory. Aborting!"); return; } // Check the price int itemPrice = itemShop.GetPrice(storeSDI, storePT, itemShop.ThisShopType); Mod.Log.Info($"BH:BMI - itemPrice:{itemPrice} for purchaseType:{storePT}"); int purchasePrice = itemPrice * quantityBought; if (purchasePrice > simGameState.Funds) { Mod.Log.Info($"BH:BMI - purchasePrice:{purchasePrice} (itemPrice:{itemPrice} x quantity:{quantityBought}) > funds: {simGameState.Funds}. Aborting!"); return; } // Check the quantity available if (storePT != Shop.PurchaseType.Normal) { if (storeSDI.Count < quantityBought) { Mod.Log.Info($"BH:BMI - store has quantity {storeSDI.Count} in inventory, but {quantityBought} was requested. Aborting!"); return; } storeSDI.Count -= quantityBought; if (storeSDI.Count < 1) { Mod.Log.Debug($"BH:BMI - Store count below 1, removing!"); activeInventory.Remove(storeSDI); } Mod.Log.Debug($"BH:BMI - Reduced store count by {quantityBought} to {storeSDI.Count}!"); } for (int i = 0; i < quantityBought; i++) { // Decrement the price simGameState.AddFunds(-itemPrice, null, true, true); simGameState.AddFromShopDefItem(storeSDI, false, itemPrice, SimGamePurchaseMessage.TransactionType.Purchase); Mod.Log.Debug($"BH:BMI - Reducing funds by -{itemPrice} and adding 1 of {storeSDI.ID}"); if (!toBuySDI.IsInfinite) { if (shopIDO.quantity > 1) { shopIDO.ModifyQuantity(-1); Mod.Log.Debug($"BH:BMI - reducing shop inventory by 1 to {shopIDO.quantity}"); } else { Mod.Log.Debug($"BH:BMI - shop inventory below 1, removing!"); inventoryWidget.RemoveDataItem(shopIDO); if (shopIDO != null) { shopIDO.Pool(); } shopIDO = null; } } } inventoryWidget.RefreshInventoryList(); Mod.Log.Debug("BH:BMI - Updating all money spots"); shopScreen.UpdateMoneySpot(); shopScreen.RefreshAllMoneyListings(); if (toBuySDI.Type == ShopItemType.MechPart) { shopScreen.OnItemSelected(inventoryWidget.GetSelectedViewItem()); } Mod.Log.Debug("BH:BMI - triggering iron man save"); Traverse ironManT = Traverse.Create(shopScreen).Field("triggerIronManAutoSave"); ironManT.SetValue(true); }
public static bool Shop_Screen_ReceiveButtonPress_Prefix(SG_Shop_Screen __instance, string button, InventoryDataObject_SHOP ___selectedController, bool ___isInBuyingState, SimGameState ___simState) { Mod.Log.Debug($"SG_S_S:RBP entered with button:({button})"); State.Reset(); if (button != "Capitalism" || ___selectedController == null) { return(true); } else { int cBillValue = ___selectedController.GetCBillValue(); if (___isInBuyingState) { Mod.Log.Debug($"SG_S_S:RBP - processing a purchase."); if (___simState.InMechLabStore() && (___selectedController.GetItemType() == MechLabDraggableItemType.StorePart || ___selectedController.GetItemType() == MechLabDraggableItemType.SalvagePart)) { // TODO: Can we handle this better than HBS does? return(false); } Shop shop = ___selectedController.GetShop(); int price = shop.GetPrice(___selectedController.shopDefItem, Shop.PurchaseType.Normal, shop.ThisShopType); if (___selectedController.quantity > 1 || ___selectedController.shopDefItem.IsInfinite) { State.StoreIsBuying = true; if (___selectedController.shopDefItem.IsInfinite) { ___selectedController.quantity = 99; } BuyHelper buyHelper = new BuyHelper(__instance, ___selectedController, ___simState); int maxCanPurchase = (int)Math.Floor(___simState.Funds / (double)price); Mod.Log.Debug($"SG_S_S:RBP - maxCanPurchase:{maxCanPurchase} = funds:{___simState.Funds} / price:{price}."); int popupQuantity = maxCanPurchase < ___selectedController.quantity ? maxCanPurchase : ___selectedController.quantity; Mod.Log.Debug($"SG_S_S:RBP - maxCanPurchase:{maxCanPurchase} controllerQuantity:{___selectedController.quantity} -> popupQuantity:{popupQuantity}."); SG_Stores_MultiPurchasePopup orCreatePopupModule = LazySingletonBehavior <UIManager> .Instance.GetOrCreatePopupModule <SG_Stores_MultiPurchasePopup>(string.Empty); orCreatePopupModule.SetData(___simState, ___selectedController.shopDefItem, ___selectedController.GetName(), popupQuantity, price, buyHelper.BuyMultipleItems); } else { GenericPopupBuilder.Create("Confirm?", Strings.T("Purchase for {0}?", SimGameState.GetCBillString(price))) .AddButton("Cancel") .AddButton("Accept", __instance.BuyCurrentSelection) .CancelOnEscape() .AddFader(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill) .Render(); } } else { Mod.Log.Debug($"SG_S_S:RBP - processing a sale."); State.StoreIsSelling = true; int num = cBillValue; if (___selectedController.quantity > 1) { SG_Stores_MultiPurchasePopup orCreatePopupModule = LazySingletonBehavior <UIManager> .Instance.GetOrCreatePopupModule <SG_Stores_MultiPurchasePopup>(string.Empty); orCreatePopupModule.SetData(___simState, ___selectedController.shopDefItem, ___selectedController.GetName(), ___selectedController.quantity, num, __instance.SoldMultipleItems); } else if (num >= ___simState.Constants.Finances.ShopWarnBeforeSellingPriceMinimum) { GenericPopupBuilder.Create("Confirm?", Strings.T("Sell for {0}?", SimGameState.GetCBillString(num))) .AddButton("Cancel") .AddButton("Accept", __instance.SellCurrentSelection) .CancelOnEscape() .AddFader(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill) .Render(); } else { // Sell a single instance __instance.SellCurrentSelection(); } } return(false); } }
private static void ProcessBuy(InventoryDataObject_SHOP selected) { var item_type = selected.GetItemType(); if (Control.State.Sim.InMechLabStore() && ( item_type == MechLabDraggableItemType.StorePart || item_type == MechLabDraggableItemType.SalvagePart )) { Control.LogDebug(DInfo.ShopActions, "- cannot buy mech and parts in mechlab, return"); return; } //if (ActiveShop is IDefaultShop def_shop) ////{ // Control.LogDebug(DInfo.ShopActions, "- IDefaultShop"); // var shop = def_shop.ShopToUse; // if (shop == null) // { // Control.LogDebug(DInfo.ShopActions, "-- no shop, return"); // return; // } int price = GetPrice_Transpliters.GetPrice(null, selected.shopDefItem, Shop.PurchaseType.Normal, Shop.ShopType.System); var money = Control.State.Sim.Funds; var max_to_buy = money / price; if (!selected.shopDefItem.IsInfinite && max_to_buy > selected.quantity) { max_to_buy = selected.quantity; } Control.LogDebug(DInfo.ShopActions, $"-- money:{money} price:{price} num:{selected.quantity} max:{max_to_buy}"); if (max_to_buy == 1 || !Control.Settings.AllowMultiBuy) { if (Control.Settings.ShowConfirm && price >= Control.Settings.ConfirmLowLimit) { GenericPopupBuilder.Create("Confirm?", $"Purchase {selected.GetName()} for {price}?") .AddButton("Cancel", null, true, null) .AddButton("Accept", () => OnBuyItem(1), true, null) .CancelOnEscape() .AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true) .Render(); } else { OnBuyItem(1); } } else { SG_Stores_MultiPurchasePopup_Handler.StartDialog("Buy", selected.shopDefItem, selected.GetName(), max_to_buy, price, OnBuyItem, null); } //} //else if (ActiveShop is IListShop shop) //{ //} //else //{ // Control.LogError("- unknown type of shop, return"); //} }