utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. const moment = require('moment'),
  3. io = require('./io');
  4. class Timer {
  5. constructor(callback, delay, paused) {
  6. this.callback = callback;
  7. this.timerId = undefined;
  8. this.start = undefined;
  9. this.paused = paused;
  10. this.remaining = moment.duration(delay, "hh:mm:ss").asSeconds() * 1000;
  11. this.timeWhenPaused = 0;
  12. this.timePaused = Date.now();
  13. if (!paused) {
  14. this.resume();
  15. }
  16. }
  17. pause() {
  18. clearTimeout(this.timerId);
  19. this.remaining -= Date.now() - this.start;
  20. this.timePaused = Date.now();
  21. this.paused = true;
  22. }
  23. ifNotPaused() {
  24. if (!this.paused) {
  25. this.resume();
  26. }
  27. }
  28. resume() {
  29. this.start = Date.now();
  30. clearTimeout(this.timerId);
  31. this.timerId = setTimeout(this.callback, this.remaining);
  32. this.timeWhenPaused = Date.now() - this.timePaused;
  33. this.paused = false;
  34. }
  35. resetTimeWhenPaused() {
  36. this.timeWhenPaused = 0;
  37. }
  38. getTimePaused() {
  39. if (!this.paused) {
  40. return this.timeWhenPaused;
  41. } else {
  42. return Date.now() - this.timePaused;
  43. }
  44. }
  45. }
  46. function convertTime (duration) {
  47. let a = duration.match(/\d+/g);
  48. if (duration.indexOf('M') >= 0 && duration.indexOf('H') == -1 && duration.indexOf('S') == -1) {
  49. a = [0, a[0], 0];
  50. }
  51. if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1) {
  52. a = [a[0], 0, a[1]];
  53. }
  54. if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1 && duration.indexOf('S') == -1) {
  55. a = [a[0], 0, 0];
  56. }
  57. duration = 0;
  58. if (a.length == 3) {
  59. duration = duration + parseInt(a[0]) * 3600;
  60. duration = duration + parseInt(a[1]) * 60;
  61. duration = duration + parseInt(a[2]);
  62. }
  63. if (a.length == 2) {
  64. duration = duration + parseInt(a[0]) * 60;
  65. duration = duration + parseInt(a[1]);
  66. }
  67. if (a.length == 1) {
  68. duration = duration + parseInt(a[0]);
  69. }
  70. let hours = Math.floor(duration / 3600);
  71. let minutes = Math.floor(duration % 3600 / 60);
  72. let seconds = Math.floor(duration % 3600 % 60);
  73. return (hours < 10 ? ("0" + hours + ":") : (hours + ":")) + (minutes < 10 ? ("0" + minutes + ":") : (minutes + ":")) + (seconds < 10 ? ("0" + seconds) : seconds);
  74. }
  75. module.exports = {
  76. htmlEntities: str => String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'),
  77. generateRandomString: function(len) {
  78. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");
  79. let result = [];
  80. for (let i = 0; i < len; i++) {
  81. result.push(chars[this.getRandomNumber(0, chars.length - 1)]);
  82. }
  83. return result.join("");
  84. },
  85. getSocketFromId: function(socketId) {
  86. return globals.io.sockets.sockets[socketId];
  87. },
  88. getRandomNumber: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
  89. convertTime,
  90. Timer,
  91. 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(''),
  92. cookies: {
  93. parseCookies: cookieString => {
  94. let cookies = {};
  95. cookieString.split("; ").map((cookie) => {
  96. (cookies[cookie.substring(0, cookie.indexOf("="))] = cookie.substring(cookie.indexOf("=") + 1, cookie.length));
  97. });
  98. return cookies;
  99. },
  100. toString: cookies => {
  101. let newCookie = [];
  102. for (let prop in cookie) {
  103. newCookie.push(prop + "=" + cookie[prop]);
  104. }
  105. return newCookie.join("; ");
  106. },
  107. removeCookie: (cookieString, cookieName) => {
  108. var cookies = this.parseCookies(cookieString);
  109. delete cookies[cookieName];
  110. return this.toString(cookies);
  111. }
  112. },
  113. socketFromSession: function(sessionId) {
  114. let ns = io.io.of("/");
  115. if (ns) {
  116. for (let id in ns.connected) {
  117. if (ns.connected[id].sessionId === sessionId) {
  118. return ns.connected[id];
  119. }
  120. }
  121. }
  122. },
  123. socketLeaveRooms: function(sessionId, room) {
  124. let socket = this.socketFromSession(sessionId);
  125. let rooms = socket.rooms;
  126. for (let j = 0; j < rooms.length; j++) {
  127. socket.leave(rooms[j]);
  128. }
  129. },
  130. socketJoinRoom: function(sessionId, room) {
  131. let socket = this.socketFromSession(sessionId);
  132. //console.log(io.io.sockets[socket.id]);
  133. let rooms = socket.rooms;
  134. for (let j = 0; j < rooms.length; j++) {
  135. socket.leave(rooms[j]);
  136. }
  137. socket.join(room);
  138. }
  139. };