globals.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. const globals = {
  75. // stores the socket.io server
  76. io: null,
  77. // stores stuff related to the Mongo Database
  78. db: {
  79. // our connection to the database
  80. connection: null,
  81. // the models we've defined in the database
  82. models: null,
  83. // the store express / socket.io uses to store sessions in the database
  84. store: null
  85. },
  86. // various utilities used through out the code base
  87. utils: {
  88. htmlEntities: str => String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'),
  89. generateRandomString: len => {
  90. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");
  91. let result = [];
  92. for (let i = 0; i < len; i++) {
  93. result.push(chars[globals.utils.getRandomNumber(0, chars.length - 1)]);
  94. }
  95. return result.join("");
  96. },
  97. getSocketFromId: function(socketId) {
  98. return globals.io.sockets.sockets[socketId];
  99. },
  100. getRandomNumber: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
  101. convertTime,
  102. Timer
  103. }
  104. };
  105. module.exports = globals;