using Newtonsoft.Json.Linq; using XFEWarframe.Core.Data; namespace XFEWarframe.Core.Services; public static class BoosterPacks { public static JObject Draw(JArray components, JObject weights, HashSet? excluded = null) { var counts = components.GroupBy(x => x.Value("Rarity") ?? "COMMON").ToDictionary(g => g.Key, g => g.Count()); var pool = new JArray(components.OfType().Where(x => excluded?.Contains(x.Value("Item") ?? "") != true).Select(x => { var copy = (JObject)x.DeepClone(); copy["probability"] = weights.Value(x.Value("Rarity") ?? "COMMON") / counts[x.Value("Rarity") ?? "COMMON"]; return copy; }).Where(x => x.Value("probability") > 0)); return SeededRng.RewardAt(pool, Random.Shared.NextDouble()) ?? throw new ApiException(400, "Booster pack has no remaining eligible rewards"); } public static JObject Open(JObject inv, JObject meta, int count, ItemAcquisition acquire, int depth, JArray? results = null) { if (count > 100) throw new ApiException(400, "At most 100 booster packs per acquisition"); var delta = new JObject(); var components = meta["components"] as JArray ?? throw new ApiException(400, "Pack has no components"); var special = components.OfType().FirstOrDefault(x => x.Value("PityIncreaseRate") > 0); void Grant(JObject item) { var type = item.RequiredString("Item"); var amount = (int)item.Integer("Amount", 1, 100000); var level = type.EndsWith("RareModFuser", StringComparison.Ordinal) ? Random.Shared.Next(2, 6) : type.EndsWith("CommonModFuser", StringComparison.Ordinal) || type.EndsWith("UncommonModFuser", StringComparison.Ordinal) ? Random.Shared.Next(1, 4) : 0; if (level > 0) { var mod = new JObject { ["ItemId"] = Wire.Oid(Wire.NewId()), ["ItemType"] = type, ["UpgradeFingerprint"] = new JObject { ["lvl"] = level }.ToString(Newtonsoft.Json.Formatting.None) }; inv.Array("Upgrades").Add(mod); Inventory.Combine(delta, new JObject { ["Upgrades"] = new JArray(mod.DeepClone()) }); } else Inventory.Combine(delta, acquire.Add(inv, type, amount, premium: true, depth: depth + 1)); results?.Add(new JObject { ["StoreItem"] = GameData.StoreType(type), ["ItemCount"] = amount, ["Fingerprint"] = new JObject { ["lvl"] = level } }); } for (var i = 0; i < count; i++) { if (special is not null) { var pool = new JArray(components.OfType().Where(x => x.Value("PityIncreaseRate") == 0).Select(x => { var result = (JObject)x.DeepClone(); result["probability"] = x.Value("Probability"); return result; })); Grant(SeededRng.RewardAt(pool, Random.Shared.NextDouble()) ?? throw new ApiException(400, "Empty pack")); var type = special.RequiredString("Item"); if (inv.Array("WeaponSkins").Any(x => x.Value("ItemType") == type)) continue; var atten = inv.Array("SpecialItemRewardAttenuation").OfType().FirstOrDefault(x => x.Value("Tag") == type); if (atten is null) { atten = new JObject { ["Tag"] = type, ["Atten"] = special.Value("Probability") }; inv.Array("SpecialItemRewardAttenuation").Add(atten); } if (Random.Shared.NextDouble() < atten.Value("Atten")) { Grant(special); atten["Atten"] = 0; } else atten["Atten"] = atten.Value("Atten") + special.Value("PityIncreaseRate"); } else { var used = new HashSet(StringComparer.Ordinal); foreach (var weights in (meta["rarityWeightsPerRoll"] as JArray ?? []).OfType()) { var item = Draw(components, weights, used); Grant(item); if (!meta.Value("canGiveDuplicates")) used.Add(item.RequiredString("Item")); } } } return delta; } }