-
Notifications
You must be signed in to change notification settings - Fork 3
/
elf.jsm
205 lines (198 loc) · 6.46 KB
/
elf.jsm
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
// -*- Mode: JavaScript; tab-width: 4 -*- vim:tabstop=4 syntax=javascript:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*global Stream: false, aucgbot: false, module: false, println: false, randint: false */
module.version = "2.3 (2013-11-09)";
module.prefix = "$";
module.chan = "##elf";
module.DB_FILENAME = "elf.csv";
module.initDB = function initDB() {
this.db = new Table();
this.db.setTitle(1, "nick");
this.db.setTitle(2, "score");
this.db.setTitle(3, "coins");
this.db.setTitle(4, "material");
this.db.setTitle(5, "rep");
this.db.setTitle(6, "total");
this.index = this.db.index("nick");
println("[ELF] Initialised scores database.");
};
module.initDB();
module.loadDB = function loadDB() {
try {
this.db = new Table(this.DB_FILENAME);
this.index = this.db.index("nick");
} catch (ex) {}
};
module.loadDB();
module.saveDB = function saveDB() {
this.db.save(this.DB_FILENAME, ",");
};
module.getUser = function getUser(nick) {
var rowNo = this.index.find(nick);
if (rowNo != -1)
return this.db.getRow(rowNo);
};
module.updateUser = function updateUser(nick, data) {
var rowNo = this.index.find(nick);
if (rowNo == -1) {
this.db.add(data);
this.index.add(nick);
} else {
this.db.setRow(rowNo, data);
}
};
module.parseln = function parseln(ln, conn) {
if (!/^:([^\s!@]+)![^\s!@]+@[^\s!@]+ JOIN :?(\S+)/.test(ln) || RegExp.$2 != this.chan || RegExp.$1 == conn.nick)
return false;
var nick = RegExp.$1, data = this.getUser(nick);
if (data) {
data = data.toObject();
conn.notice(nick, "[" + this.chan + "] Welcome back", nick + ". You have",
data.score, "points,", data.coins, "coins and", data.materials, "materials.");
} else {
this.updateUser(nick, {nick: nick, coins: 0, rep: 0, total: 0, score: 1, material: 5});
conn.notice(nick, "[" + this.chan + "] Hey! It looks like you're new!",
"You have been given 5 free materials to get you started.",
"Type", this.prefix + "rules for more info on the game.",
"Start by making some toys with", this.prefix + "make.");
}
return true;
};
module.onMsg = function onMsg(e) {
var dest = e.dest, msg = e.msg, nick = e.nick, conn = e.conn;
if (this.prefix && msg.slice(0, this.prefix.length) != this.prefix)
return false;
msg = msg.slice(this.prefix.length).toLowerCase().split(" ");
switch (msg[0]) {
case "info":
nick = msg[1] || nick;
var data = this.getUser(nick);
if (data) {
data = data.toObject();
conn.reply(dest, nick, data.score, "points,", data.material, "material,",
data.coins, "coins,", data.rep, "reputation, made", data.total, "toys.");
} else {
conn.msg(dest, nick, "has not joined the elf game yet. Try inviting him/her perhaps.");
}
return true;
case "buy":
var data = this.getUser(nick);
if (!data) {
conn.notice(nick, "You haven't joined the elf game yet. Join", this.chan, "to get started.");
return true;
}
data = user.toObject();
switch (msg[1]) {
case "material":
var n = parseInt(msg[2]) || 1, cost = n * 2;
if (data.coins >= cost) {
data.coins -= cost;
data.material = +data.material + n;
conn.msg(this.chan, nick, "has bought", n, "material. This has cost", cost, "in total.");
}
break;
case "voice":
if (data.coins >= 800) {
data.coins -= 800;
aucgbot.log(conn, "ELF VOICE", nick);
//conn.send("CS VOP", this.chan, "ADD", nick);
conn.send("CS ACCESS", this.chan, "ADD", nick, "VOP");
conn.send("MODE", this.chan, "+v", nick);
}
break;
case "hop": case "halfop":
if (data.coins >= 7500) {
data.coins -= 7500;
aucgbot.log(conn, "ELF HOP", nick);
//conn.send("CS HOP", this.chan, "ADD", nick);
conn.send("CS ACCESS", this.chan, "ADD", nick, "HOP");
conn.send("MODE", this.chan, "+vh", nick, nick);
}
break;
case "op":
if (data.coins >= 20000) {
data.coins -= 20000;
aucgbot.log(conn, "ELF OP", nick);
//conn.send("CS AOP", this.chan, "#elf ADD", nick);
conn.send("CS", this.chan, "#elf ADD", nick, "AOP");
conn.send("MODE", this.chan, "+ohv", nick, nick, nick);
}
break;
default:
conn.msg(dest, "material [amount]: costs twice the amount, so if you bought 4, you would pay 8 coins.");
conn.msg(dest, "voice: costs 800 coins. - hop: costs 7500 coins. - op: costs 20000 coins.");
conn.msg(dest, nick, "currently has", data.coins, "coins.");
return true;
}
this.updateUser(nick, data);
this.saveDB();
return true;
case "make":
var data = this.getUser(nick);
if (!data) {
conn.notice(nick, "You haven't joined the elf game yet. Join", this.chan, "to get started.");
return true;
}
data = data.toObject();
data.score = +data.score, data.coins = +data.coins, data.rep = +data.rep;
if (data.material <= 0)
conn.notice(nick, "You don't have enough material to make a toy.");
else {
data.material--;
data.total++;
switch (randint(1, 4)) {
case 1:
data.score += 100;
data.coins += 150;
data.rep += 250;
conn.msg(this.chan, nick, "makes a toy car. The toy car is fine but is a little scratched.",
nick, "gets 100 points and 150 coins.");
break;
case 2:
data.score += 500;
data.coins += 300;
data.rep += 750;
conn.msg(this.chan, nick, "makes a toy car. The toy car is perfectly made and Santa is very happy.",
nick, "gets 500 points and 300 coins.");
break;
case 3:
data.score += 50;
data.coins += 50;
data.rep += 150;
conn.msg(this.chan, nick, "makes a teddy bear.",
"The teddy bear is poorly made and is nearly falling apart. Santa is not happy.",
nick, "gets 50 points and 50 coins.");
break;
case 4:
data.score += 250;
data.coins += 150;
data.rep += 500;
conn.msg(this.chan, nick, "makes a teddy bear. The teddy bear is in good condition and is ready to sell.",
nick, "gets 250 points and 150 coins.");
break;
}
}
this.updateUser(nick, data);
this.saveDB();
return true;
case "rules":
conn.msg(dest, "buy: Buy items to use in the game. - make: Make a toy. - info: Show your current scores.");
return true;
case "elfreset":
if (aucgbot.isSU(e)) {
this.initDB();
aucgbot.log(conn, "ELF RESET", nick + (dest != nick ? " in " + dest : ""));
conn.msg(this.chan, "Database reset!!!");
}
return true;
case "reloadscores":
this.loadDB();
conn.msg(this.chan, "Scores reloaded.");
return true;
case "writescores":
this.saveDB();
return true;
}
};