From 880dfe8177549891a31f1632821f6fdefbd69c4f Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 2 Jun 2009 18:52:25 -0500 Subject: move maybe_add_player to the currentplayer role --- lib/Bot/Games/Game/Chess.pm | 3 ++- lib/Bot/Games/Game/Ghost.pm | 13 +++++-------- lib/Bot/Games/Game/Role/CurrentPlayer.pm | 12 +++++++++++- 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; -- cgit v1.2.3-54-g00ecf