-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
executable file
·193 lines (169 loc) · 6.3 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
#!/usr/bin/env node
"use strict";
var maxClients = 20,
maxMsgLength = 500,
pushInterval = 75,
port = process.argv[2] ? parseInt(process.argv[2],10) : 8080,
WebSocket = require('ws'), WebSocketServer = WebSocket.Server,
wss = new WebSocketServer( {host: '0.0.0.0', port: port}),
clients = {}, clientData = {}, nextId = 1,
changes = {};
function log(msg) { console.log(Date() + ": " + msg); }
function warn(msg) { console.warn(Date() + ": " + msg); }
function error(msg) { console.error(Date() + ": " + msg); }
function removeClient(clientId) {
if (clients[clientId]) {
warn("Removing Client ID " + clientId);
delete clients[clientId];
delete clientData[clientId];
delete changes[clientId];
log("Current number of clients: " + Object.keys(clients).length);
sendAll(JSON.stringify({"delete": clientId}));
}
};
function sendOne (clientId, json) {
var client = clients[clientId];
if (client) {
try {
if (client.readyState === WebSocket.OPEN) {
client.send(json);
}
} catch (e) {
log("Failed to send to client ID " + clientId);
removeClient(clientId);
}
}
}
function sendAll (json) {
var clientIds = Object.keys(clients), clientId;
for (var i = 0; i < clientIds.length; i++) {
clientId = clientIds[i];
sendOne(clientId, json);
}
}
function tooManyClients(client, clientId) {
warn("Too many clients, disconnecting client " +
(clientId ? clientId : ""));
client.close(1008, "Too many clients, try again later");
}
// Periodically push out accumulated changes
setInterval(function () {
if (Object.keys(changes).length > 0) {
// Game specific logic tranforms
changes = game1110Logic(clientData, changes);
for(var clientId in changes) {
var change = changes[clientId];
if(!change.temp) {
for (var key in change) {
if (key !== "whomp") {
clientData[clientId][key] = change[key];
}
}
}
}
sendAll(JSON.stringify({"change": changes}));
}
changes = {};
}, pushInterval);
wss.on('connection', function(client) {
var numClients = Object.keys(clients).length+1;
if (numClients > maxClients) {
tooManyClients(client);
return;
}
var clientId = nextId;
nextId++;
clients[clientId] = client;
clientData[clientId] = {"id": clientId};
log("New client ID: " + clientId);
log("Current number of clients: " + numClients);
// Send the new guy his ID and all the other client states
sendOne(clientId, JSON.stringify({"id": clientId, "all": clientData}));
client.on('close', function() {
warn("Client ID " + clientId + " disconnected");
removeClient(clientId);
});
client.on('message', function(message) {
var data;
//log("Received message from client ID " + clientId + ": " + message);
if (message.length > maxMsgLength) {
error("client " + clientId + " sent oversize " + message.length + " byte message ");
client.close(1009, "Message length too long");
return;
}
else if(message == "testTooManyClients") { // for testing
log("received testTooManyClients from client " + clientId);
tooManyClients(client, clientId);
return;
}
try {
data = JSON.parse(message);
} catch (e) {
error("failed to parse client " + clientId + " message: " + message);
return;
}
// We have some sort of update
if (!changes[clientId]) {
changes[clientId] = {};
}
if (!changes[clientId].whomp) {
for(var key in data) {
changes[clientId][key] = data[key];
}
}
});
});
// Game specific logic is isolated here
var warpArea = 20,
warps = [
{src: {x: -144, y: -1295}, // Middle of first tree
dst: {x: 10490, y: -2104}}, // Dead tree near mario land
{src: {x: -594, y: -1484},
dst: "random-person"}];
function game1110Logic(clientData, changes) {
for (var clientId in changes) {
var curC = clientData[clientId],
newC = changes[clientId];
if (/there'?s no place like home/i.exec(newC.msg)) {
newC.whomp = true;
newC.x = -594;
newC.y = -1284;
newC.msg = "";
changes[clientId] = newC;
changes[nextId++] = { skin: 'warp', also: curC.skin,
x: curC.x, y: curC.y, temp: true };
changes[nextId++] = { skin: 'warp', x: newC.x, y: newC.y, temp: true };
}
// Check for warp
if (newC.skin && newC.skin !== curC.skin) {
//console.log("Detected skin change to " + newC.skin + " for client id " + clientId);
for (var i=0; i < warps.length; i++) {
var warp = warps[i];
if ((curC.x < warp.src.x + warpArea) &&
(curC.x > warp.src.x - warpArea) &&
(curC.y < warp.src.y + warpArea) &&
(curC.y > warp.src.y - warpArea)) {
if (warp.dst === "random-person") {
var ids = Object.keys(clients);
if (ids.length === 1) { continue; }
var our_idx = ids.indexOf(clientId);
var rnd_idx = Math.floor(Math.random()*(ids.length - 1));
var id = ids[rnd_idx < our_idx ? rnd_idx : rnd_idx + 1];
newC.x = clientData[id].x;
newC.y = clientData[id].y;
} else {
newC.x = warp.dst.x;
newC.y = warp.dst.y;
}
newC.skin = curC.skin;
console.log("Warp of " + clientId + " to " + newC.x + "," + newC.y);
changes[clientId]["whomp"] = true;
changes[nextId++] = { skin: 'warp', also: curC.skin,
x: curC.x, y: curC.y, temp: true };
changes[nextId++] = { skin: 'warp', x: newC.x, y: newC.y, temp: true };
}
}
}
}
return changes;
}