summaryrefslogtreecommitdiff
path: root/bucky2/rest/coin-toss.pl
diff options
context:
space:
mode:
Diffstat (limited to 'bucky2/rest/coin-toss.pl')
-rwxr-xr-xbucky2/rest/coin-toss.pl66
1 files changed, 66 insertions, 0 deletions
diff --git a/bucky2/rest/coin-toss.pl b/bucky2/rest/coin-toss.pl
new file mode 100755
index 0000000..875590c
--- /dev/null
+++ b/bucky2/rest/coin-toss.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/perl
+use lib "../lib";
+use Rest::Twitter;
+use Data::Dumper;
+my $twitter = new Rest::Twitter;
+$twitter->auth("coin_toss","onetimeonly");
+
+while (1)
+ {
+ my $tweets = $twitter->tweet_get();
+print Dumper($tweets);
+ foreach my $tweet (@$tweets)
+ {
+ my $toss = coin_toss_tweet($twitter, $tweet);
+ if ($toss)
+ {
+ print "$tweet->{'user'}: $tweet->{'tweet'}\n";
+ if ($tweet->{'type'} eq "dm")
+ {
+ $twitter->dm_post($tweet->{'user'}, $toss);
+ }
+ else
+ {
+ $twitter->tweet_post("@" . $tweet->{'user'} . " " . $toss, $tweet->{'id'});
+ }
+ }
+ }
+ sleep 60;
+ }
+sub coin_toss_tweet
+ {
+ my ($twitter, $tweet) = @_;
+ my $user = $twitter->user;
+ my $msg = $tweet->{'tweet'};
+ my $toss = undef;
+ return undef if $tweet->{'user'} eq $user;
+ $msg =~ s/\@$user:?//g;
+ $msg =~ s/\?*$//g;
+ if ($msg =~ /rock/i && $msg =~ /paper/i && $msg =~ /scissors/i)
+ {
+ $toss = coin_toss("rock", "paper", "scissors");
+ }
+ elsif ($msg =~ /\sor\s/i)
+ {
+ my (@faces) = split " or ", $msg;
+ $toss = coin_toss(@faces);
+ }
+ elsif ($msg =~ /\//i)
+ {
+ my (@faces) = split "/", $msg;
+ $toss = coin_toss(@faces);
+ }
+ else
+ {
+ $toss = coin_toss("heads", "tails");
+ }
+ return $toss;
+ }
+sub coin_toss
+ {
+ my (@faces) = @_;
+ my $face = uc $faces[ int rand scalar @faces ];
+ my $face_string = join "/", @faces;
+ return "$face_string: $face";
+ }
+1;