-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathds_roll.cpp
283 lines (265 loc) · 10.5 KB
/
ds_roll.cpp
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
/* ----------------------------------------------------------------------------
* Name : ds_roll.cpp
* Author : Naram Qashat (CyberBotX)
* ----------------------------------------------------------------------------
* Description:
*
* The ROLL and EXROLL commands of DiceServ. See diceserv.cpp for more
* information about DiceServ, including version and license.
* ----------------------------------------------------------------------------
*/
#include "diceserv.h"
static ServiceReference<DiceServDataHandlerService> DiceServDataHandler("DiceServDataHandlerService", "DiceServ");
/** ROLL command
*
* Handles regular dice rolls.
*/
class DSRollCommand : public Command
{
public:
DSRollCommand(Module *creator) : Command(creator, "diceserv/roll", 1, 3)
{
this->AllowUnregistered(true);
this->RequireUser(true);
this->SetDesc(_("Rolls dice (or performs math too)"));
this->SetSyntax(_("\037dice\037 [[\037channel\037] \037comment\037]"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) anope_override
{
DiceServData data;
data.rollPrefix = "Roll";
if (!DiceServDataHandler->PreParse(data, source, params, 1))
return;
if (!DiceServDataHandler->CheckMessageLengthPreProcess(data, source))
return;
DiceServDataHandler->Roll(data);
if (data.errCode != DICE_ERROR_NONE)
{
DiceServDataHandler->HandleError(data, source);
return;
}
Anope::string output = DiceServDataHandler->GenerateNoExOutput(data);
if (!DiceServDataHandler->CheckMessageLengthPostProcess(data, source, output))
{
DiceServDataHandler->HandleError(data, source);
return;
}
DiceServDataHandler->SendReply(data, source, output);
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
{
if (subcommand.empty())
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("Echoes a dice roll to you. If you are using this command\n"
"while using a registered nick, it will come to you using\n"
"the method you tell Services to use. Otherwise, it will use\n"
"the default that Services is set to. Roll will be displayed\n"
"as follows:\n"
" \n"
"<Roll [\037dice\037]: \037result\037>\n"
" \n"
"\037Channel\037 is an optional argument, it must be a valid\n"
"channel and one that you are currently in. If you give an\n"
"invalid channel or one you are not in, the command will be\n"
"halted. If given and is valid, dice roll will be echoed to\n"
"the channel given as a NOTICE in the following form:\n"
" \n"
"<Roll for \037nick\037 [\037dice\037]: \037result\037>\n"
" \n"
"\037Comment\037 is also an optional argument. You do not need to\n"
"give a channel to use a comment. If given, this comment will\n"
"be added to the end of the result.\n"
" "));
const Anope::string &fantasycharacters = Config->GetModule("fantasy")->Get<const Anope::string>("fantasycharacter", "!");
if (!fantasycharacters.empty())
source.Reply(_("Additionally, if fantasy is enabled, this command can be triggered by using:\n"
" \n"
"!roll \037dice\037 [\037comment\037]\n"
" \n"
"where ! is one of the following characters: %s\n"
" "), fantasycharacters.c_str());
source.Reply(_(" \n"
"For help on the dice expression syntax, see \002%s%s\002\n"
"\002HELP ROLL EXPRESSIONS\002.\n"
" \n"
"Examples:\n"
" Roll 3d6: %s%s ROLL 3d6\n"
" Roll 3d6+5: %s%s ROLL 3d6+5\n"
" Roll 3d6+5, then double end result:\n"
" %s%s ROLL (3d6+5)*2\n"
" Roll 3d6, double the result, then add 5:\n"
" %s%s ROLL 3d6*2+5\n"
" Roll 3d6 three consecutive times:\n"
" %s%s ROLL 3~3d6"), Config->StrictPrivmsg.c_str(), source.service->nick.c_str(), Config->StrictPrivmsg.c_str(),
source.service->nick.c_str(), Config->StrictPrivmsg.c_str(), source.service->nick.c_str(), Config->StrictPrivmsg.c_str(),
source.service->nick.c_str(), Config->StrictPrivmsg.c_str(), source.service->nick.c_str(), Config->StrictPrivmsg.c_str(),
source.service->nick.c_str());
}
else if (subcommand.equals_ci("EXPRESSIONS"))
source.Reply(_("\002" "Dice expression syntax\002\n"
" \n"
"\037dice\037 expression must be in the form of: [\037x\037~]\037y\037\n"
" \n"
"(Alternatively, you can use the expression \037x\037[\037y\037], with the\n"
"[ and ] being actual characters in this case, and this will\n"
"be treated as if you had written \037x\037~\037y\037.)\n"
" \n"
"x and y can support very complex forms of expressions. In\n"
"order to get an actual dice roll, you must use something in\n"
"the format of: [\037z\037]d\037w\037, where z is the number of dice to\n"
"be thrown, and w is the number of sides on each die. z is\n"
"optional, will default to 1 if not given. Please note that\n"
"the sides or number of dice can not be 0 or negative, and\n"
"both can not be greater than 99999.\n"
" \n"
"x~ is used to determine how many consecutive sets of dice\n"
"will be rolled. This is optional, defaults to 1 if not\n"
"given. If you use this form, you MUST include the ~ for it\n"
"to be recognized as you wanting to throw a dice set multiple\n"
"times.\n"
" \n"
"y is normally used for the standard dice roll. You could\n"
"also do plain math within y, if you want. You must include\n"
"something here, but if it's not a number, it will usually\n"
"result in a parsing error.\n"
" \n"
"To roll what is called a \"percentile\" die, or a 100-sided\n"
"die, you can use %% as your roll expression, or include d%%\n"
"within your roll expression. For the former, the expression\n"
"will be replaced with 1d100, whereas for the latter, the\n"
"%% in the expression will be replaced with a 100. For all\n"
"other cases, the %% will signify modulus of the numbers\n"
"before and after it, the modulus being the remainder that\n"
"you'd get from dividing the first number by the second\n"
"number.\n"
" \n"
"The following math operators can be used in expressions:\n"
" \n"
"+ - * / ^ %% (in addition to 'd' for dice rolls and\n"
"parentheses to force order of operations.)\n"
" \n"
"Also note that if you use decimals in your expressions, the\n"
"result will be returned in integer form, unless you use CALC\n"
"or EXCALC. An additional note, implicit multiplication with\n"
"parentheses (example: 2(3d6)) will function as it should (as\n"
"2*(3d6)).\n"
" \n"
"In addition to the above math operators, certain functions\n"
"are also recognized. For a full list, see\n"
"\002%s%s HELP FUNCTIONS\002. The following math constants are\n"
"also recognized and will be filled in automatically:\n"
" \n"
" e Exponential growth constant\n"
" 2.7182818284590452354\n"
" pi Archimedes' constant\n"
" 3.14159265358979323846\n"
" \n"
"The dice roller will also recognize if you want to have a\n"
"negative number when prefixed with a -. This will not cause\n"
"problems even though it is also used for subtraction."), Config->StrictPrivmsg.c_str(), source.service->nick.c_str());
return true;
}
};
/** EXROLL command
*
* Handles dice rolls with extended output.
*/
class DSExrollCommand : public Command
{
public:
DSExrollCommand(Module *creator) : Command(creator, "diceserv/exroll", 1, 3)
{
this->AllowUnregistered(true);
this->RequireUser(true);
this->SetDesc(_("Rolls dice and shows each dice roll"));
this->SetSyntax(_("\037dice\037 [[\037channel\037] \037comment\037]"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) anope_override
{
DiceServData data;
data.isExtended = true;
data.rollPrefix = "Exroll";
if (!DiceServDataHandler->PreParse(data, source, params, 1))
return;
if (!DiceServDataHandler->CheckMessageLengthPreProcess(data, source))
return;
DiceServDataHandler->Roll(data);
if (data.errCode != DICE_ERROR_NONE)
{
DiceServDataHandler->HandleError(data, source);
return;
}
Anope::string output;
if (DiceServDataHandler->HasExtended(data))
{
output = DiceServDataHandler->GenerateLongExOutput(data);
if (!DiceServDataHandler->CheckMessageLengthPostProcess(data, source, output))
{
output = DiceServDataHandler->GenerateShortExOutput(data);
if (!DiceServDataHandler->CheckMessageLengthPostProcess(data, source, output))
{
data.rollPrefix = "Roll";
output = DiceServDataHandler->GenerateNoExOutput(data);
if (!DiceServDataHandler->CheckMessageLengthPostProcess(data, source, output))
{
DiceServDataHandler->HandleError(data, source);
return;
}
}
}
}
else
{
data.rollPrefix = "Roll";
output = DiceServDataHandler->GenerateNoExOutput(data);
if (!DiceServDataHandler->CheckMessageLengthPostProcess(data, source, output))
{
DiceServDataHandler->HandleError(data, source);
return;
}
}
DiceServDataHandler->SendReply(data, source, output);
}
bool OnHelp(CommandSource &source, const Anope::string &) anope_override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("This command is exactly like ROLL (see \002%s%s HELP\002\n"
"\002ROLL\002 for more information on how to use this and ROLL),\n"
"with one slight difference. EXROLL will show what was rolled\n"
"on each die as it is rolled.\n"
" \n"
"Example: Roll a 4d6: {4d6=(6 3 1 4)}=14\n"
" \n"
"This can be useful if you want to know exactly what each die\n"
"said when it was rolled."), Config->StrictPrivmsg.c_str(), source.service->nick.c_str());
const Anope::string &fantasycharacters = Config->GetModule("fantasy")->Get<const Anope::string>("fantasycharacter", "!");
if (!fantasycharacters.empty())
source.Reply(_("Additionally, if fantasy is enabled, this command can be triggered by using:\n"
" \n"
"!exroll \037dice\037 [\037comment\037]\n"
" \n"
"where ! is one of the following characters: %s\n"
" "), fantasycharacters.c_str());
source.Reply(_(" \n"
"Syntax of the dice is exactly the same as the syntax of\n"
"ROLL."));
return true;
}
};
class DSRoll : public Module
{
DSRollCommand roll_cmd;
DSExrollCommand exroll_cmd;
public:
DSRoll(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, THIRD), roll_cmd(this), exroll_cmd(this)
{
this->SetAuthor(DiceServService::Author());
this->SetVersion(DiceServService::Version());
if (!DiceServDataHandler)
throw ModuleException("No interface for DiceServ's data handler");
}
};
MODULE_INIT(DSRoll)