/*
文件名:哥布林袖珍罐.nut
路径:_DPS_/_BuiltProject/哥布林袖珍罐/哥布林袖珍罐.nut
创建日期:2026-02-06	09:37
文件用途:哥布林袖珍罐
*/

class GoblinSmallBox {

    //记录玩家缓存
    playerCache = null;

    //数据
    data = null;

    constructor() {
        playerCache = {};
        data = {};
        //读取PVF数据
        InitPvf();
        //注册道具使用回调
        RegistUseItemCallBack();


        ClientSocketPackFuncMap.rawset(21014003, function(SUser, Jso) {
            local CID = SUser.GetCID();
            if (playerCache.rawin(CID)) {
                local ItemArr = playerCache.rawget(CID);
                local ItemId = ItemArr[0].ItemId;
                local ItemCount = ItemArr[0].ItemCount;
                SUser.GiveItem(ItemId, ItemCount);
                ItemArr.remove(0);
                if (ItemArr.len() == 0) {
                    playerCache.rawdelete(CID);
                }
            }
        }.bindenv(this));
    }


    function InitPvf() {
        Script();
        ScriptData.GetFileData("etc/rindro/goldgoblin/goldgoblin.lst", function(_n, DataA) {
            while (!DataA.Eof()) {
                local Id = DataA.Get();
                local Path = DataA.Get();

                local Info = ScriptData.GetFileData("etc/" + Path, function(DataTable, Data) {
                    while (!Data.Eof()) {
                        local Fragment = Data.Get();
                        if (Fragment == "[production]") {
                            DataTable.production <- [Data.Get(), Data.Get()];
                        } else if (Fragment == "[trigger]") {
                            DataTable.trigger <- Data.Get();
                        } else if (Fragment == "[item]") {
                            DataTable.item <- [];
                            DataTable.item_total_probability <- 0;
                            while (true) {
                                local Ret = Data.Get();
                                if (Ret == "[/item]") {
                                    break;
                                }
                                local ItemInfo = {};
                                ItemInfo.ItemId <- Ret;
                                ItemInfo.ItemCount <- Data.Get();
                                ItemInfo.Probability <- Data.Get();
                                DataTable.item_total_probability += ItemInfo.Probability;
                                ItemInfo.IsNoti <- Data.Get();

                                ItemInfo.Grade <- GetItemGrade(ItemInfo.ItemId);
                                DataTable.item.push(ItemInfo);
                            }
                        }
                    }
                }.bindenv(this));
                data.rawset(Id, Info);
            }
        }.bindenv(this));
    }


    function RegistUseItemCallBack() {
        foreach(id, info in data) {
            if ("trigger" in info) {
                Cb_Use_Item_Sp_Func[info.trigger] <- function(SUser, Item_id) {
                    local Count = MathClass.Rand(data[id].production[0], data[id].production[1]);
                    local ItemArr = [];
                    for (local i = 0; i< Count; i++) {
                        local RandNum = MathClass.Rand(0, data[id].item_total_probability);
                        local ItemInfo = null;
                        for (local j = 0; j< data[id].item.len(); j++) {
                            if (RandNum< data[id].item[j].Probability) {
                                ItemInfo = clone(data[id].item[j]);
                                break;
                            }
                            RandNum -= data[id].item[j].Probability;
                        }
                        ItemInfo.rawdelete("Probability");
                        if (ItemInfo != null) {
                            ItemArr.push(ItemInfo);
                        }
                    }
                    playerCache[SUser.GetCID()] <- ItemArr;
                    SUser.SendJso({
                        op = 21014002,
                        RewardPackage = ItemArr,
                        BoxId = info.trigger,
                    })
                }.bindenv(this);
            }
        }
    }

    function GetItemGrade(ItemId) {
        local Equ = PvfItem.GetPvfItemById(ItemId);
        local Rarity = Equ.GetRarity();
        return Rarity;
    }
}



Timer.SetTimeOut(function() {

    getroottable()._GoblinSmallBox_ <- GoblinSmallBox();
    print("哥布林袖珍罐 - 已加载");
}, 1);