GameMath.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { gameMethod } from "../common/gameMethod"
  2. import GameDataCenter from "../data/GameDataCenter"
  3. export default class GameMath {
  4. /**
  5. * 保留几位有效小数
  6. * @param decimals 小数
  7. * @param num 位数
  8. */
  9. static keep(decimals: number, num: number): number {
  10. // return Math.floor(decimals * Math.pow(10, num)) / Math.pow(10, num)
  11. // 原方法精度有偏差,这里精度多一位
  12. let val = Math.floor(decimals * Math.pow(10, num + 1))
  13. if (val % 10 > 0) {
  14. // 去余保证最后一位为0
  15. val = val - val % 10
  16. }
  17. return val / Math.pow(10, num + 1)
  18. }
  19. // static unit = 9999
  20. // /**
  21. // * 长数字简化
  22. // * @param num 原数字
  23. // * @param min 大于这个数才简化(要优化成位数并表示简化后长度)
  24. // * @param mathUnit 简化的单位
  25. // * @param unitLen 一个单位表示位数。如万表示4位k表示3位
  26. // */
  27. // static showNum(num: number, min: number = GameMath.unit, mathUnit: Array<string> = GameMath.mathUnit, unitLen: number = 4): string {
  28. // if (gameMethod.isEmpty(num)) {
  29. // return "0"
  30. // }
  31. // if (num > min) {
  32. // let len = Math.floor(Math.log10(num))
  33. // let unit = Math.floor(len / unitLen)
  34. // return this.keep(num / Math.pow(Math.pow(10, unitLen), unit), unitLen - 1 - len % unitLen) + mathUnit[unit - 1]
  35. // } else {
  36. // return num.toString()
  37. // }
  38. // }
  39. static mathUnit = ["万", "亿", "兆", "京", "垓", "杼"]//
  40. // static mathUnit = ["K", "M", "B", "T", "KT", "MT"]//["万", "亿", "兆", "京", "垓", "杼"]//
  41. static showNum(num: number, index: number = 0, MinNum: number = 10000): string {
  42. if (isNaN(num)) {
  43. return "0"
  44. }
  45. let _num = num
  46. let isNeedShow = num / MinNum
  47. num = num / 10000
  48. if (isNeedShow < 1) {
  49. let strList = _num.toString().split('.')
  50. let value = ""
  51. if (strList[1]) {
  52. let count = 5 - strList[0].length
  53. let digit = Math.min(count, 2)
  54. value = strList[1].slice(0, digit)
  55. for (let i = value.length - 1; i >= 0; i--) {
  56. const element = value[i];
  57. if (element == "0") {
  58. value = value.substring(0, i)
  59. } else {
  60. break
  61. }
  62. }
  63. }
  64. if (value.length > 0) {
  65. return strList[0] + "." + value + (index > 0 ? GameMath.mathUnit[index - 1] : "")
  66. } else {
  67. return strList[0] + (index > 0 ? GameMath.mathUnit[index - 1] : "")
  68. }
  69. } else {
  70. return GameMath.showNum(num, index + 1)
  71. }
  72. }
  73. /**
  74. * 判断一个数字有几位数
  75. * @param num 数字
  76. * @returns 位数
  77. */
  78. static getNumberOfDigits(num: number): number {
  79. if (num === 0) {
  80. return 1;
  81. }
  82. return Math.floor(Math.log10(Math.abs(num))) + 1;
  83. }
  84. static addZero(num: number): string {
  85. if (num >= 10) {
  86. return num.toString()
  87. }
  88. return '0' + num.toString()
  89. }
  90. /**
  91. * 获取0点
  92. */
  93. static getDay0(time: number): number {
  94. let date = new Date(time * 1000).setHours(0, 0, 0, 0);//获取今天0点0分0秒0毫秒
  95. return Math.floor(date / 1000);;
  96. }
  97. /**
  98. * 获取每日重置时间
  99. */
  100. static getRstTime(time: number) {
  101. return this.getDay0(time) + 86400
  102. }
  103. /** 获取当天某个时间的时间戳 */
  104. static getTodaySecond(h = 0, m = 0, s = 0) {
  105. var curTime = GameDataCenter.time && GameDataCenter.time.sevTime || 0;
  106. var time = new Date(curTime * 1000).setHours(h, m, s) / 1000;
  107. return time;
  108. }
  109. /**
  110. * 时间戳转换
  111. * @param time 时间戳
  112. * @param format 返回格式,支持自定义,但参数必须与formateArr里保持一致
  113. * 年Y 月M 日D 时h 分m 秒s
  114. */
  115. static formatTime(time: number, format: string) {
  116. let formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  117. let returnArr = [];
  118. let date = new Date(time * 1000);
  119. returnArr.push(date.getFullYear());
  120. returnArr.push(this.formatNumber(date.getMonth() + 1));
  121. returnArr.push(this.formatNumber(date.getDate()));
  122. returnArr.push(this.formatNumber(date.getHours()));
  123. returnArr.push(this.formatNumber(date.getMinutes()));
  124. returnArr.push(this.formatNumber(date.getSeconds()));
  125. for (let i in returnArr) {
  126. format = format.replace(formateArr[i], returnArr[i]);
  127. }
  128. return format;
  129. }
  130. /**
  131. * 个位数十位补零
  132. * @param n 数值
  133. */
  134. static formatNumber(n: number) {
  135. let num = n.toString()
  136. return num[1] ? num : '0' + num
  137. }
  138. /**
  139. * 活跃度进度条
  140. * @param curScore 数值
  141. * @param scoreList 数值列表
  142. * @param nWidth 节点宽度
  143. */
  144. static fgetProgress(curScore: number, scoreList: number[], nWidth: number): number {
  145. let width: number = 0
  146. let index: number = 0
  147. for (let i = 0; i < scoreList.length; i++) {
  148. if (curScore <= scoreList[i]) {
  149. if (i > 0) {
  150. index = curScore - scoreList[i - 1]
  151. width += index * ((nWidth / scoreList.length) / (scoreList[i] - scoreList[i - 1]))
  152. } else {
  153. index = curScore
  154. width += index * ((nWidth / scoreList.length) / (scoreList[i]))
  155. }
  156. return width
  157. } else {
  158. width += scoreList[i] * ((nWidth / scoreList.length) / scoreList[i])
  159. }
  160. }
  161. return width
  162. }
  163. /**数组去重 */
  164. static unique<T>(arr: Array<T>): Array<T> {
  165. const res = new Map();
  166. return arr.filter((a) => !res.has(a) && res.set(a, 1))
  167. }
  168. /**传入某天零点时间戳 获取距离今天有几天 */
  169. public static getDistanceDays(sTime) {
  170. var zeroTime = this.getTodaySecond(0);//当天零点时间
  171. return Math.floor((zeroTime - sTime) / 86400) + 1;
  172. }
  173. /**
  174. * 格式化时间戳 获取 月/日 时:分:秒 2021-5-23 14:26:31
  175. * @param time 时间戳
  176. * @returns
  177. */
  178. public static formatTimeMDHMS(time) {
  179. let timeData = this.formatTimeSTamp(time * 1000);
  180. return `${timeData["month"]}/${timeData["day"]} ${timeData["hour"]}:${timeData["minute"]}:${timeData["second"]}`;
  181. }
  182. public static formatTimeSTamp(ts) {
  183. let timeData = {};
  184. var time = new Date(ts);
  185. timeData["year"] = this.getTimeAddSuffix(time.getFullYear())
  186. timeData["month"] = this.getTimeAddSuffix(time.getMonth() + 1)
  187. timeData["day"] = this.getTimeAddSuffix(time.getDate())
  188. timeData["hour"] = this.getTimeAddSuffix(time.getHours())
  189. timeData["minute"] = this.getTimeAddSuffix(time.getMinutes())
  190. timeData["second"] = this.getTimeAddSuffix(time.getSeconds())
  191. return timeData;
  192. }
  193. public static getTimeAddSuffix(time) {
  194. if (Number(time) < 10) {
  195. return "0" + time;
  196. }
  197. return time;
  198. }
  199. /**
  200. * 获取范围内的随机整数
  201. * @param min
  202. * @param max
  203. * @returns
  204. */
  205. public static getRandomNum(min, max) {
  206. return Math.floor(Math.random() * (max - min) + min)
  207. }
  208. /**
  209. * 获取范围内的随机数
  210. * @param min
  211. * @param max
  212. * @returns
  213. */
  214. public static getRandomFloor(min, max) {
  215. return Math.random() * (max - min) + min
  216. }
  217. /**
  218. * 保留小数点后不显示0
  219. * @param num
  220. * @param divNum
  221. * @param fixedNum
  222. * @returns
  223. */
  224. public static toFixNumFormat(num: number, divisor: number, fixedNum: number) {
  225. // 将数值除以给定的除数
  226. let result = num / divisor;
  227. let intPart = Math.floor(result);
  228. let hasFractionalPart = result !== intPart;
  229. if (hasFractionalPart) {
  230. // 如果有小数部分,则保留两位小数
  231. let formatted = result.toFixed(fixedNum);
  232. // 分割字符串为整数部分和小数部分
  233. let [integerPart, decimalPart] = formatted.split('.');
  234. // 去除小数部分末尾的0
  235. let trimmedDecimalPart = decimalPart.replace(/0+$/, '');
  236. // 如果小数部分被完全去除(即原小数部分只包含0),则返回整数部分
  237. if (trimmedDecimalPart === '') {
  238. return integerPart;
  239. } else {
  240. // 否则,将整数部分和修剪后的小数部分重新组合
  241. return integerPart + '.' + trimmedDecimalPart;
  242. }
  243. } else {
  244. // 如果没有小数部分,则直接返回整数部分
  245. return intPart.toString();
  246. }
  247. }
  248. }