123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- var ReportType = require("../utils/GameEnum").ReportType;
- var Base64 = require("../lib/Base64").Base64;
- var HomeApi = require("../net/HomeApi");
- var GameModule = require('./GameModule');
- class DWTool {
- static coinParse(coin) {
- if (coin < 99999) { // 1 ~ 99999 按实际显示
- return coin.toString();
- } else if (coin >= 100000 && coin < 999999) { // 以万为单位,保留2位小数,不需四舍五入
- let parseCoin = (coin / 10000).toFixed(3).slice(0, -1);
- return parseCoin.toString() + "A";
- } else if (coin >= 1000000 && coin < 9999999) { // 以万为单位,保留1位小数,不需四舍五入
- let parseCoin = (coin / 10000).toFixed(2).slice(0, -1);
- return parseCoin.toString() + "A";
- } else if (coin >= 10000000 && coin < 99999999) { // 以万为单位,保留0位小数,不需四舍五入
- let parseCoin = (coin / 10000).toFixed(1).slice(0, -2);
- return parseCoin.toString() + "A";
- } else if (coin >= 100000000 && coin < 999999999) { // 以亿为单位,保留3位小数,不需四舍五入
- let parseCoin = (coin / 100000000).toFixed(4).slice(0, -1);
- return parseCoin.toString() + "B";
- } else if (coin >= 1000000000 && coin < 9999999999) { // 以亿为单位,保留2位小数,不需四舍五入
- let parseCoin = (coin / 100000000).toFixed(3).slice(0, -1);
- return parseCoin.toString() + "B";
- } else if (coin >= 10000000000 && coin < 99999999999) { // 以亿为单位,保留1位小数,不需四舍五入
- let parseCoin = (coin / 100000000).toFixed(2).slice(0, -1);
- return parseCoin.toString() + "B";
- } else if (coin >= 100000000000 && coin < 999999999999) { // 以亿为单位,保留0位小数,不需四舍五入
- let parseCoin = (coin / 100000000).toFixed(1).slice(0, -2);
- return parseCoin.toString() + "B";
- } else if (coin >= 1000000000000 && coin < 9999999999999) { // 以兆为单位,保留3位小数,不需四舍五入
- let parseCoin = (coin / 1000000000000).toFixed(4).slice(0, -1);
- return parseCoin.toString() + "C";
- } else if (coin >= 10000000000000 && coin < 99999999999999) { // 以兆为单位,保留2位小数,不需四舍五入
- let parseCoin = (coin / 1000000000000).toFixed(3).slice(0, -1);
- return parseCoin.toString() + "C";
- } else if (coin >= 100000000000000 && coin < 999999999999999) { // 以兆为单位,保留1位小数,不需四舍五入
- let parseCoin = (coin / 1000000000000).toFixed(2).slice(0, -1);
- return parseCoin.toString() + "C";
- } else if (coin >= 1000000000000000 && coin < 9999999999999999) { // 以兆为单位,保留0位小数,不需四舍五入
- let parseCoin = (coin / 1000000000000).toFixed(1).slice(0, -2);
- return parseCoin.toString() + "C";
- }
- };
- static coinParseNoFixed(coin) {
- if (coin < 99999) { // 1 ~ 99999 按实际显示
- return coin.toString();
- } else if (coin >= 100000 && coin < 99999999) {
- let parseCoin = (coin / 10000).toFixed(0);
- return parseCoin.toString() + "万";
- } else if (coin >= 100000000 && coin < 9999999999999) {
- let parseCoin = (coin / 100000000).toFixed(0);
- return parseCoin.toString() + "亿";
- } else if (coin >= 1000000000000 && coin < 9999999999999999) {
- let parseCoin = (coin / 1000000000000).toFixed(0);
- return parseCoin.toString() + "兆";
- }
- };
- static setGenderIcon(gender) {
- return this.getGenderIconPath(gender).then((filePath) => {
- return this.loadResSpriteFrame(filePath);
- });
- };
- /**
- * 上报方法, 抽成公用方法
- * @param {*} seq
- * @param {*} totalMoney 总金币
- * @param {*} stars 星星数
- * @param {*} recordModify 是否有修改的建筑
- * @param {*} recordUnlockModify 是否有解锁的建筑
- */
- static reportInfo(seq = 1, totalMoney = 0, stars = 0, clickCount = 0, recordModify = [], recordUnlockModify = []) {
- return new Promise((resolve, reject) => {
- let reportFormInfo = {};
- reportFormInfo[ReportType.Seq] = seq;
- reportFormInfo[ReportType.Gold] = totalMoney;
- reportFormInfo[ReportType.Stars] = stars;
- reportFormInfo[ReportType.ClickCount] = clickCount;
- reportFormInfo[ReportType.Timestamp] = Date.parse(new Date());
- if (recordUnlockModify.length > 0 || recordModify.length > 0) {
- reportFormInfo[ReportType.Event] = 1;
- if (recordUnlockModify.length > 0) {
- reportFormInfo[ReportType.Build] = this.parseReportArray(recordUnlockModify);
- }
- if (recordModify.length > 0) {
- reportFormInfo[ReportType.Build] = this.parseReportArray(recordModify);
- }
- } else {
- reportFormInfo[ReportType.Event] = 0;
- }
- // 还是将上报信息log出来吧, 方便调试
- // console.log("reportFormInfo: " + JSON.stringify(reportFormInfo));
- let ecData = Base64.encode(JSON.stringify(reportFormInfo));
- HomeApi.userReportGross(ecData, (rep) => {
- // console.log("上报成功!");
- resolve();
- }, (code, msg) => {
- reject({ "code": code, "msg": msg });
- });
- });
- };
- static reportRoomsInfo(totalMoney = 0, buildingData = []) {
- let reportFormInfo = {};
- reportFormInfo[ReportType.Gold] = totalMoney;
- reportFormInfo[ReportType.Build] = this.parseReportArray(buildingData);
- let ecData = Base64.encode(JSON.stringify(reportFormInfo));
- HomeApi.userReportRooms(ecData, (rep) => {
- // console.log("上报成功!");
- }, (code, msg) => {
- console.log("上报失败!");
- });
- };
- //score对应总部等级buildingLevel,提交得分到微信后台
- static submitWechatScore(score) { //提交得分
- let key2 = Global.wechatScoreKey;
- if (window.wx != undefined) {
- // 对用户托管数据进行写数据操作
- let myValue = {
- wxgame: {
- score: score,
- update_time: Date.parse(new Date()) / 1000
- },
- gender: Global.user.gender,
- }
- var myValueString = JSON.stringify(myValue);
- window.wx.setUserCloudStorage({
- KVDataList: [{ key: key2, value: myValueString }],
- success: function (res) {
- console.log('setUserCloudStorage', 'success', res)
- },
- fail: function (res) {
- console.log('setUserCloudStorage', 'fail')
- },
- complete: function (res) {
- }
- });
- } else {
- let myValue = {
- wxgame: {
- score: score,
- update_time: Date.parse(new Date()) / 1000
- },
- gender: Global.user.gender,
- }
- var myValueString = JSON.stringify(myValue);
- // console.log("提交得分:" + key2 + " : " + myValueString)
- }
- }
- static parseReportArray(array) {
- let tempArray = [];
- // 删除没用的字段
- for (let i = 0; i < array.length; i++) {
- let model = array[i];
- let tempObj = {};
- tempObj[ReportType.ID] = model.roomId;
- if (model.level != undefined) {
- tempObj[ReportType.Level] = model.level;
- } else if (model.roomLevel != undefined) {
- tempObj[ReportType.Level] = model.roomLevel;
- }
- tempArray.push(tempObj);
- }
- return tempArray;
- };
- static getGenderIconPath(gender) {
- return new Promise((resolve, reject) => {
- // if (gender === 0 || gender === 1) {
- let filePath = (gender === 1) ? './textures/icon_male' : './textures/icon_female';
- resolve(filePath);
- // } else {
- // reject();
- // }
- })
- };
- // clickOnce(cb) {
- // if (this._clickDic === 0) { return; }
- // this._clickDic = 0;
- // setTimeout(() => {
- // this._clickDic = 1;
- // }, 1000);
- // cb && cb();
- // };
- static loadResPrefab(path) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadRes(path, cc.Prefab, (error, prefab) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- resolve(prefab)
- }
- });
- });
- return p;
- };
- static loadResSpriteAtlasToSpriteFrame(path, subAsset) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- var spriteFrame = atlas.getSpriteFrame(subAsset);
- resolve(spriteFrame)
- }
- });
- });
- return p;
- };
- static loadResSpriteAtlas(path) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- resolve(atlas);
- }
- });
- });
- return p;
- };
- static loadResSpriteFrame(path) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadRes(path, cc.SpriteFrame, (error, sf) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- resolve(sf);
- }
- });
- });
- return p;
- }
- static loadResPrefabArray(pathArr) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadResArray(pathArr, cc.Prefab, (error, resourceArr) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- resolve(resourceArr);
- }
- });
- });
- return p;
- }
- //时间戳格式化为对应文字时间显示
- static timeParse(timestamp) {
- function zeroize(num) {
- return (String(num).length == 1 ? '0' : '') + num;
- }
- var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
- var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
- var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
- var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
- var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
- var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
- if (timestampDiff < 60) { // 一分钟以内
- return "刚刚";
- } else if (timestampDiff < 3600) { // 一小时前之内
- return Math.floor(timestampDiff / 60) + "分钟前";
- } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
- return '今天' + H + ':' + zeroize(i);
- } else {
- var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
- if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
- return '昨天' + H + ':' + zeroize(i);
- } else if (curDate.getFullYear() == Y) {
- return m + '月' + d + '日 ' + H + ':' + zeroize(i);
- } else {
- return Y + '年' + m + '月' + d + '日 ' + H + ':' + zeroize(i);
- }
- }
- };
- /**
- * 将时间戳转成00:00显示
- * @param {floor} t
- */
- static calculateTime(t) {
- // 好像经常会出现时间格式化出错的问题, 这里先这样解决一下
- if (t < 0) {
- t = 0;
- console.log("出现负数的时间");
- }
- let hour = Math.floor(t / 60 / 60);
- let minute = Math.floor(t / 60 % 60);
- let second = Math.floor(t % 60);
- if (hour != 0 && hour < 10) {
- hour = "0" + hour;
- }
- if (minute < 10) {
- minute = "0" + minute;
- }
- if (second < 10) {
- second = "0" + second;
- }
- if (hour != 0) {
- return `${hour}:${minute}:${second}`;
- } else {
- return `${minute}:${second}`;
- }
- };
- static calculateTimeString(t) {
- if (t < 0) {
- t = 0;
- console.log("出现负数的时间");
- }
- let hour = Math.floor(t / 60 / 60);
- let minute = Math.floor(t / 60 % 60);
- let second = Math.floor(t % 60);
- if (hour != 0 && hour < 10) {
- hour = "0" + hour;
- }
- if (minute < 10) {
- minute = "0" + minute;
- }
- if (second < 10) {
- second = "0" + second;
- }
- if (hour != 0) {
- return `${hour}小时${minute}分${second}秒`;
- } else {
- return `${minute}分${second}秒`;
- }
- };
- /**
- * 获取Url上指定参数
- * @param {String} name 参数名
- */
- static getUrlParam(name) {
- var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
- var r = window.location.search.substr(1).match(reg);
- if (r != null) {
- return unescape(r[2]);
- } else {
- return null;
- }
- }
- }
- module.exports = DWTool;
|