| 1 | package BalanceOfPower::Commands::TargetNation; | ||
| 2 | $BalanceOfPower::Commands::TargetNation::VERSION = '0.300010'; | ||
| 3 | use Moo; | ||
| 4 | use v5.10; | ||
| 5 | use IO::Prompter; | ||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | |||
| 9 | with "BalanceOfPower::Commands::Role::Command"; | ||
| 10 | |||
| 11 | has exclude_actor => ( | ||
| 12 | is => 'rw', | ||
| 13 | default => 1 | ||
| 14 | ); | ||
| 15 | |||
| 16 | sub select_message | ||
| 17 | { | ||
| 18 | return "Select a nation:"; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub execute | ||
| 22 | { | ||
| 23 | my $self = shift; | ||
| 24 | my $query = shift; | ||
| 25 | my $nation = shift; | ||
| 26 | my $argument = $self->extract_argument($query); | ||
| 27 | $argument = $self->world->correct_nation_name($argument); | ||
| 28 | if($argument) | ||
| 29 | { | ||
| 30 | if($self->good_target($argument)) | ||
| 31 | { | ||
| 32 | return { status => 1, command => $self->name . " " . $argument }; | ||
| 33 | } | ||
| 34 | else | ||
| 35 | { | ||
| 36 | say "Bad argument provided: $argument"; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | else | ||
| 40 | { | ||
| 41 | if($self->good_target($nation)) | ||
| 42 | { | ||
| 43 | return { status => 1, command => $self->name . " " . $nation }; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | my @nations = $self->get_available_targets; | ||
| 47 | if(@nations > 0) | ||
| 48 | { | ||
| 49 | $nation = prompt $self->select_message, -menu=>\@nations; | ||
| 50 | return { status => -3} if ! $nation; | ||
| 51 | return { status => 1, command => $self->name . " " . $nation }; | ||
| 52 | } | ||
| 53 | else | ||
| 54 | { | ||
| 55 | return { status => -2 }; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub good_target | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | my $nation = shift; | ||
| 63 | return 0 if(! $nation); | ||
| 64 | my @nations = $self->get_available_targets(); | ||
| 65 | my @selected = grep { $_ eq $nation} @nations; | ||
| 66 | if(@selected >= 1) | ||
| 67 | { | ||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | else | ||
| 71 | { | ||
| 72 | return 0; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub get_available_targets | ||
| 77 | { | ||
| 78 | my $self = shift; | ||
| 79 | my @nations = @{$self->world->nation_names}; | ||
| 80 | if($self->exclude_actor) | ||
| 81 | { | ||
| 82 | @nations = grep { $_ ne $self->actor } @nations; | ||
| 83 | } | ||
| 84 | return @nations | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; |