-
Notifications
You must be signed in to change notification settings - Fork 2
/
input.js
420 lines (336 loc) · 14.9 KB
/
input.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/** Require external modules */
const crypto = require(`crypto`);
/**
* Process input from a user at the name state.
* @param socket User's socket item
* @param buffer User's input buffer
* @param user User item
*/
async function processStateName(world, user, buffer) {
/** Force name to start with uppercase letter */
const name = buffer.toString().trim().charAt(0).toUpperCase() + buffer.toString().trim().toLowerCase().slice(1);
if ( !name.match(/^[a-z]+$/i) ) {
/** Invalid characters in name */
user.send(`Your name must contain only letters.\r\n`);
user.send(`Please enter a new name: `);
} else if ( name.length < 3 || name.length > 14 ) {
/** Invalid name length */
user.send(`Your name must be between 3 and 14 characters long.\r\n`);
user.send(`Please enter a new name: `);
} else {
/** Backup socket */
const socket = user.socket();
/** Attempt to load (which initializes user first) */
let existingUser = await user.load(name, world.database());
/** Toggle user's command boolean */
user.command(true);
/** Restore user socket */
user.socket(socket);
/** If failed to load, send new password prompt */
if ( !existingUser ) {
/** Store name with original capitalization */
user.name(buffer.toString().trim().charAt(0).toUpperCase() + buffer.toString().trim().slice(1));
user.send(`Welcome to Muddy, ${name}!\r\n`);
user.send(`Please choose a password: `);
/** Move on to ask for them to pick a password */
user.state(world.constants().states.NEW_PASSWORD);
}
/** Otherwise, send old pasword prompt */
else {
/** Existing user */
user.send(`Please enter your password: `);
/** Move on to ask for existing password */
user.state(world.constants().states.OLD_PASSWORD);
}
/** Hide text for password */
user.send(world.constants().VT100_HIDE_TEXT);
}
}
/**
* Process input from a user at the old password state.
* @param socket User's socket item
* @param buffer User's input buffer
* @param user User item
*/
async function processStateOldPassword(world, user, buffer) {
/** Define recursive helper function for adding any item contents to world and setting container of each */
const recursiveItemContents = (item) => {
/** Set prototype of item */
item.prototype(world.itemPrototypes().find(x => x.id() == item.prototype().id()));
item.contents().forEach((content) => {
content.container(item);
recursiveItemContents(content);
});
};
/** Set up the crypto */
const hash = crypto.createHmac(`sha512`, user.salt());
/** Get the encrypted password */
const password = hash.update(buffer).digest(`hex`);
/** Stop hiding text */
user.send(world.constants().VT100_CLEAR);
/** Correct password? */
if ( password == user.password() ) {
/** Record the address if it's not already on the user's list */
if ( !user.addresses().includes(user.socket().address().address) )
user.addresses().push(user.socket().address().address);
await user.update(world.database());
/** Password matches, display message of the day */
user.send(world.colorize(world.motd()));
/** Get the last room */
let room = world.rooms().find(x => x.id() == user.room().id());
/** Verify last room exists or bug out */
if ( !room ) {
world.log().error(`User ${user.name()} trying to start with bad last room ${user.room().id()}, redirecting to start.`);
room = world.rooms().find(x => x.id() == world.constants().START_ROOM);
/** Verify start room exists or bug out */
if ( !room ) {
world.log().error(`Fatal error: Unable to load start room.`);
process.exit(1);
}
}
/** Set all inventory item's character to user */
user.inventory().forEach((item) => {
item.character(user);
recursiveItemContents(item);
});
/** Set all equipment item's character to user */
user.equipment().forEach((item) => {
item.character(user);
/** Add item stats to user's stats */
user.accuracy(user.accuracy() + item.accuracy());
user.air(user.air() + item.air());
user.armor(user.armor() + item.armor());
user.deflection(user.deflection() + item.deflection());
user.dodge(user.dodge() + item.dodge());
user.earth(user.earth() + item.earth());
user.fire(user.fire() + item.fire());
user.life(user.life() + item.life());
user.power(user.power() + item.power());
user.speed(user.speed() + item.speed());
user.water(user.water() + item.water());
recursiveItemContents(item);
});
/** Move the user to the last room */
await world.characterToRoom(user, room);
/** Move on and pause until they're done reading the message of the day */
user.state(world.constants().states.MOTD);
world.log().info(`User ${user.name()} connected.`);
} else {
/** Password incorrect, remove user from world and terminate socket */
if ( world.users().indexOf(user) !== -1 )
world.users().splice(world.users().indexOf(user), 1);
/** Terminate socket */
user.socket().end(world.constants().VT100_CLEAR + `Incorrect password, goodbye!\r\n`);
world.log().info(`Failed login by ${user.name()}.`);
}
}
/**
* Process input from a user at the new password state.
* @param socket User's socket item
* @param buffer User's input buffer
* @param user User item
*/
async function processStateNewPassword(world, user, buffer) {
/** Stop hiding text */
user.send(world.constants().VT100_CLEAR);
if ( buffer.match(/\s/i) ) {
/** Whitespaces in password not allowed */
user.send(`Your password must not contain spaces or other whitespace characters.\r\n`);
user.send(`Please enter a new password: `);
} else if ( buffer.length < 8 || buffer.length > 32 ) {
/** Invalid password length */
user.send(`Your password must be between 8 and 32 characters long.\r\n`);
user.send(`Please enter a new password: `);
} else {
/** Generate a random salt */
const salt = crypto.randomBytes(8).toString(`hex`);
/** Set up the crypto */
const hash = crypto.createHmac(`sha512`, salt);
/** Get the encrypted password */
const password = hash.update(buffer).digest(`hex`);
/** Store the encrypted password and salt */
user.password(password);
user.salt(salt);
user.send(`Please confirm your new password: `);
/** Move on and confirm the password */
user.state(world.constants().states.CONFIRM_PASSWORD);
}
/** Hide text for password */
user.send(world.constants().VT100_HIDE_TEXT);
}
/**
* Process input from a user at the confirm password state.
* @param socket User's socket item
* @param buffer User's input buffer
* @param user User item
*/
async function processStateConfirmPassword(world, user, buffer) {
/** Set up the crypto */
const hash = crypto.createHmac(`sha512`, user.salt());
/** Get the encrypted password back */
const password = hash.update(buffer).digest(`hex`);
/** Stop hiding text */
user.send(world.constants().VT100_CLEAR);
/** Correct password? */
if ( password == user.password() ) {
/** Record the address if it's not already on the user's list */
if ( !user.addresses().includes(user.socket().address().address) )
user.addresses().push(user.socket().address().address);
/** I don't think we want to save new character here */
//await user.update(world.database());
/** Password matches, proceed to the message of the day */
user.send(world.colorize(world.motd()));
/** Get the start room */
const room = world.rooms().find(x => x.id() == world.constants().START_ROOM);
/** Verify start room exists or bug out */
if ( !room ) {
world.log().error(`Fatal error: Unable to load start room.`);
process.exit(1);
}
/** Move the user to the start room */
await world.characterToRoom(user, room);
/** Move on and pause until they're done reading the message of the day */
user.state(world.constants().states.MOTD);
world.log().info(`User ${user.name()} connected.`);
} else {
/** Password does not match, let's try this again */
user.send(`Passwords do not match, please try again!\r\n`);
user.send(`Please choose a password: `);
user.state(world.constants().states.NEW_PASSWORD);
/** Hide text for password */
user.send(world.constants().VT100_HIDE_TEXT);
}
}
/**
* Process input from a user at the MOTD state.
* @param socket User's socket item
* @param buffer User's input buffer
* @param user User item
*/
async function processStateMOTD(world, user, buffer) {
/** Replace user if he already exists */
const oldUser = world.users().find((worldUser) => {
return user.name() == worldUser.name() && user !== worldUser;
});
if ( oldUser ) {
oldUser.room().users().forEach((roomUser) => {
if ( roomUser !== oldUser && roomUser !== user )
roomUser.send(`${oldUser.name()} suddenly gasps as if his spirit had just been taken over.\r\n`, false);
});
/** Remove user from room */
if ( oldUser.room() && oldUser.room().users().indexOf(oldUser) !== -1 )
oldUser.room().users().splice(oldUser.room().users().indexOf(oldUser), 1);
/** Remove user from world */
if ( world.users().indexOf(oldUser) !== -1 )
world.users().splice(world.users().indexOf(oldUser), 1);
world.log().info(`User ${oldUser.name()} has been replaced with a new user and socket.`);
/** Close old socket and tell them they've been taken over */
oldUser.socket().end(`Your body has been taken over!\r\n`);
/** Tell user he's taken over */
user.send(`You have reconnected to your old body.\r\n`);
}
/** Find and execute the look command for this user */
await world.commands().find(x => x.name() == `look`).execute()(world, user, ``, []);
world.send(world.colorize(`#YInfo -> ${user.name()} has come online.#n\r\n`));
/** Move on and put user in game */
user.state(world.constants().states.CONNECTED);
}
/**
* Process input from a user at the connected state.
* @param socket User's socket item
* @param buffer User's input buffer
* @param user User item
*/
async function processStateConnected(world, user, buffer) {
/** Parse arguments */
let args = [];
/** Convert buffer to lowercase string, trim spaces, except add one space at end to terminate final arg */
const argsBuffer = buffer.toString().trim() + ` `;
/** Keep track of whether we're inside quotes, what the quote character is, and the arg we're working on */
let insideQuotes = false;
let quote = ``;
let arg = ``;
/** Loop through buffer and group arguments that are quoted as args are added to list */
for ( let i = 0; i < argsBuffer.length; i++ ) {
const c = argsBuffer[i];
if ( insideQuotes && c == quote && argsBuffer[i + 1] && argsBuffer[i + 1] == ` ` ) {
args.push(arg);
arg = ``;
insideQuotes = false;
} else if ( arg.length == 0 && !insideQuotes && [`'`, `"`, `\``].includes(c) ) {
quote = c;
insideQuotes = true;
} else if ( !insideQuotes && c == ` ` ) {
if ( arg.length > 0 ) {
args.push(arg);
arg = ``;
}
} else {
arg += c;
}
}
if ( arg.length > 0 )
args.push(arg);
args = args.filter(x => x != ``);
/** If there was no command sent, just send a space to force flush of output buffer */
if ( args.length == 0 )
return user.send(` `);
/** Convert command name to lowercase for lookup */
const commandName = args[0].toLowerCase();
/** Find the first matching command, if one exists */
const command = world.commands().filter(x => x.name().startsWith(commandName))[0];
/** Allowable positions are configured positions or all by default */
const allowablePositions = command && command.positions().length > 0 ? command.positions() : world.constants().positionsAll;
/** Convert buffer to string and trim command */
buffer = buffer.toString().replace(new RegExp(`^[\\s'"]*${args[0]}[\\s'"]*`), ``);
/** If it exists, execute it for this user, otherwise send error */
if ( command && allowablePositions.includes(user.position()) )
await command.execute()(world, user, buffer, args.slice(1).map(x => x.toLowerCase()));
else if ( command && user.position() == world.constants().positions.DEAD )
user.send(`You can't do that... you are DEAD!\r\n`);
else if ( command && user.position() == world.constants().positions.INCAPACITATED )
user.send(`You can't do that while incapacitated!\r\n`);
else if ( command && user.position() == world.constants().positions.SLEEPING )
user.send(`You can't do that while sleeping.\r\n`);
else if ( command && user.position() == world.constants().positions.MEDITATING )
user.send(`You can't do that while meditating.\r\n`);
else if ( command && user.position() == world.constants().positions.LYING_DOWN )
user.send(`You can't do that while lying down.\r\n`);
else if ( command && user.position() == world.constants().positions.KNEELING )
user.send(`You can't do that while kneeling.\r\n`);
else if ( command && user.position() == world.constants().positions.SITTING )
user.send(`You can't do that while sitting.\r\n`);
else if ( command && user.position() == world.constants().positions.STANDING )
user.send(`You can't do that while standing.\r\n`);
else if ( command && user.position() == world.constants().positions.FIGHTING )
user.send(`You can't do that while fighting!\r\n`);
else
user.send(`That action does not exist in this world.\r\n`);
}
/**
* Dispatch input for processing based on user state.
* @param socket Socket that sent input
* @param buffer Input buffer
*/
module.exports.process = async function (world, user, buffer) {
/** Toggle user's command boolean */
user.command(true);
/** User is at the name state, first input of the game */
if ( user.state() == world.constants().states.NAME )
await processStateName(world, user, buffer);
/** User submitted name and was found to be previously saved, require old password */
else if ( user.state() == world.constants().states.OLD_PASSWORD )
await processStateOldPassword(world, user, buffer);
/** User submitted name and is new, ask for a new password */
else if ( user.state() == world.constants().states.NEW_PASSWORD )
await processStateNewPassword(world, user, buffer);
/** User is new and provided a password, confirm it to be sure they typed it right */
else if ( user.state() == world.constants().states.CONFIRM_PASSWORD )
await processStateConfirmPassword(world, user, buffer);
/** User has successfully logged in and is seeing MOTD, pause until they hit enter */
else if ( user.state() == world.constants().states.MOTD )
await processStateMOTD(world, user, buffer);
/** User is connected and in world */
else if ( user.state() == world.constants().states.CONNECTED )
await processStateConnected(world, user, buffer);
};