summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-06-02 18:52:25 -0500
committerJesse Luehrs <doy@tozt.net>2009-06-02 18:54:01 -0500
commit880dfe8177549891a31f1632821f6fdefbd69c4f (patch)
treea3585657d18a1370ef33ddf51b0f7186d59d44d0
parent7d059870682438390ed816330259a8e23550bfcb (diff)
downloadbot-games-880dfe8177549891a31f1632821f6fdefbd69c4f.tar.gz
bot-games-880dfe8177549891a31f1632821f6fdefbd69c4f.zip
move maybe_add_player to the currentplayer role
-rw-r--r--lib/Bot/Games/Game/Chess.pm3
-rw-r--r--lib/Bot/Games/Game/Ghost.pm13
-rw-r--r--lib/Bot/Games/Game/Role/CurrentPlayer.pm12
3 files changed, 18 insertions, 10 deletions
diff --git a/lib/Bot/Games/Game/Chess.pm b/lib/Bot/Games/Game/Chess.pm
index ad4549c..f196a09 100644
--- a/lib/Bot/Games/Game/Chess.pm
+++ b/lib/Bot/Games/Game/Chess.pm
@@ -29,7 +29,8 @@ has turn_count => (
augment turn => sub {
my $self = shift;
my ($player, $move) = @_;
- $self->add_player($player) unless $self->has_player($player);
+ $self->maybe_add_player($player);
+
return "The game has already begun between " . join ' and ', $self->players
unless $self->has_player($player);
return "It's not your turn"
diff --git a/lib/Bot/Games/Game/Ghost.pm b/lib/Bot/Games/Game/Ghost.pm
index 8a3a9c6..6f8ad83 100644
--- a/lib/Bot/Games/Game/Ghost.pm
+++ b/lib/Bot/Games/Game/Ghost.pm
@@ -118,15 +118,12 @@ command valid_word_from_state => sub {
return uc($word_prefix) eq $self->state;
}, formatter => 'Bool';
-sub maybe_add_player {
+around maybe_add_player => sub {
+ my $orig = shift;
my $self = shift;
- my ($player) = @_;
- if (!$self->has_challenger && !grep { $player eq $_ } $self->players) {
- if ($self->add_player($player)) {
- $self->current_player($player);
- }
- }
-}
+ return if $self->has_challenger;
+ return $self->$orig(@_);
+};
__PACKAGE__->meta->make_immutable;
no Bot::Games::OO::Game;
diff --git a/lib/Bot/Games/Game/Role/CurrentPlayer.pm b/lib/Bot/Games/Game/Role/CurrentPlayer.pm
index 9fd8023..2dbe524 100644
--- a/lib/Bot/Games/Game/Role/CurrentPlayer.pm
+++ b/lib/Bot/Games/Game/Role/CurrentPlayer.pm
@@ -1,7 +1,7 @@
package Bot::Games::Game::Role::CurrentPlayer;
use Bot::Games::OO::Game::Role;
-requires 'players', 'num_players';
+requires 'players', 'num_players', 'add_player';
has current_player => (
is => 'rw',
@@ -30,6 +30,16 @@ sub current_player_index {
return 0;
}
+sub maybe_add_player {
+ my $self = shift;
+ my ($player) = @_;
+ if (!grep { $player eq $_ } $self->players) {
+ if ($self->add_player($player)) {
+ $self->current_player($player);
+ }
+ }
+}
+
no Bot::Games::OO::Game::Role;
1;