/
server-chat.js
83 lines (67 loc) · 1.75 KB
/
server-chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var options = {
extras: {
good: 1,
amazing: 2,
awesome: 3,
fat: -4,
stupid: -2,
fuck: -5,
shit: -4,
like: 3,
love: 5
}
};
var chatQueue = new Array();
var chatRoom = 1;
exports.initChat = (io, socket) => {
console.log("a user connected!");
socket.on("disconnect", function() {
console.log(socket.id + " got disconnected!");
var i = chatQueue.indexOf(socket);
chatQueue.splice(i, 1);
console.log(chatQueue);
io.in(socket.room).emit("chat message", socket.id + " has left the chat.");
});
socket.on("joinRoom", function(socketId) {
chatQueue.push(socketId);
socket.join(chatRoom);
socket.room = chatRoom;
console.log(chatQueue);
if (chatQueue.length === 2) {
io.in(chatRoom).emit("matchFound", { msg: "hello world" });
chatQueue = [];
chatRoom += 1;
console.log(socket.room);
// console.log(chatQueue);
// console.log(chatRoom);
}
});
socket.on("chat message", function(msg) {
//console.log("message: " + msg);
io.in(socket.room).emit("chat message", msg);
});
};
// Updates the Firebase of a user given a message that they just sent
// REQ: GoogleID, Message
function updateUsers (G_ID, Mess){
var urlRef = db.ref().child("stat/" +G_ID);
var stats = sent.analyze(Mess.toLowerCase(), options);
var comp = stats["comparative"] * 2;
var num_mess = 1;
urlRef.once("value", function(snapshot) {
snapshot.forEach(function(child) {
if (child.key == "total_intent"){
comp += child.val();
db.ref().child("stat/" +G_ID).update({
"total_intent": comp
})
};
if (child.key == "total_messages"){
num_mess += child.val();
db.ref().child("stat/" +G_ID).update({
"total_messages": num_mess
})
};
});
});
}