summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordoy <doy@tozt.net>2009-04-26 22:24:46 -0500
committerdoy <doy@tozt.net>2009-04-26 22:24:46 -0500
commit86233c470263d60ad5aaa2e656a96c3029a54b6b (patch)
tree541a0a32919080bef5e208218c459b85a838d518
parent666ae159e66abf14e5ba3774f848f38be5940461 (diff)
downloadbot-games-86233c470263d60ad5aaa2e656a96c3029a54b6b.tar.gz
bot-games-86233c470263d60ad5aaa2e656a96c3029a54b6b.zip
use the format roles
-rw-r--r--lib/Bot/Games.pm20
-rw-r--r--lib/Bot/Games/Game.pm23
2 files changed, 11 insertions, 32 deletions
diff --git a/lib/Bot/Games.pm b/lib/Bot/Games.pm
index a1de583..3490a10 100644
--- a/lib/Bot/Games.pm
+++ b/lib/Bot/Games.pm
@@ -98,8 +98,9 @@ sub said {
$self->$say("Game $game_name hasn't started yet!");
return;
}
- $self->$say(scalar $method_meta->execute($game, $arg,
- {player => $args->{who}}));
+ my $body = $method_meta->execute($game, $arg,
+ {player => $args->{who}});
+ $self->$say($body, formatter => $method_meta->formatter);
}
else {
$self->$say("Unknown command $action for game $game_name");
@@ -152,21 +153,6 @@ sub find_game {
return;
}
-sub _format {
- my $self = shift;
- my ($to_print) = @_;
- if (blessed $to_print) {
- $to_print = "$to_print";
- }
- elsif (ref($to_print) && ref($to_print) eq 'ARRAY') {
- $to_print = join ', ', @$to_print;
- }
- elsif (!$to_print) {
- $to_print = 'false';
- }
- return $to_print;
-}
-
sub _get_command {
my ($game, $action) = @_;
my $method_meta = $game->meta->find_method_by_name($action);
diff --git a/lib/Bot/Games/Game.pm b/lib/Bot/Games/Game.pm
index ceeda9d..137d8ed 100644
--- a/lib/Bot/Games/Game.pm
+++ b/lib/Bot/Games/Game.pm
@@ -28,15 +28,19 @@ has players => (
);
command 'num_players';
-has _start_time => (
+has start_time => (
is => 'ro',
isa => 'DateTime',
default => sub { DateTime->now },
+ command => 1,
+ format => sub { _diff_from_now(shift) },
);
-has _last_turn_time => (
+has last_turn_time => (
is => 'rw',
isa => 'DateTime',
+ command => 1,
+ format => sub { _diff_from_now(shift) },
);
has is_over => (
@@ -50,7 +54,7 @@ sub turn {
return $turn if defined($turn);
return "Games must provide a turn method";
}
-after turn => sub { shift->_last_turn_time(DateTime->now) };
+after turn => sub { shift->last_turn_time(DateTime->now) };
sub allow_new_player { 1 }
around add_player => sub {
@@ -74,21 +78,10 @@ command cmdlist => sub {
return join ' ', sort map { '-' . $_ } @commands;
}, needs_init => 0;
-command start_time => sub {
- my $self = shift;
- return $self->diff_from_now($self->_start_time);
-};
-
-command last_turn_time => sub {
- my $self = shift;
- return $self->diff_from_now($self->_last_turn_time);
-};
-
# XXX: this would be much nicer as an external module, but the only one that
# really does what i want (DateTime::Format::Human::Duration) has only had one
# release, which doesn't pass tests. bleh.
-sub diff_from_now {
- my $self = shift;
+sub _diff_from_now {
my ($dt) = @_;
my $dur = DateTime->now - $dt;
my @units = qw/weeks days hours minutes seconds/;