-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
337 lines (283 loc) · 12.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
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
328
329
330
331
332
333
334
335
336
337
var serverPort = 6969;
var tickRate = 30; // ticks per second
// init dependencies
var fs = require('fs');
var request = require('request');
var gulp = require('gulp');
var app = require('express')();
var server = app.listen(serverPort);
var socket = require('socket.io');
var io = socket.listen(server);
var serverVersion = require('./package.json').version; // read version from package.json
// Start server
server.listen(serverPort);
console.log("Server version " + serverVersion + " running on port " + serverPort);
// init global variables
var chatQueue = []; // Unlikely to contain more than a few messages, rendering handled clientside
var players = []; // It is normal for this to have undefined values, please account for this!
var mapSize = [1280, 720]; // allowed movement range of the game world
// init world
// TODO: file i/o for world
var temporaryShitTile = {type: "grass", collides: false};
var temporaryShitTile2 = {type: "stone", collides: true};
var temporaryShitChunk = [
[temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile],
[temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile],
[temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile],
[temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile2,temporaryShitTile2,temporaryShitTile,temporaryShitTile,temporaryShitTile],
[temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile2,temporaryShitTile2,temporaryShitTile,temporaryShitTile,temporaryShitTile],
[temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile],
[temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile],
[temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile,temporaryShitTile]
];
var world = [
[temporaryShitChunk,temporaryShitChunk,temporaryShitChunk,temporaryShitChunk],
[temporaryShitChunk,temporaryShitChunk,temporaryShitChunk,temporaryShitChunk],
[temporaryShitChunk,temporaryShitChunk,temporaryShitChunk,temporaryShitChunk],
[temporaryShitChunk,temporaryShitChunk,temporaryShitChunk,temporaryShitChunk]
];
// Useful prototypes/helper functions
// -- << -- << -- << -- << -- << -- << -- << -- << -- << -- << -- << -- <<
// Same as .replace(), except covers all occurrences
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
// Removes all special characters from the given string.
String.prototype.removeSpecialChars = function () {
var target = this;
return target.replace(/[^\w\s]/gi, '');
};
// Add a string to the chat queue
function addToChatQueue(msg){
chatQueue.push(msg);
}
// send a boop to the given ID from a given sender object
function sendBoop(id, playerObject){
io.in(id).emit('boop', playerObject);
}
// Send a chat message to a specific player
function sendChatMessageToPlayer(playerObj, msg){
io.in(playerObj.id.toString()).emit('updateChat', [msg]);
}
// Send any request to this specific player
function sendRequestToPlayer(playerObj, requestString, obj){
io.in(playerObj.id.toString()).emit(requestString, obj);
}
// -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >>
// HTTP file serving
// -- << -- << -- << -- << -- << -- << -- << -- << -- << -- << -- << -- <<
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/favicon.ico', function (req, res) {
res.sendFile(__dirname + '/favicon.ico');
});
app.get('/oldclient.html', function (req, res) {
res.sendFile(__dirname + '/oldclient.html');
});
app.get('/client.js', function (req, res) {
res.sendFile(__dirname + '/client.js');
});
app.get('/client-old.js', function (req, res) {
res.sendFile(__dirname + '/client-old.js');
});
app.get('/easeljs.js', function (req, res) {
if(fs.existsSync(__dirname + "/easeljs.js")){ // is there a local version of the script?
console.log("Local easeljs.js is present, serving...");
res.sendFile(__dirname + "/easeljs.js");
} else {
console.log("Local easeljs.js is not present, piping from CDN...");
var reply = request("https://code.createjs.com/easeljs-0.8.2.min.js");
//req.pipe(reply);
reply.pipe(res);
}
});
app.get('/dog.png', function (req, res) {
res.sendFile(__dirname + '/assets/dog.png');
});
app.get('/boop.png', function (req, res) {
res.sendFile(__dirname + '/assets/boop.png');
});
app.get('/grass.png', function (req, res) {
res.sendFile(__dirname + '/assets/grass.png');
});
app.get('/stone.png', function (req, res) {
res.sendFile(__dirname + '/assets/stone.png');
});
// -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >>
// Player has loaded the webpage
io.on('connection', function (socket) {
// TODO: Prevent rapid-fire requests
// player-specific local variables
var thisPlayerObject;
var thisPlayerId = "";
var thisPlayerName = "";
var loggedIn = false;
socket.join(socket.id);
//var recentActions = 0; // incrememnted every time an action is performed, reset every second
// Functions for common tasks
// -- << -- << -- << -- << -- << -- << -- << -- << -- << -- << -- << -- <<
// Update all references to a player object with a given player object
function updatePlayerObject(playerObject){
for(var pl in players){
if(players[pl].id == playerObject.id) players[pl] = thisPlayerObject;
var arrayUpdateSuccess = true; // keep track of whether the player array was successfully updated
}
// Alert the console of any potential issues with player updates
if(!arrayUpdateSuccess){
console.log("Player " + playerObject.name + "'s (" + playerObject.id + ") array object failed to update! Problems may arise shortly.");
}
thisPlayerObject = playerObject;
}
// Force a player to update their local player object
function forceUpdatePlayer(playerObject){
io.in(socket.id.toString()).emit('forceUpdatePlayer', playerObject);
}
// Gets the array ID of a given player object
function getPlayerArrayID(playerObject){
for(var pl in players){
if(players[pl].id == playerObject.id) return pl;
}
}
// Calculates the distance between two [x, y] coordinate arrays.
// TODO: distance calc
function calculateDistance(){
}
// -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >> -- >>
// Player has disconnected
socket.on('disconnect', function () {
//recentActions++;
if (loggedIn) {
// get the array ID of this player
var thisPlayerArrayID = getPlayerArrayID(thisPlayerObject);
console.log(thisPlayerObject.name + " disconnected.");
updatePlayerObject(thisPlayerObject);
io.emit('playerDisconnect', thisPlayerObject);
players.splice(thisPlayerArrayID, 1); // remove current player
}
});
// Received attempt to login from client
// Arg: player object
socket.on('loginAttempt', function (playerInfo) {
//recentActions++;
console.log("Login attempt with name '" + playerInfo.name + "'");
var fixedName = playerInfo.name.removeSpecialChars(); // remove invalid characters from the name
if (fixedName != "" && fixedName.length <= 8) {
// check if name is already taken
var takenCheck = false;
for (var pl in players) {
if (players[pl].name == fixedName) {
takenCheck = true;
}
}
if (takenCheck) {
io.in(socket.id.toString()).emit('loginDenied', "Taken!"); // name was taken
} else {
if(playerInfo.version != serverVersion){
io.in(socket.id.toString()).emit('loginDenied', "Client outdated!"); // name was taken
}else {
// prepare local variables
loggedIn = true;
playerInfo.name = fixedName;
console.log(socket.id.toString() + ", " + fixedName + " has connected.");
addToChatQueue(fixedName + " has joined the server.");
playerInfo.id = socket.id;
thisPlayerId = socket.id;
thisPlayerName = fixedName;
players.push(playerInfo);
thisPlayerObject = playerInfo;
// accept login
socket.emit('loginAccepted', playerInfo);
//console.log(players);
//io.in(socket.id.toString()).emit('updatePlayer', playerInfo);
}
}
} else {
if(fixedName == ""){
socket.emit('loginDenied', "Enter a name!");
}else {
socket.emit('loginDenied', "Too long!");
}
}
});
// Received updated player from client
// Arg: player object
socket.on('updatePlayer', function (playerObject) {
//recentActions++;
var movementInvalid = false;
if (loggedIn) {
// make sure the player isn't being a little shit
if(playerObject.id != thisPlayerId || playerObject.name != thisPlayerName){
playerObject.id = thisPlayerId;
playerObject.name = thisPlayerName;
sendChatMessageToPlayer(thisPlayerObject, "Invalid player info detected! Stop trying to hack the game.");
forceUpdatePlayer(playerObject);
}
// check for invalid movement since last update
if(Math.abs(playerObject.x - thisPlayerObject.x) > 15 || playerObject.x > 1536 - 24 || playerObject.x < 24){
playerObject.x = thisPlayerObject.x;
movementInvalid = true;
}
if(Math.abs(playerObject.y - thisPlayerObject.y) > 15 || playerObject.y > 1536 - 24 || playerObject.y < 24){
playerObject.y = thisPlayerObject.y;
movementInvalid = true;
}
if(movementInvalid){
sendChatMessageToPlayer(thisPlayerObject, "Invalid movement!");
forceUpdatePlayer(playerObject);
}
updatePlayerObject(playerObject);
}
});
// Player submitted a chat message
// Arg: string
socket.on('chatMessage', function (msg) {
//recentActions++;
if (loggedIn && msg != "") {
if (msg.replaceAll(" ", "") == "") msg = "I am an immature child begging for attention.";
var formattedMsg = "<b>" + thisPlayerObject.name + "</b>: " + msg.substr(0, 100).replaceAll("<", "<").replaceAll(">", ">");
console.log("[CHAT] " + formattedMsg.replaceAll("<b>", "").replaceAll("</b>", ""));
addToChatQueue(formattedMsg);
}
});
// Player is sending a boop
// Arg: string
socket.on('boop', function (id) {
//recentActions++;
if (loggedIn) {
var target = undefined;
// check if target ID exists
// TODO: distance comparison
for (var x in players) {
if (players[x].id == id) target = players[x];
}
if (target) {
sendBoop(id, thisPlayerObject);
} else {
console.log(thisPlayerObject.name + " tried to boop an invalid target.");
}
}
})
});
// server loop
// currently ~30 ticks/sec
setInterval(function () {
if(chatQueue != []) { // make sure chatlog isn't empty
io.emit('updateChat', chatQueue);
chatQueue = [];
}
// update world for each player
for(var pl in players){
var pos = [players[pl].x, players[pl].y];
var chunks = []
for(var x in world){
for(var y in world[x]){
if(Math.abs(x * 48 * 8 - (pos[0])) <= 2000 || Math.abs(y * 48 * 8 - (pos[1])) <= 1000){
chunks.push({x: x * 48 * 8, y: y * 48 * 8, chunk: world[x][y]});
}
}
}
sendRequestToPlayer(players[pl], 'updateWorld', [players, chunks]);
}
}, Math.round(1000 / tickRate));