-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·177 lines (155 loc) · 5.02 KB
/
server.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// connection:
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
var http = require('http');
var app = require('./app');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
// mongolab database:
var mongoose = require('mongoose');
var dbuser = 'testuser';
var dbpass = 'testpassword';
mongoose.connect('mongodb://'+dbuser+':'+dbpass+'@ds031711.mongolab.com:31711/test-worderp');
var db = mongoose.connection;
// libraries:
var Stopwatch = require('./public/js/stopwatch');
var Wordbuttons = require('./databases/wordbutton');
var Story = require('./databases/story');
var Filter = require('./filter');
// global variables:
var stopwatch = new Stopwatch(); // timer
var clientVoteWords = new Array();
var clientSubmitWords = new Array();
var clientSockets = new Array();
var connectCounter = 0;
// global call-back functions:
var publishStory = function(html){
io.sockets.emit('update story', html);
io.sockets.emit('scroll');
};
var publishButtons = function(html){
io.sockets.emit('update buttons', html);
};
var addWord = function(word){
Story.addWord(word, publishStory);
};
var publishPageCount = function(pageCount){
io.sockets.emit('update page', pageCount);
};
/**
*
* stopwatch handlers
*
**/
stopwatch.start();
stopwatch.on('tick:stopwatch', function(time){
io.sockets.emit('time', { time: time });
});
stopwatch.on('restart:stopwatch', function(time){
// clear global variables:
for(client in clientVoteWords){
clientVoteWords[client] = undefined;
}
for(client in clientSubmitWords){
clientSubmitWords[client] = 0;
}
// get the favorite word:
Wordbuttons.getMostVoted(addWord);
// update the page number:
Story.getPageCount(publishPageCount);
// reset word buttons:
Wordbuttons.reset(publishButtons);
// display restarted time:
io.sockets.emit('time', { time: time });
});
/**
*
* client handlers
*
**/
io.sockets.on('connection', function(socket){
var clientID = socket.request.headers['user-agent']+socket.request.connection.remoteAddress;
if(clientSubmitWords[clientID] === undefined){
console.log("new user: "+socket.request.headers['user-agent']+" connecting from: "+socket.request.connection.remoteAddress);
console.log("user id: "+socket.request.headers['user-agent']+socket.request.connection.remoteAddress+" assigned");
// add them to our global variables:
clientVoteWords[clientID] = undefined;
clientSubmitWords[clientID] = 0;
clientSockets[clientID] = 1;
// increment the player count:
connectCounter++;
io.sockets.emit('update users', connectCounter);
console.log(connectCounter+" players");
}
else{
clientSockets[clientID]++;
io.sockets.emit('update users', connectCounter);
}
// call-back functions:
var publishStoryForClient = function(html){
socket.emit('update story', html);
socket.emit('scroll');
};
var publishButtonsForClient = function(html){
socket.emit('update buttons', html);
};
var publishPageCountForClient = function(pageCount){
socket.emit('update page', pageCount);
};
// show them the story and prospective words:
Story.getStory(0, publishStoryForClient);
Wordbuttons.getWordButtons(publishButtonsForClient);
Story.getPageCount(publishPageCountForClient);
// client submits word:
socket.on('send word', function(word){
// if they have not already submitted too many times
if(clientSubmitWords[clientID] < 5){
// callback error:
var error = function(msg, word) {
socket.emit(msg, word);
};
// callback function:
var addButton = function(word) {
socket.emit('word successful');
Wordbuttons.addWordButton(word, publishButtons);
// increment submission count
clientSubmitWords[clientID]++;
};
// filter for spam, html, and special cases:
Filter.filter(word, addButton, error);
}
else socket.emit('too many submissions');
});
// client votes on word:
socket.on('vote', function(index){
clientVoteWords[clientID] = Wordbuttons.vote(publishButtons, index, clientVoteWords[clientID]);
});
// client requests archive:
socket.on('archive', function(archiveIndex){
Story.checkArchiveIndex(archiveIndex, publishStoryForClient);
Story.getPageCount(publishPageCountForClient);
});
// client exits Worderp:
socket.on('disconnect', function (){
// decrement number of sockets for client:
clientSockets[clientID]--;
// if client no longer has any sockets open:
if(clientSockets[clientID] < 1){
// decrement the player count:
connectCounter--;
io.sockets.emit('update users', connectCounter);
// remove client from global variables:
delete clientVoteWords[clientID];
delete clientSubmitWords[clientID];
}
});
});
/**
*
* start server
*
**/
server.listen(port, ipaddress, function(){
console.log((new Date()) + ' Server is listening on port 3000');
});
console.log("Listening to " + ipaddress + ":" + port + "...");