/*
文件名:升级券.nut
路径:MyProject/升级券/升级券.nut
创建日期:2025-12-14	21:32
文件用途:
*/

class ItemEquipmentUpgrade {

    //配置表
    Info = null;

    //用户状态表
    UserState = null;

    //基础所需材料
    BasicMaterials = null;
    //等级倍率
    LevelRate = 1.0;
    //品级倍率
    RarityRate = 1.0;
    //禁止升级Map
    BanEquipmentMap = null;
    //特殊装备Map
    SpecialEquipmentMap = null;
    //特殊材料组
    SpecialMaterialsGroup = null;

    constructor() {
        Info = {};
        UserState = {};

        //读取PVF
        InitPvf();

        //注册客户端收包
        RegisterClient();
    }


    function RegisterClient() {
        ClientSocketPackFuncMap.rawset(21010001, function(SUser, Jso) {
            //获取背包
            local InvenObj = SUser.GetInven();
            //装备ID
            local Id = Jso.item.ItemId;
            local pvfItem = PvfItem.GetPvfItemById(Id)
            local Level = pvfItem.GetUsableLevel();

            //判断是否为异界装备  不是就返回 或者等级已经是85了也返回
            if (pvfItem.GetRarity() != 5 || Level == 85) {
                SUser.SendJso({
                    op = 21010002,
                    error = 1
                });
                return;
            }

            //普通升级装备
            else {
                local equ = PvfItem.GetPvfItemById(Id);
                local Rarity = equ.GetRarity();
                local Level = equ.GetUsableLevel();
                local RarityCalculateRate = Rarity * RarityRate;
                local LevelCalculateRate = Level * LevelRate;
                local MgArr = [];
                foreach(object in BasicMaterials) {
                    //查询自己有多少个道具
                    MgArr.push({
                        Item = object.Item,
                        RealCount = InvenObj.GetCountById(object.Item),
                        NeedCount = (object.Count * RarityCalculateRate * LevelCalculateRate).tointeger()
                    });
                }
                SUser.SendJso({
                    op = 21010002,
                    MgInfo = MgArr
                });
            }
        }.bindenv(this));

        ClientSocketPackFuncMap.rawset(21010003, function(SUser, Jso) {
            //获取背包
            local InvenObj = SUser.GetInven();
            //装备ID
            local Id = Jso.item.ItemId;
            local equ = PvfItem.GetPvfItemById(Id);
            local Level = equ.GetUsableLevel();
            local giveid = Id + 220000000;
            if (Level == 75 || Level == 80) {
                giveid = Id + 1000000;
            }

            //获取对应槽位的装备
            local SlotItem = InvenObj.GetSlot(Inven.INVENTORY_TYPE_ITEM, 9 + Jso.item.Pos);
            if (SlotItem.GetIndex() != Id) {
                SUser.SendNotiBox("          升级失败             升级时请不要操作背包", 1);
                return;
            } else {
                local equ = PvfItem.GetPvfItemById(Id);
                local Rarity = equ.GetRarity();
                local Level = equ.GetUsableLevel();
                local RarityCalculateRate = Rarity * RarityRate;
                local LevelCalculateRate = Level * LevelRate;
                local MgArr = [];
                foreach(object in BasicMaterials) {
                    //查询自己有多少个道具
                    MgArr.push({
                        Id = object.Item,
                        Count = (object.Count * RarityCalculateRate * LevelCalculateRate).tointeger()
                    });
                }

                local Flag = InvenObj.CheckArrItemCount(MgArr);
                if (!Flag) {
                    print("扣除失败");
                    SUser.SendNotiBox("          升级失败             升级时请不要操作背包", 1);
                    return;
                } else {
                    local SlogInfo = SUser.GiveItem(giveid, 1);
                    if (!SlogInfo) {
                        SUser.SendNotiBox("          升级失败             请确定背包有空余空间", 1);
                        return;
                    }

                    local giveItemObj = InvenObj.GetSlot(1, SlogInfo[1])
                    //扣除道具
                    InvenObj.DeleteArrItemCount(MgArr);

                    local forging = SlotItem.GetForging(); //锻造
                    local upgrade = SlotItem.GetUpgrade(); //强化
                    local amplification = SlotItem.GetAmplification(); //增幅
                    local enchanting = SlotItem.GetEnchanting(); //附魔

                    if (forging > 0) {
                        giveItemObj.SetForging(forging);
                    }

                    if (upgrade > 0) {
                        giveItemObj.SetUpgrade(upgrade);
                    }

                    if (amplification != 0) {
                        giveItemObj.SetAmplification(amplification);
                    }
                    giveItemObj.SetEnchanting(enchanting);
                    giveItemObj.SetAdd_Info(SlotItem.GetAdd_Info()); //品级
                    giveItemObj.Flush();

                    //销毁背包中的道具
                    SlotItem.Delete();
                    //刷新玩家背包列表
                    SUser.SendUpdateItemList(1, 0, 9 + Jso.item.Pos);
                    SUser.SendUpdateItemList(1, 0, SlogInfo[1]);

                    //刷新账号金库列表
                    SUser.SendNotiBox("          升级成功", 1);
                    SUser.SendJso({
                        op = 21010004
                    });

                }
            }
        }.bindenv(this));
    };

    function InitPvf() {
        // getroottable()._Script_Data_Init_ <- false;
        Script();
        ScriptData.GetFileData("etc/rindro/itemtool/itemotherworldupgrade/itemotherworldupgrade.etc", function(DataTable, Data) {
            while (!Data.Eof()) {
                local Fragment = Data.Get();
                if (Fragment == "[BASIC MATERIALS]") {
                    BasicMaterials = [];
                    while (true) {
                        local Fbuf = Data.Get();
                        if (Fbuf == "[/BASIC MATERIALS]") {
                            break;
                        }
                        local T = {
                            Item = Fbuf,
                            Count = Data.Get()
                        }
                        BasicMaterials.push(T);
                    }
                } else if (Fragment == "[LEVEL RATE]") {
                    LevelRate = Data.Get()
                } else if (Fragment == "[RARITY RATE]") {
                    RarityRate = Data.Get()
                }
            }
        }.bindenv(this));
    }
}





Timer.SetTimeOut(function() {

    getroottable()._ItemEquipmentUpgrade_ <- ItemEquipmentUpgrade();
    print("装备升级 - 已加载");
}, 1);