This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
190 lines (159 loc) · 4.5 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
'use strict';
var PORT = 8080;
var GAMELOOP_FREQUENCY = 45;
var _ = require('underscore');
var io = require('socket.io').listen(PORT, '0.0.0.0');
var StateMachine = require('sfsm');
// Global player stats
var players = {};
// Global game state
var game = {
playerCount: 0,
playersInGoal: {},
playersInStart: {},
numLevel: 0,
resetLevel: function resetLevel() {
this.playersInGoal = {};
this.playersInStart = {};
this.numLevel += 1;
}
};
var gameLoop = {
_handle: 0,
_previous: {},
_lastTime: 0,
_loop: function _loop() {
var state = {
players: _.invoke(players, 'serialize'),
game: game
};
if (!_.isEqual(state, this._previous)) {
io.sockets.emit('serverstate', state);
this._previous = state;
}
if (this._handle) {
this.start();
}
},
check: function check() {
if (game.playerCount > 0 && this._handle === 0) {
this.start();
} else if (game.playerCount === 0 && this._handle !== 0) {
this.stop();
}
},
start: function start() {
var currTime = Date.now();
var timeToCall = Math.max(0, GAMELOOP_FREQUENCY -
(currTime - this._lastTime));
this._lastTime = currTime + timeToCall;
this._handle = setTimeout(this._loop.bind(this), timeToCall);
},
stop: function stop() {
clearInterval(this._handle);
this._handle = 0;
}
};
var PlayerState = function PlayerState(player) {
this.player = player;
this.startup();
};
PlayerState.prototype.onconnected = function onconnected() {
console.log('onconnected: ', this.player.id);
game.playerCount += 1;
gameLoop.check();
};
PlayerState.prototype.ondisconnected = function ondisconnected() {
delete players[this.player.id];
console.log('player ' + this.player.id + ' disconnected and killed.');
io.sockets.emit('player disconnected', this.player.serialize());
game.playerCount -= 1;
delete game.playersInStart[this.player.id];
gameLoop.check();
};
StateMachine.create({
target: PlayerState.prototype,
final: 'disconnected',
events: [
{ name: 'startup', from: 'none', to: 'connecting' },
{ name: 'connect', from: 'connecting', to: 'connected' },
{ name: 'disconnect', from: 'connected', to: 'disconnected' }
]
});
function Player(id) {
this.id = id;
this.state = new PlayerState(this);
this.x = 0;
this.y = 0;
}
Player.prototype.serialize = function serialize() {
return {
id: this.id,
x: this.x,
y: this.y
};
};
io.configure(function () {
io.set('log level', 0);
});
io.sockets.on('connection', function onConnection(client) {
var otherPlayers = _.clone(players);
var player = players[client.id] = new Player(client.id);
client.emit('connected', {
id: client.id,
players: _.invoke(otherPlayers, 'serialize'),
numLevel: game.numLevel
});
player.state.connect();
client.on('disconnect', function onDisconnect() {
player.state.disconnect();
});
client.broadcast.emit('player connected', player.serialize());
function sendLevelState() {
_.each(players, function (player) {
io.sockets.emit('game event', {
eventName: 'enterState',
args: ['level'],
playerId: player.id
});
});
}
function loadNextLevel() {
game.resetLevel();
_.each(players, function (player) {
io.sockets.emit('game event', {
eventName: 'loadNextLevel',
args: { numLevel: game.numLevel },
playerId: player.id
});
});
}
client.on('game event', function onGameEvent(data) {
if (data.eventName === 'enterState') {
if (data.args[0] === 'warmup') {
game.playersInStart[data.playerId] = true;
if (Object.keys(game.playersInStart).length === game.playerCount) {
sendLevelState();
}
} else if (data.args[0] === 'level') {
// player could want to come back to level to retry, send level only to him
if (Object.keys(game.playersInStart).length === game.playerCount) {
io.sockets.emit('game event', data);
}
} else if (data.args[0] === 'retry') {
io.sockets.emit('game event', data);
} else if (data.args[0] === 'finish') {
game.playersInGoal[data.playerId] = true;
// At least all but one.
if (Object.keys(game.playersInStart).length >= (game.playerCount - 1)) {
loadNextLevel();
}
}
} else if (data.eventName === 'mouseMove') {
player.x = data.args[0];
player.y = data.args[1];
} else {
io.sockets.emit('game event', data);
}
});
});