Show extended hand information when the battle starts, send a little more information...
[kithairon.git] / bot.pl
blobd9a7d9d2fc21defc8af963fcc4fae7e1fa73e400
1 #!/usr/bin/perl
3 use strict;
4 use POE qw(Component::IRC);
5 use Getopt::Long;
7 use vars qw($botnick $server $botchan);
9 BEGIN {
10 use Cwd qw( abs_path getcwd );
11 use File::Basename;
12 my %consts = ( CWD => getcwd(), PREFIX => dirname(abs_path($0)), );
13 require constant;
14 import constant(\%consts);
17 chdir PREFIX;
18 use lib PREFIX;
20 use Kith::Conf qw($botnick $nickpass $server $port $serverpass $botchan); # Grab the variables we need from the conf module
21 use Kith::Stats::Registration;
22 use Kith::Stats::Cards;
23 use Kith::Stats::Battle;
25 my $debug;
26 GetOptions ('debug' => \$debug);
28 if (!-e "config/bot.conf") {
29 die("Error: Please make sure you have both \'sql.conf\' and \'bot.conf\' in this directory\n");
32 my $version = "0.0.1";
34 our %stats; # Hash for keeping stats locally. This will probably get moved in a few days.
36 our $irc = POE::Component::IRC->spawn(
37 nick => $botnick,
38 ircname => "Kithairon RPG Bot",
39 username => $botnick,
40 server => $server,
41 password => $serverpass,
42 ) or die "Oh noooo! $!";
44 POE::Session->create(
45 package_states => [
46 main => [ qw(_start irc_001 irc_public irc_msg irc_notice) ],
48 heap => { irc => $irc },
51 sub _start {
52 my $heap = $_[HEAP];
53 my $irc = $heap->{irc};
54 $irc->yield( register => 'all' );
55 $irc->yield( connect => { Flood => '1' } ); # disables the flood-throttle in POE - should come in handy
56 return;
59 sub daemonize() {
60 close STDIN;
61 close STDOUT;
62 close STDERR;
63 open(STDIN, '>', '/dev/null');
64 open(STDOUT, '>', '/dev/null');
65 open(STDERR, '>', '/dev/null');
66 if(fork()) { exit(0) }
69 daemonize() if !defined($debug);
70 $poe_kernel->run();
71 exit 0;
73 sub irc_001 {
74 my $sender = $_[SENDER];
75 my $irc = $sender->get_heap();
76 print "Connected to ", $irc->server_name(), "\n" if defined($debug);
77 $irc->yield( join => $botchan );
78 print "Joining $botchan\n" if defined($debug);
79 return;
82 sub irc_public {
83 my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2];
84 my $nick = ( split /!/, $who )[0];
85 my $channel = $where->[0];
86 print "$nick:$channel> $what\n" if defined($debug);
88 sub irc_notice {
89 my ($who, $target, $msg) = @_[ARG0 .. ARG2];
90 my $nick = (split /!/,$who)[0];
91 print "NOTICE:$nick> $msg\n" if defined($debug);
92 if ($nick eq "NickServ" && $msg =~ /msg nickserv identify/i) {
93 $irc->yield(privmsg => NickServ => "identify $nickpass") if $nickpass ne "";
96 sub irc_msg {
97 my ($who, $msg) = @_[ARG0, ARG2];
98 my $nick = (split /!/,$who)[0];
99 print "$nick> $msg\n" if defined($debug);
100 if ($msg =~ /^\.register$/i) {
101 if (!chk_registered($nick)) {
102 $irc->yield(privmsg => $nick => "[Register] Welcome to Kithairon, $nick. To begin, you must choose a card pack.");
103 $irc->yield(privmsg => $nick => "[Register] Each pack comes with a boost in whichever stat the name of the pack falls under. Choose one of the following card packs by typing \037.pack packname\037");
104 $irc->yield(privmsg => $nick => "[Register] \002Attack\002, \002spirit\002, or \002speed\002");
106 else {
107 $irc->yield(privmsg => $nick => "[Error] You are already registered.");
110 if ($msg =~ /^\.pack (attack|spirit|speed)$/i) {
111 if (!chk_registered($nick)) {
112 $irc->yield(privmsg => $nick => "[Register] You have chosen the $1 pack. Please wait while your information is recorded.");
113 register($nick,$1);
115 else {
116 $irc->yield(privmsg => $nick => "[Error] You have already chosen your pack, and are already registered.");
119 if ($msg =~ /^\.hand$/i) {
120 if (chk_registered($nick)) {
121 show_hand($nick);
123 else {
124 $irc->yield(privmsg => $nick => "[Error] You need to be registered first.");
127 if ($msg =~ /^\.battle (.+)$/i) {
128 my $nick2 = $1;
129 if ($nick ne $nick2) {
130 if (chk_registered($nick) && chk_registered($nick2)) {
131 if (!chk_battle($nick) && !chk_battle($nick2)) {
132 initiate_challenge($nick,$nick2);
133 $irc->yield(privmsg => $nick => "[Battle] You have requested a battle with \002$nick2\002. This request will expire in \002one minute\002.");
134 $irc->yield(privmsg => $nick2 => "[Battle] \002$nick\002 has requested a battle! Type \002.accept\002 or \002.decline\002");
136 elsif (chk_battle($nick)) {
137 $irc->yield(privmsg => $nick => "[Error] You are currently in a battle.");
139 elsif (chk_battle($nick2)) {
140 $irc->yield(privmsg => $nick => "[Error] Your opponent is in a battle.");
143 else {
144 $irc->yield(privmsg => $nick => "[Error] You or your opponent may not be registered.");
148 if ($msg =~ /^\.accept$/i) {
149 if (chk_battle($nick)) {
150 if (chk_opponent($nick) ne "" && chk_turn($nick) eq "deciding") {
151 my $nick2 = chk_opponent($nick);
152 start_battle($nick,$nick2);
153 $irc->yield(privmsg => $nick2 => "[Battle] Your opponent has accepted the challenge. The battle will now begin. Showing extended hand information.");
154 $irc->yield(privmsg => $nick => "[Battle] The battle will now begin. Showing extended hand information.");
155 show_hand_ext($nick);
156 show_hand_ext($nick2);
159 elsif (!chk_battle($nick)) {
160 $irc->yield(privmsg => $nick => "[Error] You are not currently in a battle.");
163 if ($msg =~ /^\.decline$/i) {
164 if (chk_battle($nick)) {
165 if (chk_opponent($nick) ne "") {
166 my $nick2 = chk_opponent($nick);
167 stop_challenge($nick,$nick2);
168 $irc->yield(privmsg => $nick => "[Battle] You have declined the challenge.");
169 $irc->yield(privmsg => $nick2 => "[Battle] Your opponent has declined the challenge.");
171 else {
172 $irc->yield(privmsg => $nick => "[Error] This is not your opponent.");