blib/lib/BalanceOfPower/Commands/Role/Command.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 130 CC 19 SCAR 45.5
CC
19
Cov
0%
allowed
182.0
extract_argument
42.0
recognize
6.0
CRAP 94.4
1 package BalanceOfPower::Commands::Role::Command;
2 $BalanceOfPower::Commands::Role::Command::VERSION = '0.200000';
3 use strict;
4 use v5.10;
5 use Moo::Role;
6  
7 has name => (
8 is => 'ro',
9 default => 'DO NOTHING'
10 );
11 has actor => (
12 is => 'rw',
13 );
14  
15 has world => (
16 is => 'ro'
17 );
18 has synonyms => (
19 is => 'rw',
20 default => sub { [] }
21 );
22 has export_cost => (
23 is => 'ro',
24 default => 0
25 );
26 has domestic_cost => (
27 is => 'ro',
28 default => 0
29 );
30 has prestige_cost => (
31 is => 'ro',
32 default => 0
33 );
34  
35 has allowed_at_war => (
36 is => 'ro',
37 default => 0
38 );
39 has production_limit => (
40 is => 'ro',
41 default => sub { {} }
42 );
43 has army_limit => (
44 is => 'ro',
45 default => sub { {} }
46 );
47 has treaty_limit => (
48 is => 'ro',
49 default => 0
50 );
51  
52 sub get_nation
53 {
54 my $self = shift;
55 return $self->world->get_nation($self->actor);
56 }
57  
58 sub allowed
59 {
60 my $self = shift;
61 my $nation = $self->get_nation();
62 return 0
63 if($nation->internal_disorder_status() eq 'Civil war');
64 if(! $self->allowed_at_war)
65 {
66 if($self->world->at_war($self->actor))
67 {
68 return 0;
69 }
70 }
71 if(exists $self->production_limit->{'<'})
72 {
73 return $nation->production() <= $self->production_limit->{'<'};
74 }
75 elsif(exists $self->production_limit->{'>'})
76 {
77 return $nation->production() >= $self->production_limit->{'>'};
78 }
79 if(exists $self->army_limit->{'<'})
80 {
81 return $nation->army() <= $self->army_limit->{'<'};
82 }
83 elsif(exists $self->army_limit->{'>'})
84 {
85 return $nation->army() >= $self->army_limit->{'>'};
86 }
87 if($nation->production_for_domestic < $self->domestic_cost)
88 {
89 return 0;
90 }
91 if($nation->production_for_export < $self->export_cost)
92 {
93 return 0;
94 }
95 if($nation->prestige < $self->prestige_cost)
96 {
97 return 0;
98 }
99 if($self->treaty_limit == 1)
100 {
101 if($self->world->get_treaties_for_nation($nation->name) >= $nation->treaty_limit)
102 {
103 return 0;
104 }
105 }
106 return 1;
107 }
108  
109 sub extract_argument
110 {
111 my $self = shift;
112 my $query = shift;
113 my $extract = shift;
114 $query = uc $query; #Commands are always all caps
115 $extract = 1 if(! defined $extract);
116 my $name = $self->name;
117 if($query =~ /^$name( (.*))?$/)
118 {
119 if($extract)
120 {
121 return $2;
122 }
123 else
124 {
125 return 1;
126 }
127 }
128 foreach my $syn (@{$self->synonyms})
129 {
130 if($query =~ /^$syn( (.*))?/)
131 {
132 if($extract)
133 {
134 return $2;
135 }
136 else
137 {
138 return 1;
139 }
140 }
141 }
142 return undef;
143 }
144  
145 sub recognize
146 {
147 my $self = shift;
148 my $query = shift;
149 if($self->extract_argument($query, 0))
150 {
151 return 1;
152 }
153 else
154 {
155 return 0;
156 }
157 }
158  
159 sub execute
160 {
161 my $self = shift;
162 my $query = shift;
163 return { status => 1, command => uc $query };
164 }
165  
166 sub print
167 {
168 my $self = shift;
169 return $self->name;
170 }
171  
172 sub IA
173 {
174 my $self = shift;
175 return $self->name;
176 }
177  
178  
179 1;