-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
169 lines (146 loc) · 4.16 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
var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var WebSocketServer = require('websocket').server;
var http = require('http');
var Game = require('./js/Game');
var Map = require('./js/Map');
var Config = require('./js/Config');
var Intent = require('./js/Intent');
var pjson = require('./package.json');
var game = new Game(new Map(Config.MAP_SEED));
var users = {};
var app = express();
app.use(bodyParser.json());
app.use(cookieParser());
express.static.mime.define({
'text/html': ['dry']
});
app.use('/lib', express.static('lib'));
app.use('/css', express.static('css'));
app.use('/img', express.static('img'));
app.use('/favicon.ico', express.static('img/favicon.ico'));
app.use('/index.html', express.static('index.html'));
app.use('/rules.html', express.static('rules.html'));
app.use('/main.js', express.static('main.js'));
if (Config.PRODUCTION) {
app.use('/js/Config.js', express.static('js/Config.js'));
app.use('/bundle.js', express.static('bundle.js'));
} else {
app.use('/js', express.static('js'));
}
function secureId() {
return require('crypto').randomBytes(64).toString('hex');
}
app.get('/', function(req, res) {
res.redirect('/index.html');
});
// authentication
app.use(function(req, res, next) {
console.log(req.method, req.url);
if (req.cookies.name) {
var name = req.cookies.name.substr(0, 30);
var auth = users[name];
if (auth === req.cookies.auth) {
req.user = name;
}
}
if ((req.url.substr(0, 7) === '/player' && req.method === 'POST') ||
req.url.substr(0, 8) === '/session') {
next();
} else if (req.user) {
next();
} else {
res.sendStatus(403);
}
});
app.get('/session', function(req, res) {
return res.json({
user: req.user,
version: pjson.version,
numplayers: Object.keys(game.players).length,
matchtime: game.getMatchTime()
});
});
app.post('/player/:name', function(req, res) {
var user = req.user;
var name = req.params.name.substr(0, 30);
var player;
if (user === name) {
player = game.getPlayer(name);
res.json(player);
} else {
player = game.addPlayer(name);
if (player) {
users[name] = secureId();
res.cookie('auth', users[name]);
res.cookie('name', name);
res.json(player);
} else {
res.sendStatus(409);
}
}
});
app.get('/player/:name', function(req, res) {
var name = req.params.name.substr(0, 30);
if (name === req.user) {
res.json(game.getStatus(name));
} else {
res.sendStatus(403);
}
});
app.get('/sector/:s', function(req, res) {
var sectors = req.params.s.split(',');
var response = [];
sectors.forEach(function(s) {
response.push(game.getSector(s, req.user));
});
var json = JSON.stringify(response);
res.setHeader('Content-Type', 'application/json');
res.write(json);
res.end();
});
app.post('/intent', function(req, res) {
var intent = req.body;
res.json(game.addIntent(req.user, Intent.fromJson(intent)));
});
app.delete('/tile/:coords/intents', function(req, res) {
var coords = req.params.coords;
res.json(game.cancelIntents(req.user, coords));
});
app.post('/turn', function(req, res) {
res.json(game.endTurn(req.user));
});
var server = app.listen(Config.PORT, function() {
console.log('Epoh server online on port ', Config.PORT);
});
/* Websockets server */
var server = http.createServer();
server.listen(1337);
var wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
var clients = [];
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
clients.push(connection);
connection.on('message', function(message) {
//console.log('Msg received: ' + message.utf8Data);
});
});
game.events.onValue(function(value) {
clients.forEach(function(client) {
client.sendUTF(JSON.stringify(value));
});
if (value.type === 'end') {
clients.forEach(function(client) {
client.close();
});
console.log('MATCH ENDED', value.scores.length);
if (value.scores.length) {
console.log('[_] Winner: _ (_)'.printf(Date(), value.scores[0].name, value.scores[0].score));
}
process.exit();
}
});