DWTool.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. var ReportType = require("../utils/GameEnum").ReportType;
  2. var Base64 = require("../lib/Base64").Base64;
  3. var HomeApi = require("../net/HomeApi");
  4. var GameModule = require('./GameModule');
  5. var Promise = require('../lib/es6-promise').Promise;
  6. class DWTool {
  7. static coinParse(coin) {
  8. if (coin < 99999) { // 1 ~ 99999 按实际显示
  9. return coin.toString();
  10. } else if (coin >= 100000 && coin < 999999) { // 以万为单位,保留2位小数,不需四舍五入
  11. let parseCoin = (coin / 10000).toFixed(3).slice(0, -1);
  12. return parseCoin.toString() + "A";
  13. } else if (coin >= 1000000 && coin < 9999999) { // 以万为单位,保留1位小数,不需四舍五入
  14. let parseCoin = (coin / 10000).toFixed(2).slice(0, -1);
  15. return parseCoin.toString() + "A";
  16. } else if (coin >= 10000000 && coin < 99999999) { // 以万为单位,保留0位小数,不需四舍五入
  17. let parseCoin = (coin / 10000).toFixed(1).slice(0, -2);
  18. return parseCoin.toString() + "A";
  19. } else if (coin >= 100000000 && coin < 999999999) { // 以亿为单位,保留3位小数,不需四舍五入
  20. let parseCoin = (coin / 100000000).toFixed(4).slice(0, -1);
  21. return parseCoin.toString() + "B";
  22. } else if (coin >= 1000000000 && coin < 9999999999) { // 以亿为单位,保留2位小数,不需四舍五入
  23. let parseCoin = (coin / 100000000).toFixed(3).slice(0, -1);
  24. return parseCoin.toString() + "B";
  25. } else if (coin >= 10000000000 && coin < 99999999999) { // 以亿为单位,保留1位小数,不需四舍五入
  26. let parseCoin = (coin / 100000000).toFixed(2).slice(0, -1);
  27. return parseCoin.toString() + "B";
  28. } else if (coin >= 100000000000 && coin < 999999999999) { // 以亿为单位,保留0位小数,不需四舍五入
  29. let parseCoin = (coin / 100000000).toFixed(1).slice(0, -2);
  30. return parseCoin.toString() + "B";
  31. } else if (coin >= 1000000000000 && coin < 9999999999999) { // 以兆为单位,保留3位小数,不需四舍五入
  32. let parseCoin = (coin / 1000000000000).toFixed(4).slice(0, -1);
  33. return parseCoin.toString() + "C";
  34. } else if (coin >= 10000000000000 && coin < 99999999999999) { // 以兆为单位,保留2位小数,不需四舍五入
  35. let parseCoin = (coin / 1000000000000).toFixed(3).slice(0, -1);
  36. return parseCoin.toString() + "C";
  37. } else if (coin >= 100000000000000 && coin < 999999999999999) { // 以兆为单位,保留1位小数,不需四舍五入
  38. let parseCoin = (coin / 1000000000000).toFixed(2).slice(0, -1);
  39. return parseCoin.toString() + "C";
  40. } else if (coin >= 1000000000000000 && coin < 9999999999999999) { // 以兆为单位,保留0位小数,不需四舍五入
  41. let parseCoin = (coin / 1000000000000).toFixed(1).slice(0, -2);
  42. return parseCoin.toString() + "C";
  43. }
  44. };
  45. static coinParseNoFixed(coin) {
  46. if (coin < 99999) { // 1 ~ 99999 按实际显示
  47. return coin.toString();
  48. } else if (coin >= 100000 && coin < 99999999) {
  49. let parseCoin = (coin / 10000).toFixed(0);
  50. return parseCoin.toString() + "万";
  51. } else if (coin >= 100000000 && coin < 9999999999999) {
  52. let parseCoin = (coin / 100000000).toFixed(0);
  53. return parseCoin.toString() + "亿";
  54. } else if (coin >= 1000000000000 && coin < 9999999999999999) {
  55. let parseCoin = (coin / 1000000000000).toFixed(0);
  56. return parseCoin.toString() + "兆";
  57. }
  58. };
  59. static setGenderIcon(gender) {
  60. return this.getGenderIconPath(gender).then((filePath) => {
  61. return this.loadResSpriteFrame(filePath);
  62. });
  63. };
  64. /**
  65. * 上报方法, 抽成公用方法
  66. * @param {*} seq
  67. * @param {*} totalMoney 总金币
  68. * @param {*} stars 星星数
  69. * @param {*} recordModify 是否有修改的建筑
  70. * @param {*} recordUnlockModify 是否有解锁的建筑
  71. */
  72. static reportInfo(seq = 1, totalMoney = 0, stars = 0, clickCount = 0, recordModify = [], recordUnlockModify = [], success, fail) {
  73. let reportFormInfo = {};
  74. reportFormInfo[ReportType.Seq] = seq;
  75. reportFormInfo[ReportType.Gold] = totalMoney;
  76. reportFormInfo[ReportType.Stars] = stars;
  77. reportFormInfo[ReportType.ClickCount] = clickCount;
  78. reportFormInfo[ReportType.Timestamp] = Date.parse(new Date());
  79. if (recordUnlockModify.length > 0 || recordModify.length > 0) {
  80. reportFormInfo[ReportType.Event] = 1;
  81. if (recordUnlockModify.length > 0) {
  82. reportFormInfo[ReportType.Build] = this.parseReportArray(recordUnlockModify);
  83. }
  84. if (recordModify.length > 0) {
  85. reportFormInfo[ReportType.Build] = this.parseReportArray(recordModify);
  86. }
  87. } else {
  88. reportFormInfo[ReportType.Event] = 0;
  89. }
  90. // 还是将上报信息log出来吧, 方便调试
  91. // console.log("reportFormInfo: " + JSON.stringify(reportFormInfo));
  92. let ecData = Base64.encode(JSON.stringify(reportFormInfo));
  93. HomeApi.userReportGross(ecData, (rep) => {
  94. // console.log("上报成功!");
  95. success();
  96. }, (code, msg) => {
  97. fail({ "code": code, "msg": msg });
  98. });
  99. };
  100. static reportRoomsInfo(totalMoney = 0, buildingData = []) {
  101. let reportFormInfo = {};
  102. reportFormInfo[ReportType.Gold] = totalMoney;
  103. reportFormInfo[ReportType.Build] = this.parseReportArray(buildingData);
  104. let ecData = Base64.encode(JSON.stringify(reportFormInfo));
  105. HomeApi.userReportRooms(ecData, (rep) => {
  106. // console.log("上报成功!");
  107. }, (code, msg) => {
  108. console.log("上报失败!");
  109. });
  110. };
  111. //score对应总部等级buildingLevel,提交得分到微信后台
  112. static submitWechatScore(score) { //提交得分
  113. let key2 = GameGlobal.wechatScoreKey;
  114. // 对用户托管数据进行写数据操作
  115. let myValue = {
  116. wxgame: {
  117. score: score,
  118. update_time: Date.parse(new Date()) / 1000
  119. },
  120. gender: GameGlobal.user.gender,
  121. }
  122. var myValueString = JSON.stringify(myValue);
  123. window.wx.setUserCloudStorage({
  124. KVDataList: [{ key: key2, value: myValueString }],
  125. success: function (res) {
  126. console.log('setUserCloudStorage', 'success', res)
  127. },
  128. fail: function (res) {
  129. console.log('setUserCloudStorage', 'fail')
  130. },
  131. complete: function (res) {
  132. }
  133. });
  134. }
  135. static submitQQScore(score) { //提交得分
  136. let key2 = GameGlobal.wechatScoreKey;
  137. let myValue = {
  138. wxgame: {
  139. score: score,
  140. update_time: Date.parse(new Date()) / 1000
  141. },
  142. gender: GameGlobal.user.gender,
  143. }
  144. var myValueString = JSON.stringify(myValue);
  145. var data = {
  146. userData: [{
  147. openId: GameStatusInfo.openId,
  148. startMs: GameStatusInfo.startMs,
  149. endMs: ((new Date()).getTime()).toString(),
  150. scoreInfo: {
  151. score: parseInt(score) //分数,类型必须是整型数
  152. }
  153. }],
  154. attr: {
  155. score: {
  156. type: 'rank',
  157. order: 4,
  158. }
  159. },
  160. };
  161. BK.QQ.uploadScoreWithoutRoom(1, data, function (errCode, cmd, data) {
  162. // 返回错误码信息
  163. if (errCode !== 0) {
  164. BK.Script.log(1, 1, '上传分数失败!错误码:' + errCode);
  165. } else {
  166. BK.Script.log(1, 1, '上传分数成功' + JSON.stringify(data));
  167. }
  168. });
  169. }
  170. static parseReportArray(array) {
  171. let tempArray = [];
  172. // 删除没用的字段
  173. for (let i = 0; i < array.length; i++) {
  174. let model = array[i];
  175. let tempObj = {};
  176. tempObj[ReportType.ID] = model.roomId;
  177. if (model.level != undefined) {
  178. tempObj[ReportType.Level] = model.level;
  179. } else if (model.roomLevel != undefined) {
  180. tempObj[ReportType.Level] = model.roomLevel;
  181. }
  182. tempArray.push(tempObj);
  183. }
  184. return tempArray;
  185. };
  186. /**
  187. *根据url加载网络图片
  188. *
  189. * @static
  190. * @param {*} url
  191. * @memberof DWTool
  192. */
  193. static loadSpriteFrame(url) {
  194. return new Promise((resolve, reject) => {
  195. cc.loader.load(url, function (err, texture) {
  196. if (err) {
  197. reject(err);
  198. } else {
  199. let frame = new cc.SpriteFrame(texture);
  200. resolve(frame);
  201. }
  202. });
  203. })
  204. }
  205. static getGenderIconPath(gender) {
  206. return new Promise((resolve, reject) => {
  207. // if (gender === 0 || gender === 1) {
  208. let filePath = (gender === 1) ? './textures/icon_male' : './textures/icon_female';
  209. resolve(filePath);
  210. // } else {
  211. // reject();
  212. // }
  213. })
  214. };
  215. // clickOnce(cb) {
  216. // if (this._clickDic === 0) { return; }
  217. // this._clickDic = 0;
  218. // setTimeout(() => {
  219. // this._clickDic = 1;
  220. // }, 1000);
  221. // cb && cb();
  222. // };
  223. static loadResPrefab(path) {
  224. let p = new Promise((resolve, reject) => {
  225. cc.loader.loadRes(path, cc.Prefab, (error, prefab) => {
  226. if (error) {
  227. console.log(error);
  228. reject(error);
  229. } else {
  230. resolve(prefab)
  231. }
  232. });
  233. });
  234. return p;
  235. };
  236. static loadResSpriteAtlasToSpriteFrame(path, subAsset) {
  237. let p = new Promise((resolve, reject) => {
  238. cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
  239. if (error) {
  240. console.log(error);
  241. reject(error);
  242. } else {
  243. var spriteFrame = atlas.getSpriteFrame(subAsset);
  244. resolve(spriteFrame)
  245. }
  246. });
  247. });
  248. return p;
  249. };
  250. static loadResSpriteAtlas(path) {
  251. let p = new Promise((resolve, reject) => {
  252. cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
  253. if (error) {
  254. console.log(error);
  255. reject(error);
  256. } else {
  257. resolve(atlas);
  258. }
  259. });
  260. });
  261. return p;
  262. };
  263. static loadResSpriteFrame(path) {
  264. let p = new Promise((resolve, reject) => {
  265. cc.loader.loadRes(path, cc.SpriteFrame, (error, sf) => {
  266. if (error) {
  267. console.log(error);
  268. reject(error);
  269. } else {
  270. resolve(sf);
  271. }
  272. });
  273. });
  274. return p;
  275. }
  276. static loadResPrefabArray(pathArr) {
  277. let p = new Promise((resolve, reject) => {
  278. cc.loader.loadResArray(pathArr, cc.Prefab, (error, resourceArr) => {
  279. if (error) {
  280. console.log(error);
  281. reject(error);
  282. } else {
  283. resolve(resourceArr);
  284. }
  285. });
  286. });
  287. return p;
  288. }
  289. //时间戳格式化为对应文字时间显示
  290. static timeParse(timestamp) {
  291. function zeroize(num) {
  292. return (String(num).length == 1 ? '0' : '') + num;
  293. }
  294. var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
  295. var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
  296. var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
  297. var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
  298. var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
  299. var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
  300. if (timestampDiff < 60) { // 一分钟以内
  301. return "刚刚";
  302. } else if (timestampDiff < 3600) { // 一小时前之内
  303. return Math.floor(timestampDiff / 60) + "分钟前";
  304. } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
  305. return '今天' + H + ':' + zeroize(i);
  306. } else {
  307. var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
  308. if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
  309. return '昨天' + H + ':' + zeroize(i);
  310. } else if (curDate.getFullYear() == Y) {
  311. return m + '月' + d + '日 ' + H + ':' + zeroize(i);
  312. } else {
  313. return Y + '年' + m + '月' + d + '日 ' + H + ':' + zeroize(i);
  314. }
  315. }
  316. };
  317. /**
  318. * 将时间戳转成00:00显示
  319. * @param {floor} t
  320. */
  321. static calculateTime(t) {
  322. // 好像经常会出现时间格式化出错的问题, 这里先这样解决一下
  323. if (t < 0) {
  324. t = 0;
  325. console.log("出现负数的时间");
  326. }
  327. let hour = Math.floor(t / 60 / 60);
  328. let minute = Math.floor(t / 60 % 60);
  329. let second = Math.floor(t % 60);
  330. if (hour != 0 && hour < 10) {
  331. hour = "0" + hour;
  332. }
  333. if (minute < 10) {
  334. minute = "0" + minute;
  335. }
  336. if (second < 10) {
  337. second = "0" + second;
  338. }
  339. if (hour != 0) {
  340. return `${hour}:${minute}:${second}`;
  341. } else {
  342. return `${minute}:${second}`;
  343. }
  344. };
  345. static calculateTimeString(t) {
  346. if (t < 0) {
  347. t = 0;
  348. console.log("出现负数的时间");
  349. }
  350. let hour = Math.floor(t / 60 / 60);
  351. let minute = Math.floor(t / 60 % 60);
  352. let second = Math.floor(t % 60);
  353. if (hour != 0 && hour < 10) {
  354. hour = "0" + hour;
  355. }
  356. if (minute < 10) {
  357. minute = "0" + minute;
  358. }
  359. if (second < 10) {
  360. second = "0" + second;
  361. }
  362. if (hour != 0) {
  363. return `${hour}小时${minute}分${second}秒`;
  364. } else {
  365. return `${minute}分${second}秒`;
  366. }
  367. };
  368. /**
  369. * 获取Url上指定参数
  370. * @param {String} name 参数名
  371. */
  372. static getUrlParam(name) {
  373. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  374. var r = window.location.search.substr(1).match(reg);
  375. if (r != null) {
  376. return unescape(r[2]);
  377. } else {
  378. return null;
  379. }
  380. }
  381. /**
  382. * 判断当前用户是否走完引导教程
  383. */
  384. static checkIsOldUser() {
  385. // debugger;
  386. if (GameModule.userInfo && GameModule.userInfo.buildingLevel && GameModule.userInfo.buyStarCount) {
  387. // 1. 总部大楼大于25级
  388. // 2. 已拥有1个明星
  389. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  390. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  391. if (unLockStatus1 && unLockStatus2) {
  392. return true;
  393. } else {
  394. return false;
  395. }
  396. } else {
  397. return false;
  398. }
  399. }
  400. static isDot(num) {
  401. var result = (num.toString()).indexOf(".");
  402. if(result != -1) {
  403. return true;
  404. } else {
  405. return false;
  406. }
  407. }
  408. }
  409. module.exports = DWTool;