blib/lib/BalanceOfPower/Executive.pm untested

filter: Statementstmt 0.0% 0 / 171 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 218 CC 8 SCAR 16.8
CC
8
Cov
0%
decide
20.0
recognize_command
12.0
allowed_orders
6.0
CRAP 5.4
1 package BalanceOfPower::Executive;
2 $BalanceOfPower::Executive::VERSION = '0.300010';
3 use strict;
4 use v5.10;
5  
6 use Moo;
7  
8 with 'BalanceOfPower::Role::Logger';
9  
10 use BalanceOfPower::Constants ":all";
11 use BalanceOfPower::Utils qw(as_active as_title);
12 use BalanceOfPower::Commands::BuildTroops;
13 use BalanceOfPower::Commands::InMilitaryRange;
14 use BalanceOfPower::Commands::DeleteRoute;
15 use BalanceOfPower::Commands::MilitarySupport;
16 use BalanceOfPower::Commands::RebelMilitarySupport;
17 use BalanceOfPower::Commands::RecallRebelMilitarySupport;
18 use BalanceOfPower::Commands::RecallMilitarySupport;
19 use BalanceOfPower::Commands::ComTreaty;
20 use BalanceOfPower::Commands::NagTreaty;
21 use BalanceOfPower::Commands::LowerDisorder;
22 use BalanceOfPower::Commands::BoostProduction;
23 use BalanceOfPower::Commands::AddRoute;
24 use BalanceOfPower::Commands::EconomicAid;
25 use BalanceOfPower::Commands::DeclareWar;
26 use BalanceOfPower::Commands::AidInsurgents;
27 use BalanceOfPower::Commands::DiplomaticPressure;
28 use BalanceOfPower::Commands::MilitaryAid;
29 use BalanceOfPower::Commands::Progress;
30  
31 has actor => (
32 is => 'rw',
33 default => sub { undef }
34 );
35  
36 has commands => (
37 is => 'ro',
38 default => sub { {} }
39 );
40  
41 sub init
42 {
43 my $self = shift;
44 $self->log_name("bop-IA.log");
45 $self->delete_log();
46 $self->log_active(1);
47 my $world = shift;
48 my $command =
49 BalanceOfPower::Commands::BuildTroops->new( name => "BUILD TROOPS",
50 domestic_cost => ARMY_COST,
51 world => $world,
52 allowed_at_war => 1, );
53 $self->commands->{"BUILD TROOPS"} = $command;
54 $command =
55 BalanceOfPower::Commands::LowerDisorder->new( name => "LOWER DISORDER",
56 world => $world,
57 domestic_cost => RESOURCES_FOR_DISORDER );
58 $self->commands->{"LOWER DISORDER"} = $command;
59 $command =
60 BalanceOfPower::Commands::AddRoute->new( name => "ADD ROUTE",
61 world => $world,
62 export_cost => ADDING_TRADEROUTE_COST );
63 $self->commands->{"ADD ROUTE"} = $command;
64 $command =
65 BalanceOfPower::Commands::DeclareWar->new( name => "DECLARE WAR TO",
66 synonyms => ["DECLARE WAR"],
67 world => $world,
68 crisis_needed => 1 );
69 $self->commands->{"DECLARE WAR TO"} = $command;
70 $command =
71 BalanceOfPower::Commands::DeleteRoute->new( name => "DELETE TRADEROUTE",
72 synonyms => ["DELETE ROUTE"],
73 world => $world );
74 $self->commands->{"DELETE TRADEROUTE"} = $command;
75 $command =
76 BalanceOfPower::Commands::BoostProduction->new( name => "BOOST PRODUCTION",
77 world => $world,
78 );
79 $self->commands->{"BOOST PRODUCTION"} = $command;
80 $command =
81 BalanceOfPower::Commands::MilitarySupport->new( name => "MILITARY SUPPORT",
82 world => $world,
83 army_limit => { '>' => ARMY_FOR_SUPPORT }
84 );
85 $self->commands->{"MILITARY SUPPORT"} = $command;
86 $command =
87 BalanceOfPower::Commands::RecallMilitarySupport->new( name => "RECALL MILITARY SUPPORT",
88 synonyms => ["RECALL SUPPORT"],
89 world => $world,
90 allowed_at_war => 1,
91 );
92 $self->commands->{"RECALL MILITARY SUPPORT"} = $command;
93 $command =
94 BalanceOfPower::Commands::AidInsurgents->new( name => "AID INSURGENTS IN",
95 synonyms => ["AID INSURGENTS", "AID INSURGENCE"],
96 world => $world,
97 export_cost => AID_INSURGENTS_COST );
98 $self->commands->{"AID INSURGENTS IN"} = $command;
99 $command =
100 BalanceOfPower::Commands::ComTreaty->new( name => "TREATY COM WITH",
101 synonyms => ["COM TREATY",
102 "COM TREATY WITH",
103 "TREATY COM",
104 ],
105 world => $world,
106 prestige_cost => TREATY_PRESTIGE_COST,
107 treaty_limit => 1,
108 );
109 $self->commands->{"TREATY COM WITH"} = $command;
110 $command =
111 BalanceOfPower::Commands::NagTreaty->new( name => "TREATY NAG WITH",
112 synonyms => ["NAG TREATY",
113 "NAG TREATY WITH",
114 "TREATY NAG"
115 ],
116 world => $world,
117 prestige_cost => TREATY_PRESTIGE_COST,
118 treaty_limit => 1,
119 );
120 $self->commands->{"TREATY NAG WITH"} = $command;
121 $command =
122 BalanceOfPower::Commands::EconomicAid->new( name => "ECONOMIC AID FOR",
123 synonyms => ["ECONOMIC AID"],
124 world => $world,
125 export_cost => ECONOMIC_AID_COST,
126 );
127 $self->commands->{"ECONOMIC AID FOR"} = $command;
128 $command =
129 BalanceOfPower::Commands::RebelMilitarySupport->new( name => "REBEL MILITARY SUPPORT",
130 world => $world,
131 army_limit => { '>' => ARMY_FOR_SUPPORT }
132 );
133 $self->commands->{"REBEL MILITARY SUPPORT"} = $command;
134 $command =
135 BalanceOfPower::Commands::DiplomaticPressure->new( name => "DIPLOMATIC PRESSURE ON",
136 world => $world,
137 prestige_cost => DIPLOMATIC_PRESSURE_PRESTIGE_COST
138 );
139 $self->commands->{"DIPLOMATIC PRESSURE ON"} = $command;
140 $command =
141 BalanceOfPower::Commands::RecallRebelMilitarySupport->new( name => "RECALL REBEL MILITARY SUPPORT",
142 world => $world,
143 );
144 $self->commands->{"RECALL REBEL MILITARY SUPPORT"} = $command;
145 $command =
146 BalanceOfPower::Commands::MilitaryAid->new( name => "MILITARY AID FOR",
147 synonyms => ["MILITARY AID"],
148 world => $world,
149 export_cost => MILITARY_AID_COST,
150 );
151 $self->commands->{"MILITARY AID FOR"} = $command;
152 $command =
153 BalanceOfPower::Commands::Progress->new( name => "PROGRESS",
154 world => $world,
155 domestic_cost => PROGRESS_COST,
156 );
157 $self->commands->{"PROGRESS"} = $command;
158 }
159  
160  
161 sub recognize_command
162 {
163 my $self = shift;
164 my $nation = shift;
165 my $query = shift;
166 my $actor = $self->actor;
167 for(keys %{$self->commands})
168 {
169 my $c = $self->commands->{$_};
170 $c->actor($actor);
171 if($c->recognize($query))
172 {
173 if($c->allowed())
174 {
175 return $c->execute($query, $nation);
176 }
177 else
178 {
179 return { status => -1 };
180 }
181
182 }
183 }
184 return { status => 0 };
185 }
186  
187 sub decide
188 {
189 my $self = shift;
190 my $order = shift;
191 if(! exists $self->commands->{$order})
192 {
193 $self->log($self->actor . ": $order doesn't exists!");
194 return undef;
195 }
196 my $c = $self->commands->{$order};
197 $c->actor($self->actor);
198 if($c->allowed())
199 {
200 my $command = $c->IA();
201 my $exit_log = $command ? $command : "KO";
202 $self->log($self->actor . ": executing $order: $exit_log");
203 return $command;
204 }
205 else
206 {
207 $self->log($self->actor . ": $order not allowed");
208 return undef;
209 }
210 }
211  
212 sub allowed_orders
213 {
214 my $self = shift;
215 my @out;
216 for(keys %{$self->commands})
217 {
218 my $c = $self->commands->{$_};
219 $c->actor($self->actor);
220 if( $c->allowed() )
221 {
222 push @out, $c->print;
223 }
224 }
225 return @out;
226 }
227  
228 sub print_orders
229 {
230 my $self = shift;
231 my $out = as_title("ORDERS FOR " . $self->actor);
232 $out .= "\n\n";
233 for(keys %{$self->commands})
234 {
235 my $c = $self->commands->{$_};
236 $c->actor($self->actor);
237 if( $c->allowed() )
238 {
239 $out .= as_active($c->print) . "\n";
240 }
241 else
242 {
243 $out .= $c->print . "\n";
244 }
245 }
246 return $out;
247 }
248  
249 1;