XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
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<string>? excluded = null)
    {
        var counts = components.GroupBy(x => x.Value<string>("Rarity") ?? "COMMON").ToDictionary(g => g.Key, g => g.Count());
        var pool = new JArray(components.OfType<JObject>().Where(x => excluded?.Contains(x.Value<string>("Item") ?? "") != true).Select(x => { var copy = (JObject)x.DeepClone(); copy["probability"] = weights.Value<double>(x.Value<string>("Rarity") ?? "COMMON") / counts[x.Value<string>("Rarity") ?? "COMMON"]; return copy; }).Where(x => x.Value<double>("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<JObject>().FirstOrDefault(x => x.Value<double>("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<JObject>().Where(x => x.Value<double>("PityIncreaseRate") == 0).Select(x => { var result = (JObject)x.DeepClone(); result["probability"] = x.Value<double>("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<string>("ItemType") == type)) continue;
                var atten = inv.Array("SpecialItemRewardAttenuation").OfType<JObject>().FirstOrDefault(x => x.Value<string>("Tag") == type);
                if (atten is null) { atten = new JObject { ["Tag"] = type, ["Atten"] = special.Value<double>("Probability") }; inv.Array("SpecialItemRewardAttenuation").Add(atten); }
                if (Random.Shared.NextDouble() < atten.Value<double>("Atten")) { Grant(special); atten["Atten"] = 0; } else atten["Atten"] = atten.Value<double>("Atten") + special.Value<double>("PityIncreaseRate");
            }
            else
            {
                var used = new HashSet<string>(StringComparer.Ordinal);
                foreach (var weights in (meta["rarityWeightsPerRoll"] as JArray ?? []).OfType<JObject>()) { var item = Draw(components, weights, used); Grant(item); if (!meta.Value<bool>("canGiveDuplicates")) used.Add(item.RequiredString("Item")); }
            }
        }
        return delta;
    }
}