-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
140 lines (119 loc) · 5.18 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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server, { pingTimeout: 60000});
io.set('close timeout', 60);
io.set('heartbeat timeout', 60);
server.listen(process.env.PORT || 1337);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
var gameList = { sessions: [], unmatched: ["fak1", "fak2"] };
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/reset', function (req, res) {
gameList = { sessions: [], unmatched: ["fak1", "fak2"] };
res.status(200).send(gameList);
});
app.get('/game', function (req, res) {
res.status(200).send(gameList);
});
/**
* We could remove all this logic and instead do it over websockets,
* but I didn't want to create a ws connection for each user until they're
* validated as legitimate (ie, valid unmatched tank requested)
*/
app.post('/game', function (req, res) {
var response = { message: null, gameList: gameList };
if(req.body.action == 'JOIN') {
// Find the requested host, ensure there is no opponent and the requested opponent is in the list of unmatched
for(var i = 0; i < gameList.sessions.length; i++) {
if(gameList.sessions[i].host.id == req.body.hostId) {
if(gameList.sessions[i].opponent == null) {
if(gameList.unmatched.indexOf(req.body.playerId) !== -1) {
gameList.sessions[i].opponent = { id: req.body.playerId, fuel: 100, health: 100 };
gameList.unmatched.splice(gameList.unmatched.indexOf(req.body.playerId), 1);
response.message = 'OK';
response.gameList = gameList;
res.status(200).send(response);
return;
} else {
response.message = 'Tank not connected or already connected to another session';
res.status(409).send(response);
return;
}
gameList.sessions[i].opponent = { id: req.body.playerId};
} else {
response.message = 'Requested session has been filled';
res.status(409).send(response);
return;
}
}
}
} else if(req.body.action == 'HOST') {
if(gameList.unmatched.indexOf(req.body.hostId) !== -1) {
gameList.sessions.push({ host: { id: req.body.hostId, fuel: 100, health: 100 }, opponent: null });
gameList.unmatched.splice(gameList.unmatched.indexOf(req.body.hostId), 1);
response.message = 'OK';
response.gameList = gameList;
res.status(200).send(response);
return;
} else {
response.message = 'Tank not connected or already connected to another session';
res.status(409).send(response);
return;
}
}
response.message = 'Host has disconnected';
res.status(409).send(response);
});
function removeTankFromGameList(socket) {
if(gameList.unmatched.indexOf(socket.clientId) === -1) {
for(var i = 0; i < gameList.sessions.length; i++) {
if(gameList.sessions[i].host && gameList.sessions[i].host.id) {
if(gameList.sessions[i].host.id == socket.clientId) {
if(gameList.sessions[i].opponent && gameList.sessions[i].opponent.id) {
gameList.unmatched.push(gameList.sessions[i].opponent.id);
}
gameList.sessions.splice(i, 1);
return;
}
}
if(gameList.sessions[i].opponent && gameList.sessions[i].opponent.id) {
if(gameList.sessions[i].opponent.id == socket.clientId) {
if(gameList.sessions[i].host && gameList.sessions[i].host.id) {
gameList.unmatched.push(gameList.sessions[i].host.id);
}
gameList.sessions.splice(i, 1);
return;
}
}
}
} else {
gameList.unmatched.splice(gameList.unmatched.indexOf(socket.clientId), 1);
}
}
io.on('connection', function (socket) {
console.log('[Connection Opened]');
io.emit('update', gameList);
socket.on('disconnect', function() {
removeTankFromGameList(socket);
io.emit('client-disconnect', { 'id': socket.clientId, 'type': socket.clientType });
});
socket.on('client-connect', function(clientInfo) {
socket.clientType = clientInfo.type;
socket.clientId = clientInfo.id;
console.log('Client Type: ' + socket.clientType +' | Client ID: ' + socket.clientId);
if(socket.clientType == 'tank') {
removeTankFromGameList(socket);
gameList.unmatched.push(socket.clientId);
}
io.emit('update', gameList);
});
socket.on('command', function (command) {
console.log( { 'command' : command } );
io.emit('command', command);
});
});