forked from inovex/planning-poker
/
poker-server.js
214 lines (183 loc) · 5.65 KB
/
poker-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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var WebSocketServer = require('websocket').server,
fs = require('fs'),
url = require('url'),
qs = require('querystring'),
express = require('express'),
iniparser = require('iniparser'),
crypto = require('crypto'),
i18n = require('i18n'),
path = require('path');
console.log('Loading config');
var config = iniparser.parseSync('./config.ini');
// HTTP Server
var http = require(config.http.protocol);
var app = express();
var httpServer = http.createServer(app);
app.use(express.static(__dirname + config.filesystem.public_files));
app.set('views', __dirname + config.filesystem.view_files);
app.engine('html', require('ejs').renderFile);
// i18n config
i18n.configure({
locales: ['en', 'de'],
defaultLocale: config.locale.default,
// See https://github.com/mashpie/i18n-node/issues/61
directory: __dirname + '/locales',
updateFiles: false
});
app.configure(function() {
app.use(i18n.init);
});
// Poker App specific Variables
var pokerConnection = require('./poker-connection.js'),
pokerBroadcaster = require('./poker-broadcaster.js'),
pokerUsers = require('./poker-users.js'),
pokerUserstory = require('./poker-userstory.js'),
pokerCards = require('./poker-cards.js');
// WebSocket Server
console.log('Creating WebSocket Server');
wsServer = new WebSocketServer({
httpServer: httpServer,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});
pokerBroadcaster.init(wsServer);
var pokerLoginListener = function(messageData) {
var user,
sha1sum,
sendData;
// For callbacks
connection = this.connection;
user = messageData.user;
if (typeof user.id !== 'undefined') {
pokerUsers.add(user);
sendData = {
type: 'login',
user: user
};
connection.sendUTF(JSON.stringify(sendData));
broadcastUsers();
} else {
// Create random id for user
sha1sum = crypto.createHash('sha1');
crypto.randomBytes(256, function(ex, buf) {
if (ex) throw ex;
sha1sum.update(buf);
user.id = sha1sum.digest('hex');
pokerUsers.add(user);
sendData = {
type: 'login',
user: user
};
connection.sendUTF(JSON.stringify(sendData));
broadcastUsers();
});
}
this.user = user;
};
var getInitialDataListener = function(messageData) {
this.connection.sendUTF(JSON.stringify(getUserUpdateList()));
this.connection.sendUTF(JSON.stringify(getCardUpdateList()));
this.connection.sendUTF(JSON.stringify(getUserstoryUpdate()));
};
var playCardListener = function(messageData) {
var cardSet = pokerCards.setCard(messageData.userId, messageData.cardValue);
// If a new card could be set, broadcast
if (cardSet) {
broadcastCards();
}
};
var showCardsListener = function(messageData) {
var pushData = {
type: 'show-cards'
};
pokerCards.show = true;
pokerBroadcaster.broadcast(pushData);
};
var resetCardsListener = function(messageData) {
pokerCards.reset();
broadcastCards();
};
var postUserstoryListener = function(messageData) {
pokerUserstory.set(messageData.userstory);
broadcastUserstory();
};
var postChatMessageListener = function(messageData) {
chatMessage = {
type: 'new-chat-message',
text: messageData.text,
user: this.user
};
pokerBroadcaster.broadcast(chatMessage);
};
var resetRoomListener = function(messageData) {
pokerUserstory.remove();
pokerCards.reset();
broadcastCards();
broadcastUserstory();
var pushData = {
type: 'reset-room'
};
pokerBroadcaster.broadcast(pushData);
}
wsServer.on('request', function(request) {
var connectionHandler = pokerConnection.getNewHandler();
connectionHandler.init(pokerUsers, pokerCards);
connectionHandler.on('login', pokerLoginListener);
connectionHandler.on('get-initial-data', getInitialDataListener);
connectionHandler.on('play-card', playCardListener);
connectionHandler.on('show-cards', showCardsListener);
connectionHandler.on('reset-cards', resetCardsListener);
connectionHandler.on('post-userstory', postUserstoryListener);
connectionHandler.on('post-chat-message', postChatMessageListener);
connectionHandler.on('reset-room', resetRoomListener);
connectionHandler.setConnection(request.accept());
console.log((new Date()) + ' Connection accepted.');
});
app.get('/', function(req, res) {
res.render(
'index.html',
{
web: config.http,
cards: config.cards
}
);
res.end();
});
getUserUpdateList = function () {
return {
type: 'userlist',
data: pokerUsers.getAll()
};
};
broadcastUsers = function() {
var pushData = getUserUpdateList();
pokerBroadcaster.broadcast(pushData);
}
getCardUpdateList = function () {
return {
type: 'carddisplay',
data: pokerCards.getAll()
};
};
broadcastCards = function() {
var pushData = getCardUpdateList();
pokerBroadcaster.broadcast(pushData);
};
getUserstoryUpdate = function() {
return {
type: 'userstory',
userstory: pokerUserstory.get()
};
}
broadcastUserstory = function() {
var pushData = getUserstoryUpdate();
pokerBroadcaster.broadcast(pushData);
};
httpServer.listen(config.http.port, function() {
console.log('HTTP Server running with config:');
console.log(config.http);
});