#!/usr/bin/perl # I don't think this works all the way yet, but its purpose is to monitor # and keep track of games of Rock Paper Scissors Russian Roulette. For # the uninitiated, this is a game of rock paper scissors, and the loser # must then "fire a bullet" at his head. One in six chance of losing. use strict; use warnings; use POE qw(Component::IRC); use POE::Component::IRC::Plugin::BotTraffic; use Switch; my %config = ( nickname => 'RPSBot', ircname => 'RPSBot of Possum', ircserver => 'irc.myserver.net', port => 6667, channels => ('#rockpaperscissors') ); my @channels=$config{channels}; my ($mykernel,$myheap,$mysender); my $GameTime=0; my $GameChannel; my ($player1,$player2,$player1old,$player2old,$p1choice,$p2choice)='NULL'; #Create a new PoCo-IRC object/component my $irc = POE::Component::IRC->spawn( nick => $config{nickname}, server => $config{ircserver}, port => $config{port}, ircname => $config{ircname} ) or die "Crap, not connected...site must be dead?"; $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() ); $irc->plugin_add( 'BotAddressed', POE::Component::IRC::Plugin::BotTraffic->new() ); POE::Session->create( package_states => [ 'main' => [ qw(_default _start irc_001 irc_public) ], ], heap => { irc => $irc }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; #Get session ID of component from object, register, #connect to specified server. my $irc_session = $heap->{irc}->session_id(); $kernel->post( $irc_session => register => 'all' ); $kernel->post( $irc_session => connect => { } ); undef; } sub irc_001 { my ($kernel,$sender) = @_[KERNEL,SENDER]; ($mykernel,$mysender)=($kernel,$sender); #Get component's object by accessing heap of SENDER my $poco_object = $sender->get_heap(); print "Connected to ",$poco_object->server_name(),"\n"; #In any irc_* events, the SENDER will be the PoCo IRC session $kernel->post( $sender => join => $_ ) for @channels; privmsg('nickserv','identify possum'); undef; } sub irc_public { my ($kernel,$sender,$who,$where,$what) = @_[KERNEL,SENDER,ARG0,ARG1,ARG2]; my $nick = ( split /!/, $who )[0]; my $channel = $where->[0]; #Next lines are commands and how to work with them my @dialogue=&irc_bot_addressed; my $command; my @args; ($command,@args) = split(/ /, $dialogue[2]); if ( $command eq '!challenge'){ if ($GameTime != 0) { privmsg($channel,"There is already a challenge in effect"); } else { $GameTime=1; $GameChannel=$channel; privmsg($channel,"$nick has challenged $args[0] to a duel. $args[0] send me a private message with the word \"accept\" to accept the duel. Type !rules for rules." ); ($player1,$player2)=($nick,$args[0]); } } elsif ( $command =~ "!abort" ){ $GameTime=0; ($p1choice,$p2choice,$GameChannel)='NULL'; privmsg($channel, "Game aborted by $nick"); } elsif ( $command eq '!rematch' ) { if ($GameTime !=0) { privmsg( $channel,"There is already a challenge in effect."); } else { $GameTime=1; $GameChannel=$channel; ($player1,$player2)=($player1old,$player2old); privmsg($channel,"$player1 challenged $player2 to a rematch!"); } } elsif ($command eq '!rules'){ privmsg($channel,"Rules, Coming Soon!"); } undef; } sub _default { my ( $event, $args ) = @_[ARG0 .. $#_]; my @output = ( "$event: " ); foreach my $arg ( @$args ) { if ( ref($arg) eq 'ARRAY' ) { push( @output, "[" . join(" ,", @$arg ) . "]" ); } else { push ( @output, "'$arg'" ); } } if ( $event eq 'irc_msg' ) { my ($player,undef)=split(/!/, $$args[0]); my $choice = $$args[2]; print @$args; if ($GameTime == 1){ if ($player eq $player2){ if ( $choice eq 'accept' ){ privmsg($GameChannel,"$player2 has accepted the challenge! $player1 and $player2 msg me with 'rock' 'paper' or 'scissors'"); $GameTime=2; }elsif ( $choice eq "deny" ){ privmsg($GameChannel, "$player2 has DECLINED the challenge and WILL BE SHAMED! ... Resetting game."); $GameTime=0; ($player1,$player2,$p1choice,$p2choice)='NULL'; } else { privmsg($player,"Say 'accept' to accept the challenge or 'deny' to turn it down and be shamed"); } } else { privmsg($player, "Tell $player2 to accept $player1\'s challenge!"); } } elsif ($GameTime == 2){ rpsget($player, $choice); } else { privmsg($player,"We're not playing!"); } } print STDOUT join ' ', @output, "\n"; return 0; } sub irc_bot_addressed { my ( $kernel,$heap ) = @_[KERNEL,HEAP]; my ($nick) = (split /!/, $_[ARG0] )[0]; my ($channel) = $_[ARG1][0]; my ($what) = $_[ARG2]; print "$nick addressed me in $channel with the message $what"; return ($nick,$channel,$what); } sub randint { open my $hat, "/dev/random" or die "Can't open /dev/random: $!"; my $buf; read $hat, $buf, 1; close $hat; return ord($buf); } sub rpsget { my ($player, $choice) = @_; if ($player eq $player1) { if ($p1choice ne 'rock' && $p1choice ne 'paper' && $p1choice ne 'scissors' ){ if ( $choice eq 'rock' || $choice eq 'scissors' || $choice eq 'paper'){ privmsg($player,"Accepted choice of $choice"); $p1choice=$choice; } else { privmsg($player,"Please choose 'rock' 'paper' or 'scissors'");} } else { privmsg($player,"You already chose $p1choice");} } elsif ($player eq $player2) { if ($p2choice ne 'rock' && $p2choice ne 'paper' && $p2choice ne 'scissors' ){ if ( $choice eq 'rock' || $choice eq 'scissors' || $choice eq 'paper'){ privmsg($player,"Accepted choice of $choice"); $p2choice=$choice; } else { privmsg($player,"Please choose 'rock' 'paper' or 'scissors'");} } else {privmsg($player,"You already chose $p2choice");} } else{ privmsg($player,"You're not currently playing!"); } unless (($p1choice ne 'rock' && $p1choice ne 'paper' && $p1choice ne 'scissors') || ( $p2choice ne 'rock' && $p2choice ne 'paper' && $p2choice ne 'scissors' )){ RPSeval($p1choice,$p2choice); } } sub RPSeval { my $win='NULL'; my @choice; ($choice[1],$choice[2])=@_; #my $r,$p,$s; my ($r,$p,$s)=('rock','paper','scissors'); switch ($choice[1]){ case ($r){ switch ($choice[2]) { case ($p) {$win=$player2;} case ($s) {$win=$player1;} case ($r) {$win='NULL';} } } case ($p){ switch ($choice[2]) { case ($r) {$win=$player1;} case ($p) {$win='NULL';} case ($s) {$win=$player2;} } } case ($s){ switch ($choice[2]) { case ($r) {$win=$player2;} case ($p) {$win=$player1;} case ($s) {$win='NULL';} } } } privmsg($GameChannel,"$player1 chose $p1choice and $player2 chose $p2choice."); if ($win eq 'NULL'){ privmsg($GameChannel,"Nobody won!"); } else {privmsg($GameChannel,"The winner was $win");} ($player1,$player2)=($player1old,$player2old); ($player1,$player2,$p1choice,$p2choice)='NULL'; $GameTime=0; } sub privmsg { # my ($kernel,$sender,$heap)=@_[KERNEL,SENDER,HEAP]; my ($channel,@message)=@_; $mykernel->post( $mysender => privmsg => $channel => "@message" ); }