utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 convertTime (duration) {
  46. let a = duration.match(/\d+/g);
  47. if (duration.indexOf('M') >= 0 && duration.indexOf('H') == -1 && duration.indexOf('S') == -1) {
  48. a = [0, a[0], 0];
  49. }
  50. if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1) {
  51. a = [a[0], 0, a[1]];
  52. }
  53. if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1 && duration.indexOf('S') == -1) {
  54. a = [a[0], 0, 0];
  55. }
  56. duration = 0;
  57. if (a.length == 3) {
  58. duration = duration + parseInt(a[0]) * 3600;
  59. duration = duration + parseInt(a[1]) * 60;
  60. duration = duration + parseInt(a[2]);
  61. }
  62. if (a.length == 2) {
  63. duration = duration + parseInt(a[0]) * 60;
  64. duration = duration + parseInt(a[1]);
  65. }
  66. if (a.length == 1) {
  67. duration = duration + parseInt(a[0]);
  68. }
  69. let hours = Math.floor(duration / 3600);
  70. let minutes = Math.floor(duration % 3600 / 60);
  71. let seconds = Math.floor(duration % 3600 % 60);
  72. return (hours < 10 ? ("0" + hours + ":") : (hours + ":")) + (minutes < 10 ? ("0" + minutes + ":") : (minutes + ":")) + (seconds < 10 ? ("0" + seconds) : seconds);
  73. }
  74. module.exports = {
  75. htmlEntities: str => String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'),
  76. generateRandomString: function(len) {
  77. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");
  78. let result = [];
  79. for (let i = 0; i < len; i++) {
  80. result.push(chars[this.getRandomNumber(0, chars.length - 1)]);
  81. }
  82. return result.join("");
  83. },
  84. getSocketFromId: function(socketId) {
  85. return globals.io.sockets.sockets[socketId];
  86. },
  87. getRandomNumber: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
  88. convertTime,
  89. Timer,
  90. guid: () => [1,1,0,1,0,1,0,1,0,1,1,1].map(b => b ? Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1) : '-').join('')
  91. };