-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
191 lines (153 loc) · 4.75 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
var WebSocketServer = require('ws').Server;
var http = require('http');
var express = require('express');
var app = express();
var port = process.env.PORT || 5000;
var GameCore = require('./core.js');
var core = new GameCore();
app.engine('ejs', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.get('/', function (req, res) {
res.render('index', {
WSS_URL: process.env.WSS_URL || 'ws://localhost:' + port
});
});
app.use(express.static(__dirname + '/public'));
var server = http.createServer(app);
server.listen(port);
var wss = new WebSocketServer({
server: server
});
console.log('WebSocket server listening on port ' + port);
// Stores connections
// With the same IDs as players
var sockets = {};
// Keep track of average tick length for interpolation when shot is fired
var recent_ticks = [tick_rate, tick_rate, tick_rate, tick_rate, tick_rate];
var average_tick_rate = tick_rate;
// Some server state
var socket_id_counter = 0;
var tick_rate = 50;
var tick_length = tick_rate;
var update_time = new Date().getTime()
var ping_rate = 5000;
var ping_time = new Date().getTime();
// The mighty game loop
var game_loop = function() {
core.updateInterpolationState();
core.updateBots(tick_length);
core.updatePlayers(tick_length);
core.spawnedPlayers.forEach(function(v) {
send_to_all('s_player_spawned', v);
});
send_to_all('s_players', {
players: core.players,
shots: core.shots,
hits: core.hits,
kills: core.kills,
control_number: core.controlNumber,
server_time: new Date().getTime()
});
Object.keys(core.players).forEach(function(v) {
send_to_one(v, 's_handled_deltas', core.handledDeltas[v]);
});
core.resetActions();
tick_length = new Date().getTime() - update_time;
// Keep an eye on latency
if (new Date().getTime() - ping_time > ping_rate) {
send_to_all('s_ping');
ping_time = new Date().getTime();
}
calculate_average_tick_length(tick_length);
update_time = new Date().getTime();
setTimeout(game_loop, tick_rate);
};
// Keep an eye on how fast the server is working
var calculate_average_tick_length = function(last_tick_length) {
recent_ticks.shift();
recent_ticks.push(last_tick_length);
average_tick_rate = 0;
recent_ticks.forEach(function(v) {
average_tick_rate += v;
});
average_tick_rate /= recent_ticks.length;
};
// A user has fired. See if someone got hit, using positions from the past.
var fire = function(socket_id, source, direction) {
var hit_data = core.fire(update_time, average_tick_rate, socket_id, source, direction);
// Send hit confirmation ASAP
if (hit_data !== undefined && hit_data.target_id > -1) {
send_to_one(socket_id, 's_hit_target', hit_data );
}
};
var handle_message = function(socket_id, message, data) {
switch (message) {
case 'c_pong':
core.setPlayerValue(socket_id, 'latency', (new Date().getTime() - ping_time)/2);
break;
case 'c_delta':
core.pushDelta(socket_id, data);
break;
case 'c_fire':
fire(socket_id, data.source, data.direction);
break;
case 'c_name':
core.setPlayerValue(socket_id, 'name', data);
break;
case 'c_add_bot':
var bot = core.addBot();
if (bot) send_to_all('s_player_connected', core.players[bot.id]);
break;
case 'c_remove_bot':
var bot = core.removeBot();
if (bot) send_to_all('s_player_disconnected', bot.id);
break;
default:
console.error('Unknown message:', message);
break;
}
};
wss.on('connection', function(ws) {
var socket_id, player, init_data;
socket_id = socket_id_counter++;
sockets[socket_id] = ws;
player = core.newPlayer(socket_id);
console.log('New player:', socket_id, player);
ws.onmessage = function(messageString) {
var message, data, seq;
message = JSON.parse(messageString.data).message;
data = JSON.parse(messageString.data).data;
handle_message(socket_id, message, data, seq);
};
ws.on('error', function(error) {
console.error('Server ERROR', error);
});
ws.on('close', function() {
console.log('Disconnected:', socket_id, core.players[socket_id].name, socket_id);
delete sockets[socket_id];
core.removePlayer(socket_id);
send_to_all('s_player_disconnected', socket_id);
});
init_data = {
player: player,
players: core.players,
constants: core.constants,
occluders: core.occluders,
control_number: core.controlNumber
};
send_to_one(socket_id, 's_init', init_data);
send_to_all('s_player_connected', player);
});
// Send a message to a specific player
var send_to_one = function(socket_id, message, data) {
if (sockets[socket_id] && sockets[socket_id].readyState === 1) {
sockets[socket_id].send(JSON.stringify({ socket_id: socket_id, message: message, data: data}));
}
};
// Send a message to all players
var send_to_all = function(message, data) {
Object.keys(sockets).forEach(function(v) {
send_to_one(v, message, data);
});
};
game_loop();