-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathoperations.js
More file actions
666 lines (543 loc) · 19.2 KB
/
operations.js
File metadata and controls
666 lines (543 loc) · 19.2 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*
* Operations do minor calculations,
* have safeguards against unexpected arguments,
* and always return an expected result.
*/
// Capitalize first character of a string, and return the resulting string.
function upperFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
// Shorthand for Object.prototype.toString.call()
// Options: [object Array], [object String], [object Object], [object Number],
// [object Undefined], [object Date], [object Math], [object Null]
function toType(obj) {
return Object.prototype.toString.call(obj);
}
// Takes a position object, for example: { 'x': 0, 'y': 0, 'z': 0 }
// and convert it into a string of '0x0y0z', which is how rooms are refered to.
function strPos(pos) {
return pos.x + 'x' + pos.y + 'y' + pos.z + 'z';
}
// Return a real object made from a dotted object notation
// string 'object.property1.property2', optionally with a value for the last property.
// Returns false if invalid string, such as '.property'.
function objDotted(str, val) {
var makeObject = {};
var sortArr = [];
var dotSplit = str.split('.');
// Must not start empty, such as '.property'.
if (dotSplit[0] == '') {
return false;
}
// Otherwise, make an object.
if (dotSplit[1]) {
for (var i=0; i < dotSplit.length; i++) {
if (i == 0) {
sortArr[0] = {};
continue;
}
// If not last property.
if (i != dotSplit.length - 1) {
sortArr[i-1][dotSplit[i]] = {};
sortArr[i] = sortArr[i-1][dotSplit[i]];
} else {
// Last one, then insert value.
sortArr[i-1][dotSplit[i]] = val;
makeObject[dotSplit[0]] = sortArr[0];
return makeObject;
}
}
} else {
// Nothing after the first dot.
makeObject[dotSplit[0]] = val;
return makeObject;
}
}
// Update existing properties, and create non-existing properties,
// if upsert is requested. Recursively changes the original object,
// returning nothing.
function updateObj(originalObj, newObj, upsert) {
// Ascertain that upsert is defined.
if (upsert == undefined) {
var upsert = false;
}
for (var propertyName in newObj) {
var curProperty = newObj[propertyName];
// Update an existing property, or create a new one.
if (originalObj[property]) {
// Recursively copy Object and Array properties.
if (toType(curProperty) != '[object Array]' && toType(curProperty) != '[object Object]') {
updateObj(originalObj[property], curProperty, upsert);
}
// Otherwise, just reference the value.
originalObj[property] = curProperty;
} else if (upsert){
// Create new property, only if upsert is requested.
originalObj[property] = curProperty;
}
}
}
// Copies Object & Array types recursively,
// and directly references the value of any other object type.
// NOT USING isChild().
function copyObj(originalObj) {
var copiedObj;
// Handle each object type case.
if (toType(originalObj) == '[object Object]') {
copiedObj = {};
for (var propertyName in originalObj) {
var curProperty = originalObj[propertyName];
// Recursively copy Object and Array properties.
if (toType(curProperty) == '[object Object]' || toType(curProperty) == '[object Array]') {
copiedObj[propertyName] = copyObj(curProperty);
continue;
}
// Copy date.
if (toType(curProperty) == '[object Date]') {
copiedObj[propertyName] = new Date(curProperty);
continue;
}
// Otherwise, just copy the value.
copiedObj[propertyName] = curProperty;
}
} else if (toType(originalObj) == '[object Array]') {
copiedObj = [];
for (var i = 0; i < originalObj.length; i++) {
var curProperty = originalObj[i];
// Recursively copy Object and Array properties.
if (toType(curProperty) == '[object Object]' || toType(curProperty) == '[object Array]') {
copiedObj[i] = copyObj(curProperty);
continue;
}
// Copy date.
if (toType(curProperty) == '[object Date]') {
copiedObj[i] = new Date(curProperty);
continue;
}
// Otherwise, just copy the value.
copiedObj[i] = curProperty;
}
} else if (toType(originalObj) == '[object Date]') {
// Copy date.
copiedObj = new Date(originalObj);
} else {
// Otherwise, just copy the value.
copiedObj = originalObj;
}
return copiedObj;
}
// Returns true, if one of the parent's children is the target.
// This is useful, for avoiding copyObj() through an infinite loop!
function isChild(target, parent) {
if (toType(parent) == '[object Object]') {
for (var name in parent) {
var curProperty = parent[name];
// Direct child.
if (curProperty = target) return true;
// Check if target is a child of this property, and so on, recursively.
if (toType(curProperty) == '[object Object]' || toType(curProperty) == '[object Array]') {
if (isChild(target, curProperty)) return true;
}
}
} else if (toType(parent) == '[object Array]') {
for (var i=0; i < parent.length; i++) {
var curItem = parent[i];
// Direct child.
if (curItem = target) return true;
// Check if target is a child of this property, and so on, recursively.
if (toType(curItem) == '[object Object]' || toType(curItem) == '[object Array]') {
if (isChild(target, curItem)) return true;
}
}
}
return false; // Not the target.
}
// Locates a property insides an object, receiving a dotted string,
// such as 'property.property.property', and returns an array with
// the reference to its' parent and property name [parentRef, propetyName],
// if it exists, or false, if not found.
function hasProperty(obj, propertyStr) {
var splitField = propertyStr.split('.');
console.log(obj[splitField[0]]);
// Parent is obj, itself.
if (!splitField[1]) {
if (obj[splitField[0]] == undefined) return false;
return [obj, splitField[0]];
}
// Get last field in the string.
// Use an array to convert dotted string into object representation.
var objArray = [];
for (var i=0; i < splitField.length; i++) {
// First item.
if (i == 0) {
// Property not found.
if (obj[splitField[0]] == undefined) return false;
objArray[0] = obj[splitField[0]];
continue;
}
// Last item.
if (i == splitField.length - 1) {
objArray[i] = objArray[i-1][splitField[i]];
// Last property doesn't exist.
if (objArray[i] == undefined) return false;
return [objArray[i-1], splitField[i]];
}
objArray[i] = objArray[i-1][splitField[i]];
// Property not found.
if (objArray[i] == undefined) return false;
}
}
// First character is upper-case, while the rest are lower-case.
// Returns re-cased name.
function caseName(name) {
var casedName = name.toLowerCase(); // Lowercase the name string.
casedName = upperFirst(casedName); // Only first character.
return casedName;
}
// Target names (pre, post, name) and rooms titles must only contain letters,
// optionally with spaces. First letter must be capital. Certains words must
// always be lower-case, except the first word.
// Returns the parsed name on success, or false on failure.
function parseName(name) {
// Remove whitespaces from start and end.
name = name.trim();
// No more than one whitespace, at a time.
name = name.replace(/\s+/g, ' ');
// Only English alphabet and spaces allowed. Can't be only spaces.
if (name.search(/[^A-Za-z ]/) >= 0 || name == '') {
return false;
}
// The following words must always be in lower-case.
var lcWords = ['a', 'an', 'the', 'for', 'and', 'nor', 'but', 'or', 'yet', 'so',
'at', 'by', 'on', 'of', 'to', 'as', 'en', 'per', 'vs', 'via', 'de',
'du', 'von', 'et', 'le', 'la', 'il', 'der', 'af', 'til', 'dit',
'del', 'zu', 'und', 'di', 'av'];
var nameArr = name.split(' ');
for (var i=0; i < nameArr.length; i++) {
// First and last words must be treated as names.
if (i == 0 || i == nameArr.length-1) {
nameArr[i] = upperFirst(nameArr[i]);
continue;
}
nameArr[i] = nameArr[i].toLowerCase();
// Check against lcWords array.
if (lcWords.indexOf(nameArr[i]) >= 0) {
nameArr[i] = nameArr[i].toLowerCase();
continue;
}
// Otherwise, treat as name.
nameArr[i] = upperFirst(nameArr[i]);
}
name = nameArr.join(' ');
return name;
}
// Returns the pre + name + post joined strings of a player or target,
// without empty spaces, if any does not exist or is empty.
function fullName(target) {
// Either a user or a target.
if (target.player) {
return (target.player.pre ? target.player.pre + ' ' : '') + target.player.name +
(target.player.post ? ' ' + target.player.post : '');
} else {
return (target.pre ? target.pre + ' ' : '') + target.name +
(target.post ? ' ' + target.post : '');
}
}
// Returns the fullName() + the strTarget() or + '[USERNAME]',
// if the fullName() is different from the username, otherwise only the fullName() returns.
function fullNameID(target) {
var result = fullName(target);
// Either a user or a target.
if (target.player) {
var username = '';
if (result != target.account.username) {
username = ' [' + format.player(target.account.username) + ']';
} else {
result = format.player(result);
}
result = result + username;
} else {
result = result + ' [' + format.target(strTarget(target)) + ']';
}
return result;
}
// Check that username is under or over the character limit,
// and is only composed of a-z & A-Z letters.
// Returns a message string on error, or false if no problem.
function parseUsername(username) {
// Username must be at least two characters long!
if (username.length < 2) {
return '<i>Username too short! Please, keep it over one character.</i>';
}
// Username size limit is 15 characters!
if (username.length > 15) {
return '<i>Username too long! Please, keep it under fifteen characters.</i>';
}
// Only English alphabet allowed.
if (username.search(/[^A-Za-z]/) >= 0) {
return '<i>Username must be one word, composed of English letters, only!</i>';
}
return false; // No problem.
}
// Converts a direction string to a position object.
// For example: 'nw' returns { 'x': curPos.x - 1, 'y': curPos.y + 1, 'z': curPos.z }.
// An unavailable option returns false.
function posDir(curPos, dir) {
// Options: n, s, e, w | nw ,ne , sw, se | u, d
switch(dir) {
case 'n':
newPos = { 'x': curPos.x, 'y': curPos.y + 1, 'z': curPos.z };
break;
case 's':
newPos = { 'x': curPos.x, 'y': curPos.y - 1, 'z': curPos.z };
break;
case 'e':
newPos = { 'x': curPos.x + 1, 'y': curPos.y, 'z': curPos.z };
break;
case 'w':
newPos = { 'x': curPos.x - 1, 'y': curPos.y, 'z': curPos.z };
break;
case 'nw':
newPos = { 'x': curPos.x - 1, 'y': curPos.y + 1, 'z': curPos.z };
break;
case 'ne':
newPos = { 'x': curPos.x + 1, 'y': curPos.y + 1, 'z': curPos.z };
break;
case 'sw':
newPos = { 'x': curPos.x - 1, 'y': curPos.y - 1, 'z': curPos.z };
break;
case 'se':
newPos = { 'x': curPos.x + 1, 'y': curPos.y - 1, 'z': curPos.z };
break;
case 'u':
newPos = { 'x': curPos.x, 'y': curPos.y, 'z': curPos.z + 1 };
break;
case 'd':
newPos = { 'x': curPos.x, 'y': curPos.y, 'z': curPos.z - 1 };
break;
default:
return false;
}
return newPos;
}
// Return the 'ID.INSTANCE' representation of a target.
function strTarget(target) {
return target['_id'] + '.' + target.instance;
}
// Parses a string that represents a target,
// so that it returns as 'ID.INSTANCE'.
// Returns false if string is Not a Number.
function parseTarget(target) {
if (isNaN(target)) {
return false;
}
var idInst = target.split('.');
// Default empty string (id or instance) to '0'.
if (idInst[0] == '') {
idInst[0] = '0';
}
if (!idInst[1] || idInst[1] == '') {
idInst[1] = '0';
}
var properTarget = idInst.join('.'); // Turn back into proper string.
return properTarget;
}
// Verifies that a target has the 'worn' property filled,
// and returns true if so, or false if not so.
function verifyWearable(target) {
if (!target.worn || JSON.stringify(target.worn) == '{}') {
return false;
}
// Otherwise.
return true;
}
// Assert if an object (Object, Array, String)
// does't exist or is empty, and recusrively checks its' children.
// Returns true if empty, or false otherwise.
function isEmpty(obj) {
if (obj === null || obj === undefined || obj === '' ||
JSON.stringify(obj) == '{}' || JSON.stringify(obj) == '[]') {
// Check children.
if (toType(obj) == '[object Object]' || toType(obj) == '[object Array]') {
for (var child in obj) {
if (isEmpty(obj[child])) return false;
}
}
return true;
}
// Otherwise.
return false;
}
// Update the world.users reference, and the basic user properties:
// account, player, ['_id'], name.
function updateUser(user, newUser) {
// Replace name for a registered user.
if (newUser.account == undefined) {
user.player.name = newUser.player.name;
user.name = fullName(user); // pre + name + post.
return;
}
delete world.users[user.account.username]; // Clean up from world.users.
// Do login, or replace name for an unregistered user.
if (newUser['_id']) {
// User variable reference (pointer) is saved, by only replacing properties.
user.account = newUser.account;
user.player = newUser.player;
user['_id'] = newUser['_id'];
} else {
user.account = newUser.account;
user.player = newUser.player;
}
// Update extra properties.
user.name = fullName(user); // pre + name + post.
// Update world.users reference.
world.users[user.account.username] = user;
}
// Returns a reference to an existing command, or false,
// with optional access level, to restrict the search.
function commandExists(cmd, access) {
// Search according to access level.
switch (access) {
case 'god':
for (var category in commands) {
var curCategory = commands[category];
if (curCategory[cmd]) {
return curCategory[cmd];
}
}
break;
case 'builder':
for (var category in commands) {
// Skip god commands.
if (category == 'god') {
continue;
}
var curCategory = commands[category];
if (curCategory[cmd]) {
return curCategory[cmd];
}
}
break;
case 'master':
for (category in commands) {
// Skip god and builder commands.
if (category == 'god' || category == 'builder') {
continue;
}
var curCategory = commands[category];
if (curCategory[cmd]) {
return curCategory[cmd];
}
}
break;
case 'player':
for (var category in commands) {
// Skip god, builder, and master commands.
if (category == 'god' || category == 'builder' || category == 'master') {
continue;
}
var curCategory = commands[category];
if (curCategory[cmd]) {
return curCategory[cmd];
}
}
break;
case 'user':
var curCategory = commands['user'];
if (curCategory[cmd]) {
return curCategory[cmd];
}
break;
default:
// Same as 'user', when no access argument.
var curCategory = commands['user'];
if (curCategory[cmd]) {
return curCategory[cmd];
}
}
// Otherwise, command not found.
return false;
}
// A random name generator, based on Prefix + middle + suffix.
// Checks if username is already in world.users[], and recursively retries.
// Optionally, check if argument name is one of the options,
// and return true if so, or false if not so (this is for the rename command.)
function randomName(name) {
// Each word must be 3 characters long, exactly.
var prefixes = ['Ale', 'Bin', 'Kor', 'Lam', 'Dar', 'Sof', 'Arn', 'Men', 'Che', 'Tai'];
// Each word must be 3 characters long, exactly.
var middles = ['gen', 'shi', 'por', 'pon', 'lam', 'att', 'raa', 'arr', 'gar', 'fen'];
// Each word must be 3 characters long, exactly.
var suffixes = ['eus', 'nos', 'gos', 'kor', 'mis', 'mal', 'dar', 'she', 'ous', 'fes'];
// In case of a name check, check against these options.
if (name) {
// No match, if not same length.
if (name.length != 9) {
return false;
}
var prefix = name.substr(0, 3);
var middle = name.substr(3, 3);
var suffix = name.substr(6, 3);
if (prefixes.indexOf(prefix) >= 0 && middles.indexOf(middle) >= 0 &&
suffixes.indexOf(suffix) >= 0) {
return true; // Found a match!
}
return false; // Did not find a match.
}
// Get a random number between min and max, including them.
var min = 0;
var max = 9;
var prefix = prefixes[Math.floor(Math.random() * (max - min + 1)) + min];
var middle = middles[Math.floor(Math.random() * (max - min + 1)) + min];
var suffix = suffixes[Math.floor(Math.random() * (max - min + 1)) + min];
var name = prefix + middle + suffix;
// Must not be already in-use.
if (world.users[name]) {
name = randomName(); // Recursively, try again!
}
return name;
}
// Returns the string with <>&" escaped for HTML.
function escapeHTML(str) {
return String(str).replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
// Returns a pretty string of which body parts wear which items.
// Returns false if no 'worn' property.
function strWearing(user) {
var str = '';
if (!user.player.worn) {
return false;
}
for (var location in user.player.worn) {
var item = user.player.worn[location];
if (!isEmpty(item)) str += upperFirst(location) + ': ' + JSON.stringify(item) + format.newline;
}
return str;
}
//*** EXPORTS ***//
exports.upperFirst = upperFirst;
exports.toType = toType;
exports.strPos = strPos;
exports.objDotted = objDotted;
exports.updateObj = updateObj;
exports.copyObj = copyObj;
exports.hasProperty = hasProperty;
exports.caseName = caseName;
exports.parseName = parseName;
exports.fullName = fullName;
exports.fullNameID = fullNameID;
exports.parseUsername = parseUsername;
exports.posDir = posDir;
exports.strTarget = strTarget;
exports.parseTarget = parseTarget;
exports.verifyWearable = verifyWearable;
exports.isEmpty = isEmpty;
exports.updateUser = updateUser;
exports.commandExists = commandExists;
exports.randomName = randomName;
exports.escapeHTML = escapeHTML;
exports.strWearing = strWearing;
// *** //