-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbackupcode.js
More file actions
308 lines (255 loc) · 9.07 KB
/
backupcode.js
File metadata and controls
308 lines (255 loc) · 9.07 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
// offer TARGET ITEM (ITEM)
'offer': function (user, cmdArray) {
if (!user.player.worn) {
user.socket.emit('message', '<i>Only registered players can use this command.</i>');
return;
}
if (!cmdArray[1] || !cmdArray[2]) {
user.socket.emit('message', '<i>Syntax: ' + cmdChar + 'give TARGET ITEM (ITEM)</i>');
return;
}
var target = caseName(cmdArray[1]);
var item = parseTarget(cmdArray[2]);
var price = '';
if (cmdArray[3]) {
price = parseTarget(cmdArray[3]);
}
// Make sure both item and price parsed correctly.
if (!item || (cmdArray[3] && !price)) {
user.socket.emit('message', '<i>Items must be formatted as ID.INSTANCE, such as \'0.0\'.</i>');
return;
}
// Try parsing if the target is not a user.
var parsedTarget = parseTarget(target);
// If target is a username (parsed as NaN, which returns false.)
if (!parsedTarget) {
offerPlayer(user, target, item, price);
} else {
offerTarget(user, target, item, price);
}
},
/* Offer to give a player or a target an item, and optionally expect an item in return.
*/
// request ITEM (ITEM)
'request': function (user, cmdArray) {
if (!user.player.worn) {
user.socket.emit('message', '<i>Only registered players can use this command.</i>');
return;
}
if (!cmdArray[1]) {
user.socket.emit('message', '<i>Syntax: ' + cmdChar + 'request ITEM (ITEM)</i>');
return;
}
var item = parseTarget(cmdArray[1]);
var price = ''; // Nothing in return.
if (cmdArray[2]) {
price = parseTarget(cmdArray[2]);
}
// Make sure both item and price parsed correctly.
if (!item || (cmdArray[2] && !price)) {
user.socket.emit('message', '<i>Items must be formatted as ID.INSTANCE, such as \'0.0\'.</i>');
return;
}
// Add to user object.
user.player.trade.requests.push(item, price);
},
/* Request an item, and optionally offer an item in return.
*/
// accept TARGET ITEM
'accept': function (user, cmdArray) {
if (!user.player.worn) {
user.socket.emit('message', '<i>Only registered players can use this command.</i>');
return;
}
if (!cmdArray[1] || !cmdArray[2]) {
user.socket.emit('message', '<i>Syntax: ' + cmdChar + 'accept TARGET ITEM</i>');
return;
}
var username = caseName(cmdArray[1]);
var item = cmdArray[2];
// Try parsing if the target is not a user.
var parsedTarget = parseTarget(target);
// If target is a username (parsed as NaN, which returns false.)
if (!parsedTarget) {
exchangeItems(user, username, item); // Verifies and makes the exchange.
} else {
exchangeItems(user, parsedTarget, item); // Verifies and makes the exchange.
}
},
/* Accept an offer or request of an item, according to the price.
*/
// trade (ITEM)(,AMOUNT) (ITEM)(,AMOUNT) (TARGET)
'trade': function (user, cmdArray) {
if (!user.player.worn) {
user.socket.emit('message', '<i>Only registered players can use this command.</i>');
return;
}
if (!cmdArray[1]) {
user.socket.emit('message', '<i>Syntax: ' + cmdChar + 'trade ITEM(,AMOUNT) (ITEM)(,AMOUNT) (TARGET)</i>');
return;
}
}
/* Create or remove a trade request, available for any target/player to accept,
* or accept a trade with a wanting target/player - or request a trade
* with that target/player.
* AMOUNT defaults to 1. ITEM is either case-insensitive fullName() or ID.INSTANCE.
* ITEM defaults to '-', which means nothing.
*/
// Verifies the target, item and price, and makes the exchange,
// either between players or targets.
// The user accept the price that the target sets,
// in order to get the item.
// Notice that target, item & price are just strings.
function exchangeItems(user, target, item, price) {
// Make sure both user and target have at least one hand available.
var userHand = false;
if (user.player.worn.hands) {
for (var hand in user.player.worn.hands) {
var curHand = user.player.worn.hands[hand];
if (JSON.stringify(curHand) == '{}') {
userHand = hand;
}
}
if (!userHand) {
user.socket.emit('message', '<i>You need a free hand to hold with!</i>');
return;
}
} else {
user.socket.emit('message', '<i>You have no hands to hold with!</i>');
return;
}
// Match either a target object or a player object.
var targetHand = false;
var targetLocation;
if (target.player) {
targetLocation = target.player.worn.hands;
} else {
targetLocation = target.worn.hands;
}
// Make sure the target has a hand to hold with.
if (targetLocation) {
for (var hand in targetLocation) {
var curHand = targetLocation[hand];
if (JSON.stringify(curHand) == '{}') {
targetHand = hand;
}
}
} else {
user.socket.emit('message', '<i>' + target + ' has no hands to hold with!</i>');
return;
}
// Check that the target is in the room.
if (target.player) {
for (var i=0; i< world.watch[strPos(user.player.position)].length; i++) {
var curPlayer = world.watch[strPos(user.player.position)][i];
if (curPlayer.account.username == target) {
// Make sure that I have the price.
if (price) {
var priceLocation = locateItem(user, price); // Returns location name, or false.
if (!priceLocation) {
user.socket.emit('message', '<i>You do not have item [' + price + '].</i>');
return;
}
}
// Make sure that the target has the item.
var itemLocation = locateItem(curPlayer, item); // Returns location name, or false.
if (!itemLocation) {
user.socket.emit('message', '<i>' + fullName(curPlayer) + ' does not have item [' + item + '].</i>');
return;
}
// Attempt to remove item and price from players.
var holder = {};
holder.item = removeItem(curPlayer, item, itemLocation);
if (!holder.item) {
user.socket.emit('message', '<i>' + fullName(curPlayer) + ' does not have item [' + item + '].</i>');
return;
}
holder.price = removeItem(user, price, priceLocation);
if (!holder.price) {
addItem(curPlayer, holder.item, itemLocation); // Undo removal from target.
user.socket.emit('message', '<i>You do not have item [' + price + '].</i>');
return;
}
// Add items to players and finish.
addItem(user, holder.item, userHand);
addItem(curPlayer, holder.price, targetHand);
// Notify players of success.
if (price) {
user.socket.emit('message', '<i>You have received ' + FullNameID(holder.item) + ' from ' + fullNameID(curPlayer) +
' in exchange for your ' + FullNameID(holder.price) + '.</i>');
curPlayer.socket.emit('message', '<i>' + fullNameID(user) + ' has given you ' + FullNameID(holder.price) +
' in exchange for ' + FullNameID(holder.item) + '.</i>')
} else {
user.socket.emit('message', '<i>You have received ' + FullNameID(holder.item) + ' from ' + fullNameID(curPlayer) + '.</i>');
curPlayer.socket.emit('message', '<i>You have given ' + FullNameID(holder.item) + ' to ' + fullNameID(user) + '.</i>')
}
return;
}
}
// Target player not in the room.
user.socket.emit('message', '<i>Could not find [' + target + '] here!</i>');
} else {
}
}
// Attempt to remove an item from a player by ID.INSTANCE,
// and optionally by location, returning the item reference, or false.
function removeItem(user, item, location) {
var itemObject;
// From location.
if (location) {
// Generally body.
if (user.player.worn[location]) {
itemObject = user.player.worn[location];
user.player.worn[location] = {};
return itemObject;
}
// Or specifically hands.
if (user.player.worn.hands[location]) {
itemObject = user.player.worn.hands[location];
user.player.worn.hands[location] = {};
return itemObject;
}
}
// Otherwise, find it.
for (var location in user.player.worn) {
var curItem = user.player.worn[location];
// Hands special case.
if (location == 'hands') {
for (var hand in curItem) {
curItem = curItem[hand];
if (strTarget(curItem) == item) {
itemObject = curItem;
user.player.worn.hands[location] = {};
break;
}
}
if (playerItem) {
break;
}
}
if (strTarget(curItem) == item) {
itemObject = curItem;
user.player.worn[location] = {};
break;
}
}
if (!itemObject) {
return false;
} else {
return itemObject;
}
}
// Adds an items to a given location on player,
// either on entire body or in a hand.
function addItem(user, item, location) {
// Generally body.
if (user.player.worn[location]) {
user.player.worn[location] = item;
return;
}
// Or specifically hands.
if (user.player.worn.hands[location]) {
user.player.worn.hands[location] = item;
return;
}
}