summaryrefslogtreecommitdiffstats
path: root/lib/Bot/Games.pm
diff options
context:
space:
mode:
authordoy <doy@tozt.net>2008-12-18 01:16:02 -0500
committerdoy <doy@tozt.net>2008-12-18 01:16:02 -0500
commit76c44fb3db64dbd3db5465213febacd755cd944f (patch)
treef70f01b16b7349dfea9ff73c2c23cd5bc7b3770c /lib/Bot/Games.pm
downloadbot-games-76c44fb3db64dbd3db5465213febacd755cd944f.tar.gz
bot-games-76c44fb3db64dbd3db5465213febacd755cd944f.zip
initial bot api
Diffstat (limited to 'lib/Bot/Games.pm')
-rw-r--r--lib/Bot/Games.pm60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/Bot/Games.pm b/lib/Bot/Games.pm
new file mode 100644
index 0000000..ef2a17d
--- /dev/null
+++ b/lib/Bot/Games.pm
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+package Bot::Games;
+use Moose;
+use Module::Pluggable
+ search_path => 'Bot::Games'
+ except => ['Bot::Games::Ghostlike', 'Bot::Games::Game'],
+ require => 1,
+ sub_name => 'games';
+extends 'Bot::BasicBot';
+
+has prefix => (
+ is => 'ro',
+ isa => 'Str',
+ default => '!',
+);
+
+has active_games => (
+ is => 'ro',
+ isa => 'HashRef[Bot::Games::Game]',
+ default => sub { {} },
+);
+
+sub said {
+ my $self = shift;
+ my %args = @_;
+ my $prefix = $self->prefix;
+
+ return unless $args{body} =~ /^$prefix(\w+)\s+(.*)/;
+ my ($game_name, $action) = ($1, $2);
+ return unless $self->valid_game($game_name);
+
+ my $game = $self->games->{$game_name};
+ $game = $self->active_games->{$game_name}
+ = $self->game_package($game_name)->new
+ unless defined $game;
+
+ if ($action =~ /-(.*)/) {
+ my $action = $1;
+ return $game->$action($args{who}) if $game->can($action);
+ return "Unknown command $action for game $game_name.";
+ }
+
+ $game->turn($args{who}, $action);
+ delete $self->active_games->{$game_name} if $game->is_over;
+}
+
+sub valid_game {
+ my $self = shift;
+ my ($name) = @_;
+ my $package = $self->game_package($name);
+ return (grep { $package eq $_ } $self->games) ? 1 : 0;
+}
+
+sub game_package {
+ my $self = shift;
+ my ($name) = @_;
+ return 'Bot::Games::' . ucfirst($name);
+}
+
+1;