forked from phantomesse/code-names
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
125 lines (104 loc) · 3.45 KB
/
server.js
File metadata and controls
125 lines (104 loc) · 3.45 KB
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
const express = require('express');
const server = express();
const http = require('http').Server(server);
const io = require('socket.io')(http);
const sassMiddleware = require('node-sass-middleware')
const path = require('path');
const app = require('./app/app.js');
const port = process.env.PORT || 1337;
/** SASS compilations. */
server.use(sassMiddleware({
debug: false,
dest: 'public/css',
force: true,
outputStyle: 'compressed',
prefix: '/css',
root: __dirname,
sourceMap: true,
src: 'sass'
}));
server.use(express.static(path.join(__dirname, 'public')));
/** Serve index.html. */
server.get('/', function(request, response) {
response.sendFile(path.join(__dirname, '/public/index.html'));
});
/** Check if a game session exists based on a given session name. */
server.get('/game-session-exists', function(request, response) {
response.setHeader('Content-Type', 'application/json');
response.send(JSON.stringify({
'exists': app.doesGameSessionExist(request.query.sessionName)
}));
});
/**
* Get GameSession object for a given session name.
*
* If the GameSession does not already exist, create one.
*/
server.get('/game-session', function(request, response) {
response.setHeader('Content-Type', 'application/json');
// Get the session name from the request.
const sessionName = request.query.sessionName;
// Return an error if the session name is invalid.
if (sessionName === undefined) {
response.send(JSON.stringify({
error: 'session name is undefined'
}));
return;
}
// Create session if it doesn't exist.
const gameSession = app.createGameSession(sessionName);
// Return GameSession as a JSON.
response.send(JSON.stringify(gameSession));
});
/**
* Generate new cards for a given game session.
*/
server.get('/reset', function(request, response) {
response.setHeader('Content-Type', 'application/json');
// Get the session name from the request.
const sessionName = request.query.sessionName;
// Return an error if the session name is invalid.
if (!app.doesGameSessionExist(sessionName)) {
response.send(JSON.stringify({
error: 'session name does not exist'
}));
return;
}
const gameSession = app.getGameSession(sessionName);
gameSession.reset();
// Return GameSession as a JSON.
response.send(JSON.stringify(gameSession));
});
/**
* Get number of cards left for each team for a given game session.
*/
server.get('/cards-left', function(request, response) {
response.setHeader('Content-Type', 'application/json');
// Get the session name from the request.
const sessionName = request.query.sessionName;
// Return an error if the session name is invalid.
if (!app.doesGameSessionExist(sessionName)) {
response.send(JSON.stringify({
error: 'session name does not exist'
}));
return;
}
// Return cards left as a JSON.
response.send(JSON.stringify(app.getGameSession(sessionName).getCardsLeft()));
});
/** Handle socket.io connections. */
io.on('connection', function(socket) {
//console.log('a user connected');
socket.on('disconnect', function() {
//console.log('user disconnected');
});
socket.on('flip word', function(word, sessionName) {
console.log(`flipped word (${word}) in session (${sessionName})`);
app.getGameSession(sessionName).flipWord(word);
socket.broadcast.emit('flip word', word, sessionName);
});
});
/** Start up server. */
http.listen(port, function() {
console.log(`listening on localhost:${port}`);
});