global.js 2.7 KB

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