-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
327 lines (265 loc) · 8.31 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
'use strict';
const logger = require('./logger.js');
const World = require('./game/world.js');
const CommandFactory = require('./game/command');
const {ServerConfig, WorldConfig, Commands} = require('../shared/constant.js');
const describeAll = require('./network/descriptor.js');
/**
* Server is the main server-side singleton code.
* This class should not be used to contain the actual game logic.
* Only logging gameplay statistics and registering user activity and user data.
*/
class Server {
constructor(io) {
/**
* Key: Socket.id
* @type {Map<String, Object>} HashTable that contains the currently.
* connected players.
*/
this.connectedPlayers = new Map();
/**
* @type {Map<Number, Array.<Function>>}
*/
this.playerInputQueues = new Map();
this.intervalFrameRate = ServerConfig.STEP_RATE || 60;
this.maximumPlayer = ServerConfig.MAX_PLAYERS || 50;
this.timeoutInterval = ServerConfig.TIMEOUT_INTERVAL || 40;
this.lastPlayerID = 0;
/**
* @type {World} The actual game world
*/
this.world = null;
this.gameTick = 0;
this.outgoingBuffer = [];
this.setupSocketIO(io);
}
/**
* @param io {Socket}
*/
setupSocketIO(io) {
this.io = io;
io.on('connection', (socket) => {
this.onPlayerConnected(socket);
});
}
/**
* @param worldSettings {Object=}
* @param worldSettings.filename {String}
*/
initWorld(worldSettings = {filename: null}) {
this.world = new World(this, worldSettings.filename);
/**
* The worldSettings defines the game world constants, such
* as width, height, depth, etc. such that all other classes
* can reference these values.
* @member {Object} worldSettings
* @memberof Server
*/
this.worldSettings = Object.assign({}, worldSettings);
}
/**
* Setup very thing needed before the first game tick.
*/
setup(args) {
this.initWorld({filename: args[0]});
}
/**
* Start game logic and the clock
*/
start() {
const intervalDelta = Math.floor(1000 / this.intervalFrameRate);
this.outgoingDelta = Math.floor((1000 / this.intervalFrameRate) * 2);
this.serverStartTime = (new Date().getTime());
this.lastServerTime = this.serverStartTime;
// game ticks at 60 fps
// world data is sent 30 fps
this.intervalGameTick = setInterval(() => {
let timeNow = (new Date().getTime());
const dt = timeNow - this.lastServerTime;
this.step(dt);
// ------- create world descriptor, ignore if there is no players
if (this.connectedPlayers.size !== 0) {
let str = describeAll(this.world.players);
this.outgoingBuffer.push({
t: this.gameTick,
d: str,
});
this.sendOutgoingBuffer();
}
// ------
this.lastServerTime = timeNow;
this.gameTick++;
}, intervalDelta);
}
/**
* @return {number}
*/
getSeverTime() {
return (new Date().getTime()) - this.serverStartTime;
}
sendOutgoingBuffer() {
this.io.emit('update', this.outgoingBuffer);
this.outgoingBuffer = [];
}
resetIdleTimeout(socket) {
if (socket.idleTimeout) {
clearTimeout(socket.idleTimeout);
}
if (this.timeoutInterval > 0) {
socket.idleTimeout = setTimeout(() => {
this.onPlayerTimeout(socket);
}, this.timeoutInterval * 1000);
}
}
/**
* Single game step.
*
* @param {Number=} dt - elapsed time since last step was called.
*/
step(dt) {
let step = ++this.world.stepCount;
// process input commands together.
this.playerInputQueues.forEach((commands, playerId) => {
commands.forEach((cmd)=>{
this.world.processInput(cmd);
});
commands.length = 0; // clear the array
});
// Main Game Update Goes Here
this.world.step(dt);
}
/**
* Called when the Client first opens up the browser.
* However the players is no yet joined to the world.
* @param socket {Socket}
*/
onPlayerConnected(socket) {
let onlineCount = this.connectedPlayers.size + 1;
logger.info(`[${onlineCount}] A Client connected`);
// get next available id
let playerId = ++this.lastPlayerID;
// save player
this.playerInputQueues.set(playerId, []);
this.connectedPlayers.set(socket.id, {
socket: socket,
state: 'new',
playerId: playerId,
});
// create a new Event (indicating connection)
let playerEvent = {
id: socket.id,
playerId: playerId,
joinTime: this.getSeverTime(),
disconnectTime: 0,
};
logger.info(`[${playerEvent.id}] Has joined the world
playerId ${playerEvent.playerId}
joinTime ${playerEvent.joinTime}
disconnectTime ${playerEvent.disconnectTime}`);
this.onPlayerJoinWorld(socket, playerEvent);
socket.on('disconnect', () => {
this.onPlayerDisconnected(socket, playerEvent);
logger.info(`[${playerEvent.id}][playerEvent] disconnect
playerId ${playerEvent.playerId}
joinTime ${playerEvent.joinTime}
disconnectTime ${playerEvent.disconnectTime}`);
});
}
/**
* handle player when join the world
* @param socket {Socket}
* @param playerEvent {Object}
*/
onPlayerJoinWorld(socket, playerEvent) {
this.resetIdleTimeout(socket);
// This send the data of all player to the new-joined client.
let newX;
let newY;
do {
newX = Math.floor(Math.random() * WorldConfig.WIDTH);
newY = Math.floor(Math.random() * WorldConfig.HEIGHT);
} while (!this.world.isPassable(newX, newY, 2));
playerEvent.x = newX;
playerEvent.y = newY;
this.world.addPlayer(newX, newY, playerEvent.playerId);
socket.emit('initWorld', {
players: describeAll(this.world.players),
tiles: this.world.tilemap.serialize(),
trees: describeAll(this.world.getTreePosArray()),
chests: describeAll(this.world.getChestPosArray()),
id: playerEvent.playerId,
weather: this.world.currentWeather,
});
// This send out the new-joined client's data to all other clients
socket.broadcast.emit('playerEvent', playerEvent);
socket.on('inputCommand', (cmd) => {
this.onReceivedInput(cmd, socket, playerEvent.playerId);
});
this.world.emit('playerSpawn');
}
onPlayerTimeout(socket) {
logger.info(`A Client timed out after ${
this.timeoutInterval} seconds`);
socket.disconnect();
}
/**
* handle player dis-connection
* @param socket {Socket}
*/
onPlayerDisconnected(socket, playerEvent) {
playerEvent.disconnectTime = this.getSeverTime();
socket.broadcast.emit('playerEvent', playerEvent);
// Remove from Game World
let player = this.connectedPlayers.get(socket.id);
if (player) {
this.world.removePlayer(player.playerId);
} else {
logger.error('should not happen');
}
// Remove from server
this.connectedPlayers.delete(socket.id);
let onlineCount = this.connectedPlayers.size;
logger.info(`[${onlineCount}] A Client disconnected`);
}
/**
* @param cmd {Object}
* @param cmd.type {Number} import from 'constant.js' Commands
* @param cmd.params {Object}
* @param socket {Socket}
* @param playerId {Number}
*/
onReceivedInput(cmd, socket, playerId) {
const player = this.world.players.get(playerId);
this.resetIdleTimeout(socket);
let command;
switch (cmd.type) {
case Commands.MOVEMENT:
command = CommandFactory.makeMoveCommand(player, cmd.params);
break;
case Commands.ALTER_TILE:
command = CommandFactory.makeChangeTileCommand(player, cmd.params);
break;
case Commands.COMMUNICATION:
command = CommandFactory.makeCommunicateCommand(player, cmd.params);
break;
case Commands.INTERACTION:
command = CommandFactory.makeInteractCommand(player, cmd.params);
break;
default:
logger.error(`Invalid Command ${cmd.type}`);
return;
}
this.queueInputForPlayer(command, playerId);
}
/**
* Add an input to the input-queue for the specific player, each queue is
* key'd by step, because there may be multiple inputs per step.
* @param cmd {Function}
* @param playerId
*/
queueInputForPlayer(cmd, playerId) {
let queue = this.playerInputQueues.get(playerId);
queue.push(cmd);
}
}
module.exports = Server;