-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuctionScript.js
More file actions
546 lines (526 loc) · 18.6 KB
/
AuctionScript.js
File metadata and controls
546 lines (526 loc) · 18.6 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
/* Original Script by Shadowfist
Instructions
- Paste into client scripts window
- Type ~init to start the system
- Type ~start to get signups happening
- Type ~add [name] for each player you want to add
- Type addmoney/takemoney to calibrate
- Type ~loaditems [file] to load the auction items (the file should be seperated on to different lines)
- Type ~start again to start the first round!
*/
/*jshint "laxbreak":true,"shadow":true,"undef":true,"evil":true,"trailing":true,"proto":true,"withstmt":true*/
/*global sys,client, print */
/* Globals */
var messagessent = 0; // maximum it can send per minute.
var nooveractive = false; //set to true if you cannot go overactive on the server
var userPMs, auctionbot, auctionchan, poScript;
var help = ["*** COMMANDS LIST ***",
"~help: shows the command list",
"~test: Send a test message",
"~data: Gets data",
"~addmoney [name]:[money]: add money to a player",
"~takemoney [name]:[money]: take money from a player",
"~add [name]: add a player to an auction",
"~remove [name]: remove a player from an auction",
"~alias [player]:[alias] adds an alias to a player",
"~start: start a round of auction",
"~end: end a round of auction",
"~loaditems: load items",
"~err: send an error message",
"~init: reset vars"];
/* Safe scripts need to be disabled to use some cool functions like loading items from files */
// Case insensitive comparison.
function cmp(x1, x2) {
if (typeof x1 !== typeof x2) {
return false;
}
else if (typeof x1 === "string") {
if (x1.toLowerCase() === x2.toLowerCase()) {
return true;
}
}
return x1 === x2;
}
function nonFlashing(name) {
return name[0] + '' + name.substr(1);
}
// Returns a name in correct case.
String.prototype.toCorrectCase = function () {
if (isNaN(this) && client.id(this) !== -1) {
return client.name(client.id(this));
}
else {
return this;
}
};
// Rearranges the elements of an array in a random order. Doesn't change the original array.
Array.prototype.shuffle = function () {
var oldarray = this;
var newarray = [];
while (oldarray.length > 0) {
var element = oldarray.splice(sys.rand(0, oldarray.length), 1);
newarray.push(element);
}
return newarray;
};
function init() {
auctionbot = new Auction();
print("Auction Initiated");
if (typeof userPMs !== "object") {
userPMs = [];
}
}
function printMessage(msg) {
var channel = client.channelId("Auction House");
if (channel !== undefined)
client.printChannelMessage(msg, channel, false);
else {
print(msg);
}
}
function sendAll(msg, chan) {
if (messagessent < 50) {
client.network().sendChanMessage(chan, msg);
if (nooveractive) {
messagessent += 1;
}
}
else {
print(msg);
}
}
function getRealBid(init) {
return Math.floor(init / auctionbot.interval) * auctionbot.interval;
}
function Auction() {
auctionchan = client.channelId("Auction House");
this.resetVars = function () {
auctionbot.players = {};
auctionbot.ticks = -1;
auctionbot.items = [];
auctionbot.bids = {};
auctionbot.saleitem = "";
auctionbot.style = "turn";
auctionbot.state = "off";
auctionbot.startmoney = 175000;
auctionbot.minbid = 3000;
auctionbot.interval = 100;
auctionbot.turns = [];
auctionbot.round = 0;
auctionbot.issuedMsgs = [];
printMessage("Reset auction vars!");
};
this.startAuction = function () {
if (auctionbot.hasOwnProperty("state")) {
if (auctionbot.state !== "off") {
printMessage("An auction is already in progress!");
return;
}
}
auctionbot.resetVars();
auctionbot.state = "standby";
printMessage("An auction is now ready to start!");
};
this.addMoney = function (name, money) {
if (auctionbot.state === "off") {
printMessage("No auction is in progress!");
return;
}
var lname = name.toLowerCase();
if (!auctionbot.players.hasOwnProperty(lname)) {
printMessage("No such player exists!");
return;
}
if (isNaN(money)) {
printMessage("Invalid money value!");
return;
}
auctionbot.players[lname].money += money;
sendAll(name.toCorrectCase() + " now has " + auctionbot.players[lname].money + " gold coins.", auctionchan);
};
this.startRound = function () {
if (auctionbot.state !== "standby") {
printMessage("A round is already in progress!");
return;
}
if (auctionbot.items.length === 0) {
sendAll("No more items, auction will end!", auctionchan);
auctionbot.endAuction();
return;
}
if (auctionbot.style === "default") {
var sales = auctionbot.items;
var item = sales.splice(sys.rand(0, sales.length), 1);
auctionbot.saleitem = item;
sendAll("Now selling " + item + "!", auctionchan);
auctionbot.ticks = 30;
auctionbot.state = "round";
auctionbot.round += 1;
}
else {
if (auctionbot.round === 0) {
for (var x in auctionbot.players) {
if (auctionbot.players.hasOwnProperty(x)) {
auctionbot.turns.push(x);
}
}
auctionbot.turns = auctionbot.turns.shuffle();
}
auctionbot.state = "nominate";
var toSend = auctionbot.items.map(nonFlashing).sort();
var turn = auctionbot.turns[auctionbot.round % auctionbot.turns.length].toString().toLowerCase();
var name = turn.toCorrectCase() + (auctionbot.players[turn].aliases ? "/" + auctionbot.players[turn].aliases : "");
sendAll("It is " + name + "'s turn to nominate an item from the following: " + toSend, auctionchan);
}
};
this.nominateRound = function (name, nomination) {
if (auctionbot.state !== "nominate") {
printMessage("Not in nomination stage!");
return;
}
for (var x in auctionbot.players) {
if (auctionbot.players.hasOwnProperty(x)) {
if (auctionbot.players[x].aliases === name) {
name = x;
}
}
}
if (!cmp(auctionbot.turns[auctionbot.round % auctionbot.turns.length].toString(), name)) {
printMessage("Nomination not accepted!");
return;
}
if (auctionbot.style !== "default") {
var sales = auctionbot.items;
var item = false;
for (var x = 0; x < sales.length; x++) {
if (cmp(sales[x], nomination)) {
item = sales.splice(x, 1);
break;
}
}
if (item === false) {
sendAll("Nomination not found!");
return;
}
auctionbot.saleitem = item;
sendAll("Now selling " + item + "!", auctionchan);
auctionbot.ticks = 30;
auctionbot.state = "round";
auctionbot.round += 1;
if (auctionbot.players[name.toLowerCase()].money >= 3000) {
auctionbot.makebid(name, 3000);
}
}
};
this.endRound = function () {
var bidders = auctionbot.bids;
var winner = [false, 0];
for (var x in bidders) {
if (bidders.hasOwnProperty(x)) {
var bidinfo = [x, bidders[x]];
if (bidinfo[1] >= auctionbot.minbid && bidinfo[1] > winner[1]) {
winner = bidinfo;
}
}
}
if (winner[0] === false) {
sendAll("No bids were made this round! Too bad!", auctionchan);
}
else {
sendAll("I sold the " + auctionbot.saleitem + " to " + winner[0].toCorrectCase() + " for " + winner[1] + " gold coins!", auctionchan);
auctionbot.players[winner[0]].money -= winner[1];
auctionbot.players[winner[0]].possessions.push(auctionbot.saleitem);
sendAll(winner[0].toCorrectCase() + " (" + auctionbot.players[winner[0]].money + ") now has: " + auctionbot.players[winner[0]].possessions.join(", "), auctionchan);
}
auctionbot.saleitem = "";
auctionbot.state = "standby";
auctionbot.bids = {};
auctionbot.issuedMsgs = [];
if (auctionbot.style !== "default") {
this.startRound();
}
};
this.endAuction = function () {
if (auctionbot.state === "off") {
printMessage("No auction is in progress!");
return;
}
auctionbot.state = "off";
sendAll("The auction ended!", auctionchan);
};
this.getItemList = function (file) {
if (auctionbot.state !== "standby") {
printMessage("An auction is already in progress!");
return;
}
if (sys.isSafeScripts()) {
printMessage("Safe scripts is on, unable to load from files!");
return;
}
var content = sys.getFileContent(file);
if (content === undefined) {
printMessage("Couldn't find anything here!");
return;
}
var items = content.split("\n");
var itemsadded = 0;
for (var x = 0; x < items.length; x++) {
if (items[x].length >= 1) {
auctionbot.items.push(items[x].trim());
itemsadded += 1;
}
}
printMessage("Added " + itemsadded + " items to the auction block!");
};
this.getData = function () {
var aplayers = auctionbot.players;
for (var x in aplayers) {
if (aplayers.hasOwnProperty(x)) {
printMessage(x.toCorrectCase() + " : " + "(" + aplayers[x].money + ") - " + aplayers[x].possessions.join(", "));
}
}
};
this.addAlias = function (name, alias) {
var aplayers = auctionbot.players;
if (auctionbot.state !== "standby") {
printMessage("An auction is already in progress!");
return;
}
if (name === undefined || alias === undefined) {
printMessage("Names are incorrect");
return;
}
if (aplayers.hasOwnProperty(alias.toLowerCase())) {
printMessage("They are already in the auction!");
return;
}
if (aplayers[name].aliases === alias) {
printMessage("This player is already an alias");
return;
}
aplayers[name].aliases = alias.toLowerCase();
sendAll(alias.toCorrectCase() + " added as an alias to " + name.toCorrectCase(), auctionchan);
};
this.addPlayer = function (name) {
var aplayers = auctionbot.players;
if (auctionbot.state !== "standby") {
printMessage("An auction is already in progress!");
return;
}
if (aplayers.hasOwnProperty(name.toLowerCase())) {
printMessage("They are already in the auction!");
return;
}
aplayers[name.toLowerCase()] = {
money: auctionbot.startmoney,
possessions: [],
aliases: ""
};
sendAll(name.toCorrectCase() + " is now in the auction!", auctionchan);
};
this.removePlayer = function (name) {
var aplayers = auctionbot.players;
if (auctionbot.state !== "standby") {
printMessage("An auction is already in progress!");
return;
}
if (!aplayers.hasOwnProperty(name.toLowerCase())) {
printMessage("They are not in the auction!");
return;
}
delete aplayers[name.toLowerCase()];
sendAll(name + " is no longer in the auction!", auctionchan);
};
this.makebid = function (name, bid) {
if (auctionbot.state !== "round") {
return;
}
for (var x in auctionbot.players) {
if (auctionbot.players.hasOwnProperty(x)) {
if (auctionbot.players[x].aliases === name) {
name = x;
}
}
}
var aplayers = auctionbot.players;
var bids = auctionbot.bids;
var lname = name.toLowerCase();
if (!aplayers.hasOwnProperty(lname)) {
return;
}
if (bid < auctionbot.minbid) {
return;
}
if (aplayers[name.toLowerCase()].money < bid) {
if (auctionbot.issuedMsgs.indexOf(lname) === -1) {
sendAll(name + " doesn't have enough gold coins!", auctionchan);
auctionbot.issuedMsgs.push(lname);
}
return;
}
for (var x in bids) {
if (bids.hasOwnProperty(x)) {
if (bids[x] >= bid) {
return;
}
}
}
bids[lname] = bid;
sendAll(name.toCorrectCase() + " made a bid of " + bid + " gold coins!", auctionchan);
auctionbot.ticks = 30;
};
}
function handleCommand(command, data, channel) {
if (command === "help" || command === "commands") {
for (var h = 0; h < help.length; h++) {
client.printChannelMessage(help[h], channel, false);
}
return;
}
if (command === "data") {
auctionbot.getData();
return;
}
if (command === "add") {
auctionbot.addPlayer(data);
return;
}
if (command === "remove") {
auctionbot.removePlayer(data);
return;
}
if (command === "start") {
if (auctionbot.state === "off") {
auctionbot.startAuction();
}
else {
auctionbot.startRound();
}
return;
}
if (command === "addmoney") {
var tmp = data.split(":", 2);
auctionbot.addMoney(tmp[0], parseInt(tmp[1], 10));
return;
}
if (command === "takemoney") {
var tmp = data.split(":", 2);
auctionbot.addMoney(tmp[0], 0 - parseInt(tmp[1], 10));
return;
}
if (command === "end") {
auctionbot.endAuction();
return;
}
if (command === "loaditems") {
auctionbot.getItemList(data);
return;
}
if (command === "test") {
client.network().sendChanMessage(channel, "This is a script test");
return;
}
if (command === "alias") {
var tmp = data.split(":", 2);
auctionbot.addAlias(tmp[0], tmp[1]);
return;
}
if (command === "eval") {
try {
var result = eval(data);
client.printChannelMessage("±ModBot: Result is: " + result, channel, false);
}
catch (err) {
client.printChannelMessage("±ModBot: Error in eval: " + err, channel, false);
}
return;
}
if (command === "init") {
init();
auctionbot.resetVars();
client.printChannelMessage("±ModBot: Reset system vars", channel, false);
return;
}
throw ("no valid command");
}
poScript = ({
clientStartUp: function () {
init();
},
stepEvent: function () {
if (sys.time() % 60 === 0) {
messagessent = 0;
}
if (typeof auctionbot === "object") {
if (auctionbot.ticks > 0)
auctionbot.ticks -= 1;
if (auctionbot.state === "round" && auctionbot.ticks === 0) {
auctionbot.endRound();
}
}
},
beforePMReceived: function (src, message) {
if (userPMs.indexOf(src) === -1 && messagessent <= 50) {
client.network().sendPM(src, "This user is unable to respond to regular PMs!");
userPMs.push(src);
}
if (client.auth(src) < 1) {
sys.stopEvent();
}
},
beforeSendMessage: function (msg, channel) {
var isCommand = msg.charAt(0) === "~" && msg.length > 1;
if (isCommand) {
msg = msg.toLowerCase();
var pos = msg.indexOf(" ");
var command, commandData;
if (pos === -1) {
command = msg.substring(1);
commandData = "";
}
else {
command = msg.substring(1, pos);
commandData = msg.substring(pos + 1);
}
try {
handleCommand(command, commandData, channel);
}
catch (err) {
if (err === "no valid command") {
client.printChannelMessage("±Fat Policeman: The command " + command + " doesn't exist! Use ~help to get a list that you can use!", channel, false);
}
else {
client.printChannelMessage("±Fat Policeman: Error on command '" + command + "': " + err, channel, false);
}
}
sys.stopEvent();
}
},
afterChannelMessage: function (message, channel, html) {
message = message.toLowerCase();
var user, msg;
if (!html) {
var pos = message.indexOf(': ');
if (pos !== -1) {
user = message.substr(0, pos);
msg = message.substring(pos + 2);
var userid = client.id(user);
if (!client.playerExist(userid)) {
return;
}
var channelname = client.channelName(channel);
if (channelname !== "Auction House") {
return;
}
if (msg.indexOf("bid ") === 0) {
client.printChannelMessage(user + " --- " + parseInt(msg.substring(4), 10), channel, false);
auctionbot.makebid(user, getRealBid(parseInt(msg.substring(4), 10)));
}
if (msg.indexOf("nominate ") === 0) {
client.printChannelMessage(user + " --- " + msg.substring(9), channel, false);
auctionbot.nominateRound(user, msg.substring(9));
}
}
}
}
});