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

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 133 CC 19 SCAR 45.1
CC
19
Cov
0%
allowed
182.0
extract_argument
42.0
recognize
6.0
CRAP 91.2
1 package BalanceOfPower::Commands::Role::Command;
2 $BalanceOfPower::Commands::Role::Command::VERSION = '0.300010';
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 has_argument
53 {
54 return 1;
55 }
56  
57 sub get_nation
58 {
59 my $self = shift;
60 return $self->world->get_nation($self->actor);
61 }
62  
63 sub allowed
64 {
65 my $self = shift;
66 my $nation = $self->get_nation();
67 return 0
68 if($nation->internal_disorder_status() eq 'Civil war');
69 if(! $self->allowed_at_war)
70 {
71 if($self->world->at_war($self->actor))
72 {
73 return 0;
74 }
75 }
76 if(exists $self->production_limit->{'<'})
77 {
78 return $nation->production() <= $self->production_limit->{'<'};
79 }
80 elsif(exists $self->production_limit->{'>'})
81 {
82 return $nation->production() >= $self->production_limit->{'>'};
83 }
84 if(exists $self->army_limit->{'<'})
85 {
86 return $nation->army() <= $self->army_limit->{'<'};
87 }
88 elsif(exists $self->army_limit->{'>'})
89 {
90 return $nation->army() >= $self->army_limit->{'>'};
91 }
92 if($nation->production_for_domestic < $self->domestic_cost)
93 {
94 return 0;
95 }
96 if($nation->production_for_export < $self->export_cost)
97 {
98 return 0;
99 }
100 if($nation->prestige < $self->prestige_cost)
101 {
102 return 0;
103 }
104 if($self->treaty_limit == 1)
105 {
106 if($self->world->get_treaties_for_nation($nation->name) >= $nation->treaty_limit)
107 {
108 return 0;
109 }
110 }
111 return 1;
112 }
113  
114 sub extract_argument
115 {
116 my $self = shift;
117 my $query = shift;
118 my $extract = shift;
119 $query = uc $query; #Commands are always all caps
120 $extract = 1 if(! defined $extract);
121 my $name = $self->name;
122 if($query =~ /^$name( (.*))?$/)
123 {
124 if($extract)
125 {
126 return $2;
127 }
128 else
129 {
130 return 1;
131 }
132 }
133 foreach my $syn (@{$self->synonyms})
134 {
135 if($query =~ /^$syn( (.*))?/)
136 {
137 if($extract)
138 {
139 return $2;
140 }
141 else
142 {
143 return 1;
144 }
145 }
146 }
147 return undef;
148 }
149  
150 sub recognize
151 {
152 my $self = shift;
153 my $query = shift;
154 if($self->extract_argument($query, 0))
155 {
156 return 1;
157 }
158 else
159 {
160 return 0;
161 }
162 }
163  
164 sub execute
165 {
166 my $self = shift;
167 my $query = shift;
168 return { status => 1, command => uc $query };
169 }
170  
171 sub print
172 {
173 my $self = shift;
174 return $self->name;
175 }
176  
177 sub IA
178 {
179 my $self = shift;
180 return $self->name;
181 }
182  
183  
184 1;