-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
133 lines (117 loc) · 4.17 KB
/
http.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Created by Aleksandar on 2014-08-05.
*/
// Setup basic express server
var express = require('express');
var voteAlgo = require('./voteAlgo');
var randomName=require('./server/randomNameGenerator');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
process.env.PORT = process.env.PORT || 3000;
var port = process.env.PORT;
var channels={
//qwdkwek:{ [0] -- [1] -- ..}
};
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
// Routing
app.use(express.static(__dirname + '/app'));
io.on('connection', function (socket) {
socket.username = randomName();
socket.channel = null;
socket.on("getDetails",function(){
socket.emit("getDetails",{
name:socket.username
});
});
// when the client emits 'new message', this listens and executes
socket.on('newMessage', function (data) {
var socketChannelIsNotSet = socket.channel==null;
if (socketChannelIsNotSet) {
socket.emit();
} else {
var data = {
username: socket.username,
message: data.message
};
socket.broadcast.to(socket.channel).emit('newMessage', data);
}
});
socket.on("guess",function(data){
socket.votes = socket.votes || {};
socket.votes[data.votes.index] = {
answered: {
time: new Date().getTime(),
name: data.votes.artist
}
};
});
socket.on('suggestTracks', function (data) {
channels[socket.channel] = {
"votedata" : data
};
setTimeout(function(){
var socketIdsOfPersonsInRoom = io.nsps["/"].adapter.rooms[socket.channel]; //@TODO make sure we are connected.
var answers={};
for(var i = 0; i < channels[socket.channel].votedata.tracks.length; i++) {
var artists = channels[socket.channel].votedata.tracks[i].artists;
var correctAnswer="";
for (var j = 0; j<artists.length;j++) {
if (artists[j].correct) {
correctAnswer=artists[j].name;
}
}
answers[i]=correctAnswer;
}
var results=voteAlgo.loopThrough(io.sockets.connected,socketIdsOfPersonsInRoom,new Date().getTime(),answers);
var winner = voteAlgo.getMinimum(results);
console.log("winner",winner);
if (winner) {
winner.socket.emit("youAreTheKingOfDiscovery",{
"whatup" : true
});
var hillData = voteAlgo.getHillData(io.sockets.connected,socketIdsOfPersonsInRoom);
console.log("hillData",hillData);
winner.socket.broadcast.to(socket.channel).emit("youHaveNotWon",{
"answer":answers,
"hillData":hillData
});
}
//io.sockets.in(socket.channel).emit("finished",data);
},90000);
socket.broadcast.to(socket.channel).emit("tracksHaveBeenSuggested",data);
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
var chatCount=Object.keys(io.sockets.adapter.rooms[socket.channel] || []);
if (chatCount==0) {
channels[socket.channel]={};
}
});
socket.on('clear', function () {
if (socket.channel) {
socket.leave(socket.channel);
}
})
socket.on('joinRoom',function(data){
if (socket.channel) {
socket.leave(socket.channel);
}
var joinRoom = data.joinRoom;
socket.channel=joinRoom;
var roomCount = Object.keys(io.sockets.adapter.rooms[joinRoom] || []).length;
console.log(io.sockets.adapter.rooms[joinRoom]);
var roomIsEmpty = roomCount == 0;
if (roomIsEmpty) {
socket.emit("youAreTheKingOfDiscovery",{ });
}
socket.join(joinRoom,function(){
socket.emit("joinedRoom", {
"joinRoom": joinRoom,
count: roomCount
});
});
});
});