-
Notifications
You must be signed in to change notification settings - Fork 235
/
dice_concise.pl
231 lines (217 loc) · 7.23 KB
/
dice_concise.pl
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
# dice_concise / Based on Marcel Kossin's 'dice' RP Dice Simulator
#
# What is this?
#
# -- Marcel Kossin's notes: --
#
# I (mkossin) often Dungeon Master on our Neverwinternights Servers called 'Bund der
# alten Reiche' (eng. 'Alliance of the old realms') at bundderaltenreiche.de
# (German Site) Often idling in our Channel I thought it might be Fun to have
# a script to dice. Since I found nothing for irssi I wrote this little piece
# of script. The script assumes, that if a 'd' for english dice is given it
# should print the output in English. On the other hand if a 'w' for German
# 'Würfel' is given it prints the output in German.
#
# Usage.
#
# Anyone on the Channel kann ask '!roll' to toss the dice for him. He just has
# to say what dice he want to use. The notation should be well known from
# RP :-) Thus
#
# Write: !roll <quantity of dice>d[or w for german users]<sides on dice>
#
# Here are some examples
#
# !roll 2d20
# !roll 3d6
#
# OK, I think you got it already :-)
#
# Write: !roll version
# For Version Information
#
# Write: !roll help
# For Information about how to use it
#
# -- Makaze's notes: --
#
# [Changes in dice_concise:]
#
# Features added:
#
# [ ] Can add bonuses to the roll. e.g. "!roll 3d6+10"
# [ ] Output changed to one line only. e.g. "Makaze rolls the 3d6 and gets: 9 [4,
# 4, 1]"
# [ ] Corrected English grammar.
# [ ] Removed insults.
# [ ] Cleaner code with fewer nested if statements and true case switches.
# [ ] Errors call before the loop, saving clock cycles.
#
# Bugs fixed:
#
# [ ] Rolls within the correct range.*
#
# Edge cases added:
#
# [ ] Catch if rolling less than 1 dice.
# [ ] Catch if dice or sides are above 100 instead of 99.
#
# -----------------------------------------
#
# * [The original dice.pl rolled a number between 1 and (<number of sides> - 1)]
# [instead of using the full range. e.g. "!roll 1d6" would output 1 through ]
# [5, but never 6. ]
#
# -----------------------------------------
#
# Original script 'dice.pl' by mkossin.
#
# Updated script 'dice_concise.pl' by Makaze.
use strict;
use vars qw($VERSION %IRSSI);
use Scalar::Util qw(looks_like_number);
use Irssi qw(command_bind signal_add);
$VERSION = '0.1.6';
%IRSSI = (
authors => 'Marcel Kossin, Makaze',
contact => 'izaya.orihara@gmail',
name => 'dice_concise',
description => 'A concise dice simulator for channels.',
license => 'GNU GPL v2 or later'
);
sub own_question {
my ($server, $msg, $nick, $address, $target) = @_;
question($server, $msg, $nick, $target);
}
sub public_question {
my ($server, $msg, $nick, $address, $target) = @_;
question($server, $msg, $nick, $target);
}
sub question {
my ($server, $msg, $nick, $target) = @_;
$_ = $msg;
my $msgCompare = lc;
if (substr($msgCompare, 0, 5) ne '!roll') {
return 0;
}
unless (length $target) {
$target = $nick;
$nick = $server->{nick};
}
if (/\d[dw]\d/i) {
my $rnd;
my $forloop;
my $lang;
my @roll = split(/\s/, $_, 2);
my ($dice, $sides) = (@roll[1] =~ /(\d+)[dw](\d+)/i);
my @modifiers = ($roll[1] =~ /([\+\-\*\/]\d+)/gi);
my $modifyType;
my $modifyVal;
my @modifyErrors = ($roll[1] =~ /([\+\-\*\/][^\d\+\-\*\/]+)/);
my $value;
# Plus support added
my @rolls;
if (/\d[w]\d/i) {
$lang = 'DE';
} else {
$lang = 'EN';
}
if ($dice < 1) {
if ($lang eq 'DE') {
$server->command('msg ' . $target . ' ' . $nick . ' macht nichts... Würfeln funktioniert am besten mit Würfeln.');
}
if ($lang eq 'EN') {
$server->command('msg ' . $target . ' ' . $nick . ' does nothing... Rolling dice works best with dice.');
}
return 0;
} elsif ($dice > 100) {
if ($lang eq 'DE') {
$server->command('msg ' . $target . ' ' . $nick . ' scheitert den ' . $roll[1] . ' zu werfen... Versuch es mit weniger Würfeln.');
}
if ($lang eq 'EN') {
$server->command('msg ' . $target . ' ' . $nick . ' fails to roll the ' . $roll[1] . '... Try fewer dice.');
}
return 0;
} elsif ($sides <= 1) {
if ($sides == 0) {
if ($lang eq 'DE') {
$server->command('msg ' . $target . ' ' . $nick . ' verursacht ein Paradox... Oder hat jemand schon mal einen Würfel ohne Seiten gesehen?');
}
if ($lang eq 'EN') {
$server->command('msg ' . $target . ' ' . $nick . ' causes a paradox... Or has anybody ever seen a die without sides?');
}
return 0;
} elsif ($sides == 1) {
if ($lang eq 'DE') {
$server->command('msg ' . $target . ' ' . $nick . ' verursacht ein Paradox... Oder hat jemand schon mal einen Würfel mit nur einer Seite gesehen?');
}
if ($lang eq 'EN') {
$server->command('msg ' . $target . ' ' . $nick . ' causes a paradox... Or has anybody ever seen a die with only one side?');
}
return 0;
}
} elsif ($sides > 100) {
if ($lang eq 'DE') {
$server->command('msg ' . $target . ' ' . $nick . ' scheitert den ' . $roll[1] . ' zu werfen... Versuch es mit weniger Augen.');
}
if ($lang eq 'EN') {
$server->command('msg ' . $target . ' ' . $nick . ' fails to roll the ' . $roll[1] . '... Try fewer sides.');
}
return 0;
}
for ($forloop = 0; $forloop < $dice; $forloop++) {
$rnd = int(rand($sides));
if ($rnd == 0) {
$rnd = $sides;
}
$value += $rnd;
$rolls[$forloop] = $rnd;
}
foreach (@modifiers) {
($modifyType) = ($_ =~ /([\+\-\*\/])/);
($modifyVal) = ($_ =~ /(\d+)/);
if ($modifyType eq '*') {
$value = $value * $modifyVal;
}
if ($modifyType eq '/') {
$value = $value / $modifyVal;
}
if ($modifyType eq '+') {
$value = $value + $modifyVal;
}
if ($modifyType eq '-') {
$value = $value - $modifyVal;
}
}
if ($lang eq 'DE') {
$server->command('msg ' . $target . ' '. $nick . ' würfelt mit dem ' . $roll[1] . ' und erhält: ' . $value . ' [' . join(', ', @rolls) . ']');
}
if ($lang eq 'EN') {
$server->command('msg ' . $target . ' '. $nick . ' rolls the ' . $roll[1] . ' and gets: ' . $value . ' [' . join(', ', @rolls) . ']');
}
if (@modifyErrors) {
if ($lang eq 'DE') {
$server->command('msg ' . $target . ' ' . $nick . ' scheitert ihr Ergebnis zu ändern. Versuch es mit Zahlen. [' . join(', ', @modifyErrors) . ']');
}
if ($lang eq 'EN') {
$server->command('msg ' . $target . ' ' . $nick . ' fails to modify their result. Try using numbers. [' . join(', ', @modifyErrors) . ']');
}
}
return 1;
} elsif (substr($msgCompare, 0, 13) eq '!roll version') {
$server->command('msg ' . $target . " \x039" . $IRSSI{'name'} . ": Version " . $VERSION . " by Makaze & mkossin");
return 0;
} elsif (substr($msgCompare, 0, 10) eq '!roll help') {
$server->command('msg ' . $target . ' Syntax: "!roll <quantity of dice>d<sides on dice>[<+-*/>modifier]" - e.g. "!roll 2d20", "!roll 2d20*2+10"');
return 0;
} elsif (substr($msgCompare, 0, 11) eq '!roll hilfe') {
$server->command('msg ' . $target . ' Syntax: "!roll <Anzahl der Würfel>w<Augen des Würfels>[<+-*/>Modifikator]" - z.B. "!roll 2w20", "!roll 2w20*2+10"');
return 0;
} else {
$server->command('msg ' . $target .' "!roll help" - gives the English help');
$server->command('msg ' . $target . ' "!roll hilfe" - zeigt die deutsche Hilfe an');
return 0;
}
}
signal_add('message public', 'public_question');
signal_add('message own_public', 'own_question');