summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordoy <doy@tozt.net>2009-04-26 18:50:48 -0500
committerdoy <doy@tozt.net>2009-04-26 18:50:48 -0500
commitb6b768619fd072d0d92760ba820ba02a8924e5f9 (patch)
treeda9884bcd9be2d13b7eec9781a9e72d3ffbf227d
parent663e1b3a9ff49b6a9c62143755d7a1f35aa6ab68 (diff)
downloadbot-games-b6b768619fd072d0d92760ba820ba02a8924e5f9.tar.gz
bot-games-b6b768619fd072d0d92760ba820ba02a8924e5f9.zip
make the datetime objects format more nicely
-rw-r--r--lib/Bot/Games/Game.pm31
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/Bot/Games/Game.pm b/lib/Bot/Games/Game.pm
index 3bf8f7b..80e5506 100644
--- a/lib/Bot/Games/Game.pm
+++ b/lib/Bot/Games/Game.pm
@@ -28,17 +28,15 @@ has players => (
);
command 'num_players';
-has start_time => (
+has _start_time => (
is => 'ro',
isa => 'DateTime',
default => sub { DateTime->now },
- command => 1,
);
-has last_turn_time => (
+has _last_turn_time => (
is => 'rw',
isa => 'DateTime',
- command => 1,
);
has is_over => (
@@ -52,7 +50,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 {
@@ -76,6 +74,29 @@ 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;
+ my ($dt) = @_;
+ my $dur = DateTime->now - $dt;
+ my @units = qw/weeks days hours minutes seconds/;
+ $dur->in_units(@units);
+ my @dur_values = map { $dur->$_ . " $_" } grep { $dur->$_ } @units;
+ return join(', ', @dur_values) . " ago";
+}
+
# this happens in Bot::Games, since we want to add the say method from there
#__PACKAGE__->meta->make_immutable;
no Bot::Games::OO;