| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | # stupid irc bot to rage out autists |
|---|
| 4 | # demands roots for a polynomial |
|---|
| 5 | # kicks if they get it wrong or run out of time |
|---|
| 6 | |
|---|
| 7 | # XXX needs srs cleaning, but works very well |
|---|
| 8 | |
|---|
| 9 | use warnings; |
|---|
| 10 | use strict; |
|---|
| 11 | |
|---|
| 12 | use Math::Polynomial; |
|---|
| 13 | use POE; |
|---|
| 14 | use POE::Component::IRC::State; |
|---|
| 15 | use POE::Component::IRC::Plugin::AutoJoin; |
|---|
| 16 | use POE::Component::IRC::Plugin::BotCommand; |
|---|
| 17 | |
|---|
| 18 | my $irc = POE::Component::IRC::State->spawn(Nick => 'polynomial', |
|---|
| 19 | Username => 'polynomial', |
|---|
| 20 | Ircname => 'polynomial', |
|---|
| 21 | Server => 'irc.buttes.org', |
|---|
| 22 | Port => '6667',); |
|---|
| 23 | |
|---|
| 24 | POE::Session->create(package_states => [main => [qw(_start irc_public irc_botcmd_test test_timer)]]); |
|---|
| 25 | |
|---|
| 26 | $poe_kernel->run(); |
|---|
| 27 | |
|---|
| 28 | sub _start { |
|---|
| 29 | $irc->plugin_add('Autojoin', POE::Component::IRC::Plugin::AutoJoin->new(Channels => ['#anti'])); |
|---|
| 30 | $irc->plugin_add('BotCommand', |
|---|
| 31 | POE::Component::IRC::Plugin::BotCommand->new(Commands => {test => 'lol'}, |
|---|
| 32 | Addressed => 0, |
|---|
| 33 | Eat => 1, |
|---|
| 34 | Prefix => '!', |
|---|
| 35 | Ignore_unknown => 1)); |
|---|
| 36 | $_[HEAP]->{busy} = 0; |
|---|
| 37 | $_[HEAP]->{nick} = ''; |
|---|
| 38 | $_[HEAP]->{root1} = 0; |
|---|
| 39 | $_[HEAP]->{root2} = 0; |
|---|
| 40 | $irc->yield(register => qw(join botcmd_test public)); |
|---|
| 41 | $irc->yield('connect'); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | sub irc_botcmd_test { |
|---|
| 45 | if ($_[ARG2] =~ /pynchon/) {return} |
|---|
| 46 | my $nick = (split /!/, $_[ARG0])[0]; |
|---|
| 47 | if ($_[HEAP]->{busy} == 0) { |
|---|
| 48 | $_[HEAP]->{channel} = $_[ARG1]; |
|---|
| 49 | $_[HEAP]->{nick} = $_[ARG2]; |
|---|
| 50 | if ($_[ARG2] =~ /polynomial/) {$irc->yield(kick => $_[ARG1], $nick, "fuck off kid")} |
|---|
| 51 | s/\s+$// for $_[HEAP]->{nick}; |
|---|
| 52 | $_[HEAP]->{busy} = 1; |
|---|
| 53 | $_[HEAP]->{root1} = int(rand(20)) - int(rand(20)); |
|---|
| 54 | $_[HEAP]->{root2} = int(rand(20)) - int(rand(20)); |
|---|
| 55 | my $seconds = 60; |
|---|
| 56 | my $p = Math::Polynomial->from_roots($_[HEAP]->{root1}, $_[HEAP]->{root2}); |
|---|
| 57 | $p->string_config({fold_sign => 1, |
|---|
| 58 | times => '', |
|---|
| 59 | prefix => '', |
|---|
| 60 | suffix => ''}); |
|---|
| 61 | print "nick: $_[HEAP]->{nick}\t poly: $p\t answers: { $_[HEAP]->{root1}, $_[HEAP]->{root2} } \n"; |
|---|
| 62 | $irc->yield(privmsg => $_[HEAP]->{channel}, |
|---|
| 63 | "$_[HEAP]->{nick}: you have $seconds seconds to find the roots of $p"); |
|---|
| 64 | $_[KERNEL]->delay(test_timer => $seconds); |
|---|
| 65 | } else { |
|---|
| 66 | $irc->yield(privmsg => $_[HEAP]->{channel}, "$nick: im busy with someone else"); |
|---|
| 67 | } |
|---|
| 68 | return; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | sub test_timer { |
|---|
| 72 | $irc->yield(kick => $_[HEAP]->{channel}, |
|---|
| 73 | $_[HEAP]->{nick}, |
|---|
| 74 | "times up, answer is { $_[HEAP]->{root1}, $_[HEAP]->{root2} }"); |
|---|
| 75 | $_[KERNEL]->alarm_remove_all(); |
|---|
| 76 | $_[HEAP]->{busy} = 0; |
|---|
| 77 | return; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | sub irc_public { |
|---|
| 81 | my $nick = (split /!/, $_[ARG0])[0]; |
|---|
| 82 | my $root1 = $_[HEAP]->{root1}; |
|---|
| 83 | my $root2 = $_[HEAP]->{root2}; |
|---|
| 84 | if ($_[ARG2] =~ /^\!/) {return;} |
|---|
| 85 | if (($nick eq $_[HEAP]->{nick}) && ($_[HEAP]->{busy} == 1)) { |
|---|
| 86 | if (my ($ans1, my $ans2) = ($_[ARG2] =~ /(-?\d+)/g)) { |
|---|
| 87 | if ( (($ans1 == $root1) && ($ans2 == $root2)) |
|---|
| 88 | || (($ans2 == $root1) && ($ans1 == $root2))) { |
|---|
| 89 | $irc->yield(privmsg => $_[HEAP]->{channel}, |
|---|
| 90 | "$_[HEAP]->{nick} is a confirmed autist"); |
|---|
| 91 | $_[KERNEL]->alarm_remove_all(); |
|---|
| 92 | $_[HEAP]->{busy} = 0; |
|---|
| 93 | return; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | $irc->yield(kick => $_[HEAP]->{channel}, |
|---|
| 97 | => $_[HEAP]->{nick}, |
|---|
| 98 | "wrong answer, correct answer is { $root1, $root2 }"); |
|---|
| 99 | $_[KERNEL]->alarm_remove_all(); |
|---|
| 100 | $_[HEAP]->{busy} = 0; |
|---|
| 101 | } |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|