/*
文件名:装备融合.nut
路径:MyProject/装备融合/装备融合.nut
创建日期:2025-12-06	19:34
文件用途:
*/

class ItemFusionC {

    //配置表
    Info = null;

    //用户状态表
    UserState = null;

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

        //读取PVF
        InitPvf();

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

    function RegisterClient() {
        ClientSocketPackFuncMap.rawset(21006001, function(SUser, Jso) {
            local ItemInfo1 = Jso.item1;
            local ItemInfo2 = Jso.item2;
            local Key = ItemInfo1.itemId + "_" + ItemInfo2.itemId;
            local Key2 = ItemInfo2.itemId + "_" + ItemInfo1.itemId;
            local CheckFlag1 = Info.rawin(Key);
            local CheckFlag2 = Info.rawin(Key2);
            if (CheckFlag1 || CheckFlag2) {
                SUser.SendJso({
                    op = 21006002,
                    msg = 1,
                    value = Info[CheckFlag1 ? Key : Key2]
                });
                local Cid = SUser.GetCID();
                UserState.rawset(Cid, {
                    time = Sq_GetTimestampString().slice(-9).tointeger(),
                    info = Jso
                    reward = Info[CheckFlag1 ? Key : Key2]
                });
            } else {
                SUser.SendJso({
                    op = 21006002,
                    msg = 2,
                });
            }
        }.bindenv(this));

        ClientSocketPackFuncMap.rawset(21006003, function(SUser, Jso) {
            local Cid = SUser.GetCID();
            if (!UserState.rawin(Cid)) {
                SUser.SendNotiBox("          融合失败         请放入两个可融合的装备   并且融合时请不要操作背包", 1);
                SUser.SendJso({
                    op = 21006004,
                    error = true,
                });
            } else {
                //获取存储的融合信息
                local UserInfo = UserState[Cid].info;

                local ItemInfo1 = UserInfo.item1;
                local ItemInfo2 = UserInfo.item2;
                //获取需要融合的两件装备
                local InvenObj = SUser.GetInven();
                local Equ1 = InvenObj.GetSlot(Inven.INVENTORY_TYPE_ITEM, 9 + ItemInfo1.pos);
                local Equ2 = InvenObj.GetSlot(Inven.INVENTORY_TYPE_ITEM, 9 + ItemInfo2.pos);
                //判断ID 以免被移动
                if (Equ1.GetIndex() != ItemInfo1.itemId || Equ2.GetIndex() != ItemInfo2.itemId) {
                    SUser.SendNotiBox("          融合失败         请放入两个可融合的装备   并且融合时请不要操作背包", 1);
                    SUser.SendJso({
                        op = 21006004,
                        error = true,
                    });
                } else {
                    //告诉客户端播放动画
                    SUser.SendJso({
                        op = 21006004,
                    });
                    //获取信息
                    local forging = Equ1.GetForging(); //锻造
                    local upgrade = Equ1.GetUpgrade(); //强化
                    local amplification = Equ1.GetAmplification(); //增幅
                    local enchanting = Equ1.GetEnchanting(); //附魔
                    //销毁融合的两件装备
                    Equ1.Delete();
                    Equ2.Delete();
                    SUser.SendUpdateItemList(1, 0, 9 + ItemInfo1.pos);
                    SUser.SendUpdateItemList(1, 0, 9 + ItemInfo2.pos);

                    //获取融合后的装备ID
                    local reward1 = UserState[Cid].reward[0];
                    local reward2 = UserState[Cid].reward[1];

                    Timer.SetTimeOut(function() {
                        local RewardNotiList = [
                            [reward1, 1]
                        ];
                        //发送奖励
                        local Info = SUser.GiveItem(reward1, 1);
                        if (reward2 != 0) {
                            SUser.GiveItem(reward2, 1);
                            RewardNotiList.append([reward2, 1]);
                        }
                        SUser.SendItemWindowNotification(RewardNotiList);
      
                        //获取装备 并设置继承属性
                        local InvenObj = SUser.GetInven();
                        if (InvenObj) {
                            local ItemObj1 = InvenObj.GetSlot(1, Info[1])
                            if (ItemObj1) {
                                if (forging > 0) {
                                    ItemObj1.SetForging(forging);
                                }
                                if (upgrade > 0) {
                                    ItemObj1.SetUpgrade(upgrade);
                                }
                                if (amplification != 0) {
                                    ItemObj1.SetAmplification(amplification);
                                }
                                ItemObj1.SetEnchanting(enchanting);
                                ItemObj1.Flush();
                                SUser.SendUpdateItemList(1, 0, Info[1]);
                            }
                        }
                    }.bindenv(this), 2400);

                    UserState.rawdelete(Cid);
                }
            }
        }.bindenv(this));
    };


    function InitPvf() {
        Script();
        ScriptData.GetFileData("etc/rindro/itemtool/itemfusion/itemfusion.etc", function(DataTable, Data) {
            while (!Data.Eof()) {
                local Fragment = Data.Get();
                if (Fragment == "[ITEM]") {
                    while (true) {
                        local Fbuf = Data.Get();
                        if (Fbuf == "[/ITEM]") {
                            break;
                        }
                        local Item1 = Fbuf;
                        local Item2 = Data.Get();
                        local GiveItem1 = Data.Get();
                        local GiveItem2 = Data.Get();
                        local Key = Item1 + "_" + Item2;
                        local Value = [GiveItem1, GiveItem2];
                        Info.rawset(Key, Value);
                    }
                }
            }
        }.bindenv(this));
    }
}





Timer.SetTimeOut(function() {

    getroottable()._ItemFusion_ <- ItemFusionC();

    // print("融合装备已加载");
}, 1);