-
Notifications
You must be signed in to change notification settings - Fork 2
/
interaction.js
461 lines (389 loc) · 18.5 KB
/
interaction.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
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
module.exports.createCommands = (world) => {
return [
new world.Command({
name: `drop`,
positions: world.constants().positionsMobile,
execute: async (world, user, buffer, args) => {
/** Verify item argument exists */
if ( typeof args[0] != `string` ) {
user.send(`Drop what?\r\n`);
}
/** Drop all */
else if ( args[0] == `all` ) {
const items = user.inventory();
/** Verify there's at least one item to drop */
if ( items.length == 0 ) {
user.send(`There are no items in your inventory to drop.\r\n`);
}
/** Loop through inventory items and transfer each to the room */
else {
for ( let i = items.length - 1; i >= 0; i-- ) {
user.send(`You drop ${items[i].name()}.\r\n`);
await world.itemToRoom(items[i], user.room());
}
}
}
/** Drop single item */
else {
/** Parse item name and count */
const [name, count] = world.parseName(user, args, 0);
/** Compile list of inventory items matching that item name */
const items = user.inventory().filter(x => x.names().some(y => y.toLowerCase().startsWith(name)));
/** Verify there is an item at desired count */
if ( items.length < count ) {
user.send(`You don't have that item in your inventory.\r\n`);
}
/** Transfer item to the room */
else {
user.send(`You drop ${items[count - 1].name()}.\r\n`);
await world.itemToRoom(items[count - 1], user.room());
}
}
}
}),
new world.Command({
name: `get`,
positions: world.constants().positionsMobile,
execute: async (world, user, buffer, args) => {
let containerName, containerCount, containers = [], items = [];
/** Handle container argument, if passed */
if ( typeof args[1] == `string` ) {
/** Parse container name and count */
[containerName, containerCount] = world.parseName(user, args, 1);
/** Compile list of inventory and room items matching that container name */
containers = user.inventory().filter(x => x.names().some(y => y.toLowerCase().startsWith(containerName)));
containers = containers.concat(user.room().items().filter(x => x.names().some(y => y.toLowerCase().startsWith(containerName))));
}
/** Verify item argument exists */
if ( typeof args[0] != `string` ) {
user.send(`Get what?\r\n`);
}
/** If container is specified, verify there is an item at desired container count */
else if ( containers.length > 0 && containers.length < containerCount ) {
user.send(`You can't find that container anywhere.\r\n`);
}
/** If container is specified, verify item is a container */
else if ( containers.length > 0 && !containers[containerCount - 1].flags().includes(world.constants().itemFlags.CONTAINER) ) {
user.send(`That item is not a container.\r\n`);
}
/** Get all */
else if ( args[0] == `all` ) {
/** Get all (container) - use container's contents, get all - use room items */
if ( containers.length > 0 )
items = containers[containerCount - 1].contents();
else
items = user.room().items().filter(x => !x.flags().includes(world.constants().itemFlags.FIXED));
/** Verify there's at least one item in the container */
if ( containers.length > 0 && items.length == 0 ) {
user.send(`There are no items in that container.\r\n`);
}
/** Verify there's at least one item in the room */
else if ( items.length == 0 ) {
user.send(`There are no items that you can pick up.\r\n`);
}
/** Loop through the items list and transfer all to inventory */
else {
for ( let i = items.length - 1; i >= 0; i-- ) {
if ( containers.length > 0 )
user.send(`You get ${items[i].name()} from ${containers[containerCount - 1].name()}.\r\n`);
else
user.send(`You pick up ${items[i].name()}.\r\n`);
await world.itemToInventory(items[i], user);
}
}
}
/** Get single item */
else {
/** Parse item name and count */
const [itemName, itemCount] = world.parseName(user, args, 0);
/** Get (item) (container) - use container's contents, get (item) - use room items */
if ( containers.length > 0 )
items = containers[containerCount - 1].contents().filter(x => x.names().some(y => y.toLowerCase().startsWith(itemName)));
else
items = user.room().items().filter(x => x.names().some(y => y.toLowerCase().startsWith(itemName)));
/** Verify there is an item at desired item count */
if ( items.length < itemCount ) {
user.send(`You can't find that item anywhere.\r\n`);
}
/** Verify the item doesn't have the 'fixed' flag */
else if ( items[itemCount - 1].flags().includes(world.constants().itemFlags.FIXED) ) {
user.send(`You are not able to pick up that item.\r\n`);
}
/** Transfer the item to inventory */
else {
if ( containers.length > 0 )
user.send(`You get ${items[itemCount - 1].name()} from ${containers[containerCount - 1].name()}.\r\n`);
else
user.send(`You pick up ${items[itemCount - 1].name()}.\r\n`);
await world.itemToInventory(items[itemCount - 1], user);
}
}
}
}),
new world.Command({
name: `put`,
positions: world.constants().positionsMobile,
execute: async (world, user, buffer, args) => {
/** Verify item argument exists */
if ( typeof args[0] != `string` ) {
user.send(`Put what in what?\r\n`);
}
/** Verify container argument exists */
else if ( typeof args[1] != `string` ) {
user.send(`Put it in what?\r\n`);
}
/** Try to put item in container */
else {
/** Parse container name and count */
const [containerName, containerCount] = world.parseName(user, args, 1);
/** Compile list of inventory and room items matching that container name */
let containers = user.inventory().filter(x => x.names().some(y => y.toLowerCase().startsWith(containerName)));
containers = containers.concat(user.room().items().filter(x => x.names().some(y => y.toLowerCase().startsWith(containerName))));
/** Verify there is an item at desired container count */
if ( containers.length < containerCount ) {
user.send(`You can't find that container.\r\n`);
}
/** Put all (container) */
else if ( args[0] == `all` ) {
/** Compile list of inventory items (excluding the desired container */
const items = user.inventory().filter(x => x != containers[containerCount - 1]);
/** Verify there's at least one item in inventory */
if ( items.length == 0 ) {
user.send(`There are no items in your inventory to put into that container.\r\n`);
}
/** Loop through inventory items and transfer each to the container */
else {
for ( let i = items.length - 1; i >= 0; i-- ) {
user.send(`You put ${items[i].name()} in ${containers[containerCount - 1].name()}.\r\n`);
await world.itemToContainer(items[i], containers[containerCount - 1]);
}
}
}
/** Put (item) (container) */
else {
/** Parse item name and count */
const [itemName, itemCount] = world.parseName(user, args, 0);
/** Compile list of inventory items matching that item name */
const items = user.inventory().filter(x => x.names().some(y => y.toLowerCase().startsWith(itemName)));
/** Verify there is an item at desired item count */
if ( items.length < itemCount ) {
user.send(`You don't have that item in your inventory.\r\n`);
}
/** Transfer the item to the container */
else {
user.send(`You put ${items[itemCount - 1].name()} in ${containers[containerCount - 1].name()}.\r\n`);
await world.itemToContainer(items[itemCount - 1], containers[containerCount - 1]);
}
}
}
}
}),
new world.Command({
name: `remove`,
positions: world.constants().positionsMobile,
execute: async (world, user, buffer, args) => {
/** Verify item argument exists */
if ( typeof args[0] != `string` ) {
user.send(`Remove what?\r\n`);
}
/** Remove all */
else if ( args[0] == `all` ) {
/** Compile list of equipment items */
const items = user.equipment();
/** Verify there's at least one item in equipment */
if ( user.equipment().length == 0 ) {
user.send(`You are already not wearing or wielding anything.\r\n`);
}
/** Loop through equipment items and transfer each item to inventory */
else {
for ( let i = items.length - 1; i >= 0; i-- ) {
if ( items[i].slot() == world.constants().slots.WIELD )
user.send(`You return ${items[i].name()} to your inventory.\r\n`);
else
user.send(`You remove ${items[i].name()}.\r\n`);
await world.itemToInventory(items[i], user);
}
}
}
/** Remove (item) */
else {
/** Parse item name and count */
const [name, count] = world.parseName(user, args, 0);
/** Compile list of equipment items matching that item name */
const items = user.equipment().filter(x => x.names().some(y => y.toLowerCase().startsWith(name)));
/** Verify there is an item at desired count */
if ( items.length < count ) {
user.send(`You are not wearing or wielding anything like that.\r\n`);
}
/** Transfer item to inventory */
else {
if ( items[count - 1].slot() == world.constants().slots.WIELD )
user.send(`You return ${items[count - 1].name()} to your inventory.\r\n`);
else
user.send(`You remove ${items[count - 1].name()}.\r\n`);
await world.itemToInventory(items[count - 1], user);
}
}
}
}),
new world.Command({
name: `say`,
positions: world.constants().positionsAwake,
execute: async (world, user, buffer, args) => {
/** Verify at least one argument exists */
if ( buffer.trim().length == 0 ) {
user.send(`Say what?\r\n`);
}
/** Color and send the text to all users in the room */
else {
/** Colorize the string if it has any color code sequences */
const text = world.colorize(buffer.toString().trim() + `#n`);
/** Loop through users in room and send text */
user.room().users().forEach((otherUser) => {
if ( user != otherUser )
otherUser.send(`${user.name()} says \`${text}\`.\r\n`);
else
otherUser.send(`You say \`${text}\`.\r\n`);
});
}
}
}),
new world.Command({
name: `wear`,
positions: world.constants().positionsMobile,
execute: async (world, user, buffer, args) => {
/** Verify item argument exists */
if ( typeof args[0] != `string` ) {
user.send(`Wear what?\r\n`);
}
/** Wear all */
else if ( args[0] == `all` ) {
/** Compile list of items in inventory that are equippable and don't need to be wielded */
const items = user.inventory().filter(x => x.slot() != world.constants().slots.NONE && x.slot() != world.constants().slots.WIELD);
/** Verify there is at least one item in inventory to wear */
if ( items.length == 0 ) {
user.send(`You don't anything wearable in your inventory.\r\n`);
}
/** Try to wear all wearable items */
else {
let count = 0;
/** Loop through wearable inventory items */
for ( let i = items.length - 1; i >= 0; i-- ) {
/** Transfer item to equipment if there is a slot open */
if ( items[i].slot() != world.constants().slots.WIELD && !user.equipment().find(x => x.slot() == items[i].slot()) ) {
user.send(`You put on ${items[i].name()}.\r\n`);
await world.itemToEquipment(items[i], user);
count++;
}
}
/** If nothing was worn, items were wearable, but slots occupied */
if ( count == 0 )
user.send(`You have already worn everything you can.\r\n`);
}
}
/** Wear (item) */
else {
/** Parse item name and count */
const [name, count] = world.parseName(user, args, 0);
/** Compile list of items in inventory matching that item name */
const items = user.inventory().filter(x => x.names().some(y => y.toLowerCase().startsWith(name)));
/** Verify there is an item at desired count */
if ( items.length < count ) {
user.send(`You don't have that item in your inventory.\r\n`);
}
/** Verify item is wearable */
else if ( items[count - 1].slot() == world.constants().slots.WIELD && world.constants().itemTypesWieldable.includes(items[count - 1].type()) ) {
user.send(`You need to 'wield' weapons and held items, not 'wear' them.\r\n`);
}
/** Verify item is equippable */
else if ( items[count - 1].slot() != world.constants().slots.ARMOR ) {
user.send(`That item is not equippable.\r\n`);
}
/** Verify the appropriate slot is available */
else if ( user.equipment().find(x => x.slot() == items[count - 1].slot()) ) {
user.send(`You are already wearing something in that slot.\r\n`);
}
/** Transfer item to equipment */
else {
user.send(`You put on ${items[count - 1].name()}.\r\n`);
await world.itemToEquipment(items[count - 1], user);
}
}
}
}),
new world.Command({
name: `wield`,
positions: world.constants().positionsMobile,
execute: async (world, user, buffer, args) => {
/** Verify item argument exists */
if ( typeof args[0] != `string` ) {
user.send(`Wield what?\r\n`);
}
/** Wield all */
else if ( args[0] == `all` ) {
/** Compile list of items in inventory that are equippable and wieldable */
const items = user.inventory().filter(x => x.slot() != world.constants().slots.NONE && x.slot() == world.constants().slots.WIELD);
/** Verify there is at least one item in inventory to wield */
if ( items.length == 0 ) {
user.send(`You don't anything wieldable in your inventory.\r\n`);
}
/** Try to wield all wieldable items */
else {
let count = 0;
/** Loop through wieldable inventory items */
for ( let i = items.length - 1; i >= 0; i-- ) {
const wieldedItems = user.equipment().filter(x => x.slot() == world.constants().slots.WIELD);
/** Transfer item to equipment if there is a slot open */
if ( wieldedItems.length == 0 || ( wieldedItems.length == 1 && wieldedItems[0].type() != world.constants().itemTypes.WEAPON_2H ) ) {
user.send(`You wield ${items[i].name()}.\r\n`);
await world.itemToEquipment(items[i], user);
count++;
}
}
/** If nothing was worn, items were wieldable, but slots occupied */
if ( count == 0 )
user.send(`You have already wielded all that you are capable of holding.\r\n`);
}
}
/** Wield (item) */
else {
/** Parse item name and count */
const [name, count] = world.parseName(user, args, 0);
/** Compile list of items in inventory matching that item name */
const items = user.inventory().filter(x => x.names().some(y => y.toLowerCase().startsWith(name)));
/** Verify there is an item at desired count */
if ( items.length < count ) {
user.send(`You don't have that item in your inventory.\r\n`);
}
/** Verify item is equippable */
else if ( items[count - 1].slot() == world.constants().slots.NONE ) {
user.send(`That item is not equippable.\r\n`);
return;
}
/** Verify item is wieldable */
else if ( items[count - 1].slot() != world.constants().slots.WIELD ) {
user.send(`You need to 'wear' armor, not 'wield' it.\r\n`);
}
/** Verify item is a weapon */
else if ( !world.constants().itemTypesWieldable.includes(items[count - 1].type()) ) {
user.send(`That item is not wieldable.\r\n`);
}
/** Try to wield item */
else {
/** Compile list of wielded items in equipment */
const wieldedItems = user.equipment().filter(x => x.slot() == world.constants().slots.WIELD);
/** Verify there is a slot available for wielding */
if ( wieldedItems.length == 2 || ( wieldedItems.length == 1 && wieldedItems[0].type() == world.constants().itemTypes.WEAPON_2H ) ) {
user.send(`You have already wielded all that you are capable of holding.\r\n`);
}
/** Transfer item to equipment */
else {
user.send(`You wield ${items[count - 1].name()}.\r\n`);
await world.itemToEquipment(items[count - 1], user);
}
}
}
}
})
];
};