Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Channel() {
- EventEmitter.call(this);
- this.callbacks = [];
- this.messages = [];
- this.channels = [];
- this.listSs = [];
- this.chCfg = require("./config").Config("CHANNEL");
- var a = this;
- setInterval(function () {
- var b = new Date;
- for(var c in a.callbacks) {
- while(a.callbacks[c].length > 0 && b - a.callbacks[c][0].timestamp > a.chCfg.callback_timeout) a.callbacks[c].shift().callback([])
- }
- }, 5e3)
- }
- var util = require("util");
- var EventEmitter = require("events").EventEmitter;
- util.inherits(Channel, EventEmitter);
- module.exports = Channel;
- Channel.prototype.createChannel = function (a, b, c, d) {
- var e = {};
- switch(d) {
- case "randomChat":
- e = {
- id: a,
- chId: Math.floor(Math.random() * 99999999).toString(),
- gender: b,
- find: c,
- available: true,
- type: d
- };
- break;
- case "groupChat":
- e = {
- mem: [a],
- chId: Math.floor(Math.random() * 99999999).toString(),
- type: d,
- identify: 1
- };
- break;
- default:
- break
- }
- this.channels.push(e);
- if(!this.messages[e.chId]) this.messages[e.chId] = [];
- if(!this.callbacks[e.chId]) this.callbacks[e.chId] = [];
- return e.chId
- };
- Channel.prototype.joinChannel = function (a, b, c, d) {
- var e = "";
- var f = "";
- var g = {
- wait: true,
- chId: f
- };
- switch(d) {
- case "randomChat":
- e = this.joinRandomChannel(b, c);
- break;
- case "groupChat":
- e = this.joinGroupChannel(a);
- break;
- default:
- break
- }
- if(e !== -1) {
- f = this.channels[e].chId;
- var h = {};
- if(d === "groupChat") {
- if(this.channels[e].mem.length > 2) g.wait = false;
- this.channels[e].identify++;
- g.identify = this.channels[e].identify
- } else if(d === "randomChat") {
- h = {
- id: a,
- chId: f,
- gender: b,
- find: c,
- available: false,
- type: d
- };
- g.wait = false;
- this.channels.push(h)
- }
- } else {
- f = this.createChannel(a, b, c, d);
- if(d == "groupChat") g.identify = 1
- }
- g.chId = f;
- if(!this.listSs[f]) this.listSs[f] = [];
- this.listSs[f].push({
- id: a
- });
- return g
- };
- Channel.prototype.joinRandomChannel = function (a, b) {
- var c = -1;
- switch(b) {
- case "all":
- 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)) {
- this.channels[d].available = false;
- c = d;
- break
- }
- break;
- case "male":
- case "female":
- 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)) {
- this.channels[d].available = false;
- c = d;
- break
- }
- break;
- case "none":
- for(var d in this.channels) if(this.channels[d].type == "randomChat" && this.channels[d].available == true) {
- this.channels[d].available = false;
- c = d;
- break
- }
- break;
- default:
- break
- }
- return c
- };
- Channel.prototype.joinGroupChannel = function (a) {
- var b = -1;
- for(var c in this.channels) if(this.channels[c].type === "groupChat" && this.channels[c].mem.length < chCfg.group_chat_max_user) {
- this.channels[c].mem.push(a);
- b = c;
- break
- }
- return b
- };
- Channel.prototype.countUserOnline = function () {
- var a = 0;
- for(var b in this.channels) {
- if(this.channels[b].type == "randomChat") a++;
- else if(this.channels[b].type == "groupChat") a += this.channels[b].mem.length
- }
- return a
- };
- Channel.prototype.leaveChannel = function (a, b, c) {
- switch(c) {
- case "randomChat":
- this.leaveRandomChannel(a, b);
- break;
- case "groupChat":
- this.leaveGroupChannel(a, b);
- break;
- default:
- break
- }
- return
- };
- Channel.prototype.leaveRandomChannel = function (a, b) {
- for(var c in this.channels) {
- if(this.channels[c].id == a && this.channels[c].chId == b) {
- this.channels.splice(c, 1);
- break
- }
- }
- for(var c in this.channels) {
- if(this.channels[c].chId == b) {
- this.channels[c].available = true;
- break
- }
- }
- };
- Channel.prototype.leaveGroupChannel = function (a, b) {
- for(var c in this.channels) if(this.channels[c].chId == b) {
- for(var d in this.channels[c].mem) if(this.channels[c].mem[d] == a) {
- this.channels[c].mem.splice(d, 1);
- break
- }
- if(this.channels[c].mem.length == 0) this.channels.splice(c, 1);
- break
- }
- };
- Channel.prototype.countGroup = function (a) {
- for(var b in this.channels) if(this.channels[b].chId == a) return this.channels[b].mem.length;
- return 0
- };
- Channel.prototype.reconnect = function (a) {
- for(var b in this.channels) {
- if(this.channels[b].chId == a) {
- this.channels[b].available = true;
- break
- }
- }
- return
- };
- Channel.prototype.deleteListMessageAndSession = function (a, b) {
- for(var c in this.messages[a]) if(c == b) this.messages[a].splice(c, 1);
- for(var c in this.listSs[a]) if(this.listSs[a][c].id == b) this.listSs[a].splice(c, 1)
- };
- Channel.prototype.appendMessage = function (a, b, c, d, e) {
- var f = {
- id: a,
- chId: b,
- timestamp: (new Date).getTime(),
- type: c,
- textchat: d
- };
- switch(c) {
- case "join":
- case "quit":
- f.counter = this.countUserOnline();
- break;
- default:
- break
- }
- if(e) f.identify = e;
- if(this.listSs[b]) var g = this.listSs[b].slice(0);
- else var g = [];
- while(this.callbacks[b].length > 0) {
- var h = this.callbacks[b].shift();
- h.callback([f]);
- for(var i in g) if(g[i].id == h.id) {
- g.splice(i, 1);
- break
- }
- }
- if(g) while(g.length > 0) {
- var a = g.shift().id;
- if(!this.messages[b][a]) this.messages[b][a] = [];
- this.messages[b][a].push(f)
- }
- };
- Channel.prototype.query = function (a, b, c, d) {
- var e = [];
- if(this.messages[c] && this.messages[c][d]) while(this.messages[c][d].length > 0) {
- var f = this.messages[c][d].shift();
- e.push(f)
- }
- if(e.length != 0) b(e);
- else this.callbacks[c].push({
- id: d,
- timestamp: new Date,
- callback: b
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement