FlyUp/assets/Script/tool/GameTool.ts
2024-07-10 18:24:08 +08:00

337 lines
12 KiB
TypeScript

import GameData from "../GameData";
import HttpUtil from "../crypto/HttpUtil";
import { StorageMessage } from "./Storage";
//最大工具类
var GameTool = {
_startTime: 0,
_endTime: 0,
_totalTime: 0,
getSeedRandom: function (min, max) {//包含min 不包含max
max = max || 1;
min = min || 0;
GameData._instance.GM_INFO.currSeed = (GameData._instance.GM_INFO.currSeed * 9301 + 49297) % 233280;
let rnd = GameData._instance.GM_INFO.currSeed / 233280.0;
let tmp = min + rnd * (max - min);
return Math.floor(tmp);
},
//获取userId
Authentication(){
let name = "user_" + GameData._instance.GM_INFO.gameId;
var data = JSON.parse(localStorage.getItem(name));
if(data == "undifend" || data==null || data == ""){
let url = "https://api.sparkus.cn/api/user/auth/login?domain=hui32579WdYPsgYq&callback="+location.href;
window.location.href = url;
}
else{
StorageMessage.setStorage(name,data);
GameData._instance.GM_INFO.userId = parseInt(data.userId);
}
},
//埋点上传
setGameData(){
//GAME_DATA 初始化 每次清零
GameData._instance.GAME_DATA = [];
GameData._instance.GAME_DATA.push(GameData._instance.CLICK_DATA);
//GAME_DATA 赋值后 CLICK_DATA清零
GameData._instance.CLICK_init();
let matchId = this.getMatchId(GameData._instance.GAME_DATA[0].round);
let postData = {
"matchId":matchId,
"data": GameData._instance.GAME_DATA
};
// console.log("上传数据:",postData);
HttpUtil.uploadUserLogData(postData,function(){})
},
setRank(){
//GAME_DATA 初始化 每次清零
let postData = {
"type":1,
"round":"1",
"score": GameData._instance.GM_INFO.score,
"success": GameData._instance.GM_INFO.success
};
HttpUtil.rankData(1,function(){},postData);
},
//获取matchId 用于上传每次点击数据里面记录id方便查询
getMatchId (level){
let matchId = cc.sys.localStorage.getItem("matchId");
if(matchId == "undifend" || matchId==null){
matchId = this.setMatchId();
}
else{
if(this.containsNanana(matchId) == true){
matchId = this.setMatchId();
}
else{
let char = parseInt(cc.sys.localStorage.getItem("matchNumber"));
if(level == 1){
char += 1;
cc.sys.localStorage.setItem("matchNumber",char);
}
matchId = matchId.slice(0, 10) + char + "";
GameData._instance.GM_INFO.matchId = matchId;
cc.sys.localStorage.setItem("matchId",matchId);
}
}
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);
GameData._instance.GM_INFO.matchId = data;
return data;
},
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 = GameTool.getSetScreenResolutionFlag();
if (flag) {
// console.log("不是全面屏");
} else {
// console.log("是全面屏");
}
return flag;
},
getTip: function () {
let index = Math.floor(Math.random() * 4) + 1;
switch (index) {
case 1: {
return "根据小球的位置,合理晃动手机,确保小球不掉出木板。"
}
case 2: {
return "根据小球位置控制晃动幅度,过大或过小都不是个好选择。"
}
case 3: {
return "不要被天上掉落的障碍吓到,有时候它反而会帮你保持平衡。"
}
case 4: {
return "尝试着顶开掉落的障碍,是个很酷的玩法。"
}
}
},
beginTiming: function () {
this._startTime = new Date().getTime();
},
endTiming: function () {
this._endTime = new Date().getTime();
// this._totalTime = endTime - this._startTime;
},
//如果是判断时常的游戏 调用此方法
updateScoreTime: function () {
let endTime = new Date().getTime();
let tempTime = endTime - this._startTime;
if (tempTime < 0 || tempTime > 500) {
tempTime = Math.round(cc.director.getAnimationInterval() * 1000);
}
this._endTime += tempTime / 1000;
this._totalTime = this._endTime.toFixed(2);
GameData._instance.GM_INFO.currScore = this._totalTime;
this._startTime = endTime;
},
//倒计时调用此方法
countDown: function () {
this._endTime = new Date().getTime();
this._totalTime = Math.floor((this._endTime - this._startTime) / 1000);
GameData._instance.GM_INFO.remainingTime = GameData._instance.GM_INFO.totalTime - this._totalTime;
},
getGameInfo: function(node){
var jg = false;
return jg;
},
//根据服务器数据重置所需要所有数据
reset: function(){
// console.log("本地数据丢失,重新启动游戏");
cc.director.loadScene('LoadScene');
},
setGameInfo: function(pd){
},
//输入秒,返回需要展示时间格式
getTimeMargin:(second) => {
let total = second;
let hour = Math.floor(total / (60 * 60));//计算整数小时数
let afterHour = total - hour * 60 * 60;//取得算出小时数后剩余的秒数
let min = Math.floor(afterHour / 60);//计算整数分
let afterMin = total - hour * 60 * 60 - min * 60;//取得算出分后剩余的秒数
return '剩余:' + hour + ':' + min + ':' + afterMin
},
getTimeMargin2:(second) => {
let total = second;
let min = null;
let afterMin = null;
let hour = Math.floor(total / (60 * 60));//计算整数小时数
let afterHour = total - hour * 60 * 60;//取得算出小时数后剩余的秒数
min = Math.floor(afterHour / 60);//计算整数分
if(min < 10) min = "0"+min;
afterMin = total - hour * 60 * 60 - min * 60;//取得算出分后剩余的秒数
if(afterMin < 10) afterMin = "0" + afterMin;
return '剩余:' + min + ':' + afterMin
},
//将时间转换为59:23
getTime: function(time){
var label = "";
var number_1 = null;
var number_2 = null;
number_1 = Math.floor(time/60)
number_2 = time-number_1*60;
if(number_1 < 10){
number_1 = "0"+number_1;
}
if(number_2 < 10){
number_2 = "0"+number_2;
}
label = number_1 + ":" + number_2;
return label;
},
//number 为传进来的数值,type为最多显示几位数
getNumber: function(number,type){
// console.log(type,"需要转换的数字为:",number);
var count = 1000;
var place = type - 3;
if(number > Math.pow(count,8)*Math.pow(10,place)){
if(number/Math.pow(count,8) < 100)
number = Math.floor(number/Math.pow(count,8)*10)/10+"S";
else
number = Math.floor(number/Math.pow(count,8))+"S";
}
else if(number > Math.pow(count,7)*Math.pow(10,place)){
if(number/Math.pow(count,7) < 100)
number = Math.floor(number/Math.pow(count,7)*10)/10+"s";
else
number = Math.floor(number/Math.pow(count,7))+"s";
}
else if(number > Math.pow(count,6)*Math.pow(10,place)){
if(number/Math.pow(count,6) < 100)
number = Math.floor(number/Math.pow(count,6)*10)/10+"Q";
else
number = Math.floor(number/Math.pow(count,6))+"Q";
}
else if(number > Math.pow(count,5)*Math.pow(10,place)){
if(number/Math.pow(count,5) < 100)
number = Math.floor(number/Math.pow(count,5)*10)/10+"q";
else
number = Math.floor(number/Math.pow(count,5))+"q";
}
else if(number > Math.pow(count,4)*Math.pow(10,place)){
if(number/Math.pow(count,4) < 100)
number = Math.floor(number/Math.pow(count,4)*10)/10+"t";
else
number = Math.floor(number/Math.pow(count,4))+"t";
}
else if(number > Math.pow(count,3)*Math.pow(10,place)){
if(number/Math.pow(count,3) < 100)
number = Math.floor(number/Math.pow(count,3)*10)/10+"b";
else
number = Math.floor(number/Math.pow(count,3))+"b";
}
else if(number > Math.pow(count,2)*Math.pow(10,place)){
if(number/Math.pow(count,2) < 100)
number = Math.floor(number/Math.pow(count,2)*10)/10+"m";
else
number = Math.floor(number/Math.pow(count,2))+"m";
}
else if(number > Math.pow(count,1)*Math.pow(10,place)){
if(number/Math.pow(count,1) < 100){
number = Math.floor(number/Math.pow(count,1)*10)/10+"k";
}
else
number = Math.floor(number/Math.pow(count,1))+"k";
}
else{
number = Math.floor(number/Math.pow(count,0))+"";
}
// console.log("结果为"+type+"位数",number);
return number;
},
begainTiming: function () {
this._startTime = new Date().getTime();
},
endTiming2: function () {
let endTime = new Date().getTime();
this._endTime = endTime;
this._totalTime = endTime - this._startTime;
},
getScoreTime: function () {
let endTime = new Date().getTime();
let tempTime = endTime - this._startTime;
if (tempTime < 0 || tempTime > 500) {
tempTime = Math.round(cc.director.getAnimationInterval() * 1000);
}
GameData._instance.GM_INFO.currScore += tempTime;
this._startTime = endTime;
},
getFormatTime: function (time) {
let date = new Date();
date.setTime(time);
let ms = date.getMilliseconds();
// let msString = date.getMilliseconds();
let msString = null;
let secondString = null;
let minString = null;
msString = Math.floor(ms / 10) % 100;
secondString = date.getSeconds();
minString = date.getMinutes();
if (msString < 10) {
msString = "0" + msString;
}
if (secondString < 10) {
secondString = "0" + secondString;
}
secondString = secondString + ":";
if (minString < 10) {
minString = "0" + minString;
}
minString = minString + ":";
return minString + secondString + msString;
},
pushLister:function () {
},
removeAllLister:function () {
},
};
export { GameTool };