Skip to content

Instantly share code, notes, and snippets.

@codeHusky
Created January 15, 2016 22:45
Show Gist options
  • Save codeHusky/bba06240f1a659f59ee9 to your computer and use it in GitHub Desktop.
Save codeHusky/bba06240f1a659f59ee9 to your computer and use it in GitHub Desktop.
var sio = require("socket.io");
var io = sio(1000);
console.log("Listening on port 1000");
io.on("connection", function(socket){
console.log("Connection");
socket.on("getUserInfo", function(userId) {
Chat.getUserInfo(socket, userId);
});
socket.on("fetchRoom", function(rid){
Chat.getRoomInfo(socket, rid);
})
socket.on("userSentMessage", function(roomId, message){
})
})
function ChatInstance() {
//SETUP Step
this.accessLvls = {
user: 0,
bot:1,
globaladmin: 2,
staff: 3
};
this.userDefault = {
"name": "New User",
"creationDate": "",
"profilepic": "http://placehold.it/200x200",
"accesslevel": this.accessLvls.user,
"verified": false
};
this.roomDefault = {
"name": "Empty Room",
"roomid": "GenericRoomId",
"creator": 0,
"creationDate": "",
"profilepic": "http://placehold.it/200x200",
"admins": [],
"moderators": [],
"users": [],//Users contain admins, moderators and owners for chat purposes.
"recentMessageLog": [], //Contains last 500 messages in a room.
}
this.users = {
"0": {
"name": "Lokio",
"creationDate": (new Date()).toISOString(),
"profilepic": "http://lokiraut.io/images/fur.png",
"accesslevel":this.accessLvls.staff,
"verified": true
},
"1": {
"name": "Austin",
"creationDate": (new Date()).toISOString(),
"profilepic": "http://placehold.it/200x200",
"accesslevel":this.accessLvls.user,
"verified": false
}
};
this.extraUserData = {
"0": {
"online": false
},
"1": {
"online": true
}
};
this.rooms = {
"0": {
"name": "Public Room",
"roomid": "public",
"creator": 0,
"creationDate": (new Date()).toISOString(),
"profilepic": "http://placehold.it/200x200",
"admins": [],
"moderators": [],
"users": [0,1],//Users contain admins, moderators and owners for chat purposes.
"recentMessageLog": [
[0, "Testing 123! Let's see if it works."],
[0, "I do think this is working as expect, though."],
[0, "This is the deciding test. :)"],
[0, "*Formatting* ~Test~ _that_ `works` *with markdown*_because_ it formats nicely."],
[1, "This is another user! Wow!"],
[1, "Spam"],
[1, "Spam"],
[1, "Spam"],
[1, "Spam"]
],
}
}
//
}
ChatInstance.prototype.getUserInfo = function(socket, userId) {
userId = this.fixId(userId);
if(this.users.hasOwnProperty(parseFloat(userId))){
//Okay, it exists
var userid = parseFloat(userId);
var toSend = this.users[userid];
toSend.online = this.extraUserData[userid].online;
socket.emit("getUserInfoSuccess", toSend);
}else{
socket.emit("getUserInfoFailure", "User " + userId + " does not exist.")
}
};
ChatInstance.prototype.getSendableChatLog = function(roomId) {
roomId = this.fixId(roomId);
var model = {
author:{
online:true,
name:"GenericGuy",
picsrc:"http://lokiraut.io/images/fur.png",
},
message:"Testing of population."
};
var log = [];
var oldLog = this.rooms[roomId].recentMessageLog;
for(var i in oldLog){
var authorId = this.fixId(oldLog[i][0]);
var messageContent = oldLog[i][1];
var author = this.users[authorId];
var authorExtra = this.extraUserData[authorId];
log.push({
author: {
online: authorExtra.online,
name: author.name,
verified: author.verified,
picsrc: author.profilepic
},
message: messageContent
})
}
return log;
}
ChatInstance.prototype.getRoomInfo = function(socket, roomId){
roomId = this.fixId(roomId);
if(this.rooms[roomId]){
socket.emit("fetchRoomSuccess", this.getSendableRoom(roomId))
}else{
//handle error
console.warn("fetch didn't work")
}
}
ChatInstance.prototype.getSendableRoom = function(roomId){
var roomFixed = {};
for(var i in this.rooms[roomId]){
roomFixed[i] = this.rooms[roomId][i];
}
roomFixed.recentMessageLog = this.getSendableChatLog(roomId);
return roomFixed;
}
ChatInstance.prototype.fixId = function(id){
if(id != undefined){
return parseFloat(id.toString()).toString();
}else{
console.warn("invalid id");
return "-1";
}
}
var Chat = new ChatInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment