2
0

global.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. const moment = require('moment');
  3. class Timer {
  4. constructor(callback, delay, paused) {
  5. this.callback = callback;
  6. this.timerId = undefined;
  7. this.start = undefined;
  8. this.paused = paused;
  9. this.remaining = moment.duration(delay, "hh:mm:ss").asSeconds() * 1000;
  10. this.timeWhenPaused = 0;
  11. this.timePaused = Date.now();
  12. if (!paused) {
  13. this.resume();
  14. }
  15. }
  16. pause() {
  17. clearTimeout(this.timerId);
  18. this.remaining -= Date.now() - this.start;
  19. this.timePaused = Date.now();
  20. this.paused = true;
  21. }
  22. ifNotPaused() {
  23. if (!this.paused) {
  24. this.resume();
  25. }
  26. }
  27. resume() {
  28. this.start = Date.now();
  29. clearTimeout(this.timerId);
  30. this.timerId = setTimeout(this.callback, this.remaining);
  31. this.timeWhenPaused = Date.now() - this.timePaused;
  32. this.paused = false;
  33. }
  34. resetTimeWhenPaused() {
  35. this.timeWhenPaused = 0;
  36. }
  37. getTimePaused() {
  38. if (!this.paused) {
  39. return this.timeWhenPaused;
  40. } else {
  41. return Date.now() - this.timePaused;
  42. }
  43. }
  44. }
  45. function getRandomNumber(min, max) {
  46. return Math.floor(Math.random() * (max - min + 1)) + min;
  47. }
  48. function convertTime(duration) {
  49. let a = duration.match(/\d+/g);
  50. if (duration.indexOf('M') >= 0 && duration.indexOf('H') == -1 && duration.indexOf('S') == -1) {
  51. a = [0, a[0], 0];
  52. }
  53. if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1) {
  54. a = [a[0], 0, a[1]];
  55. }
  56. if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1 && duration.indexOf('S') == -1) {
  57. a = [a[0], 0, 0];
  58. }
  59. duration = 0;
  60. if (a.length == 3) {
  61. duration = duration + parseInt(a[0]) * 3600;
  62. duration = duration + parseInt(a[1]) * 60;
  63. duration = duration + parseInt(a[2]);
  64. }
  65. if (a.length == 2) {
  66. duration = duration + parseInt(a[0]) * 60;
  67. duration = duration + parseInt(a[1]);
  68. }
  69. if (a.length == 1) {
  70. duration = duration + parseInt(a[0]);
  71. }
  72. let hours = Math.floor(duration / 3600);
  73. let minutes = Math.floor(duration % 3600 / 60);
  74. let seconds = Math.floor(duration % 3600 % 60);
  75. return (hours < 10 ? ("0" + hours + ":") : (hours + ":")) + (minutes < 10 ? ("0" + minutes + ":") : (minutes + ":")) + (seconds < 10 ? ("0" + seconds) : seconds); // 00:00:23
  76. // return moment.duration(final, "hh:mm:ss").asSeconds(); // 23
  77. }
  78. module.exports = {
  79. io: null, // Socket.io
  80. db: null, // Database
  81. htmlEntities: str => {
  82. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  83. },
  84. getRandomNumber,
  85. convertTime,
  86. generateRandomString: len => {
  87. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");
  88. let result = [];
  89. for (let i = 0; i < len; i++) {
  90. result.push(chars[getRandomNumber(0, chars.length - 1)]);
  91. }
  92. return result.join("");
  93. },
  94. getSocketFromId: function(socketId) {
  95. return this.io.sockets.sockets[socketId];
  96. },
  97. Timer
  98. };