utils.js 5.3 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. if (cookieString) 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(socketId) {
  115. let ns = io.io.of("/");
  116. if (ns) {
  117. return ns.connected[socketId];
  118. }
  119. },
  120. socketsFromUser: function(userId, cb) {
  121. let ns = io.io.of("/");
  122. let sockets = [];
  123. if (ns) {
  124. let total = Object.keys(ns.connected).length;
  125. let done = 0;
  126. for (let id in ns.connected) {
  127. let session = ns.connected[id].session;
  128. cache.hget('sessions', session.sessionId, (err, session) => {
  129. if (!err && session && session.userId === userId) {
  130. sockets.push(ns.connected[id]);
  131. }
  132. checkComplete();
  133. });
  134. }
  135. function checkComplete() {
  136. done++;
  137. if (done === total) {
  138. cb(sockets);
  139. }
  140. }
  141. }
  142. },
  143. socketLeaveRooms: function(socketid) {
  144. let socket = this.socketFromSession(socketid);
  145. let rooms = socket.rooms;
  146. for (let j = 0; j < rooms.length; j++) {
  147. socket.leave(rooms[j]);
  148. }
  149. },
  150. socketJoinRoom: function(socketId, room) {
  151. let socket = this.socketFromSession(socketId);
  152. let rooms = socket.rooms;
  153. for (let j = 0; j < rooms.length; j++) {
  154. socket.leave(rooms[j]);
  155. }
  156. socket.join(room);
  157. },
  158. socketJoinSongRoom: function(socketId, room) {
  159. let socket = this.socketFromSession(socketId);
  160. let rooms = socket.rooms;
  161. for (let j = 0; j < rooms.length; j++) {
  162. if (socket.indexOf('song.') !== -1) {
  163. socket.leave(rooms[j]);
  164. }
  165. }
  166. socket.join(room);
  167. },
  168. socketsJoinSongRoom: function(sockets, room) {
  169. for (let id in sockets) {
  170. let socket = sockets[id];
  171. let rooms = socket.rooms;
  172. for (let roomId in rooms) {
  173. console.log(roomId);
  174. if (roomId.indexOf('song.') !== -1) {
  175. socket.leave(roomId);
  176. }
  177. }
  178. socket.join(room);
  179. }
  180. },
  181. socketsLeaveSongRooms: function(sockets) {
  182. for (let id in sockets) {
  183. let socket = sockets[id];
  184. let rooms = socket.rooms;
  185. for (let roomId in rooms) {
  186. console.log(roomId);
  187. if (roomId.indexOf('song.') !== -1) {
  188. socket.leave(roomId);
  189. }
  190. }
  191. }
  192. }
  193. };