cb/assets/Script/GameManager.ts
YZ\249929363 03562bb36f 暂存
2025-08-08 18:24:56 +08:00

957 lines
44 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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;
nowTime: 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();
this.getShareInfo();
}, 200);
if (GameManager._instance == null) {
GameManager._instance = this;
cc.game.addPersistRootNode(this.node);
}
else {
return;
}
// 检测微信小游戏切到后台
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
//@ts-ignore
wx.onHide(() => {
this.onHide();
});
// 检测微信小游戏回到前台
//@ts-ignore
wx.onShow(() => {
this.onShow();
});
}
}
start() {
setTimeout(() => {
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();
});
}, 100);
}
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;
});
}
startGame() {
// 加载成功后进入 HomeScene
// cc.assetManager.loadBundle('music', (err, bundle) => {
// if (err) {
// console.error('加载 music bundle 失败:', err);
// // 加载失败时仍尝试进入 HomeScene
// cc.director.loadScene("HomeScene");
// } else {
// //console.log('music bundle 加载成功');
// }
// cc.director.loadScene("HomeScene");
// });
cc.assetManager.loadBundle('shop', (err, bundle) => {
if (err) {
console.error('加载 shop 失败:', err);
} else {
//console.log('shop加载成功');
// 加载成功后进入 HomeScene
cc.assetManager.loadBundle('music', (err, bundle) => {
if (err) {
console.error('加载 music bundle 失败:', err);
// 加载失败时仍尝试进入 HomeScene
cc.director.loadScene("HomeScene");
} else {
//console.log('music bundle 加载成功');
}
cc.director.loadScene("HomeScene");
});
}
});
// 加载 music bundle
}
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();
this.nowTime = Date.now();
//console.log("开始读取用户信息的时间:", this.nowTime);
const MAX_RETRIES = 15;
// 延迟时间数组,按照 1 秒 3 次、2 秒 5 次、5 秒 6 次、15 秒 5 次的规则生成
const delays = [
...Array(3).fill(2000),
...Array(5).fill(3000),
...Array(6).fill(5000),
...Array(5).fill(15000)
];
const attemptUserInfo = () => {
Utils.getUserInfo((data) => {
if (data.code == 1) { // 假设返回数据中有 success 字段表示成功
//console.log("登录成功时间耗时:", Date.now() - this.nowTime);
// //console.log("111登陆成功_____________");
console.log("登錄", data);
//console.log("uid", data.data.onlyId);
if (data.data._id) {
cc.fx.GameConfig.GM_INFO.uid = data.data._id;
cc.fx.StorageMessage.setStorage("uid", data.data._id);
}
if (data.data.onlyId) {
//console.log(":应该进入这里");
//console.log("_______________Uid:", data.data.onlyId);
cc.fx.GameConfig.GM_INFO.userId = data.data.onlyId;
}
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);
}
if (data.data.shareLv) {
//console.log("有分享信息", data.data.shareLv)
if (data.data.shareLv.length > 0) {
//console.log(data.data.shareLv[0]);
//console.log("设置分享等级", data.data.shareLv[0].lv);
cc.fx.GameConfig.GM_INFO.helpLevel = data.data.shareLv[0].lv;
}
}
this.setUserPower(data);
this.setmonth(data);
this.setRevive(data)
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;
//console.log("获取体力值成功时间耗时:", Date.now() - this.nowTime);
});
this.load6 = true;
}
else {
if (retryCount < MAX_RETRIES && retryCount < delays.length) {
const delay = delays[retryCount];
console.error(`获取用户信息失败,第 ${retryCount + 1} 次重试,将在 ${delay / 1000} 秒后重试`);
setTimeout(() => {
retryCount++;
attemptUserInfo();
}, delay);
} else {
console.error('获取用户信息失败,达到最大重试次数,退出游戏');
// 退出游戏
cc.game.end();
}
}
});
};
attemptUserInfo();
}
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)
}
})
//laf云函数
Utils.getUserData((data) => {
})
}
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;
//console.log(cc.fx.GameConfig.GM_INFO.helpLevel, "___________________", cc.fx.GameConfig.GM_INFO.level);
if (cc.fx.GameConfig.GM_INFO.helpLevel == (cc.fx.GameConfig.GM_INFO.level + 1)) {
cc.fx.GameConfig.GM_INFO.level += 1;
if ((cc.fx.GameConfig.GM_INFO.level) > 323) {
cc.fx.GameConfig.GM_INFO.level = 323;
}
//console.log("好友帮助通过第" + cc.fx.GameConfig.GM_INFO.helpLevel + "关");
}
}
this.load4 = true;
levelInfo.level = cc.fx.GameConfig.GM_INFO.level;
levelInfo.timestamp = timestamp;
cc.fx.StorageMessage.setStorage("level", levelInfo);
//console.log("关卡等级成功时间耗时:", Date.now() - this.nowTime);
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 && levelInfo.level >= data.result.data) {
if (levelInfo.level) {
//console.log("以游戏前端等级为准", data.result.data);
cc.fx.GameConfig.GM_INFO.level = levelInfo.level;
//console.log(cc.fx.GameConfig.GM_INFO.helpLevel, "___________________", cc.fx.GameConfig.GM_INFO.level);
if (cc.fx.GameConfig.GM_INFO.helpLevel == (cc.fx.GameConfig.GM_INFO.level + 1)) {
cc.fx.GameConfig.GM_INFO.level += 1;
if ((cc.fx.GameConfig.GM_INFO.level) > 323) {
cc.fx.GameConfig.GM_INFO.level = 323;
}
//console.log("好友帮助通过第" + cc.fx.GameConfig.GM_INFO.helpLevel + "关");
}
levelInfo.level = cc.fx.GameConfig.GM_INFO.level;
levelInfo.timestamp = timestamp;
cc.fx.StorageMessage.setStorage("level", levelInfo);
// //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;
//console.log("关卡等级成功时间耗时:", Date.now() - this.nowTime);
}
});
//金币信息
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);
//console.log("金币成功时间耗时:", Date.now() - this.nowTime);
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;
//console.log("金币成功时间耗时:", Date.now() - this.nowTime);
}
});
//道具信息
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("____________________道具数据异常");
cc.fx.GameConfig.GM_INFO.freezeAmount = 3;
cc.fx.GameConfig.GM_INFO.hammerAmount = 3;
cc.fx.GameConfig.GM_INFO.magicAmoun = 3;
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("_______________1111上传道具信息:", propInfoNew);
cc.fx.StorageMessage.setStorage("prop", propInfoNew);
}
else {
//console.log("_______________________道具数据正常", propInfo);
// 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.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('11111111读取用户数据成功', 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.GameTool.getUserLevel((data) => {
if (data.result.code == 404 && data.result.message == "未找到关卡数据") {
//console.log("没有等级信息,从用户接口拿到数据");
cc.fx.GameConfig.GM_INFO.first = true;
//console.log("金币成功时间耗时:", Date.now() - this.nowTime);
this.load3 = true;
this.load4 = true;
//console.log("关卡等级成功时间耗时:", Date.now() - this.nowTime);
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": 3,
"hammerAmount": 3,
"magicAmount": 3,
"timestamp": timestamp,
}
//console.log("最新上传道具信息——————————:", propInfo);
cc.fx.GameConfig.GM_INFO.freezeAmount = 3;
cc.fx.GameConfig.GM_INFO.hammerAmount = 3;
cc.fx.GameConfig.GM_INFO.magicAmount = 3;
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);
}
}
})
//laf云函数
Utils.getUserData((data) => {
})
}
});
//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 = 3;
cc.fx.GameConfig.GM_INFO.hammerAmount = 3;
cc.fx.GameConfig.GM_INFO.magicAmount = 3;
cc.fx.GameTool.setUserProp(0, 0, (data) => {
if (data.result.code == 200) {
//console.log("1111111上传道具信息成功", data);
}
else {
MiniGameSdk.API.showToast("网络异常,正在努力加载");
setTimeout(() => {
this.oldReadData(0);
}, 1000);
}
});
}, 0);
}
else if (data.result.code == 200) {
//console.log("有等级信息,从关卡接口拿到数据", data.result.data);
this.readSever(timestamp);
}
})
}
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);
}
this.readSever(timestamp);
}
},
fail: err => {
console.error('读取用户数据失败', err)
if (retryCount < MAX_RETRIES) {
console.error(`读取用户数据失败,第 ${retryCount + 1} 次重试,错误信息:`, err);
// 延迟 2 秒后重试
setTimeout(() => {
this.oldReadData(retryCount + 1);
}, 2000);
} else {
console.error('读取用户数据失败,达到最大重试次数,退出游戏', err);
// 退出游戏
cc.game.end();
}
}
})
//laf云函数
// Utils.getUserData((data) => {
// })
}
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);
}
}
setUserPower(data) {
let nowTime = Math.floor(Date.now() / 1000);
let powerTime = cc.fx.StorageMessage.getStorage("userPowerTime");
//本地没缓存
if (powerTime == "undifend" || powerTime == null || powerTime == "" || powerTime == 0) {
//本地没缓存,但是服务器有缓存,判断服务器的无限体力时间是否过期
if (data.data.userPowerTime && data.data.userPowerTime != 0) {
powerTime = data.data.userPowerTime;
if (powerTime > nowTime) {
cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
}
else {
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
//本地没缓存,服务器也没缓存
else {
powerTime = 0;
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
//本地有缓存
else {
//本地有缓存,且服务器有缓存
if (data.data.userPowerTime && data.data.userPowerTime != 0) {
//服务器缓存大于本地缓存,以服务器缓存为主
if (data.data.userPowerTime > powerTime) {
powerTime = data.data.userPowerTime;
if (powerTime > nowTime) {
cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
}
else {
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
//服务器缓存小于本地缓存,以本地缓存为主
else {
if (powerTime > nowTime) {
cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
}
else {
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
}
//本地有缓存,服务器没缓存
else {
if (powerTime > nowTime) {
cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
}
else {
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
}
}
setmonth(data) {
console.log("得到的月卡信息:", data.data.monthCardTime);
//如果给的时间戳大于当前时间hpmax=7,否则等于5
let nowTime = Math.floor(Date.now() / 1000);
if (data.data.monthCardTime > nowTime) {
cc.fx.GameConfig.GM_INFO.hp_Max = 7;
cc.fx.GameConfig.GM_INFO.doubleCoin = 2;
} else {
cc.fx.GameConfig.GM_INFO.hp_Max = 5;
cc.fx.GameConfig.GM_INFO.doubleCoin = 1;
}
//本地储存当前服务器时间
let dateStr = new Date(data.data.monthCardTime);
cc.fx.StorageMessage.setStorage("mCardDate", dateStr);
//console.log(cc.fx.GameConfig.GM_INFO.hp_Max, "00000000000");
}
//复活购买次数初始化
setRevive(data) {
cc.fx.GameConfig.GM_INFO.revive = data.data.rebornGiftCount;
//console.log(data.data.rebornGiftCount, cc.fx.GameConfig.GM_INFO.revive, "🔥🔥🔥🔥🔥🔥🔥🔥")
}
readSever(timestamp) {
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;
//console.log("金币成功时间耗时:", Date.now() - this.nowTime);
});
}
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;
//console.log("金币成功时间耗时:", Date.now() - this.nowTime);
}
})
}, 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;
//console.log("关卡等级成功时间耗时:", Date.now() - this.nowTime);
});
}
else if (data.result.code == 200) {
//console.log("有等级信息,从关卡接口拿到数据", data.result.data);
cc.fx.GameConfig.GM_INFO.level = data.result.data;
//console.log(cc.fx.GameConfig.GM_INFO.helpLevel, "___________________", cc.fx.GameConfig.GM_INFO.level);
if (cc.fx.GameConfig.GM_INFO.helpLevel == (cc.fx.GameConfig.GM_INFO.level + 1)) {
cc.fx.GameConfig.GM_INFO.level += 1;
if ((cc.fx.GameConfig.GM_INFO.level) > 323) {
cc.fx.GameConfig.GM_INFO.level = 323;
}
//console.log("好友帮助通过第" + cc.fx.GameConfig.GM_INFO.helpLevel + "关");
}
let levelInfo = { "level": cc.fx.GameConfig.GM_INFO.level, "timestamp": timestamp };
// //console.log("444444存储关卡信息:",levelInfo);
cc.fx.StorageMessage.setStorage("level", levelInfo);
this.load4 = true;
//console.log("关卡等级成功时间耗时:", Date.now() - this.nowTime);
}
})
setTimeout(() => {
cc.fx.GameTool.getUserProp((data) => {
if (data.result.code == 404 && data.result.message == "未找到道具数据") {
//console.log("没有道具信息,从用户接口拿到数据");
cc.fx.GameConfig.GM_INFO.freezeAmount = 3;
cc.fx.GameConfig.GM_INFO.hammerAmount = 3;
cc.fx.GameConfig.GM_INFO.magicAmount = 3;
let propInfo = {
"freezeAmount": 3,
"hammerAmount": 3,
"magicAmount": 3,
"timestamp": timestamp,
}
//console.log("上传道具信息44444444:", 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 || 3;
cc.fx.GameConfig.GM_INFO.hammerAmount = data.result.data.hammer || 3;
cc.fx.GameConfig.GM_INFO.magicAmount = data.result.data.magic_wand || 3;
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("上传道具信息6666666:", propInfo);
cc.fx.StorageMessage.setStorage("prop", propInfo);
}
})
}, 0);
}
//获取有没有分享信息
getShareInfo() {
// 检查微信小游戏启动参数
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
//@ts-ignore
const launchOptions = wx.getLaunchOptionsSync();
const query = launchOptions.query;
if (query.level && query.uid) {
const level = parseInt(query.level, 10);
const uid = query.uid;
cc.fx.GameConfig.GM_INFO.otherUid = uid;
cc.fx.GameConfig.GM_INFO.otherLevel = level;
//console.log('从分享链接获取到的关卡信息:', level);
//console.log('从分享链接获取到的 UID:', uid);
// 可以在这里处理关卡信息和 UID
}
}
}
update(dt) {
////console.log("加载1:", this.load1, "加载2:", this.load2, "加载3:", this.load3, "加载4:", this.load4, "加载5:", this.load5, "加载6:", this.load6, "时间:", this.timeNumber);
if (this.load1 && this.load2 && this.load3 && this.load4 && this.load5 && this.load6 == true && this.timeNumber <= 0) {
//console.log("进入游戏_______________");
this.load1 = this.load2 = false;
MiniGameSdk.API.shushu_Login();
MiniGameSdk.API.yinli_Init();
MiniGameSdk.API.yinli_Login();
this.startGame();
}
}
}