Description: |
This program is an IRC dice roller that connects to an IRC server and joins a room specified on the command line. Once connected, the program waits for someone to say a slightly modified form of the D&D dice format: "~xdy+b". Here, x is the number of numbers to generate, y is the max number, eg: 1..y and b is an optional number to add to the total. When it recieves this, it generates the numbers and spits out a list of each number it generated, followed by their sum.
It was originally written for and tested with the following, but barring any significant changes in interface, it should work cross-platform and with any newer versions of the modules:
Net::IRC 0.73
Pod::Usage 1.14
Getopt::Long 2.32
strict 1.02
Perl 5.008
OS MSWin32
Further documentation can be found at the end of the program as POD. |
use Net::IRC;
use Getopt::Long;
use Pod::Usage;
use strict;
my $VERSION = "1.1.0";
my $server = "";
my @room;
my $room;
my $port = 6667;
my $help = 0;
my $versions = 0;
my $pass = '';
my $name = "Roller";
GetOptions(
'help|h' => \$help,
'port|p=i' => \$port,
'versions|v' => \$versions,
'pass|p=s' => \$pass,
'name|n=s' => \$name,
) or (pod2usage() && die);
if($help){
pod2usage(-verbose=>1);
exit;
}
if($versions){
print
"\nModules, Perl, OS, Program info:\n",
" Net::IRC $Net::IRC::VERSION\n",
" Pod::Usage $Pod::Usage::VERSION\n",
" Getopt::Long $Getopt::Long::VERSION\n",
" strict $strict::VERSION\n",
" Perl $]\n",
" OS $^O\n",
" irc.pl $VERSION\n",
" $0\n\n";
exit;
}
{
local $@;
eval{
$server = shift(@ARGV) or (pod2usage() && die);
@room = @ARGV or (pod2usage() && die);
};
if($@ =~ m#Can't open -e for reading#){
#Used with tinyperl
pod2usage(-verbose=>1);
exit;
}elsif($@){
die($@);
}
}
my $irc = new Net::IRC;
my $conn = $irc->newconn(
Nick => $name,
Server => $server,
Port => $port,
Ircname => 'Dice Roler Bot',
($pass ? (Password => $pass) : ()),
);
$conn->add_handler('376',\&on_connect);
$conn->add_handler('msg',\&on_msg);
$conn->add_handler('public',\&on_public);
$irc->start;
sub on_connect{
my $self = shift;
for(@room){
print "Joining $_\n";
$self->join($_);
}
print "Connection phase complete... Waiting...\n";
}
sub on_public{
my $self = shift;
my $event = shift;
my $cmd = $event->{'args'}[0];
return unless($cmd =~ m/^~(\d+)d(\d+)(?:(?:(?=-)|\+)(-?\d+))?$/);
my @list;
print "Rolling...\n";
if($1 > 50){
$self->privmsg($event->{'to'}[0],"Error: You cannot roll more th
+an 50 dice.");
return;
}
my $total = $3; #Set the bonus to the base!
for(1..$1){
my $num = int(rand($2)+1);
$total += $num;
push(@list,$num);
}
$self->privmsg($event->{'to'}[0],$event->{'nick'}.
' rolled '.
$1.'d'.$2.($3 ? ($3 > 0 ? "+$3" : "$3") : '').
' for '.
join(', ',@list).
": $total"
);
}
sub on_msg{
my $self = shift;
my $event = shift;
print "Private message detected... processing/explaining\n";
if($event->{'args'}[0] eq 'quit'){
warn($event->{'nick'});
exit;
}
$self->privmsg($event->{'nick'},"This is $name, a dice roller.");
$self->privmsg($event->{'nick'},"Summery of use: type ~xdy+b into t
+he chat and I will roll the dice as specified, adding b to the total
+and reporting the results.");
}
__END__
=head1 NAME
D&D - Dice Roller
=head1 SYNOPSIS
perl irc.pl [options] server room
Options:
--help|-h brief help message
--port|-p=PORT# specify the port
--versions|-v version information
--name|n=botname specify the name of the bot
--pass|p=password specify a password
=head1 OPTIONS
=over 8
=item B<--help>
Prints a brief help message and then quits
=item B<--port>
Allows for specifying the port.
=item B<--versions>
Prints out a list of versions for the modules used by the program, and
+ the
program itself.
=item B<--name>
Specify a name for the bot
=item B<--pass>
If the server is password protected, set this.
=back
=head1 DESCRIPTION
B<This program> connects to the specified server and joins the request
+ed room.
Once connected, it waits for anyone in that room to say "~xdy+b" each
+variable
representing the following.
=over 8
=item B<x>
B<x> = the number of dice
=item B<d>
B<d> = a seperator common to D&D and other dice-based games.
=item B<y>
B<y> = the number of sides on the dice
=item B<b>
B<b> = something to arbitraraly add to the sum of the rolls, it is not
+ necessary
=back
This program will then reply to the current chat-room something to the
+ effect
of "E<lt>NAMEE<gt> rolls 4d10+2 and got the following: 4, 6, 10, 9: 31
+"
This program automatically terminates when private messaged the word "
+quit".
=head1 AUTHOR
This program was written by Stephen "Flame" Couchman E<lt>guildms@user
+s.sourceforge.netE<gt>
=head1 COPYRIGHT and REDISTRIBUTION
This program is copyright 2003 Stephen Couchman and may be freely modi
+fied and redistributed under the same terms as Perl itself.
=cut
Edit: Some changes made to properly apply the bonus, now only applys to the total rather than to each roll... thought it looked a little funny. Also expanded docs a little more... could still be better documented but for now... |