public void NewRucksack([FromBody] RucksackDataRequest value) { if (value == null || !ModelState.IsValid) { Debug.WriteLine("POST get bad value"); } using (ApplicationContext db = new ApplicationContext()) { Rucksack NewRucksack = new Rucksack { Name = value.NameRucksack, Weight = value.WeightRucksack, Cost = 0, Status = false }; db.Rucksacks.Add(NewRucksack); db.SaveChanges(); foreach (ThingDataRequest thing in value.Things) { Thing NewThing = new Thing { Name = thing.Name, Weight = thing.Weight, Cost = thing.Cost, Put = false, Rucksack = NewRucksack }; db.Things.AddRange(new List <Thing> { NewThing }); } db.SaveChanges(); MainThread.CreateThread(NewRucksack.Id); } }
private void HandlePrimaryAction() { if ((IsLeftSidebarVisible || IsRightSidebarVisible) && HoveredButton.HasValue) { if (IsLeftSidebarVisible && HoveredButton.Value == SidebarButton.DepositAll) { List <Object> ToDeposit = InventorySource.Where(x => x != null && x is Object Obj && Bag.IsValidBagObject(Obj)).Cast <Object>().ToList(); Bag.MoveToBag(ToDeposit, ToDeposit.Select(x => x.Stack).ToList(), out int TotalMovedQty, true, InventorySource); } else if (IsLeftSidebarVisible && HoveredButton.Value == SidebarButton.WithdrawAll) { List <Object> ToWithdraw = Bag.Contents.Where(x => x != null).ToList(); Bag.MoveFromBag(ToWithdraw, ToWithdraw.Select(x => x.Stack).ToList(), out int TotalMovedQty, true, InventorySource, ActualInventoryCapacity); } else if (IsLeftSidebarVisible && HoveredButton.Value == SidebarButton.Autoloot) { if (Bag is BoundedBag BB) { BB.Autofill = !BB.Autofill; } else if (Bag is Rucksack RS) { RS.CycleAutofill(); } } else if (IsRightSidebarVisible && HoveredButton.Value == SidebarButton.HelpInfo) { } else if (IsRightSidebarVisible && HoveredButton.Value == SidebarButton.CustomizeIcon && Bag.CanCustomizeIcon()) { ItemBag Copy; if (Bag is BoundedBag BB) { if (BB is BundleBag) { Copy = new BundleBag(Bag.Size, false); } else { Copy = new BoundedBag(BB.TypeInfo, Bag.Size, false); } } else if (Bag is Rucksack RS) { Copy = new Rucksack(Bag.Size, false); } else if (Bag is OmniBag OB) { Copy = new OmniBag(Bag.Size); } else { throw new NotImplementedException(string.Format("Unexpected Bag Type while creating CustomizeIconMenu: {0}", Bag.GetType().ToString())); } CustomizeIconMenu = new CustomizeIconMenu(this, this.Bag, Copy, 24); } } }
internal bool TryDecode(out ItemBag Decoded) { // Handle BundleBags if (this.TypeId == BundleBag.BundleBagTypeId) { Decoded = new BundleBag(this); return(true); } // Handle Rucksacks else if (this.TypeId == Rucksack.RucksackTypeId) { Decoded = new Rucksack(this); return(true); } // Handle OmniBags else if (this.TypeId == OmniBag.OmniBagTypeId) { Decoded = new OmniBag(this); return(true); } // Handle all other types of Bags else if (ItemBagsMod.BagConfig.IndexedBagTypes.TryGetValue(this.TypeId, out BagType BagType)) { BagSizeConfig SizeConfig = BagType.SizeSettings.FirstOrDefault(x => x.Size == this.Size); if (SizeConfig != null) { Decoded = new BoundedBag(BagType, this); return(true); } else { string Warning = string.Format("Warning - BagType with Id = {0} was found, but it does not contain any settings for Size={1}. Did you manually edit your {2} json file? The saved bag cannot be loaded without the corresponding settings for this size!", this.TypeId, this.Size.ToString(), ItemBagsMod.BagConfigDataKey); ItemBagsMod.ModInstance.Monitor.Log(Warning, LogLevel.Warn); Decoded = null; return(false); } } else { string Warning = string.Format("Warning - no BagType with Id = {0} was found. Did you manually edit your {1} json file or delete a .json file from 'Modded Bags' folder? The saved bag cannot be loaded without a corresponding type!", this.TypeId, ItemBagsMod.BagConfigDataKey); ItemBagsMod.ModInstance.Monitor.Log(Warning, LogLevel.Warn); Decoded = null; return(false); } }
public RucksackMenu(ItemBagMenu IBM, Rucksack Bag, int Columns, int SlotSize, bool ShowLockedSlots, int Padding) { this.IBM = IBM; this.Rucksack = Bag; Bag.OnContentsChanged += Bag_ContentsChanged; this.Padding = Padding; this.ColumnCount = Math.Min(Rucksack.NumSlots, Columns); this.OriginalSlotSize = SlotSize; this.SlotSize = SlotSize; this.ShowLockedSlots = ShowLockedSlots; this.IsRightSidebarVisible = true; InitializePlaceholders(); SetTopLeft(Point.Zero, false); InitializeLayout(1); }
private static void RegisterAddRucksackCommand() { List <string> ValidSizes = Enum.GetValues(typeof(ContainerSize)).Cast <ContainerSize>().Select(x => x.ToString()).ToList(); //Possible TODO: Add translation support for this command string CommandName = Constants.TargetPlatform == GamePlatform.Android ? "addrucksack" : "player_addrucksack"; string CommandHelp = string.Format("Adds an empty Rucksack of the desired size to your inventory.\n" + "Arguments: <BagSize>\n" + "Example: {0} Large\n\n" + "Valid values for <BagSize>: {1}\n\n", CommandName, string.Join(", ", ValidSizes)); Helper.ConsoleCommands.Add(CommandName, CommandHelp, (string Name, string[] Args) => { if (Game1.player.isInventoryFull()) { Monitor.Log("Unable to execute command: Inventory is full!", LogLevel.Alert); } else if (Args.Length < 1) { Monitor.Log("Unable to execute command: Required arguments missing!", LogLevel.Alert); } else { string SizeName = Args[0]; if (!Enum.TryParse(SizeName, out ContainerSize Size) || !ValidSizes.Contains(SizeName)) { Monitor.Log(string.Format("Unable to execute command: <BagSize> \"{0}\" is not valid. Expected valid values: {1}", SizeName, string.Join(", ", ValidSizes)), LogLevel.Alert); } else { try { Rucksack NewBag = new Rucksack(Size, true); Game1.player.addItemToInventory(NewBag); } catch (Exception ex) { Monitor.Log(string.Format("ItemBags: Unhandled error while executing command: {0}", ex.Message), LogLevel.Error); } } } }); }
public static void MonsterDrop_Postfix(GameLocation __instance, Monster monster, int x, int y, Farmer who) { try { MonsterLootSettings LootSettings = MonsterLootSettings; if (who.UniqueMultiplayerID == Game1.player.UniqueMultiplayerID && LootSettings.CanReceiveBagsAsDrops) { // Roll chance at receiving an ItemBag double Chance = LootSettings.GetItemBagDropChance(__instance, monster, out double BaseChance, out double LocationMultiplier, out double ExpMultiplier, out double HPMultiplier); bool Success = Randomizer.NextDouble() <= Chance; string LogMessage; if (Success) { if (MonsterLootSettings.LogDropChancesToConsole) { LogMessage = string.Format("Succeeded drop chance: Location = {0}, monster.ExperienceGained = {1}, monster.MaxHealth = {2}\n" + "BaseChance = {3} ({4}%), LocationMultiplier = {5} (+{6}%), ExpMultiplier = {7}, HPMultiplier = {8} (+{9}%), TotalChance = {10} ({11}%)", __instance.Name, monster.ExperienceGained, monster.MaxHealth, BaseChance, (BaseChance * 100.0).ToString("0.##"), LocationMultiplier, ((LocationMultiplier - 1.0) * 100.0).ToString("0.##"), ExpMultiplier.ToString("#.####"), HPMultiplier, ((HPMultiplier - 1.0) * 100.0).ToString("0.##"), Chance, (Chance * 100.0).ToString("0.###")); Monitor.Log(LogMessage, LogLevel.Info); } int SpawnDirection = Randomizer.Next(4); List <ItemBag> OwnedBags = ItemBag.GetAllBags(true); // Compute the odds of receiving each type of bag bool CanReceiveRucksack = LootSettings.RucksackDropSettings.SizeWeights.Any(size => size.Value > 0); int RucksackWeight = CanReceiveRucksack ? LootSettings.RucksackDropSettings.TypeWeight : 0; bool CanReceiveOmniBag = LootSettings.OmniBagDropSettings.SizeWeights.Any(size => size.Value > 0); int OmniBagWeight = CanReceiveOmniBag ? LootSettings.OmniBagDropSettings.TypeWeight : 0; bool CanReceiveBundleBag = LootSettings.BundleBagDropSettings.SizeWeights.Any(size => BundleBag.ValidSizes.Contains(size.Key) && size.Value > 0 && !IsSizeObsolete(OwnedBags, BundleBag.BundleBagTypeId, size.Key)); int BundleBagWeight = CanReceiveBundleBag ? LootSettings.BundleBagDropSettings.TypeWeight : 0; bool CanReceiveStandardBag = LootSettings.StandardBagDropSettings.SizeWeights.Any(size => size.Value > 0); int StandardBagWeight = CanReceiveStandardBag ? LootSettings.StandardBagDropSettings.TypeWeight : 0; int TotalTypeWeight = RucksackWeight + OmniBagWeight + BundleBagWeight + StandardBagWeight; if (TotalTypeWeight > 0) { ItemBag ChosenBag; // Pick the type of bag to spawn (Rucksack, OmniBag, BundleBag or a standard BoundedBag) int ChosenTypeWeight = Randomizer.Next(0, TotalTypeWeight); if (ChosenTypeWeight < RucksackWeight) { ContainerSize CurrentSize = GetWeightedRandomSize(LootSettings.RucksackDropSettings.SizeWeights); // Try to force a non-obsolete bag to spawn if (RollDice(LootSettings.ForceNewBagTypeChance)) { List <ContainerSize> ValidSizes = LootSettings.RucksackDropSettings.SizeWeights.Where(size => size.Value > 0).Select(size => size.Key).ToList(); ContainerSize MaxSize = ValidSizes.DefaultIfEmpty(ContainerSize.Small).Max(); while (CurrentSize < MaxSize && IsSizeObsolete(OwnedBags, Rucksack.RucksackTypeId, CurrentSize)) { CurrentSize = ValidSizes.Where(size => size > CurrentSize).OrderBy(size => size).First(); } } // Spawn a Rucksack ChosenBag = new Rucksack(CurrentSize, false); } else if (ChosenTypeWeight < RucksackWeight + OmniBagWeight) { ContainerSize CurrentSize = GetWeightedRandomSize(LootSettings.OmniBagDropSettings.SizeWeights); // Try to force a non-obsolete bag to spawn if (RollDice(LootSettings.ForceNewBagTypeChance)) { List <ContainerSize> ValidSizes = LootSettings.OmniBagDropSettings.SizeWeights.Where(size => size.Value > 0).Select(size => size.Key).ToList(); ContainerSize MaxSize = ValidSizes.DefaultIfEmpty(ContainerSize.Small).Max(); while (CurrentSize < MaxSize && IsSizeObsolete(OwnedBags, OmniBag.OmniBagTypeId, CurrentSize)) { CurrentSize = ValidSizes.Where(size => size > CurrentSize).OrderBy(size => size).First(); } } // Spawn an OmniBag ChosenBag = new OmniBag(CurrentSize); } else if (ChosenTypeWeight < RucksackWeight + OmniBagWeight + BundleBagWeight) { // Spawn a BundleBag ContainerSize Size = GetWeightedRandomSize(LootSettings.BundleBagDropSettings.SizeWeights.Where(size => BundleBag.ValidSizes.Contains(size.Key) && !IsSizeObsolete(OwnedBags, BundleBag.BundleBagTypeId, size.Key))); ChosenBag = new BundleBag(Size, true); } else { ContainerSize CurrentSize = GetWeightedRandomSize(LootSettings.StandardBagDropSettings.SizeWeights); // Get all standard BagTypes that are available in the chosen size List <BagType> StandardTypes = ItemBagsMod.BagConfig.BagTypes.Where(type => type.SizeSettings.Any(sizeCfg => sizeCfg.Size == CurrentSize)).ToList(); // Try to force a non-obsolete bag to spawn if (RollDice(LootSettings.ForceNewBagTypeChance)) { StandardTypes.RemoveAll(type => IsSizeObsolete(OwnedBags, type.Id, CurrentSize)); // If all bag types were obsolete, then keep incrementing the size until we find a non-obsolete bag to spawn if (!StandardTypes.Any()) { List <ContainerSize> ValidSizes = LootSettings.StandardBagDropSettings.SizeWeights.Where(size => size.Value > 0).Select(size => size.Key).ToList(); ContainerSize MaxSize = ValidSizes.DefaultIfEmpty(ContainerSize.Small).Max(); while (CurrentSize < MaxSize && !StandardTypes.Any()) { CurrentSize = ValidSizes.Where(size => size > CurrentSize).OrderBy(size => size).First(); StandardTypes = ItemBagsMod.BagConfig.BagTypes.Where(type => type.SizeSettings.Any(sizeCfg => sizeCfg.Size == CurrentSize) && !IsSizeObsolete(OwnedBags, type.Id, CurrentSize)).ToList(); } } } if (StandardTypes.Any()) { // Spawn a standard BoundedBag int ChosenTypeIndex = Randomizer.Next(StandardTypes.Count); ChosenBag = new BoundedBag(StandardTypes[ChosenTypeIndex], CurrentSize, false); } else { ChosenBag = null; } } if (ChosenBag != null) { Game1.createItemDebris(ChosenBag, Game1.player.getStandingPosition(), SpawnDirection, null, -1); } } } else if (MonsterLootSettings.LogDropChancesToConsole) { LogMessage = string.Format("Failed drop chance: Location = {0}, monster.ExperienceGained = {1}, monster.MaxHealth = {2}\n" + "BaseChance = {3} ({4}%), LocationMultiplier = {5} (+{6}%), ExpMultiplier = {7}, HPMultiplier = {8} (+{9}%), TotalChance = {10} ({11}%)", __instance.Name, monster.ExperienceGained, monster.MaxHealth, BaseChance, (BaseChance * 100.0).ToString("0.##"), LocationMultiplier, ((LocationMultiplier - 1.0) * 100.0).ToString("0.##"), ExpMultiplier.ToString("#.####"), HPMultiplier, ((HPMultiplier - 1.0) * 100.0).ToString("0.##"), Chance, (Chance * 100.0).ToString("0.###")); Monitor.Log(LogMessage, LogLevel.Info); } } } catch (Exception ex) { Monitor.Log(string.Format("Unhandled Error in {0}:\n{1}", nameof(MonsterDrop_Postfix), ex), LogLevel.Error); } }
private void Display_MenuChanged(object sender, MenuChangedEventArgs e) { // Refresh completed Bundles in the community center if (e.OldMenu != null && e.OldMenu is JunimoNoteMenu) { CommunityCenterBundles.Instance = new CommunityCenterBundles(); } if (e.NewMenu is ShopMenu SM) { // Determine if the shop menu belongs to one of our managed shops bool IsModifiableShop = false; BagShop BagShop = BagShop.Pierre; if (SM.portraitPerson?.Name != null) { //TODO test if the Stardew Valley Expanded shops like Isaac/Sophia/Alesia have non-null values for ShopMenu.portraitPerson.Name if (Enum.TryParse(SM.portraitPerson.Name, out BagShop)) { IsModifiableShop = true; } } else if (SM.storeContext != null) { if (SM.storeContext.Equals("Forest", StringComparison.CurrentCultureIgnoreCase)) { if (SM.onPurchase?.GetMethodInfo().Name == "onTravelingMerchantShopPurchase") // nameof(Utility.onTravelingMerchantShopPurchase) { BagShop = BagShop.TravellingCart; } else { BagShop = BagShop.HatMouse; } IsModifiableShop = true; } else if (SM.storeContext.Equals("Town", StringComparison.CurrentCultureIgnoreCase) && SM.potraitPersonDialogue != null && SM.potraitPersonDialogue.Contains("Khadija")) { BagShop = BagShop.Khadija; IsModifiableShop = true; } } // Add Bag items to the shop's stock if (IsModifiableShop) { Dictionary <ISalable, int[]> Stock = SM.itemPriceAndStock; bool ShouldModifyStock = true; if (BagShop == BagShop.Clint) { // Assume user is viewing Clint tool upgrades if the stock doesn't contain Coal if (!Stock.Any(x => x.Key is Object Obj && Obj.Name.Equals("Coal", StringComparison.CurrentCultureIgnoreCase))) { ShouldModifyStock = false; } } if (ShouldModifyStock) { bool HasChangedStock = false; List <ItemBag> OwnedBags = UserConfig.HideObsoleteBagsFromShops ? ItemBag.GetAllBags(true) : new List <ItemBag>(); // Add Bounded Bags to stock foreach (BagType Type in BagConfig.BagTypes) { foreach (BagSizeConfig SizeCfg in Type.SizeSettings) { bool IsSoldByShop = UserConfig.IsSizeVisibleInShops(SizeCfg.Size) && SizeCfg.Sellers.Contains(BagShop); #if DEBUG //IsSoldByShop = true; #endif if (IsSoldByShop) { bool IsObsolete = false; if (UserConfig.HideObsoleteBagsFromShops) { IsObsolete = OwnedBags.Any(x => x is BoundedBag BB && BB.TypeInfo == Type && (int)BB.Size > (int)SizeCfg.Size); } if (!IsObsolete) { BoundedBag SellableInstance = new BoundedBag(Type, SizeCfg.Size, false); int Price = SellableInstance.GetPurchasePrice(); #if DEBUG //Price = 1 + (int)SizeCfg.Size; #endif Stock.Add(SellableInstance, new int[] { Price, ShopMenu.infiniteStock }); HasChangedStock = true; } } } } // Add Bundle Bags to stock foreach (BundleBagSizeConfig SizeCfg in UserConfig.BundleBagSettings) { ContainerSize Size = SizeCfg.Size; if (BundleBag.ValidSizes.Contains(Size) && SizeCfg.Sellers.Contains(BagShop) && UserConfig.IsSizeVisibleInShops(Size)) { bool IsObsolete = false; if (UserConfig.HideObsoleteBagsFromShops) { IsObsolete = OwnedBags.Any(x => x is BundleBag BB && (int)BB.Size > (int)Size); } if (!IsObsolete) { BundleBag BundleBag = new BundleBag(Size, true); int Price = BundleBag.GetPurchasePrice(); Stock.Add(BundleBag, new int[] { Price, ShopMenu.infiniteStock }); HasChangedStock = true; } } } // Add Rucksacks to stock foreach (RucksackSizeConfig SizeCfg in UserConfig.RucksackSettings) { ContainerSize Size = SizeCfg.Size; if (SizeCfg.Sellers.Contains(BagShop) && UserConfig.IsSizeVisibleInShops(Size)) { bool IsObsolete = false; if (UserConfig.HideObsoleteBagsFromShops) { IsObsolete = OwnedBags.Any(x => x is Rucksack RS && (int)RS.Size > (int)Size); } if (!IsObsolete) { Rucksack Rucksack = new Rucksack(Size, false, AutofillPriority.Low); int Price = Rucksack.GetPurchasePrice(); Stock.Add(Rucksack, new int[] { Price, ShopMenu.infiniteStock }); HasChangedStock = true; } } } // Add Omni Bags to stock foreach (OmniBagSizeConfig SizeCfg in UserConfig.OmniBagSettings) { ContainerSize Size = SizeCfg.Size; if (SizeCfg.Sellers.Contains(BagShop) && UserConfig.IsSizeVisibleInShops(Size)) { bool IsObsolete = false; if (UserConfig.HideObsoleteBagsFromShops) { IsObsolete = OwnedBags.Any(x => x is OmniBag OB && (int)OB.Size > (int)Size); } if (!IsObsolete) { OmniBag OmniBag = new OmniBag(Size); int Price = OmniBag.GetPurchasePrice(); Stock.Add(OmniBag, new int[] { Price, ShopMenu.infiniteStock }); HasChangedStock = true; } } } if (HasChangedStock) { SM.setItemPriceAndStock(Stock); } } } } }