Advertisement
Kotka

Untitled

Dec 17th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. function Channel() {
  2. EventEmitter.call(this);
  3. this.callbacks = [];
  4. this.messages = [];
  5. this.channels = [];
  6. this.listSs = [];
  7. this.chCfg = require("./config").Config("CHANNEL");
  8. var a = this;
  9. setInterval(function () {
  10. var b = new Date;
  11. for(var c in a.callbacks) {
  12. while(a.callbacks[c].length > 0 && b - a.callbacks[c][0].timestamp > a.chCfg.callback_timeout) a.callbacks[c].shift().callback([])
  13. }
  14. }, 5e3)
  15. }
  16. var util = require("util");
  17. var EventEmitter = require("events").EventEmitter;
  18. util.inherits(Channel, EventEmitter);
  19. module.exports = Channel;
  20. Channel.prototype.createChannel = function (a, b, c, d) {
  21. var e = {};
  22. switch(d) {
  23. case "randomChat":
  24. e = {
  25. id: a,
  26. chId: Math.floor(Math.random() * 99999999).toString(),
  27. gender: b,
  28. find: c,
  29. available: true,
  30. type: d
  31. };
  32. break;
  33. case "groupChat":
  34. e = {
  35. mem: [a],
  36. chId: Math.floor(Math.random() * 99999999).toString(),
  37. type: d,
  38. identify: 1
  39. };
  40. break;
  41. default:
  42. break
  43. }
  44. this.channels.push(e);
  45. if(!this.messages[e.chId]) this.messages[e.chId] = [];
  46. if(!this.callbacks[e.chId]) this.callbacks[e.chId] = [];
  47. return e.chId
  48. };
  49. Channel.prototype.joinChannel = function (a, b, c, d) {
  50. var e = "";
  51. var f = "";
  52. var g = {
  53. wait: true,
  54. chId: f
  55. };
  56. switch(d) {
  57. case "randomChat":
  58. e = this.joinRandomChannel(b, c);
  59. break;
  60. case "groupChat":
  61. e = this.joinGroupChannel(a);
  62. break;
  63. default:
  64. break
  65. }
  66. if(e !== -1) {
  67. f = this.channels[e].chId;
  68. var h = {};
  69. if(d === "groupChat") {
  70. if(this.channels[e].mem.length > 2) g.wait = false;
  71. this.channels[e].identify++;
  72. g.identify = this.channels[e].identify
  73. } else if(d === "randomChat") {
  74. h = {
  75. id: a,
  76. chId: f,
  77. gender: b,
  78. find: c,
  79. available: false,
  80. type: d
  81. };
  82. g.wait = false;
  83. this.channels.push(h)
  84. }
  85. } else {
  86. f = this.createChannel(a, b, c, d);
  87. if(d == "groupChat") g.identify = 1
  88. }
  89. g.chId = f;
  90. if(!this.listSs[f]) this.listSs[f] = [];
  91. this.listSs[f].push({
  92. id: a
  93. });
  94. return g
  95. };
  96. Channel.prototype.joinRandomChannel = function (a, b) {
  97. var c = -1;
  98. switch(b) {
  99. case "all":
  100. for(var d in this.channels) if(this.channels[d].type == "randomChat" && (this.channels[d].find == b && this.channels[d].available == true || this.channels[d].find == a && this.channels[d].available == true)) {
  101. this.channels[d].available = false;
  102. c = d;
  103. break
  104. }
  105. break;
  106. case "male":
  107. case "female":
  108. for(var d in this.channels) if(this.channels[d].type == "randomChat" && (this.channels[d].find == a && b == this.channels[d].gender && this.channels[d].available == true || this.channels[d].find == "all" && this.channels[d].available == true)) {
  109. this.channels[d].available = false;
  110. c = d;
  111. break
  112. }
  113. break;
  114. case "none":
  115. for(var d in this.channels) if(this.channels[d].type == "randomChat" && this.channels[d].available == true) {
  116. this.channels[d].available = false;
  117. c = d;
  118. break
  119. }
  120. break;
  121. default:
  122. break
  123. }
  124. return c
  125. };
  126. Channel.prototype.joinGroupChannel = function (a) {
  127. var b = -1;
  128. for(var c in this.channels) if(this.channels[c].type === "groupChat" && this.channels[c].mem.length < chCfg.group_chat_max_user) {
  129. this.channels[c].mem.push(a);
  130. b = c;
  131. break
  132. }
  133. return b
  134. };
  135. Channel.prototype.countUserOnline = function () {
  136. var a = 0;
  137. for(var b in this.channels) {
  138. if(this.channels[b].type == "randomChat") a++;
  139. else if(this.channels[b].type == "groupChat") a += this.channels[b].mem.length
  140. }
  141. return a
  142. };
  143. Channel.prototype.leaveChannel = function (a, b, c) {
  144. switch(c) {
  145. case "randomChat":
  146. this.leaveRandomChannel(a, b);
  147. break;
  148. case "groupChat":
  149. this.leaveGroupChannel(a, b);
  150. break;
  151. default:
  152. break
  153. }
  154. return
  155. };
  156. Channel.prototype.leaveRandomChannel = function (a, b) {
  157. for(var c in this.channels) {
  158. if(this.channels[c].id == a && this.channels[c].chId == b) {
  159. this.channels.splice(c, 1);
  160. break
  161. }
  162. }
  163. for(var c in this.channels) {
  164. if(this.channels[c].chId == b) {
  165. this.channels[c].available = true;
  166. break
  167. }
  168. }
  169. };
  170. Channel.prototype.leaveGroupChannel = function (a, b) {
  171. for(var c in this.channels) if(this.channels[c].chId == b) {
  172. for(var d in this.channels[c].mem) if(this.channels[c].mem[d] == a) {
  173. this.channels[c].mem.splice(d, 1);
  174. break
  175. }
  176. if(this.channels[c].mem.length == 0) this.channels.splice(c, 1);
  177. break
  178. }
  179. };
  180. Channel.prototype.countGroup = function (a) {
  181. for(var b in this.channels) if(this.channels[b].chId == a) return this.channels[b].mem.length;
  182. return 0
  183. };
  184. Channel.prototype.reconnect = function (a) {
  185. for(var b in this.channels) {
  186. if(this.channels[b].chId == a) {
  187. this.channels[b].available = true;
  188. break
  189. }
  190. }
  191. return
  192. };
  193. Channel.prototype.deleteListMessageAndSession = function (a, b) {
  194. for(var c in this.messages[a]) if(c == b) this.messages[a].splice(c, 1);
  195. for(var c in this.listSs[a]) if(this.listSs[a][c].id == b) this.listSs[a].splice(c, 1)
  196. };
  197. Channel.prototype.appendMessage = function (a, b, c, d, e) {
  198. var f = {
  199. id: a,
  200. chId: b,
  201. timestamp: (new Date).getTime(),
  202. type: c,
  203. textchat: d
  204. };
  205. switch(c) {
  206. case "join":
  207. case "quit":
  208. f.counter = this.countUserOnline();
  209. break;
  210. default:
  211. break
  212. }
  213. if(e) f.identify = e;
  214. if(this.listSs[b]) var g = this.listSs[b].slice(0);
  215. else var g = [];
  216. while(this.callbacks[b].length > 0) {
  217. var h = this.callbacks[b].shift();
  218. h.callback([f]);
  219. for(var i in g) if(g[i].id == h.id) {
  220. g.splice(i, 1);
  221. break
  222. }
  223. }
  224. if(g) while(g.length > 0) {
  225. var a = g.shift().id;
  226. if(!this.messages[b][a]) this.messages[b][a] = [];
  227. this.messages[b][a].push(f)
  228. }
  229. };
  230. Channel.prototype.query = function (a, b, c, d) {
  231. var e = [];
  232. if(this.messages[c] && this.messages[c][d]) while(this.messages[c][d].length > 0) {
  233. var f = this.messages[c][d].shift();
  234. e.push(f)
  235. }
  236. if(e.length != 0) b(e);
  237. else this.callbacks[c].push({
  238. id: d,
  239. timestamp: new Date,
  240. callback: b
  241. })
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement