utils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. 'use strict';
  2. const moment = require('moment'),
  3. io = require('./io'),
  4. cache = require('./cache');
  5. class Timer {
  6. constructor(callback, delay, paused) {
  7. this.callback = callback;
  8. this.timerId = undefined;
  9. this.start = undefined;
  10. this.paused = paused;
  11. this.remaining = moment.duration(delay, "hh:mm:ss").asSeconds() * 1000;
  12. this.timeWhenPaused = 0;
  13. this.timePaused = Date.now();
  14. if (!paused) {
  15. this.resume();
  16. }
  17. }
  18. pause() {
  19. clearTimeout(this.timerId);
  20. this.remaining -= Date.now() - this.start;
  21. this.timePaused = Date.now();
  22. this.paused = true;
  23. }
  24. ifNotPaused() {
  25. if (!this.paused) {
  26. this.resume();
  27. }
  28. }
  29. resume() {
  30. this.start = Date.now();
  31. clearTimeout(this.timerId);
  32. this.timerId = setTimeout(this.callback, this.remaining);
  33. this.timeWhenPaused = Date.now() - this.timePaused;
  34. this.paused = false;
  35. }
  36. resetTimeWhenPaused() {
  37. this.timeWhenPaused = 0;
  38. }
  39. getTimePaused() {
  40. if (!this.paused) {
  41. return this.timeWhenPaused;
  42. } else {
  43. return Date.now() - this.timePaused;
  44. }
  45. }
  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 < 10 ? ("0" + hours + ":") : (hours + ":")) + (minutes < 10 ? ("0" + minutes + ":") : (minutes + ":")) + (seconds < 10 ? ("0" + seconds) : seconds);
  75. }
  76. module.exports = {
  77. htmlEntities: str => String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'),
  78. generateRandomString: function(len) {
  79. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");
  80. let result = [];
  81. for (let i = 0; i < len; i++) {
  82. result.push(chars[this.getRandomNumber(0, chars.length - 1)]);
  83. }
  84. return result.join("");
  85. },
  86. getSocketFromId: function(socketId) {
  87. return globals.io.sockets.sockets[socketId];
  88. },
  89. getRandomNumber: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
  90. convertTime,
  91. Timer,
  92. 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(''),
  93. cookies: {
  94. parseCookies: cookieString => {
  95. let cookies = {};
  96. cookieString.split("; ").map((cookie) => {
  97. (cookies[cookie.substring(0, cookie.indexOf("="))] = cookie.substring(cookie.indexOf("=") + 1, cookie.length));
  98. });
  99. return cookies;
  100. },
  101. toString: cookies => {
  102. let newCookie = [];
  103. for (let prop in cookie) {
  104. newCookie.push(prop + "=" + cookie[prop]);
  105. }
  106. return newCookie.join("; ");
  107. },
  108. removeCookie: (cookieString, cookieName) => {
  109. var cookies = this.parseCookies(cookieString);
  110. delete cookies[cookieName];
  111. return this.toString(cookies);
  112. }
  113. },
  114. socketFromSession: function(sessionId) {
  115. let ns = io.io.of("/");
  116. if (ns) {
  117. for (let id in ns.connected) {
  118. if (ns.connected[id].sessionId === sessionId) {
  119. return ns.connected[id];
  120. }
  121. }
  122. }
  123. },
  124. socketsFromUser: function(userId, cb) {
  125. let ns = io.io.of("/");
  126. let sockets = [];
  127. if (ns) {
  128. let total = Object.keys(ns.connected).length;
  129. let done = 0;
  130. for (let id in ns.connected) {
  131. let sessionId = ns.connected[id].sessionId;
  132. if (sessionId) {
  133. cache.hget('sessions', sessionId, (err, session) => {
  134. if (!err && session && session.userSessionId) {
  135. cache.hget('userSessions', session.userSessionId, (err, userSession) => {
  136. if (!err && userSession && userSession.userId === userId) {
  137. sockets.push(ns.connected[id]);
  138. }
  139. checkComplete();
  140. })
  141. } else checkComplete();
  142. });
  143. } else checkComplete();
  144. }
  145. function checkComplete() {
  146. done++;
  147. if (done === total) {
  148. cb(sockets);
  149. }
  150. }
  151. }
  152. },
  153. socketLeaveRooms: function(sessionId) {
  154. let socket = this.socketFromSession(sessionId);
  155. let rooms = socket.rooms;
  156. for (let j = 0; j < rooms.length; j++) {
  157. socket.leave(rooms[j]);
  158. }
  159. },
  160. socketJoinRoom: function(sessionId, room) {
  161. let socket = this.socketFromSession(sessionId);
  162. //console.log(io.io.sockets[socket.id]);
  163. let rooms = socket.rooms;
  164. for (let j = 0; j < rooms.length; j++) {
  165. socket.leave(rooms[j]);
  166. }
  167. socket.join(room);
  168. },
  169. socketJoinSongRoom: function(sessionId, room) {
  170. let socket = this.socketFromSession(sessionId);
  171. //console.log(io.io.sockets[socket.id]);
  172. let rooms = socket.rooms;
  173. for (let j = 0; j < rooms.length; j++) {
  174. if (socket.indexOf('song.') !== -1) {
  175. socket.leave(rooms[j]);
  176. }
  177. }
  178. socket.join(room);
  179. },
  180. socketsJoinSongRoom: function(sockets, room) {
  181. for (let id in sockets) {
  182. let socket = sockets[id];
  183. let rooms = socket.rooms;
  184. for (let roomId in rooms) {
  185. console.log(roomId);
  186. if (roomId.indexOf('song.') !== -1) {
  187. socket.leave(roomId);
  188. }
  189. }
  190. socket.join(room);
  191. }
  192. }
  193. };