DWTool.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. static getGenderIconPath(gender) {
  187. return new Promise((resolve, reject) => {
  188. // if (gender === 0 || gender === 1) {
  189. let filePath = (gender === 1) ? './textures/icon_male' : './textures/icon_female';
  190. resolve(filePath);
  191. // } else {
  192. // reject();
  193. // }
  194. })
  195. };
  196. // clickOnce(cb) {
  197. // if (this._clickDic === 0) { return; }
  198. // this._clickDic = 0;
  199. // setTimeout(() => {
  200. // this._clickDic = 1;
  201. // }, 1000);
  202. // cb && cb();
  203. // };
  204. static loadResPrefab(path) {
  205. let p = new Promise((resolve, reject) => {
  206. cc.loader.loadRes(path, cc.Prefab, (error, prefab) => {
  207. if (error) {
  208. console.log(error);
  209. reject(error);
  210. } else {
  211. resolve(prefab)
  212. }
  213. });
  214. });
  215. return p;
  216. };
  217. static loadResSpriteAtlasToSpriteFrame(path, subAsset) {
  218. let p = new Promise((resolve, reject) => {
  219. cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
  220. if (error) {
  221. console.log(error);
  222. reject(error);
  223. } else {
  224. var spriteFrame = atlas.getSpriteFrame(subAsset);
  225. resolve(spriteFrame)
  226. }
  227. });
  228. });
  229. return p;
  230. };
  231. static loadResSpriteAtlas(path) {
  232. let p = new Promise((resolve, reject) => {
  233. cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
  234. if (error) {
  235. console.log(error);
  236. reject(error);
  237. } else {
  238. resolve(atlas);
  239. }
  240. });
  241. });
  242. return p;
  243. };
  244. static loadResSpriteFrame(path) {
  245. let p = new Promise((resolve, reject) => {
  246. cc.loader.loadRes(path, cc.SpriteFrame, (error, sf) => {
  247. if (error) {
  248. console.log(error);
  249. reject(error);
  250. } else {
  251. resolve(sf);
  252. }
  253. });
  254. });
  255. return p;
  256. }
  257. static loadResPrefabArray(pathArr) {
  258. let p = new Promise((resolve, reject) => {
  259. cc.loader.loadResArray(pathArr, cc.Prefab, (error, resourceArr) => {
  260. if (error) {
  261. console.log(error);
  262. reject(error);
  263. } else {
  264. resolve(resourceArr);
  265. }
  266. });
  267. });
  268. return p;
  269. }
  270. //时间戳格式化为对应文字时间显示
  271. static timeParse(timestamp) {
  272. function zeroize(num) {
  273. return (String(num).length == 1 ? '0' : '') + num;
  274. }
  275. var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
  276. var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
  277. var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
  278. var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
  279. var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
  280. var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
  281. if (timestampDiff < 60) { // 一分钟以内
  282. return "刚刚";
  283. } else if (timestampDiff < 3600) { // 一小时前之内
  284. return Math.floor(timestampDiff / 60) + "分钟前";
  285. } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
  286. return '今天' + H + ':' + zeroize(i);
  287. } else {
  288. var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
  289. if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
  290. return '昨天' + H + ':' + zeroize(i);
  291. } else if (curDate.getFullYear() == Y) {
  292. return m + '月' + d + '日 ' + H + ':' + zeroize(i);
  293. } else {
  294. return Y + '年' + m + '月' + d + '日 ' + H + ':' + zeroize(i);
  295. }
  296. }
  297. };
  298. /**
  299. * 将时间戳转成00:00显示
  300. * @param {floor} t
  301. */
  302. static calculateTime(t) {
  303. // 好像经常会出现时间格式化出错的问题, 这里先这样解决一下
  304. if (t < 0) {
  305. t = 0;
  306. console.log("出现负数的时间");
  307. }
  308. let hour = Math.floor(t / 60 / 60);
  309. let minute = Math.floor(t / 60 % 60);
  310. let second = Math.floor(t % 60);
  311. if (hour != 0 && hour < 10) {
  312. hour = "0" + hour;
  313. }
  314. if (minute < 10) {
  315. minute = "0" + minute;
  316. }
  317. if (second < 10) {
  318. second = "0" + second;
  319. }
  320. if (hour != 0) {
  321. return `${hour}:${minute}:${second}`;
  322. } else {
  323. return `${minute}:${second}`;
  324. }
  325. };
  326. static calculateTimeString(t) {
  327. if (t < 0) {
  328. t = 0;
  329. console.log("出现负数的时间");
  330. }
  331. let hour = Math.floor(t / 60 / 60);
  332. let minute = Math.floor(t / 60 % 60);
  333. let second = Math.floor(t % 60);
  334. if (hour != 0 && hour < 10) {
  335. hour = "0" + hour;
  336. }
  337. if (minute < 10) {
  338. minute = "0" + minute;
  339. }
  340. if (second < 10) {
  341. second = "0" + second;
  342. }
  343. if (hour != 0) {
  344. return `${hour}小时${minute}分${second}秒`;
  345. } else {
  346. return `${minute}分${second}秒`;
  347. }
  348. };
  349. /**
  350. * 获取Url上指定参数
  351. * @param {String} name 参数名
  352. */
  353. static getUrlParam(name) {
  354. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  355. var r = window.location.search.substr(1).match(reg);
  356. if (r != null) {
  357. return unescape(r[2]);
  358. } else {
  359. return null;
  360. }
  361. }
  362. }
  363. module.exports = DWTool;