-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·199 lines (151 loc) · 4.47 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var TEAM_SIZE = 3;
var NUM_TEAMS = 2;
var team = [];
var inProgress = false;
var BOARD_SIZE;
var trs = [];
// Check the configuration file for more details
var config = require('./config');
// Express.js stuff
var express = require('express');
var app = require('express')();
var server = require('http').Server(app);
// Websockets with socket.io
var io = require('socket.io')(server);
console.log("Trying to start server with config:", config.serverip + ":" + config.serverport);
// Both port and ip are needed for the OpenShift, otherwise it tries
// to bind server on IP 0.0.0.0 (or something) and fails
server.listen(config.serverport, config.serverip, function() {
console.log("Server running @ http://" + config.serverip + ":" + config.serverport);
//createTeams();
});
// Allow some files to be served over HTTP
app.use(express.static(__dirname + '/'));
// Serve GET on http://domain/
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
// Server GET on http://domain/api/config
// A hack to provide client the system config
app.get('/api/config', function(req, res) {
res.send('var config = ' + JSON.stringify(config));
});
// And finally some websocket stuff
io.on('connection', function (socket) { // Incoming connections from clients
console.log( socket.id + ' connected' );
//On click of board piece
socket.on('clicked', function (value) {
});
// Handle reset event
socket.on('reset', function () {
});
//Handle score event
socket.on('score', function (teamName) {
});
//Handle clueGiven by spyMaster
socket.on('clueGiven', function (data) {
});
//Handle switch
socket.on('switch', function (data) {
});
// Handle getTeams event
socket.on('getTeams', function (data) {
});
//Handle client disconnect event
socket.on('disconnect', function(data) {
});
socket.on('checkClue', function (data, callback) {
});
socket.on('disableGuessers', function () {
});
socket.on('enableGuessers', function () {
});
socket.on('getStatus', function (callback) {
});
//add player to team which has space
socket.on('registerPlayer', function (data, callback) {
console.log("received join game request from client:", data);
console.log("Calling Create teams with values " + TEAM_SIZE + " " + NUM_TEAMS);
var added = false;
var left;
var teamSize = team.players.length;
var i=0;
if (teamSize < TEAM_SIZE){
// check name is not already in one of the teams
if (isNameAvail(data))
{
socket.nickname = data;
added = true;
var player = {
name : data,
master : false,
id : socket.id
};
if(teams[i].players.length === 0) {
console.log("Adding spymaster: " + data + " " + socket.id);
player.master = true;
}
team.players.push(player);
sendTeams();
callback (true);
teamSize++;
left = (TEAM_SIZE*NUM_TEAMS) - teamSize;
io.sockets.emit('teamSize', {teamSize : teamSize, left : left} );
//If team size is reached the max
if (teamSize == TEAM_SIZE) {
// If no game in progress then create the board
if(!inProgress) {
createBoard();
}
//Show countdown timer then start game
var secs;
var duration = readyDuration;
readyTimer = setInterval(function () {
secs = parseInt(duration % 60, 10);
io.sockets.emit('getReady', secs);
duration--;
if(duration < 0) {
clearInterval(readyTimer);
duration = readyDuration;
console.log("Calling startGame()");
startGame();
}
}, 1000);
}
} else {
callback(false);
}
} else {
//game is full
console.log("Game Full");
socket.emit('gameFull', teams);
}
});
});
function isNameAvail(data) {
console.log("Checking if name " + data + " is available");
var isAvail = true;
if(team.players[data] != -1) {
console.log("Player " + data + " already exists");
isAvail = false;
}
return isAvail;
}
// Function to create a new board
function createBoard(){
//get seed and set the seed for randomizer
//var gameHash = Math.floor((Math.random() * maxHash) + 1);
//Math.seedrandom(gameHash);
//reset state to pristine state
//sessionData = data.slice(0);
//Fill wordsSelected array & create 5 table rows with 5 element
var output = "<table><tbody>";
if (!trs[i%5]){
trs[i%5] = "";
}
for(var i = 0; i < BOARD_SIZE; i++){
trs[i%5] += "<td><div class=\"imgDiv\" ondrop=\"drop(event)\" ondragover=\"allowDrop(event)\"></div></td>";
}
output += trs;
output += "</tbody></table>";
}