979 lines
34 KiB
TypeScript
979 lines
34 KiB
TypeScript
|
||
import SceneManager from "../../SceneManager";
|
||
import { MiniGameSdk } from "../../Sdk/MiniGameSdk";
|
||
import Utils from "../Pay/Utils";
|
||
//@ts-ignore
|
||
//最大工具类 各种公共方法,以及处理上传,获取后端接口数据
|
||
var GameTool = {
|
||
_startTime: 0,
|
||
_endTime: 0,
|
||
_totalTime: 0,
|
||
|
||
//获取userId
|
||
Authentication(){
|
||
let name = "user_" + cc.fx.GameConfig.GM_INFO.gameId;
|
||
var data = JSON.parse(localStorage.getItem(name));
|
||
if(data == "undifend" || data==null || data == ""){
|
||
var urlNow = window.location.href;
|
||
if(!this.containsTrain(urlNow)){
|
||
let url = "https://api.sparkus.cn/api/user/auth/login?domain=hui32579WdYPsgYq&callback="+location.href;
|
||
window.location.href = url;
|
||
}
|
||
}
|
||
else{
|
||
cc.fx.StorageMessage.setStorage(name,data);
|
||
cc.fx.GameConfig.GM_INFO.userId = parseInt(data.userId);
|
||
}
|
||
},
|
||
|
||
containsTrain(str) {
|
||
return /from=train/i.test(str);
|
||
},
|
||
|
||
//埋点上传
|
||
setGameData(){
|
||
//GAME_DATA 初始化 每次清零
|
||
cc.fx.GameConfig.GAME_DATA = [];
|
||
cc.fx.GameConfig.GAME_DATA.push(cc.fx.GameConfig.CLICK_DATA);
|
||
cc.fx.GameConfig.CLICK_init();
|
||
let data = cc.fx.GameConfig.GAME_DATA;
|
||
let matchId = this.getMatchId();
|
||
let postData = {
|
||
"gameId":cc.fx.GameConfig.GM_INFO.gameId,
|
||
"userId":cc.fx.GameConfig.GM_INFO.userId,
|
||
"scode": cc.fx.GameConfig.GM_INFO.scode,
|
||
"matchId":matchId,
|
||
"data": data
|
||
};
|
||
|
||
// console.log("上传数据:",postData);
|
||
// cc.fx.HttpUtil.uploadUserLogData(postData,function(){})
|
||
},
|
||
//上传排行榜 type为1
|
||
setRank(data){
|
||
//GAME_DATA 初始化 每次清零
|
||
let postData = {
|
||
"gameId":cc.fx.GameConfig.GM_INFO.gameId,
|
||
"userId":cc.fx.GameConfig.GM_INFO.userId,
|
||
"type":1,
|
||
"score": data.score,
|
||
"accuracy": data.date,
|
||
"success": cc.fx.GameConfig.GM_INFO.success
|
||
};
|
||
// cc.fx.HttpUtil.rankData(1,function(){},postData);
|
||
},
|
||
//获取排行榜 type为2
|
||
getRank(data,callback){
|
||
let rankLength = data.length;
|
||
let postData = {
|
||
"gameId":cc.fx.GameConfig.GM_INFO.gameId,
|
||
"userId":cc.fx.GameConfig.GM_INFO.userId,
|
||
"page":1,
|
||
"pageSize":rankLength
|
||
};
|
||
//回调进getRankData
|
||
// cc.fx.HttpUtil.rankData(2,data =>{callback(data)},postData);
|
||
},
|
||
//获取matchId 用于上传每次点击数据里面记录id方便查询
|
||
getMatchId (){
|
||
let matchId = cc.sys.localStorage.getItem("matchId");
|
||
let tempId = matchId;
|
||
if(matchId == "undifend" || matchId==null){
|
||
matchId = this.setMatchId();
|
||
}
|
||
else{
|
||
if(this.containsNanana(matchId) == true){
|
||
matchId = this.setMatchId();
|
||
}
|
||
else{
|
||
let char = parseInt(tempId.substring(10,tempId.length));
|
||
if(cc.fx.GameConfig.GM_INFO.level == 1){
|
||
char += 1;
|
||
matchId = tempId.slice(0, 10) + char + "";
|
||
if(this.containsNanana(matchId)) matchId = this.setMatchId();
|
||
cc.fx.GameConfig.GM_INFO.matchId = matchId;
|
||
cc.sys.localStorage.setItem("matchId",matchId);
|
||
}
|
||
}
|
||
}
|
||
|
||
if(this.containsNanana(matchId) == true){
|
||
matchId = this.setMatchId();
|
||
}
|
||
return matchId;
|
||
},
|
||
//检测matchId 如果有缓存以前的nanana数据清除
|
||
containsNanana(str) {
|
||
return /na/i.test(str);
|
||
},
|
||
//重新设置MatchId
|
||
setMatchId (){
|
||
// 定义包含可用字符的字符集
|
||
const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||
// 创建一个数组以保存随机字符
|
||
const uuidArray = [];
|
||
// 循环10次 生成10位的UUID
|
||
for (let i = 0; i < 10; i++) {
|
||
// 生成随机索引,范围是字符集的长度
|
||
const randomIndex = Math.floor(Math.random() * characters.length);
|
||
// 从字符集中获取随机字符
|
||
const randomChar = characters.charAt(randomIndex);
|
||
// 将字符添加到数组中
|
||
uuidArray.push(randomChar);
|
||
}
|
||
let data = uuidArray.join('') + 1 + "";
|
||
cc.sys.localStorage.setItem("matchNumber",1);
|
||
cc.sys.localStorage.setItem("matchId",data);
|
||
cc.fx.GameConfig.GM_INFO.matchId = data;
|
||
return data;
|
||
},
|
||
//截取名字
|
||
subName(name,length){
|
||
if(name.length > length){
|
||
name = name.substring(0,length) + "..."
|
||
}
|
||
return name;
|
||
},
|
||
//设置头像
|
||
setPic(node,pic){
|
||
node.active = false;
|
||
let url = pic;
|
||
setTimeout(() => {
|
||
fetch(url)
|
||
.then(response => {
|
||
return response.headers.get('Content-Length');
|
||
})
|
||
.then(errNo => {
|
||
if(errNo == "5093"){
|
||
node.active = true;
|
||
}
|
||
})
|
||
.catch(error => {
|
||
// console.error('Error fetching X-Info:', error);
|
||
});
|
||
}, 100);
|
||
cc.assetManager.loadRemote(url, {ext:'.png'},(err, texture:cc.Texture2D) => {
|
||
if(texture){
|
||
node.active = true;
|
||
node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
|
||
}
|
||
else{
|
||
// console.log(err,texture)
|
||
}
|
||
})
|
||
},
|
||
//第一个参数把目标带进来处理,第二个参数为名字长度,不同场景不同需求
|
||
//名字4短,小排行,名字6长,大排行
|
||
getRankData(data,target,nameLength){
|
||
target.listData = data.data.list;
|
||
target.selfData = data.data.info;
|
||
let rankData = [];
|
||
let self = false;
|
||
cc.fx.GameTool.setPic(target.selfNode.getChildByName("pic").getChildByName("icon"),target.selfData.pic);
|
||
for(let i=0;i<=target.listData.length-1;i++){
|
||
rankData.push({rank:(i+1), name:target.listData[i].nickName, total:target.listData[i].score,time:null, pic:target.listData[i].pic});
|
||
if(cc.fx.GameConfig.GM_INFO.userId == target.listData[i].userId){
|
||
self = true;
|
||
target.rankNumber = i;
|
||
target.selfNode.getChildByName("rankLab").getComponent(cc.Label).string =(i+1) + "";
|
||
}
|
||
if(i == (target.listData.length-1) && self == false){
|
||
target.rankNumber = i;
|
||
target.selfNode.getChildByName("rankLab").getComponent(cc.Label).string = "99+";
|
||
}
|
||
}
|
||
target.selfData.nickName = cc.fx.GameTool.subName(target.selfData.nickName,nameLength);
|
||
target.selfNode.getChildByName("nameLab").getComponent(cc.Label).string = target.selfData.nickName;
|
||
target.selfNode.getChildByName("totalLab").getComponent(cc.Label).string = target.selfData.score;
|
||
let timeTemp = cc.fx.GameTool.getTimeShenNong(target.selfData.totleTimes);
|
||
// target.selfNode.getChildByName("timeLab").getComponent(cc.Label).string = timeTemp + "";
|
||
switch(target.selfNode.getChildByName("rankLab").getComponent(cc.Label).string){
|
||
case "1":
|
||
target.selfNode.getChildByName("rank").getChildByName("one").active = true;
|
||
break;
|
||
case "2":
|
||
target.selfNode.getChildByName("rank").getChildByName("two").active = true;
|
||
break;
|
||
case "3":
|
||
target.selfNode.getChildByName("rank").getChildByName("three").active = true;
|
||
break;
|
||
}
|
||
// 大排行
|
||
if(nameLength == 6){
|
||
target.rankList.setData(rankData);
|
||
target.selfNode.opacity = 255;
|
||
if(target.selfData.totalSunCount == 0) target.selfNode.opacity = 0;
|
||
}
|
||
},
|
||
|
||
getSeedRandom: function (min, max) {//包含min 不包含max
|
||
// console.log("随机数:",cc.fx.GameConfig.GM_INFO.currSeed);
|
||
max = max || 1;
|
||
min = min || 0;
|
||
cc.fx.GameConfig.GM_INFO.currSeed = (cc.fx.GameConfig.GM_INFO.currSeed * 9301 + 49297) % 233280;
|
||
let rnd = cc.fx.GameConfig.GM_INFO.currSeed / 233280.0;
|
||
let tmp = min + rnd * (max - min);
|
||
return parseInt(tmp);
|
||
},
|
||
//获取关卡配置的那个关卡数
|
||
getCustom(type){
|
||
let custom = cc.fx.StorageMessage.getStorage(cc.fx.storageType.storageTypeCustom);
|
||
if(custom == "undifend" || custom==null || custom == ""){
|
||
this.setCustom();
|
||
}
|
||
else{
|
||
cc.fx.GameConfig.GM_INFO_SET("custom",custom[0]);
|
||
if(custom[0] != 0 || type == true){
|
||
custom.shift();
|
||
if(custom.length == 0){
|
||
this.setCustom();
|
||
}
|
||
else cc.fx.StorageMessage.setStorage(cc.fx.storageType.storageTypeCustom,custom);
|
||
}
|
||
}
|
||
},
|
||
//本地没有存储到配置,或者配置用完,重新创建配置
|
||
setCustom(){
|
||
let arrayLength = cc.fx.GameConfig.LEVEL_INFO.length;
|
||
let arrayList = [];
|
||
for(let i=1; i<arrayLength;i++){
|
||
arrayList.push(i);
|
||
}
|
||
arrayList.sort(() => Math.random() - 0.5);
|
||
arrayList.unshift(0)
|
||
cc.fx.GameConfig.GM_INFO_SET("custom",arrayList[0]);
|
||
cc.fx.StorageMessage.setStorage(cc.fx.storageType.storageTypeCustom,arrayList);
|
||
},
|
||
|
||
|
||
getSetScreenResolutionFlag: function () {
|
||
let size = cc.winSize;
|
||
let width = size.width;
|
||
let height = size.height;
|
||
if ((height / width) > (16.2 / 9)) return false;
|
||
return true;
|
||
},
|
||
//判断全面屏适配
|
||
setFit: function (canvas) {
|
||
let flag = cc.fx.GameTool.getSetScreenResolutionFlag();
|
||
if (flag) {
|
||
// console.log("不是全面屏");
|
||
} else {
|
||
// console.log("是全面屏");
|
||
}
|
||
return flag;
|
||
},
|
||
//获取游戏信息
|
||
getGameInfo: function(node){
|
||
var jg = false;
|
||
return jg;
|
||
},
|
||
//设置游戏信息
|
||
setGameInfo: function(pd){
|
||
|
||
},
|
||
|
||
//打字机效果
|
||
typingAni(label,text,cb,target){
|
||
var self = target;
|
||
var html = '';
|
||
var arr = text.split('');
|
||
var len = arr.length;
|
||
var step = 0;
|
||
self.func = ()=>{
|
||
html += arr[step];
|
||
label.string = html;
|
||
if (++step == len) {
|
||
self.unschedule(self.func);
|
||
cb && cb();
|
||
}
|
||
}
|
||
self.schedule(self.func,0.1, cc.macro.REPEAT_FOREVER, 0)
|
||
},
|
||
|
||
//输入秒,返回需要展示时间格式
|
||
getTimeMargin:(second) => {
|
||
let total = 0;
|
||
total = second;
|
||
let hour = 0;
|
||
hour = parseInt((total / 3600) + "");//计算整数小时数
|
||
let afterHour = total - hour * 60 * 60;//取得算出小时数后剩余的秒数
|
||
let min = parseInt((afterHour / 60)+"");//计算整数分
|
||
let m = "" + min;
|
||
if(min < 10) m = "0"+min;
|
||
let afterMin = total - hour * 60 * 60 - min * 60;//取得算出分后剩余的秒数
|
||
let miao = afterMin + "";
|
||
if(afterMin < 10) miao = "0" + afterMin;
|
||
return m + ':' + miao
|
||
},
|
||
|
||
//输入秒,返回需要展示时间格式
|
||
getTimeShenNong:(second) => {
|
||
second = parseInt(second/1000+"");
|
||
let total = 0;
|
||
total = second;
|
||
let min = 0;
|
||
if(total > 60){
|
||
min = parseInt((total / 60)+"");//计算整数分
|
||
}
|
||
let m = min + "'";
|
||
|
||
let afterMin = total - min * 60;//取得算出分后剩余的秒数
|
||
let miao = afterMin + "''";
|
||
return m + miao
|
||
},
|
||
|
||
//打乱数组
|
||
shuffleArray: function (array) {
|
||
for (let i = array.length - 1; i > 0; i--) {
|
||
const j = Math.floor(Math.random() * (i + 1));
|
||
[array[i], array[j]] = [array[j], array[i]];
|
||
}
|
||
return array;
|
||
},
|
||
|
||
|
||
//增加关卡数
|
||
addLevel(time1,time2){
|
||
cc.fx.GameConfig.GM_INFO.level += 1;
|
||
if(cc.fx.GameConfig.GM_INFO.level > 204){
|
||
cc.fx.GameConfig.GM_INFO.level = 204;
|
||
}
|
||
const timestamp = Date.now();
|
||
let levelInfo = {
|
||
level:cc.fx.GameConfig.GM_INFO.level,
|
||
timestamp:timestamp,
|
||
}
|
||
// console.log("55555存储关卡数据:",levelInfo);
|
||
cc.fx.StorageMessage.setStorage("level",levelInfo);
|
||
if(time1 && time2){
|
||
let data = {
|
||
time:time1,
|
||
add_Time:time2,
|
||
result:"success"
|
||
}
|
||
cc.fx.GameTool.shushu_Track("finish_stage",data);
|
||
MiniGameSdk.API.shushu_SetSuperProperties(null);
|
||
}
|
||
|
||
cc.fx.GameTool.setUserLevel((data)=>{
|
||
// console.log("存储结果:",data);
|
||
// console.log("上传",data);
|
||
})
|
||
},
|
||
|
||
//改变金币信息
|
||
changeCoin(coin) {
|
||
console.log("changeCoin", coin);
|
||
if (coin == undefined) return;
|
||
if (coin < 0 && cc.fx.GameConfig.GM_INFO.coin < -coin) {
|
||
// console.log("金币不足",cc.fx.GameConfig.GM_INFO.coin,-coin);
|
||
return;
|
||
}
|
||
cc.fx.GameConfig.GM_INFO.coin += coin;
|
||
|
||
if (cc.fx.GameConfig.GM_INFO.coin < 0) {
|
||
cc.fx.GameConfig.GM_INFO.coin = 0;
|
||
}
|
||
// console.log("改变的金币:",coin);
|
||
// console.log("自身金币信息:",cc.fx.GameConfig.GM_INFO.coin);
|
||
const timestamp = Date.now();
|
||
let coinInfo = {
|
||
coin: cc.fx.GameConfig.GM_INFO.coin,
|
||
timestamp: timestamp,
|
||
}
|
||
MiniGameSdk.API.shushu_SetSuperProperties(null);
|
||
cc.fx.StorageMessage.setStorage("coin", coinInfo);
|
||
// console.log("存储金币数据:",cc.fx.GameConfig.GM_INFO.coin,coinInfo);
|
||
cc.fx.GameTool.setUserCoin((data) => {
|
||
// console.log("上传",data);
|
||
})
|
||
},
|
||
//@ts-ignore
|
||
//获取用户金币数量
|
||
getUserCoin(callback: Function) {
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userCoin',
|
||
data: {
|
||
action: 'read'
|
||
},
|
||
success: res => {
|
||
// console.log('读取用户金币数据成功', res.result)
|
||
if(callback)
|
||
callback(res);
|
||
|
||
},
|
||
fail: err => {
|
||
callback(err);
|
||
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
//改变用户金币
|
||
setUserCoin(callback){
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
if(cc.fx.GameConfig.GM_INFO.coin <= 0 || cc.fx.GameConfig.GM_INFO.coin == undefined){
|
||
console.log("金币上传失败",cc.fx.GameConfig.GM_INFO.coin);
|
||
// MiniGameSdk.API.showToast(cc.fx.GameConfig.GM_INFO.coin);
|
||
cc.fx.GameConfig.GM_INFO.coin = 0;
|
||
}
|
||
console.log("即将上传的金币数量:",cc.fx.GameConfig.GM_INFO.coin);
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userCoin',
|
||
data: {
|
||
action: 'save',
|
||
coinAmount: cc.fx.GameConfig.GM_INFO.coin
|
||
},
|
||
success: res => {
|
||
// console.log('云函数返回结果:', res);
|
||
if(res.result.code == 200){
|
||
// console.log('存储金币数据成功',cc.fx.GameConfig.GM_INFO.coin);
|
||
}
|
||
if(callback){
|
||
callback(res);
|
||
}
|
||
|
||
},
|
||
fail: err => {
|
||
// console.log('存储金币数据失败')
|
||
if(callback){
|
||
callback(err);
|
||
}
|
||
console.error('存储用户数据失败', err)
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
|
||
//获取用户关卡数
|
||
getUserLevel(callback: Function) {
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userLevel',
|
||
data: {
|
||
action: 'read'
|
||
},
|
||
success: res => {
|
||
if(callback)
|
||
callback(res);
|
||
|
||
},
|
||
fail: err => {
|
||
callback(err);
|
||
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
//进游戏处理 体力值情况
|
||
getHealth(callback: Function){
|
||
let health = cc.fx.StorageMessage.getStorage("health");
|
||
const timestamp = Date.now();
|
||
cc.fx.GameConfig.GM_INFO.hp = 5;
|
||
//没有存储过体力值
|
||
if(health == null || health == undefined || health == ""){
|
||
console.log("没存储过体力值,初进");
|
||
cc.fx.GameConfig.GM_INFO.hp = 5;
|
||
cc.fx.GameTool.getUserHealth((data)=>{
|
||
console.log("体力接口返回结果",data);
|
||
if(data.result.code == 200){
|
||
cc.fx.GameConfig.GM_INFO.hp = data.result.data;
|
||
cc.fx.GameConfig.GM_INFO.min_Time = data.result.timestamp;
|
||
let healthInfo = {
|
||
"health":cc.fx.GameConfig.GM_INFO.hp,
|
||
"timestamp":data.result.timestamp
|
||
}
|
||
MiniGameSdk.API.shushu_SetSuperProperties(null);
|
||
cc.fx.StorageMessage.setStorage("health",healthInfo);
|
||
}
|
||
else if(data.result.code == 404 && data.result.message == "未找到体力数据"){
|
||
cc.fx.GameTool.setUserHealth(0,()=>{
|
||
});
|
||
}
|
||
if(callback) callback();
|
||
})
|
||
}
|
||
else{
|
||
console.log("有存储过体力值:",health);
|
||
cc.fx.GameConfig.GM_INFO.hp = health.health;
|
||
if(callback){
|
||
console.log("体力值读取完毕");
|
||
callback();
|
||
}
|
||
if(health.health < 0){
|
||
console.log("体力值异常,归零");
|
||
cc.fx.GameConfig.GM_INFO.hp = 0;
|
||
cc.fx.GameTool.setUserHealth(0,()=>{
|
||
if(callback) callback();
|
||
});
|
||
}
|
||
else if(health.health < 5){
|
||
// 30分钟的毫秒数
|
||
const thirtyMinutes = 30 * 60 * 1000;
|
||
const elapsedTime = timestamp - health.timestamp;
|
||
// 计算恢复的体力值
|
||
const recoveredHealth = Math.min(5 - health.health, Math.floor(elapsedTime / thirtyMinutes));
|
||
if (recoveredHealth > 0) {
|
||
health.health += recoveredHealth;
|
||
cc.fx.GameTool.setUserHealth(recoveredHealth,()=>{
|
||
if(callback) callback();
|
||
});
|
||
console.log(`体力值恢复 ${recoveredHealth} 点,当前体力值: ${cc.fx.GameConfig.GM_INFO.hp}`);
|
||
} else {
|
||
// 计算距离下一次恢复的剩余时间
|
||
const remainingTime = Math.ceil((thirtyMinutes - (elapsedTime % thirtyMinutes)) / 1000);
|
||
console.log(`体力值未满,待恢复,距离下一次恢复还剩 ${remainingTime} 秒`);
|
||
cc.fx.GameConfig.GM_INFO.min_Time = remainingTime;
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
//获取用户体力值
|
||
getUserHealth(callback: Function) {
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
console.log("即将进入体力获取接口");
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userHealth',
|
||
data: {
|
||
action: 'read'
|
||
},
|
||
success: res => {
|
||
// console.log("体力获取成功",res);
|
||
if(callback)
|
||
callback(res);
|
||
},
|
||
fail: err => {
|
||
// console.log("体力获取失败",err);
|
||
callback(err);
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
//设置用户体力值 有可能加,有可能定时器减
|
||
setUserHealth(health,callback){
|
||
cc.fx.GameConfig.GM_INFO.hp += health;
|
||
// if(health < 0) {
|
||
// }
|
||
// else MiniGameSdk.API.showToast("体力值恢复");
|
||
const timestamp = Date.now();
|
||
let healthInfo ={}
|
||
|
||
//如果消耗之前不是满体力,说明已经在恢复期,并不用修改时间
|
||
if(health < 0 && cc.fx.GameConfig.GM_INFO.hp != 4) {
|
||
let oldTime = cc.fx.StorageMessage.getStorage("health").timestamp;
|
||
healthInfo = {
|
||
"health":cc.fx.GameConfig.GM_INFO.hp,
|
||
"timestamp":oldTime
|
||
}
|
||
}
|
||
else{
|
||
healthInfo = {
|
||
"health":cc.fx.GameConfig.GM_INFO.hp,
|
||
"timestamp":timestamp
|
||
}
|
||
}
|
||
cc.fx.StorageMessage.setStorage("health",healthInfo);
|
||
MiniGameSdk.API.shushu_SetSuperProperties(null);
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userHealth',
|
||
data: {
|
||
action: 'save',
|
||
healthAmount: cc.fx.GameConfig.GM_INFO.hp,
|
||
timestamp:timestamp
|
||
},
|
||
success: res => {
|
||
if(callback){
|
||
callback(res);
|
||
}
|
||
|
||
},
|
||
fail: err => {
|
||
if(callback){
|
||
callback(err);
|
||
}
|
||
// console.error('存储关卡数据失败', err)
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
//购买行为
|
||
buyReview(coin,callback: Function){
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
// console.log("实际即将消耗金币:",coin);
|
||
// this.changeCoin(coin);
|
||
callback();
|
||
}
|
||
},
|
||
|
||
//更改用户道具数
|
||
buyProp(propid,callback: Function) {
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
cc.fx.GameTool.changeCoin(-1500);
|
||
cc.fx.GameTool.setUserProp(propid,3,(data)=>{
|
||
})
|
||
const data = {
|
||
id: (propid + ""),
|
||
num:3
|
||
}
|
||
cc.fx.GameTool.shushu_Track("resource_get",data);
|
||
let _id = "";
|
||
if(propid == 2001) _id = "freeze_in_game";
|
||
else if(propid == 2002) _id = "hammer_in_game";
|
||
else if(propid == 2003) _id = "wand_in_game";
|
||
|
||
const buyData = {
|
||
item_id:_id,
|
||
item_num:3,
|
||
item_price:1500,
|
||
cost_type:"gold"
|
||
}
|
||
console.log("____________即将上传Shop_buy",buyData);
|
||
cc.fx.GameTool.shushu_Track("shop_buy",buyData);
|
||
callback();
|
||
}
|
||
},
|
||
|
||
|
||
//改变用户关卡
|
||
setUserLevel(callback){
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
// if(cc.fx.GameConfig.GM_INFO.level <= 0 || cc.fx.GameConfig.GM_INFO.level == undefined){
|
||
// console.log("等级重置为0");
|
||
// cc.fx.GameConfig.GM_INFO.level = 0;
|
||
// }
|
||
// console.log("即将上传的关卡数:",cc.fx.GameConfig.GM_INFO.level);
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userLevel',
|
||
data: {
|
||
action: 'save',
|
||
levelAmount: cc.fx.GameConfig.GM_INFO.level
|
||
},
|
||
success: res => {
|
||
// console.log('云函数返回结果:', res);
|
||
// console.log('存储关卡数据成功',cc.fx.GameConfig.GM_INFO.level);
|
||
if(callback){
|
||
callback(res);
|
||
}
|
||
|
||
},
|
||
fail: err => {
|
||
// console.log('存储关卡数据失败')
|
||
return;
|
||
if(callback){
|
||
callback(err);
|
||
}
|
||
console.error('存储关卡数据失败', err)
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
//获取用户关卡数
|
||
getUserProp(callback: Function) {
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userProp',
|
||
data: {
|
||
action: 'read'
|
||
},
|
||
success: res => {
|
||
// console.log('读取用户道具数据成功', res.result)
|
||
if(res.result.data){
|
||
// cc.fx.GameConfig.GM_INFO.freezeAmount = res.result.data.freeze;
|
||
// cc.fx.GameConfig.GM_INFO.hammerAmount = res.result.data.hammer;
|
||
// cc.fx.GameConfig.GM_INFO.magicAmount = res.result.data.magic_wand;
|
||
}
|
||
if(callback)
|
||
callback(res);
|
||
|
||
},
|
||
fail: err => {
|
||
callback(err);
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
//改变用户道具
|
||
setUserProp(propid,amount,callback){
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
let newPropData = null;
|
||
if(propid == 0){
|
||
newPropData = {
|
||
freeze:cc.fx.GameConfig.GM_INFO.freezeAmount,
|
||
hammer:cc.fx.GameConfig.GM_INFO.hammerAmount,
|
||
magic_wand:cc.fx.GameConfig.GM_INFO.magicAmount,
|
||
}
|
||
}
|
||
else if(propid == 2001){
|
||
newPropData = amount;
|
||
}
|
||
else if(propid == 2002){
|
||
newPropData = amount;
|
||
}
|
||
else if(propid == 2003){
|
||
newPropData = amount;
|
||
}
|
||
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userProp',
|
||
data: {
|
||
action: 'save',
|
||
propType: propid,
|
||
propData: newPropData
|
||
},
|
||
success: res => {
|
||
// console.log('云函数返回结果:', res);
|
||
if(callback){
|
||
callback(res);
|
||
}
|
||
// console.log('存储道具数据成功')
|
||
},
|
||
fail: err => {
|
||
// console.log('存储道具数据失败')
|
||
if(callback){
|
||
callback(err);
|
||
}
|
||
console.error('存储道具数据失败', err)
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
// 检查是否有足够的硬币
|
||
consumeCoins(requiredCoins: number, propName: string, amount: number, callback) {
|
||
|
||
|
||
},
|
||
|
||
// 设置用户信息
|
||
setUserInfo(callback: Function) {
|
||
//@ts-ignore
|
||
if (typeof wx!== 'undefined' && wx!== null) {
|
||
const time = cc.fx.GameTool.formatDate(new Date());
|
||
let userInfo = {
|
||
// 这里填写要存储的用户数据
|
||
username: cc.fx.GameConfig.GM_INFO.username, //用户名称
|
||
useravatar: cc.fx.GameConfig.GM_INFO.useravatar, //用户头像
|
||
register_time: time
|
||
}
|
||
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: 'userData',
|
||
data: {
|
||
action: 'save',
|
||
userData: userInfo
|
||
},
|
||
success: res => {
|
||
// console.log('存储用户数据成功')
|
||
if(callback){
|
||
callback(res);
|
||
}
|
||
|
||
},
|
||
fail: err => {
|
||
if(callback){
|
||
callback("fail");
|
||
}
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
//商城购买
|
||
shopBuy(productId){
|
||
let coin = 0;
|
||
let price = 0;
|
||
switch(productId){
|
||
case "gold_1":
|
||
cc.fx.GameTool.changeCoin(1200);
|
||
coin = 1200;
|
||
price = 600;
|
||
MiniGameSdk.API.showToast("充值成功,获得1200金币");
|
||
break;
|
||
case "gold_2":
|
||
cc.fx.GameTool.changeCoin(8000);
|
||
coin = 8000;
|
||
price = 3600;
|
||
MiniGameSdk.API.showToast("充值成功,获得8000金币");
|
||
break;
|
||
case "gold_3":
|
||
cc.fx.GameTool.changeCoin(16000);
|
||
coin = 16000;
|
||
price = 6800;
|
||
MiniGameSdk.API.showToast("充值成功,获得16000金币");
|
||
break;
|
||
case "gold_4":
|
||
cc.fx.GameTool.changeCoin(32000);
|
||
coin = 32000;
|
||
price = 12800;
|
||
MiniGameSdk.API.showToast("充值成功,获得32000金币");
|
||
break;
|
||
case "gold_5":
|
||
cc.fx.GameTool.changeCoin(100000);
|
||
coin = 100000;
|
||
price = 32800;
|
||
MiniGameSdk.API.showToast("充值成功,获得100000金币");
|
||
break;
|
||
case "gold_6":
|
||
cc.fx.GameTool.changeCoin(240000);
|
||
coin = 240000;
|
||
price = 64800;
|
||
MiniGameSdk.API.showToast("充值成功,获得240000金币");
|
||
break;
|
||
}
|
||
const buyData = {
|
||
item_id:productId,
|
||
item_num:coin,
|
||
item_price:price,
|
||
cost_type:"cash"
|
||
}
|
||
cc.fx.GameTool.shushu_Track("shop_buy",buyData);
|
||
},
|
||
|
||
formatDate(date: Date): string {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
const hours = String(date.getHours()).padStart(2, '0');
|
||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||
const seconds = String(date.getSeconds()).padStart(2, '0');
|
||
const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
|
||
|
||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
|
||
},
|
||
|
||
//获取时间戳
|
||
getTime(){
|
||
const timestamp = (new Date().getTime())
|
||
return timestamp;
|
||
},
|
||
pushLister:function () {
|
||
|
||
},
|
||
removeAllLister:function () {
|
||
|
||
},
|
||
|
||
shushu_Track: function(name,data){
|
||
let eventData = {}
|
||
switch(name){
|
||
case "register":
|
||
eventData = {
|
||
register_time: data.register_time,
|
||
}
|
||
break;
|
||
case "enter_stage":
|
||
eventData = {
|
||
stage_id: (cc.fx.GameConfig.GM_INFO.level + 1)
|
||
}
|
||
break;
|
||
case "finish_stage":
|
||
eventData = {
|
||
stage_id: (cc.fx.GameConfig.GM_INFO.level + 1),
|
||
stage_duration: data.time,
|
||
added_time: data.add_Time,
|
||
result: data.result
|
||
}
|
||
break;
|
||
case "resource_get":
|
||
eventData = {
|
||
change_reason:"购买道具", //获得来源
|
||
change_num:data.num, //获得数量
|
||
resource_id:data.id //道具id
|
||
}
|
||
break;
|
||
case "resource_cost":
|
||
eventData = {
|
||
change_reason:"使用道具", //获得来源
|
||
change_num:data.num, //获得数量
|
||
resource_id:data.id //道具id
|
||
}
|
||
break;
|
||
case "shop_buy":
|
||
eventData = {
|
||
item_id: data.item_id,
|
||
item_num: data.item_num,
|
||
item_price: data.item_price,
|
||
cost_type: data.cost_type,
|
||
}
|
||
break;
|
||
case "init_order": //发起充值时
|
||
eventData = {
|
||
order_id: data.outTradeNo,
|
||
pay_amount: data.price,
|
||
payment_name: data.payment_name,
|
||
payment_num: data.payment_num,
|
||
payment_type: data.type,
|
||
}
|
||
break;
|
||
case "payment": //发起充值时
|
||
eventData = {
|
||
order_id: data.outTradeNo,
|
||
pay_amount: data.price,
|
||
payment_name: data.payment_name,
|
||
payment_num: data.payment_num,
|
||
payment_type: data.type,
|
||
}
|
||
break;
|
||
case "payment_fail": //发起充值时
|
||
eventData = {
|
||
order_id: data.outTradeNo,
|
||
pay_amount: data.price,
|
||
payment_name: data.payment_name,
|
||
payment_num: data.payment_num,
|
||
payment_type: data.type,
|
||
fail_reason: data.fail_reason,
|
||
}
|
||
break;
|
||
}
|
||
// MiniGameSdk.API.shushu_Track(name,eventData);
|
||
MiniGameSdk.API.shushu_Track(name, eventData);
|
||
},
|
||
|
||
getWechatGameVersion: function(){
|
||
//@ts-ignore
|
||
const accountInfo = wx.getAccountInfoSync();
|
||
const miniProgram = accountInfo.miniProgram;
|
||
switch (miniProgram.envVersion) {
|
||
case 'develop':
|
||
return '开发版';
|
||
case 'trial':
|
||
return '体验版';
|
||
case 'release':
|
||
return '正式版';
|
||
default:
|
||
return '未知版本';
|
||
}
|
||
},
|
||
|
||
};
|
||
export { GameTool }; |