// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Utils from "./module/Pay/Utils";
import { MiniGameManager } from "./Sdk/MiniGameManager";
import { MiniGameSdk } from "./Sdk/MiniGameSdk";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';
    static _instance: GameManager = null;

    @property({ type: [cc.Prefab], tooltip: "方块数组" })
    Block_Array: Array<cc.Prefab> = [];

    @property({ type: [cc.Prefab], tooltip: "墙壁数组" })
    Wall_Prefab: Array<cc.Prefab> = [];

    @property({ type: [cc.SpriteAtlas], tooltip: "方块颜色" })
    Block_Color: Array<cc.SpriteAtlas> = [];

    particleEffects: cc.ParticleAsset[];
    // @property({type: [cc.ParticleSystem], tooltip:"粒子数组"})
    // particleEffects : Array<cc.ParticleSystem> = [];

    load1: boolean = false;
    load2: boolean = false;
    load3: boolean = false;
    load4: boolean = false;
    load5: boolean = false;
    load6: boolean = false;
    scheduleCallback: any;
    timeNumber: number;


    // LIFE-CYCLE CALLBACKS:

    onLoad() {

        window.initMgr();
        this.timeNumber = 1;
        this.startTimeCutDown();
        cc.fx.GameConfig.init(true);
        cc.fx.GameConfig.GM_INFO.gameState = false;
        this.readMusicConfig();


        this.load1 = this.load2 = this.load3 = this.load4 = this.load5 = this.load6 = false;
        setTimeout(() => {
            this.readUserData();
        }, 200);


        if (GameManager._instance == null) {
            GameManager._instance = this;
            cc.game.addPersistRootNode(this.node);
        }
        else {
            return;
        }

        const path = 'prefab/block';
        const path2 = 'prefab/wall';
        cc.resources.loadDir(path, cc.Prefab, (err, assets: cc.Prefab[]) => {
            if (err) {
                console.log('动态加载 block 失败:', err);
                cc.director.loadScene("LoadScene");
                return;
            }
            // 将加载的 Prefab 赋值给 Block_Array
            this.Block_Array = assets;
            console.log('方块加载成功，共加载了', this.Block_Array.length, '个方块');
            this.setSort();
            this.load1 = true;
        });

        cc.resources.loadDir(path2, cc.Prefab, (err, assets: cc.Prefab[]) => {
            if (err) {
                console.log('动态加载 wall 失败:', err);
                cc.director.loadScene("LoadScene");
                return;
            }
            // 将加载的 Prefab 赋值给 Block_Array
            this.Wall_Prefab = assets;
            this.load2 = true;
            console.log('墙加载成功，共加载了', this.Wall_Prefab.length, '个墙');
            this.setWallPrefabSort();
        });


        // 检测微信小游戏切到后台
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            //@ts-ignore
            wx.onHide(() => {
                this.onHide();
            });
            // 检测微信小游戏回到前台
            //@ts-ignore
            wx.onShow(() => {
                this.onShow();
            });
        }

    }

    onHide() {
        cc.audioEngine.stopMusic();
        cc.game.pause();
    }

    onShow() {
        cc.audioEngine.resumeMusic();
        cc.game.resume();
    }

    loadParticleEffects() {
    }




    setWallPrefabSort() {
        const order = ['down', 'downLeft', 'downRight', 'left', 'right', 'up', 'upLeft', 'upRight'];
        this.Wall_Prefab.sort((a, b) => {
            const indexA = order.indexOf(a.name);
            const indexB = order.indexOf(b.name);
            if (indexA === -1) return 1;
            if (indexB === -1) return -1;
            return indexA - indexB;
        });

    }

    setParticleSort() {
        const order = ['top', 'bot', 'rig', 'lef'];
        this.particleEffects.sort((a, b) => {
            const indexA = order.indexOf(a.name.substr(0, 3));
            const indexB = order.indexOf(b.name.substr(0, 3));
            if (indexA === -1) return 1;
            if (indexB === -1) return -1;
            return indexA - indexB;
        });
    }

    setSort() {
        this.Block_Array.sort((a, b) => {
            // 从名称中提取数字部分
            const numberA = parseInt(a.name.match(/\d+/)?.[0] || '0', 10);
            const numberB = parseInt(b.name.match(/\d+/)?.[0] || '0', 10);
            return numberA - numberB;
        });
    }

    start() {

    }

    startGame() {
        console.log("进入场景之前_____________", cc.fx.GameConfig.GM_INFO.first);
        // 加载 music bundle
        cc.assetManager.loadBundle('music', (err, bundle) => {
            if (err) {
                console.error('加载 music bundle 失败:', err);
                // 加载失败时仍尝试进入 HomeScene
                cc.director.loadScene("HomeScene");
            } else {
                console.log('music bundle 加载成功');
                // 加载成功后进入 HomeScene
                cc.director.loadScene("HomeScene");
            }
        });
    }

    returnHome() {
        cc.tween(this.node.getChildByName("Game"))
            .to(0.5, { opacity: 100 })
            .call(() => {

                // 预加载成功后加载场景
                cc.director.loadScene("HomeScene", (err) => {
                    if (err) {
                        console.error('加载 HomeScene 场景失败:', err);
                    } else {
                        cc.director.loadScene("HomeScene");
                    }
                });
            })
            .start();

        cc.tween(this.node.getChildByName("mask"))
            .to(0.5, { opacity: 255 })
            .start();

        // cc.loader.releaseAll();
        // 预加载 HomeScene 场景


    }


    readUserData(retryCount = 0) {
        //@ts-ignore
        if (typeof wx !== 'undefined' && wx !== null) {
            MiniGameSdk.API.shushu_Init();
            Utils.getUserInfo((data) => {
                console.log("登陆成功", data);
                if (data.data.outTradeNo.length > 0) {
                    console.log("设置补发数据", data.data.outTradeNo);
                    cc.fx.GameConfig.GM_INFO.allOutTradeNo = [];
                    cc.fx.GameConfig.GM_INFO.allOutTradeNo = data.data.outTradeNo;
                    console.log("______________________________有未发放奖励", cc.fx.GameConfig.GM_INFO.allOutTradeNo);
                }
                this.load6 = true;
            });
            let levelInfo = cc.fx.StorageMessage.getStorage("level");
            //旧的读取数据设置数据方法,以强联网为主
            if (levelInfo == undefined || levelInfo == "" || levelInfo == null) {
                this.oldReadData(retryCount);
            }
            //新的读取数据设置方法，以本地为主
            else {
                this.newReadData();
            }
            console.log("即将读取体力值");
            cc.fx.GameTool.getHealth((data) => {
                console.log("体力值完成进入游戏");
                this.load5 = true;
            });

        }
        else {
            this.load3 = true;
            this.load4 = true;
            this.load5 = true;
            this.load6 = true;
            cc.fx.GameTool.getHealth(null);
        }
        // 存储用户数据
    }

    readMusicConfig() {
        let audioInfo = cc.fx.StorageMessage.getStorage("music");
        if (audioInfo == undefined || audioInfo == "" || audioInfo == null) {
            audioInfo = {
                "musicOpen": true,             //音乐
                "effectOpen": true,            //音效
                "vibrateOpen": true,           //震动
            }
            cc.fx.StorageMessage.setStorage("music", audioInfo);
        }
        else {
            cc.fx.GameConfig.GM_INFO.musicOpen = audioInfo.musicOpen;
            cc.fx.GameConfig.GM_INFO.effectOpen = audioInfo.effectOpen;
            cc.fx.GameConfig.GM_INFO.vibrateOpen = audioInfo.vibrateOpen;
        }
    }

    //新用户，有本地缓存读取配置
    newReadData() {
        console.log("读取新信息2");
        let openid = cc.fx.StorageMessage.getStorage("openid");
        if (openid == null || openid == "" || openid == undefined) {
            console.log("没有openid");
            //@ts-ignore
            wx.cloud.callFunction({
                name: 'userData',
                data: {
                    action: 'read'
                },
                success: res => {
                    console.log('读取用户数据成功_____________________', res.result)
                    if (res.result.code == 404 && res.result.message == "未找到用户数据") {
                    }
                    else if (res.result.code == 200) {
                        if (res.result.data) {
                            cc.fx.GameConfig.GM_INFO.openid = res.result.openid;
                            cc.fx.GameConfig.GM_INFO.username = res.result.data.username;
                            cc.fx.GameConfig.GM_INFO.useravatar = res.result.data.useravatar;
                            const register_time = res.result.data.register_time;
                            MiniGameSdk.API.shushu_userSet(register_time);
                            cc.fx.StorageMessage.setStorage("openid", cc.fx.GameConfig.GM_INFO.openid);
                        }
                    }
                },
                fail: err => {
                    console.error('读取用户数据失败', err)
                }
            })
        }
        else {
            if (openid)
                cc.fx.GameConfig.GM_INFO.openid = openid;
        }



        //等级信息
        cc.fx.GameTool.getUserLevel((data) => {
            const timestamp = Date.now();
            let levelInfo = cc.fx.StorageMessage.getStorage("level");
            if (data.result.code == 404 && data.result.message == "未找到关卡数据") {
                // console.log("没有等级信息，从用户接口拿到数据");
                if (levelInfo.level) {
                    cc.fx.GameConfig.GM_INFO.level = levelInfo.level;
                }
                this.load4 = true;
                cc.fx.GameTool.setUserLevel((data) => {
                });
            }
            else if (data.result.code == 200) {
                // console.log("有等级信息，从关卡接口拿到数据",data.result.data);
                //游戏前端存储 新于服务器端,以游戏前端信息为主，放服务器存储
                let temp = data.result.timestamp - levelInfo.timestamp;
                console.log("等级时间差:", temp);
                if ((data.result.timestamp - levelInfo.timestamp) < 10000) {
                    if (levelInfo.level) {
                        console.log("以游戏前端等级为准", data.result.data);
                        cc.fx.GameConfig.GM_INFO.level = levelInfo.level;
                        // console.log("等级为:",cc.fx.GameConfig.GM_INFO.level);
                        cc.fx.GameTool.setUserLevel((data) => {
                        });
                    }

                }
                //服务器端存储时间新于游戏端，以服务器端为主,往前端存储
                else {
                    console.log("以服务器等级为准", data.result.data);
                    cc.fx.GameConfig.GM_INFO.level = data.result.data;
                    levelInfo.level = cc.fx.GameConfig.GM_INFO.level;
                    levelInfo.timestamp = timestamp;
                    // console.log("1111111存储关卡数据:",cc.fx.GameConfig.GM_INFO.level);
                    cc.fx.StorageMessage.setStorage("level", levelInfo);
                }
                this.load4 = true;
            }
        });
        //金币信息
        cc.fx.GameTool.getUserCoin((data) => {
            const timestamp = Date.now();
            let coinInfo = cc.fx.StorageMessage.getStorage("coin");
            if (data.result.code == 404 && data.result.message == "未找到金币数据") {
                if (coinInfo.coin)
                    cc.fx.GameConfig.GM_INFO.coin = coinInfo.coin;
                console.log("没有金币信息，从用户接口拿到数据", cc.fx.GameConfig.GM_INFO.coin);
                this.load3 = true;
                cc.fx.GameTool.setUserCoin((data) => {
                });
            }
            else if (data.result.code == 200) {
                // console.log("有金币信息，从金币接口拿到数据",data.result.data);
                //游戏前端存储 新于服务器端,以游戏前端信息为主，放服务器存储
                let temp = data.result.timestamp - coinInfo.timestamp;
                console.log("金币时间差:", temp);
                if ((data.result.timestamp - coinInfo.timestamp) < 10000) {
                    console.log("以前端金幣为准:", coinInfo);
                    if (coinInfo.coin)
                        cc.fx.GameConfig.GM_INFO.coin = coinInfo.coin;
                    else {
                        cc.fx.GameConfig.GM_INFO.coin = data.result.data;
                        coinInfo.coin = cc.fx.GameConfig.GM_INFO.coin;
                        coinInfo.timestamp = timestamp;
                        console.log("11111111111");
                        cc.fx.StorageMessage.setStorage("coin", coinInfo);
                    }
                    console.log("改变后金币:", cc.fx.GameConfig.GM_INFO.coin);
                    cc.fx.GameTool.setUserCoin((data) => {
                    });
                }
                //服务器端存储时间新于游戏端，以服务器端为主,往前端存储
                else {
                    console.log("以服务器金幣为准:", data.result.data);
                    cc.fx.GameConfig.GM_INFO.coin = data.result.data;
                    coinInfo.coin = cc.fx.GameConfig.GM_INFO.coin;
                    coinInfo.timestamp = timestamp;
                    console.log("2222222222");
                    cc.fx.StorageMessage.setStorage("coin", coinInfo);
                }
                this.load3 = true;
            }
        });
        //道具信息
        cc.fx.GameTool.getUserProp((data) => {
            const timestamp = Date.now();
            let propInfo = cc.fx.StorageMessage.getStorage("prop");
            if (data.result.code == 404 && data.result.message == "未找到道具数据") {
                console.log("没有道具信息，从用户接口拿到数据", propInfo);
                if (propInfo.freezeAmount == undefined || propInfo.hammerAmount == undefined || propInfo.magicAmount == undefined) {
                    console.log("2222222道具数据异常");
                    cc.fx.GameConfig.GM_INFO.freezeAmount = 0;
                    cc.fx.GameConfig.GM_INFO.hammerAmount = 0;
                    cc.fx.GameConfig.GM_INFO.magicAmoun = 0;
                    let propInfoNew = {
                        "freezeAmount": cc.fx.GameConfig.GM_INFO.freezeAmount,
                        "hammerAmount": cc.fx.GameConfig.GM_INFO.hammerAmount,
                        "magicAmount": cc.fx.GameConfig.GM_INFO.magicAmoun,
                        "timestamp": timestamp
                    }
                    console.log("1111111上传道具信息:", propInfoNew);
                    cc.fx.StorageMessage.setStorage("prop", propInfoNew);
                }
                else {
                    // cc.fx.GameConfig.GM_INFO.freezeAmount = data.result.data.freeze;
                    // cc.fx.GameConfig.GM_INFO.hammerAmount = data.result.data.hammer;
                    // cc.fx.GameConfig.GM_INFO.magicAmount = data.result.data.magic_wand;
                    cc.fx.GameConfig.GM_INFO.freezeAmount = propInfo.freezeAmount;
                    cc.fx.GameConfig.GM_INFO.hammerAmount = propInfo.hammerAmount;
                    cc.fx.GameConfig.GM_INFO.magicAmount = propInfo.magicAmount;
                }


                // cc.fx.GameConfig.GM_INFO.freezeAmount = propInfo.freezeAmount;
                // cc.fx.GameConfig.GM_INFO.hammerAmount = propInfo.hammerAmount;
                // cc.fx.GameConfig.GM_INFO.magicAmount = propInfo.magicAmount;
                cc.fx.GameTool.setUserProp(0, 0, (data) => {
                });
            }
            else if (data.result.code == 200) {
                // console.log("本地时间戳",propInfo.timestamp,"服务器时间戳",data.result.timestamp);
                if ((data.result.timestamp - propInfo.timestamp) < 10000) {
                    console.log("以前端道具为主:", propInfo);
                    if (propInfo.freezeAmount == undefined || propInfo.hammerAmount == undefined || propInfo.magicAmount == undefined) {
                        console.log("111111道具数据异常", data.result.data);
                        cc.fx.GameConfig.GM_INFO.freezeAmount = data.result.data.freeze;
                        cc.fx.GameConfig.GM_INFO.hammerAmount = data.result.data.hammer;
                        cc.fx.GameConfig.GM_INFO.magicAmount = data.result.data.magic_wand;
                        let propInfoNew = {
                            "freezeAmount": cc.fx.GameConfig.GM_INFO.freezeAmount,
                            "hammerAmount": cc.fx.GameConfig.GM_INFO.hammerAmount,
                            "magicAmount": cc.fx.GameConfig.GM_INFO.magicAmount,
                            "timestamp": timestamp
                        }
                        console.log("333333上传道具信息:", propInfoNew);
                        cc.fx.StorageMessage.setStorage("prop", propInfoNew);
                    }
                    else {
                        console.log("道具数据正常");
                        cc.fx.GameConfig.GM_INFO.freezeAmount = propInfo.freezeAmount;
                        cc.fx.GameConfig.GM_INFO.hammerAmount = propInfo.hammerAmount;
                        cc.fx.GameConfig.GM_INFO.magicAmount = propInfo.magicAmount;
                        cc.fx.GameTool.setUserProp(0, 0, (data) => {
                        });
                    }
                }
                else {
                    cc.fx.GameConfig.GM_INFO.freezeAmount = data.result.data.freeze;
                    cc.fx.GameConfig.GM_INFO.hammerAmount = data.result.data.hammer;
                    cc.fx.GameConfig.GM_INFO.magicAmount = data.result.data.magic_wand;
                    let propInfoNew = {
                        "freezeAmount": cc.fx.GameConfig.GM_INFO.freezeAmount,
                        "hammerAmount": cc.fx.GameConfig.GM_INFO.hammerAmount,
                        "magicAmount": cc.fx.GameConfig.GM_INFO.magicAmount,
                        "timestamp": timestamp
                    }
                    console.log("22222222上传道具信息:", propInfoNew, data.result);
                    cc.fx.StorageMessage.setStorage("prop", propInfoNew);
                }
            }
        });
    }
    //旧用户，无本地缓存读取配置
    oldReadData(retryCount: number) {
        console.log("读取旧信息");
        const MAX_RETRIES = 15;
        const timestamp = Date.now();
        // 读取用户数据
        //@ts-ignore
        wx.cloud.callFunction({
            name: 'userData',
            data: {
                action: 'read'
            },
            success: res => {
                console.log('读取用户数据成功', res.result)
                if (res.result.code == 404 && res.result.message == "未找到用户数据") {
                    if (res.result.openid) {
                        console.log("用户从未进入过，第一次设置openid:");
                        console.log(res.result.openid);
                        cc.fx.GameConfig.GM_INFO.openid = res.result.openid
                    }
                    else {
                        console.log("用户此一次进入游戏为正确获取openid");
                    }
                    cc.fx.GameConfig.GM_INFO.first = true;
                    this.load3 = true;
                    this.load4 = true;
                    let levelInfo = { "level": 0, "timestamp": timestamp };
                    cc.fx.StorageMessage.setStorage("level", levelInfo);
                    let coinInfo = { "coin": 0, "timestamp": timestamp };
                    console.log("33333333333");
                    cc.fx.StorageMessage.setStorage("coin", coinInfo);
                    let propInfo = {
                        "freezeAmount": 0,
                        "hammerAmount": 0,
                        "magicAmount": 0,
                        "timestamp": timestamp,
                    }
                    cc.fx.StorageMessage.setStorage("prop", propInfo);


                    cc.fx.GameTool.setUserInfo((data) => {
                        if (data.result.code == 200) {
                            console.log("上传用户信息成功", data);
                            //@ts-ignore
                            wx.cloud.callFunction({
                                name: 'userData',
                                data: {
                                    action: 'read'
                                },
                                success: res => {
                                    console.log("上传后，读取用户信息，为上报注册时间")
                                    if (res.result.code == 200) {
                                        const time = res.result.data.register_time;
                                        MiniGameSdk.API.shushu_userSet(time);
                                    }
                                }
                            })
                        }
                    });
                    console.log("服务器也没有金币信息", cc.fx.GameConfig.GM_INFO.coin);
                    cc.fx.GameTool.setUserCoin(cc.fx.GameConfig.GM_INFO.coin, (data) => {

                    });
                    cc.fx.GameTool.setUserLevel((data) => {
                    });
                    setTimeout(() => {
                        cc.fx.GameConfig.GM_INFO.freezeAmount = 0;
                        cc.fx.GameConfig.GM_INFO.hammerAmount = 0;
                        cc.fx.GameConfig.GM_INFO.magicAmount = 0;
                        cc.fx.GameTool.setUserProp(0, 0, (data) => {
                            if (data.result.code == 200) {
                                console.log("上传道具信息成功", data);
                            }
                            else {
                                MiniGameSdk.API.showToast("网络异常，正在努力加载");
                                setTimeout(() => {
                                    this.oldReadData(0);
                                }, 1000);
                            }
                        });
                    }, 0);
                }
                else if (res.result.code == 200) {
                    if (res.result.data) {
                        cc.fx.GameConfig.GM_INFO.openid = res.result.openid;
                        cc.fx.GameConfig.GM_INFO.level = res.result.data.level;
                        cc.fx.GameConfig.GM_INFO.coin = res.result.data.coinAmount;
                        cc.fx.GameConfig.GM_INFO.freezeAmount = res.result.data.freezeAmount;
                        cc.fx.GameConfig.GM_INFO.hammerAmount = res.result.data.hammerAmount;
                        cc.fx.GameConfig.GM_INFO.magicAmount = res.result.data.magicAmount;
                        cc.fx.GameConfig.GM_INFO.username = res.result.data.username;
                        cc.fx.GameConfig.GM_INFO.useravatar = res.result.data.useravatar;
                        cc.fx.StorageMessage.setStorage("openid", cc.fx.GameConfig.GM_INFO.openid);
                        const register_time = res.result.data.register_time;
                        MiniGameSdk.API.shushu_userSet(register_time);
                    }
                    setTimeout(() => {
                        cc.fx.GameTool.getUserCoin((data) => {
                            if (data.result.code == 404 && data.result.message == "未找到金币数据") {

                                let coinInfo = { "coin": 0, "timestamp": timestamp };
                                cc.fx.GameConfig.GM_INFO.coin = 0;
                                cc.fx.StorageMessage.setStorage("coin", coinInfo);
                                console.log("没有金币信息，从用户接口拿到数据", cc.fx.GameConfig.GM_INFO.coin);
                                cc.fx.GameTool.setUserCoin((data) => {
                                    // console.log("上传",data);
                                    this.load3 = true;
                                });
                            }
                            else if (data.result.code == 200) {
                                // console.log("有金币信息，从金币接口拿到数据",data.result.data);
                                cc.fx.GameConfig.GM_INFO.coin = data.result.data;
                                let coinInfo = { "coin": cc.fx.GameConfig.GM_INFO.coin, "timestamp": timestamp };
                                console.log("存储金币信息:", cc.fx.GameConfig.GM_INFO.coin);
                                console.log("4444444444");
                                cc.fx.StorageMessage.setStorage("coin", coinInfo);
                                this.load3 = true;
                            }
                        })
                    }, 500);

                    cc.fx.GameTool.getUserLevel((data) => {
                        if (data.result.code == 404 && data.result.message == "未找到关卡数据") {
                            console.log("没有等级信息，从用户接口拿到数据");
                            let levelInfo = { "level": 0, "timestamp": timestamp };
                            // console.log("333333存储关卡数据:",levelInfo);
                            cc.fx.GameConfig.GM_INFO.level = 0;
                            cc.fx.StorageMessage.setStorage("level", levelInfo);
                            cc.fx.GameTool.setUserLevel((data) => {
                                console.log("拿到的数据", data);
                                this.load4 = true;
                            });
                        }
                        else if (data.result.code == 200) {
                            console.log("有等级信息，从关卡接口拿到数据", data.result.data);
                            cc.fx.GameConfig.GM_INFO.level = data.result.data;
                            let levelInfo = { "level": cc.fx.GameConfig.GM_INFO.level, "timestamp": timestamp };
                            // console.log("444444存储关卡信息:",levelInfo);
                            cc.fx.StorageMessage.setStorage("level", levelInfo);
                            this.load4 = true;
                        }
                    })
                    setTimeout(() => {
                        cc.fx.GameTool.getUserProp((data) => {
                            if (data.result.code == 404 && data.result.message == "未找到道具数据") {
                                console.log("没有道具信息，从用户接口拿到数据");
                                cc.fx.GameConfig.GM_INFO.freezeAmount = 0;
                                cc.fx.GameConfig.GM_INFO.hammerAmount = 0;
                                cc.fx.GameConfig.GM_INFO.magicAmount = 0;
                                let propInfo = {
                                    "freezeAmount": 0,
                                    "hammerAmount": 0,
                                    "magicAmount": 0,
                                    "timestamp": timestamp,
                                }
                                console.log("上传道具信息:", propInfo);
                                cc.fx.StorageMessage.setStorage("prop", propInfo);
                                cc.fx.GameTool.setUserProp(0, 0, (data) => {
                                });
                            }
                            else if (data.result.code == 200) {
                                console.log("有道具信息，从道具接口拿到数据", data.result.data);
                                cc.fx.GameConfig.GM_INFO.freezeAmount = data.result.data.freeze || 0;
                                cc.fx.GameConfig.GM_INFO.hammerAmount = data.result.data.hammer || 0;
                                cc.fx.GameConfig.GM_INFO.magicAmount = data.result.data.magic_wand || 0;
                                let propInfo = {
                                    "freezeAmount": cc.fx.GameConfig.GM_INFO.freezeAmount,
                                    "hammerAmount": cc.fx.GameConfig.GM_INFO.hammerAmount,
                                    "magicAmount": cc.fx.GameConfig.GM_INFO.magicAmount,
                                    "timestamp": timestamp,
                                }
                                console.log("上传道具信息:", propInfo);
                                cc.fx.StorageMessage.setStorage("prop", propInfo);

                            }
                        })
                    }, 0);

                }

            },
            fail: err => {
                console.error('读取用户数据失败', err)
                if (retryCount < MAX_RETRIES) {
                    console.error(`读取用户数据失败，第 ${retryCount + 1} 次重试，错误信息:`, err);
                    // 延迟 2 秒后重试
                    setTimeout(() => {
                        this.readUserData(retryCount + 1);
                    }, 2000);
                } else {
                    console.error('读取用户数据失败，达到最大重试次数，退出游戏', err);
                    // 退出游戏
                    cc.game.end();
                }
            }
        })
    }

    setUserData() {
        cc.fx.GameConfig.GameTool((data) => {
            console.log("上传", data);
        })
    }

    startTimeCutDown() {
        this.scheduleCallback = function () {
            if (this.timeNumber <= 0) {
                this.stopTimeCutDown();
            }
            else {
                this.timeNumber -= 1;
            }
        }.bind(this);
        this.schedule(this.scheduleCallback, 1);
    }

    stopTimeCutDown() {
        if (this.scheduleCallback) {
            this.unschedule(this.scheduleCallback);
        }
    }

    update(dt) {
        if (this.load1 && this.load2 && this.load3 && this.load4 && this.load5 && this.load6 == true && this.timeNumber <= 0) {
            this.load1 = this.load2 = false;
            MiniGameSdk.API.shushu_Login();
            MiniGameSdk.API.yinli_Init();
            MiniGameSdk.API.yinli_Login();
            this.startGame();
        }
    }
}
