/*
文件名:跨界石.nut
路径:MyProject/跨界石/跨界石.nut
创建日期:2025-12-14	21:32
文件用途:
*/

class ItemCrossoverC {

    //配置表
    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(21007001, function(SUser, Jso) {
            //获取背包
            local InvenObj = SUser.GetInven();
            //装备ID
            local Id = Jso.item.ItemId;
            //判断是否为不可跨界装备
            if (BanEquipmentMap.rawin(Id)) {
                SUser.SendJso({
                    op = 21007002,
                    error = 1
                });
                return;
            }

            //判断装备是否为特殊跨界装备
            if (SpecialEquipmentMap.rawin(Id)) {
                //获取特殊跨界材料组
                local MaterialsGroup = SpecialMaterialsGroup[SpecialEquipmentMap[Id]];
                local MgArr = [];
                foreach(object in MaterialsGroup) {
                    //查询自己有多少个道具
                    MgArr.push({
                        Item = object.Item,
                        RealCount = InvenObj.GetCountById(object.Item),
                        NeedCount = object.Count
                    });
                }
                SUser.SendJso({
                    op = 21007002,
                    MgInfo = MgArr
                });
            }
            //普通跨界装备
            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 = 21007002,
                    MgInfo = MgArr
                });
            }
        }.bindenv(this));

        ClientSocketPackFuncMap.rawset(21007003, function(SUser, Jso) {
            //获取背包
            local InvenObj = SUser.GetInven();
            //装备ID
            local Id = Jso.item.ItemId;

            //获取账号金库对象
            local CargoObj = SUser.GetAccountCargo();
            //获取账号金库中的一个空格子
            local EmptySlot = CargoObj.GetEmptySlot();

            //如果没有空格子
            if (EmptySlot == -1) {
                SUser.SendNotiBox("          跨界失败             账号金库没有剩余空位", 1);
                return;
            }

            //获取对应槽位的装备
            local SlotItem = InvenObj.GetSlot(Inven.INVENTORY_TYPE_ITEM, 9 + Jso.item.Pos);
            if (SlotItem.GetIndex() != Id) {
                SUser.SendNotiBox("          跨界失败             跨界时请不要操作背包", 1);
                return;
            } else {
                local MgArr = [];
                //判断装备是否为特殊跨界装备
                if (SpecialEquipmentMap.rawin(Id)) {
                    //获取特殊跨界材料组
                    local MaterialsGroup = SpecialMaterialsGroup[SpecialEquipmentMap[Id]];
                    foreach(object in MaterialsGroup) {
                        //查询自己有多少个道具
                        MgArr.push({
                            Id = object.Item,
                            Count = object.Count
                        });
                    }
                }
                //普通跨界装备
                else {
                    //读取装备品级
                    local equ = PvfItem.GetPvfItemById(Id);
                    local Rarity = equ.GetRarity();
                    local Level = equ.GetUsableLevel();
                    local RarityCalculateRate = Rarity * RarityRate;
                    local LevelCalculateRate = Level * LevelRate;
                    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 {
                    InvenObj.DeleteArrItemCount(MgArr);
                    //跨界
                    local InsertFlag = CargoObj.InsertItem(SlotItem, EmptySlot);
                    if (InsertFlag == -1) {
                        SUser.SendNotiBox("          跨界失败             发生错误请联系管理员", 1);
                    } else {
                        //销毁背包中的道具
                        SlotItem.Delete();
                        //刷新玩家背包列表
                        SUser.SendUpdateItemList(1, 0, 9 + Jso.item.Pos);
                        //刷新账号金库列表
                        CargoObj.SendItemList();
                        SUser.SendNotiBox("          跨界成功", 1);
                        SUser.SendJso({
                            op = 21007004
                        });
                    }
                }
            }
        }.bindenv(this));
    };

    function InitPvf() {
        // Script();
        ScriptData.GetFileData("etc/rindro/itemtool/itemcrossover/itemcrossover.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()
                } else if (Fragment == "[BAN EQUIPMENT]") {
                    BanEquipmentMap = {};
                    while (true) {
                        local Fbuf = Data.Get();
                        if (Fbuf == "[/BAN EQUIPMENT]") {
                            break;
                        }
                        BanEquipmentMap.rawset(Fbuf, true);
                    }
                } else if (Fragment == "[SPECIAL EQUIPMENT]") {
                    SpecialEquipmentMap = {};
                    while (true) {
                        local Fbuf = Data.Get();
                        if (Fbuf == "[/SPECIAL EQUIPMENT]") {
                            break;
                        }
                        SpecialEquipmentMap.rawset(Fbuf, Data.Get());
                    }
                } else if (Fragment == "[SPECIAL MATERIALS GROUP]") {
                    SpecialMaterialsGroup = [];
                    while (true) {
                        local Fbuf = Data.Get();
                        if (Fbuf == "[/SPECIAL MATERIALS GROUP]") {
                            break;
                        }
                        if (Fbuf == "[ITEMS]") {
                            local ItemList = [];
                            while (true) {
                                local FSbuf = Data.Get();
                                if (FSbuf == "[/ITEMS]") {
                                    break;
                                }
                                local T = {
                                    Item = FSbuf,
                                    Count = Data.Get()
                                }
                                ItemList.push(T);
                            }
                            SpecialMaterialsGroup.push(ItemList);
                        }
                    }
                }
            }
        }.bindenv(this));
    }
}




Timer.SetTimeOut(function() {

    getroottable()._ItemCrossover_ <- ItemCrossoverC();
    print("双端插件·装备跨界 - 已加载");
}, 1);