332 lines
12 KiB
JavaScript
332 lines
12 KiB
JavaScript
"use strict";
|
||
cc._RF.push(module, '43bfcJ6/25Fs4fHUE0PeBOX', 'GameTool');
|
||
// Script/module/Tool/GameTool.ts
|
||
|
||
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.GameTool = void 0;
|
||
//最大工具类 各种公共方法,以及处理上传,获取后端接口数据
|
||
var GameTool = {
|
||
_startTime: 0,
|
||
_endTime: 0,
|
||
_totalTime: 0,
|
||
//获取userId
|
||
Authentication: function () {
|
||
var 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)) {
|
||
var 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: function (str) {
|
||
return /from=train/i.test(str);
|
||
},
|
||
//埋点上传
|
||
setGameData: function () {
|
||
//GAME_DATA 初始化 每次清零
|
||
cc.fx.GameConfig.GAME_DATA = [];
|
||
cc.fx.GameConfig.GAME_DATA.push(cc.fx.GameConfig.CLICK_DATA);
|
||
cc.fx.GameConfig.CLICK_init();
|
||
var data = cc.fx.GameConfig.GAME_DATA;
|
||
var matchId = this.getMatchId();
|
||
var postData = {
|
||
"gameId": cc.fx.GameConfig.GM_INFO.gameId,
|
||
"userId": cc.fx.GameConfig.GM_INFO.userId,
|
||
"matchId": matchId,
|
||
"data": data
|
||
};
|
||
// console.log("上传数据:",data);
|
||
cc.fx.HttpUtil.uploadUserLogData(postData, function () { });
|
||
},
|
||
//上传排行榜 type为1
|
||
setRank: function (data) {
|
||
//GAME_DATA 初始化 每次清零
|
||
var 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: function (data, callback) {
|
||
var rankLength = data.length;
|
||
var 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, function (data) { callback(data); }, postData);
|
||
},
|
||
//获取matchId 用于上传每次点击数据里面记录id方便查询
|
||
getMatchId: function () {
|
||
var matchId = cc.sys.localStorage.getItem("matchId");
|
||
var tempId = matchId;
|
||
if (matchId == "undifend" || matchId == null) {
|
||
matchId = this.setMatchId();
|
||
}
|
||
else {
|
||
if (this.containsNanana(matchId) == true) {
|
||
matchId = this.setMatchId();
|
||
}
|
||
else {
|
||
var 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: function (str) {
|
||
return /na/i.test(str);
|
||
},
|
||
//重新设置MatchId
|
||
setMatchId: function () {
|
||
// 定义包含可用字符的字符集
|
||
var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||
// 创建一个数组以保存随机字符
|
||
var uuidArray = [];
|
||
// 循环10次 生成10位的UUID
|
||
for (var i = 0; i < 10; i++) {
|
||
// 生成随机索引,范围是字符集的长度
|
||
var randomIndex = Math.floor(Math.random() * characters.length);
|
||
// 从字符集中获取随机字符
|
||
var randomChar = characters.charAt(randomIndex);
|
||
// 将字符添加到数组中
|
||
uuidArray.push(randomChar);
|
||
}
|
||
var 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: function (name, length) {
|
||
if (name.length > length) {
|
||
name = name.substring(0, length) + "...";
|
||
}
|
||
return name;
|
||
},
|
||
//设置头像
|
||
setPic: function (node, pic) {
|
||
node.active = false;
|
||
var url = pic;
|
||
setTimeout(function () {
|
||
fetch(url)
|
||
.then(function (response) {
|
||
return response.headers.get('Content-Length');
|
||
})
|
||
.then(function (errNo) {
|
||
if (errNo == "5093") {
|
||
node.active = true;
|
||
}
|
||
})
|
||
.catch(function (error) {
|
||
// console.error('Error fetching X-Info:', error);
|
||
});
|
||
}, 100);
|
||
cc.assetManager.loadRemote(url, { ext: '.png' }, function (err, texture) {
|
||
if (texture) {
|
||
node.active = true;
|
||
node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
|
||
}
|
||
else {
|
||
console.log(err, texture);
|
||
}
|
||
});
|
||
},
|
||
//第一个参数把目标带进来处理,第二个参数为名字长度,不同场景不同需求
|
||
//名字4短,小排行,名字6长,大排行
|
||
getRankData: function (data, target, nameLength) {
|
||
target.listData = data.data.list;
|
||
target.selfData = data.data.info;
|
||
var rankData = [];
|
||
var self = false;
|
||
cc.fx.GameTool.setPic(target.selfNode.getChildByName("pic").getChildByName("icon"), target.selfData.pic);
|
||
for (var 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+";
|
||
}
|
||
}
|
||
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;
|
||
var 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) {
|
||
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;
|
||
var rnd = cc.fx.GameConfig.GM_INFO.currSeed / 233280.0;
|
||
var tmp = min + rnd * (max - min);
|
||
return parseInt(tmp);
|
||
},
|
||
//获取关卡配置的那个关卡数
|
||
getCustom: function (type) {
|
||
var 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: function () {
|
||
var arrayLength = cc.fx.GameConfig.LEVEL_INFO.length;
|
||
var arrayList = [];
|
||
for (var i = 1; i < arrayLength; i++) {
|
||
arrayList.push(i);
|
||
}
|
||
arrayList.sort(function () { return 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 () {
|
||
var size = cc.winSize;
|
||
var width = size.width;
|
||
var height = size.height;
|
||
if ((height / width) > (16.2 / 9))
|
||
return false;
|
||
return true;
|
||
},
|
||
//判断全面屏适配
|
||
setFit: function (canvas) {
|
||
var 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: function (label, text, cb, target) {
|
||
var self = target;
|
||
var html = '';
|
||
var arr = text.split('');
|
||
var len = arr.length;
|
||
var step = 0;
|
||
self.func = function () {
|
||
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: function (second) {
|
||
var total = 0;
|
||
total = second;
|
||
var hour = 0;
|
||
hour = parseInt((total / 3600) + ""); //计算整数小时数
|
||
var afterHour = total - hour * 60 * 60; //取得算出小时数后剩余的秒数
|
||
var min = parseInt((afterHour / 60) + ""); //计算整数分
|
||
var m = "" + min;
|
||
if (min < 10)
|
||
m = "0" + min;
|
||
var afterMin = total - hour * 60 * 60 - min * 60; //取得算出分后剩余的秒数
|
||
var miao = afterMin + "";
|
||
if (afterMin < 10)
|
||
miao = "0" + afterMin;
|
||
return m + ':' + miao;
|
||
},
|
||
//输入秒,返回需要展示时间格式
|
||
getTimeShenNong: function (second) {
|
||
second = parseInt(second / 1000 + "");
|
||
var total = 0;
|
||
total = second;
|
||
var min = 0;
|
||
if (total > 60) {
|
||
min = parseInt((total / 60) + ""); //计算整数分
|
||
}
|
||
var m = min + "'";
|
||
var afterMin = total - min * 60; //取得算出分后剩余的秒数
|
||
var miao = afterMin + "''";
|
||
return m + miao;
|
||
},
|
||
//获取时间戳
|
||
getTime: function () {
|
||
var timestamp = (new Date().getTime());
|
||
return timestamp;
|
||
},
|
||
pushLister: function () {
|
||
},
|
||
removeAllLister: function () {
|
||
},
|
||
};
|
||
exports.GameTool = GameTool;
|
||
|
||
cc._RF.pop(); |