summaryrefslogtreecommitdiffstats
path: root/lib/Bot/Games/Game/Role/CurrentPlayer.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Bot/Games/Game/Role/CurrentPlayer.pm')
-rw-r--r--lib/Bot/Games/Game/Role/CurrentPlayer.pm35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/Bot/Games/Game/Role/CurrentPlayer.pm b/lib/Bot/Games/Game/Role/CurrentPlayer.pm
new file mode 100644
index 0000000..9fd8023
--- /dev/null
+++ b/lib/Bot/Games/Game/Role/CurrentPlayer.pm
@@ -0,0 +1,35 @@
+package Bot::Games::Game::Role::CurrentPlayer;
+use Bot::Games::OO::Game::Role;
+
+requires 'players', 'num_players';
+
+has current_player => (
+ is => 'rw',
+ isa => 'Str',
+ predicate => 'has_current_player',
+ command => 1,
+);
+
+command previous_player => sub {
+ my $self = shift;
+ return unless $self->has_current_player;
+ return $self->players->[$self->current_player_index - 1];
+};
+
+command next_player => sub {
+ my $self = shift;
+ return unless $self->has_current_player;
+ return $self->players->[($self->current_player_index + 1) % $self->num_players];
+};
+
+sub current_player_index {
+ my $self = shift;
+ for (0..($self->num_players - 1)) {
+ return $_ if $self->current_player eq $self->players->[$_];
+ }
+ return 0;
+}
+
+no Bot::Games::OO::Game::Role;
+
+1;