TapTapTool.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // {
  2. // n:最大12位数子的金币数/
  3. // e:科学计数法的等级 1 A ....26 Z 27AA.............56AZ
  4. // }
  5. const GameNotificationKey = require('./GameEnum').GameNotificationKey;
  6. class TapTapTool {
  7. //// 有个时候输入的不是对象。。。可能是字符串或者数字 这里做一个兼容
  8. static toNormal(coinCount) {
  9. /// 不是正常的金币对象
  10. let newCoin = coinCount;
  11. if (coinCount.n == undefined) {
  12. newCoin = TapTapTool.goldStrToClass(coinCount + '');
  13. /// 说明是数字
  14. if (newCoin.n == undefined) {
  15. newCoin = {'n': newCoin, 'e': 0};
  16. }
  17. }
  18. return {'e': newCoin.e, 'n': Number(newCoin.n)};
  19. }
  20. static add(coin1, coin2) {
  21. coin1 = TapTapTool.toNormal(coin1);
  22. coin2 = TapTapTool.toNormal(coin2);
  23. let e1 = coin1.e;
  24. let e2 = coin2.e;
  25. let n1 = coin1.n;
  26. let n2 = coin2.n;
  27. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  28. console.log("请输入正确的格式计算");
  29. return {};
  30. }
  31. let resultN = 0;
  32. let resultE = 0;
  33. let n1Length = `${n1}`.length;
  34. let n2length = `${n2}`.length;
  35. /// 说明两个是一个等级的 直接相加
  36. if(e1 == e2) {
  37. resultN = n1 + n2;
  38. resultE = e1;
  39. /// 如果超出了12位,那么进位。
  40. if (`${resultN}`.length > 12) {
  41. resultN = parseInt(resultN / 1000);
  42. resultE += 1;
  43. }
  44. } else if (e1 > e2) {
  45. let dif = e1 - e2;
  46. resultN = n1;
  47. resultE = e1;
  48. /// 只计算三位以内的
  49. if (dif < 4) {
  50. /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
  51. let num = 10 ** (dif * 3);
  52. /// 如果是这种情况就只能小化大
  53. if (n1Length > (12 - dif * 3)) {
  54. let newN2 = parseInt(n2 / num);
  55. ////说明可以加。
  56. if (newN2 > 0) {
  57. resultN = n1 + newN2;
  58. let resultStr = `${resultN}`;
  59. /// 如果超出了12位,那么进位。
  60. if (resultStr.length > 12) {
  61. resultN = parseInt(resultN / 1000);
  62. resultE += 1;
  63. }
  64. }
  65. //// 可以大化小
  66. } else {
  67. let newN1 = num * n1;
  68. resultN = newN1 + n2;
  69. resultE = e2;
  70. let resultStr = `${resultN}`;
  71. /// 如果超出了12位,那么进位。
  72. if (resultStr.length > 12) {
  73. resultN = parseInt(resultN / 1000);
  74. resultE += 1;
  75. }
  76. }
  77. }
  78. } else {
  79. let dif = e2 - e1;
  80. resultN = n2;
  81. resultE = e2;
  82. /// 只计算三位以内的
  83. if (dif < 4) {
  84. /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
  85. let num = 10 ** (dif * 3);
  86. if (n2length > (12 - dif * 3)) {
  87. let newN1 = parseInt(n1 / num);
  88. ////说明可以加。
  89. if (newN1 > 0) {
  90. resultN = n2 + newN1;
  91. let resultStr = `${resultN}`;
  92. /// 如果超出了12位,那么进位。
  93. if (resultStr.length > 12) {
  94. resultN = parseInt(resultN / 1000);
  95. resultE += 1;
  96. }
  97. }
  98. } else {
  99. let newN2 = num * n2;
  100. resultN = newN2 + n1;
  101. resultE = e1;
  102. let resultStr = `${resultN}`;
  103. /// 如果超出了12位,那么进位。
  104. if (resultStr.length > 12) {
  105. resultN = parseInt(resultN / 1000);
  106. resultE += 1;
  107. }
  108. }
  109. }
  110. }
  111. return {'n': resultN, 'e': resultE};
  112. }
  113. /// 这里都是用大的减去小的
  114. static sub(coin1, coin2) {
  115. coin1 = TapTapTool.toNormal(coin1);
  116. coin2 = TapTapTool.toNormal(coin2);
  117. let e1 = coin1.e;
  118. let e2 = coin2.e;
  119. let n1 = coin1.n;
  120. let n2 = coin2.n;
  121. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  122. console.log("请输入正确的格式计算");
  123. return {};
  124. }
  125. let resultN = 0;
  126. let resultE = 0;
  127. let n1Length = `${n1}`.length;
  128. let n2Length = `${n2}`.length;
  129. if (e1 >= e2 && n1 >= n2) {
  130. let dif = e1 - e2;
  131. resultN = n1;
  132. resultE = e1;
  133. if (dif < 4) {
  134. /// 10的dif*3次幂 把n1化为跟n2一个等级或者n2 -> n1 然后直接相加
  135. let num = 10 ** (dif * 3);
  136. /// 如果是这种情况就只能化n2了
  137. if (n1Length > (12 - dif * 3)) {
  138. let newN2 = parseInt(n2 / num);
  139. ////说明可以加。
  140. if (newN2 > 0) {
  141. resultN = n1 - newN2;
  142. }
  143. } else {
  144. let newN1 = num * n1;
  145. resultN = newN1 - n2;
  146. resultE = e2;
  147. }
  148. }
  149. } else if (e1 >= e2 && n1 < n2) {
  150. if (e1 == e2) {
  151. resultE = e1;
  152. resultN = n2 - n1;
  153. } else {
  154. let dif = e1 - e2;
  155. resultN = n1;
  156. resultE = e1;
  157. if (dif < 4) {
  158. if (n1Length > (12 - dif * 3)) {
  159. let newN2 = parseInt(n2 / 10 ** (3 * (e1 - e2)));
  160. if (n1 >= newN2) {
  161. resultN = n1 - newN2;
  162. } else {
  163. resultN = newN2 - n1;
  164. }
  165. } else {
  166. let newN1 = n1 * 10 ** (3 * (e1 - e2));
  167. if (newN1 >= n2) {
  168. resultN = newN1 - n2;
  169. } else {
  170. resultN = n2 - newN1;
  171. }
  172. resultE = e2;
  173. }
  174. }
  175. }
  176. } else if (e1 < e2 && n1 <= n2) {
  177. let dif = e2 - e1;
  178. resultN = n2;
  179. resultE = e2;
  180. if (dif < 4) {
  181. let num = 10 ** (dif * 3);
  182. if (n2Length > (12 - dif * 3)) {
  183. let newN1 = parseInt(n1 / num);
  184. if (newN1 > 0) {
  185. resultN = n2 - newN1;
  186. }
  187. } else {
  188. let newN2 = num * n2;
  189. resultN = newN2 - n1;
  190. resultE = e1;
  191. }
  192. }
  193. } else if (e1 < e2 && n1 > n2) {
  194. let dif = e2 - e1;
  195. resultN = n2;
  196. resultE = e2;
  197. if (dif < 4) {
  198. if (n2Length > (12 - dif * 3)) {
  199. let newN1 = parseInt(n2 / 10 ** (3 * dif));
  200. if (n2 >= newN1) {
  201. resultN = n2 - newN1;
  202. } else {
  203. resultN = newN1 - n2;
  204. }
  205. } else {
  206. let newN2 = n2 * 10 ** (3 * dif);
  207. if (newN2 >= n1) {
  208. resultN = newN2 - n2;
  209. } else {
  210. resultN = n1 - newN2;
  211. }
  212. resultE = e1;
  213. }
  214. }
  215. }
  216. return {'n': resultN, 'e': resultE};
  217. }
  218. /// 乘法 变成最大六位乘以六位的乘法, 因为9999 * 9999 最大也只有11位
  219. /// 小数的乘法,最后也会变成整数表示
  220. static multiple(coin1, coin2) {
  221. coin1 = TapTapTool.toNormal(coin1);
  222. coin2 = TapTapTool.toNormal(coin2);
  223. let e1 = coin1.e;
  224. let e2 = coin2.e;
  225. let n1 = coin1.n;
  226. let n2 = coin2.n;
  227. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  228. console.log("请输入正确的格式计算");
  229. return {};
  230. }
  231. let resultN = 0;
  232. let resultE = e1 + e2;
  233. let n1Str = n1 + '';
  234. let n2Str = n2 + '';
  235. let n1Arr = n1Str.split('.');
  236. let n2Arr = n2Str.split('.');
  237. let n1Length = n1Str.length;
  238. let n2length = n2Str.length;
  239. //// 这里是小数运算
  240. if (n1Arr.length > 1 || n2Arr.length > 1) {
  241. let count1Length = n1Arr[0].length;
  242. let count2Length = n2Arr[0].length;
  243. let newN1 = n1;
  244. let newN2 = n2;
  245. if (count1Length > 6) {
  246. //// 减去的要配成3的倍数 才能
  247. let sub = (count1Length - 6);
  248. let rm = sub % 3;
  249. /// 说明去掉的不是3的倍数 那就让它成为3的倍数
  250. if (rm != 0) {
  251. sub += 3 - rm;
  252. }
  253. newN1 = parseInt(parseInt(n1Arr[0]) / 10 ** sub);
  254. resultE += (sub / 3);
  255. }
  256. if (count2Length > 6) {
  257. //// 减去的要配成3的倍数 才能
  258. let sub = (count2Length - 6);
  259. let rm = sub % 3;
  260. /// 说明去掉的不是3的倍数 那就让它成为3的倍数
  261. if (rm != 0) {
  262. sub += 3 - rm;
  263. }
  264. newN2 = parseInt(parseInt(n2Arr[0]) / 10 ** sub);
  265. resultE += (sub / 3);
  266. }
  267. resultN = newN1 * newN2;
  268. let resultStr = resultN + '';
  269. let resultArr = resultStr.split('.');
  270. /// 说明是小数
  271. if (resultArr.length > 1) {
  272. if (resultE > 0 && resultStr.length < 13) {
  273. resultN *= 1000;
  274. resultE -= 1;
  275. }
  276. }
  277. return {'n': parseInt(resultN), 'e': resultE};
  278. }
  279. /// 下面的只是计算n的相乘 最大6位乘以6位
  280. let newN1 = n1;
  281. let newN2 = n2;
  282. if (n1Length > 6) {
  283. //// 减去的要配成3的倍数 才能
  284. let sub = (n1Length - 6);
  285. let rm = sub % 3;
  286. /// 说明去掉的不是3的倍数 那就让它成为3的倍数
  287. if (rm != 0) {
  288. sub += 3 - rm;
  289. }
  290. newN1 = parseInt(n1 / 10 ** sub);
  291. resultE += (sub / 3);
  292. }
  293. if (n2length > 6) {
  294. //// 减去的要配成3的倍数 才能
  295. let sub = (n2length - 6);
  296. let rm = sub % 3;
  297. /// 说明去掉的不是3的倍数 那就让它成为3的倍数
  298. if (rm != 0) {
  299. sub += 3 - rm;
  300. }
  301. newN2 = parseInt(n2 / 10 ** sub);
  302. resultE += (sub / 3);
  303. }
  304. resultN = newN1 * newN2;
  305. let result = {"n": resultN, "e": resultE};
  306. return result;
  307. }
  308. //// 这里默认用大的除以小的
  309. static division(coin1, coin2) {
  310. coin1 = TapTapTool.toNormal(coin1);
  311. coin2 = TapTapTool.toNormal(coin2);
  312. let e1 = coin1.e;
  313. let e2 = coin2.e;
  314. let n1 = coin1.n;
  315. let n2 = coin2.n;
  316. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  317. console.log("请输入正确的格式计算");
  318. return {};
  319. }
  320. let resultN = 0;
  321. let resultE = 0;
  322. if (e1 >= e2) {
  323. resultE = e1 - e2;
  324. resultN = parseInt(n1 / n2);
  325. } else {
  326. resultE = e2 - e1;
  327. resultN = parseInt(n2 / n1);
  328. }
  329. return {"n": resultN, "e": resultE};
  330. }
  331. /// 除法返回比例 第一个除以第二个的比例
  332. static divisionToRatio(coin1, coin2) {
  333. coin1 = TapTapTool.toNormal(coin1);
  334. coin2 = TapTapTool.toNormal(coin2);
  335. let e1 = coin1.e;
  336. let e2 = coin2.e;
  337. let n1 = coin1.n;
  338. let n2 = coin2.n;
  339. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  340. console.log("请输入正确的格式计算");
  341. return {};
  342. }
  343. let resultN = n1 / n2;
  344. let resultE = e1 - e2;
  345. return resultN * (10 ** (3 * resultE));
  346. }
  347. //// 传入coinCount成为显示的科学计数法
  348. static parseToString(coinCount) {
  349. coinCount = TapTapTool.toNormal(coinCount);
  350. let n = coinCount.n;
  351. let e = coinCount.e;
  352. if (n == undefined || e == undefined) {
  353. console.log("请输入正确的格式计算");
  354. return {};
  355. }
  356. if (n == 0) {
  357. return "0";
  358. }
  359. let resultN = 0;
  360. let resultE = 0;
  361. let nLength = `${n}`.length;
  362. let rm = nLength % 3;
  363. /// 位数正好是3的整数倍 说明前面有三位啦
  364. if (rm == 0) {
  365. if (nLength == 3) {
  366. resultN = n;
  367. if (e > 0) {
  368. resultN = parseFloat(n).toFixed(3);
  369. }
  370. resultE = e;
  371. } else {
  372. /// 保留三位小数
  373. resultN = (n / 10 ** (nLength - 3)).toFixed(3);
  374. resultE = e + (nLength - 3) / 3;
  375. }
  376. } else {
  377. if (nLength < 4) {
  378. let count = e > 0 ? 3 : 0;
  379. resultN = (n / 10 ** (nLength - rm)).toFixed(count);
  380. } else {
  381. resultN = (n / 10 ** (nLength - rm)).toFixed(3);
  382. }
  383. resultE = e + (nLength - rm) / 3;
  384. }
  385. if (resultE == 0) {
  386. return resultN;
  387. }
  388. return resultN + TapTapTool.toRankString(resultE);
  389. }
  390. ///根据e获取等级 A B
  391. static toRankString(e) {
  392. let rk = e / 26;
  393. if (rk <= 1) {
  394. return TapTapTool._rankMap[e - 1];
  395. } else {
  396. return TapTapTool._rankMap[parseInt(rk) - 1] + TapTapTool._rankMap[(e - 1) % 26];
  397. }
  398. }
  399. //// 比较两个数字的大小 count1 >= count2 返回true
  400. static compare(count1, count2) {
  401. count1 = TapTapTool.toNormal(count1);
  402. count2 = TapTapTool.toNormal(count2);
  403. let e1 = count1.e;
  404. let e2 = count2.e;
  405. let n1 = count1.n;
  406. let n2 = count2.n;
  407. if (e1 == undefined || e2 == undefined || n1 == undefined || n2 == undefined) {
  408. console.log("请输入正确的格式计算");
  409. return {};
  410. }
  411. if (e1 >= e2 && n1 >= n2) {
  412. return true;
  413. } else if (e1 >= e2 && n1 < n2) {
  414. if (e1 == e2) {
  415. return false;
  416. } else {
  417. if (e1 - e2 > 4) {
  418. return true;
  419. }
  420. let newN2 = n2 / 10 ** (3 * (e1 - e2));
  421. return n1 >= newN2;
  422. }
  423. } else if (e1 < e2 && n1 <= n2) {
  424. return false;
  425. } else if (e1 < e2 && n1 > n2) {
  426. if (e2 - e1 > 4) {
  427. return false;
  428. }
  429. let sub = 3 * (e2 - e1);
  430. let mu = 10 ** sub;
  431. let mul = n1 / mu;
  432. return mul >= n2;
  433. }
  434. return false;
  435. }
  436. /// 金币的字符串转化为正常金币对象,如果没有';'那就直接返回数字 '2133;1'
  437. static goldStrToClass(goldStr) {
  438. let countArr = goldStr.split(';');
  439. let n = 0;
  440. let e = 0;
  441. if (countArr.length > 1 && countArr[1] != '') {
  442. n = parseFloat(countArr[0]);
  443. e = parseInt(countArr[1]);
  444. let nStr = "" + n;
  445. /// 说明是小数
  446. if (nStr.indexOf('.') !== -1) {
  447. ////如果等级是0 那么直接去掉小数。。。。因为基本没有影响
  448. /// /// 说明位数小于等于13位 保留小数 退位
  449. if (e > 0 && n.length <= 13) {
  450. n = n * 1000;
  451. e -= 1;
  452. }
  453. }
  454. } else if (countArr[1] == '') {
  455. e = 0;
  456. n = parseInt(countArr[0]);
  457. //// 说明没有;符号 那么直接返回这个整型
  458. } else {
  459. return parseInt(goldStr);
  460. }
  461. return {'e': e, 'n': parseInt(n)};
  462. }
  463. //// 下面这个方法只是针对1.07的几次幂
  464. static toPower(e) {
  465. let n = 1.07;
  466. if (e <= 250) {
  467. return {'n': parseFloat(Math.pow(n, e).toFixed(3)), 'e': 0};
  468. } else {
  469. //// 向下取整
  470. let count = Math.floor(e / 250);
  471. let result = {'n': parseFloat(Math.pow(n, 250).toFixed(3)), 'e': 0};
  472. for (let i = 0; i < count - 1; i ++) {
  473. result = TapTapTool.multiple(result, {'n': parseFloat(Math.pow(n, 250).toFixed(3)), 'e': 0});
  474. }
  475. let rm = e % 250;
  476. if (rm != 0) {
  477. result = TapTapTool.multiple(result, {'n': parseFloat(Math.pow(n, rm).toFixed(3)) , 'e': 0});
  478. }
  479. return result;
  480. }
  481. }
  482. /// 删除游戏中的红点数 传入GameRedDot
  483. static removeRedDot(gameRedDotType) {
  484. let isContain = false;
  485. for (let i = 0; i < 10; ++i) {
  486. let index = GameGlobal._redTypes.indexOf(gameRedDotType);
  487. if (index == -1) {
  488. break;
  489. ///说明里面有包含这个红点
  490. } else {
  491. GameGlobal._redTypes.splice(index, 1);
  492. isContain = true;
  493. }
  494. }
  495. if (isContain) {
  496. GameEvent.fire(GameNotificationKey.GameRedDotUpdate);
  497. }
  498. }
  499. static _rankMap = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W','X','Y','Z'];
  500. }
  501. module.exports = TapTapTool;