File Coverage

blib/lib/Games/Lacuna/Task/Action/EmpireFind.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Games::Lacuna::Task::Action::EmpireFind;
2              
3 1     1   1406 use 5.010;
  1         4  
  1         59  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   448 use Moose -traits => 'NoAutomatic';
  0            
  0            
7             extends qw(Games::Lacuna::Task::Action);
8             with qw(Games::Lacuna::Task::Role::Stars);
9              
10             use Games::Lacuna::Task::Utils qw(normalize_name format_date);
11             use Games::Lacuna::Task::Table;
12              
13             has 'empire' => (
14             is => 'rw',
15             isa => 'ArrayRef[Str]',
16             required => 1,
17             documentation => 'Empire name you are looking for [Required, Multiple]',
18             );
19              
20             sub description {
21             return q[Get all available data for a given empire];
22             }
23              
24             sub run {
25             my ($self) = @_;
26            
27             my (@query_parts,@query_params);
28             foreach my $empire (@{$self->empire}) {
29             push(@query_parts,'name = ?');
30             push(@query_parts,'normalized_name = ?');
31             push(@query_params,$empire);
32             push(@query_params,normalize_name($empire));
33             }
34            
35             my $sth_empire = $self->storage_prepare('SELECT
36             id,
37             name,
38             alignment,
39             is_isolationist,
40             alliance,
41             colony_count,
42             level,
43             date_founded,
44             affinity,
45             last_checked
46             FROM empire
47             WHERE '.join(' OR ',@query_parts));
48            
49             my $found = 0;
50             $sth_empire->execute(@query_params);
51            
52             while (my $empire = $sth_empire->fetchrow_hashref) {
53             $empire->{affinity} = $Games::Lacuna::Task::Storage::JSON->decode($empire->{affinity})
54             if defined $empire->{affinity};
55             $self->empire_info($empire);
56             $found++;
57             }
58            
59             $self->abort('No empires found')
60             unless $found;
61             }
62              
63             sub empire_info {
64             my ($self,$empire) = @_;
65            
66             say "-" x $Games::Lacuna::Task::Constants::SCREEN_WIDTH;
67             say "Empire ".$empire->{name};
68             say "-" x $Games::Lacuna::Task::Constants::SCREEN_WIDTH;
69             unless (defined $empire->{colony_count}) {
70             say "No empire information available";
71             say "Please run 'lacuna_run empire_cache' first";
72             } else {
73             say "Alliance: ".($empire->{alliance} ? 'Yes':'No');
74             if ($empire->{alliance}) {
75             my ($alliance_size,$alliance_avg_level,$alliance_max_level) = $self->client->storage_selectrow_array('SELECT
76             COUNT(1),AVG(level),MAX(level)
77             FROM empire
78             WHERE alliance = ?',$empire->{alliance});
79             say "Alliance size: $alliance_size";
80             say "Alliance level: $alliance_avg_level(avg) / $alliance_max_level(max)";
81             }
82             say "Alignment: ".$empire->{alignment};
83             say "Is isolationist: ".($empire->{is_isolationist} ? 'Yes':'No');
84             say "Colony count: ".$empire->{colony_count};
85             say "Level: ".$empire->{level};
86             say "Date founded: ".format_date($empire->{date_founded});
87             }
88             say "";
89             $self->empire_affinity($empire);
90             $self->empire_body($empire);
91            
92             }
93              
94             sub empire_affinity {
95             my ($self,$empire) = @_;
96            
97             return
98             unless defined $empire->{affinity};
99            
100             my $my_affinity = $self->my_affinity;
101            
102             my $table = Games::Lacuna::Task::Table->new({
103             headline => 'Affinity report',
104             columns => ['Affinity','Level','My Level','Delta'],
105             });
106            
107             while (my ($affinity,$level) = each %{$empire->{affinity}}) {
108             next
109             if $affinity eq 'name' || $affinity eq 'description';
110             my $label = $affinity;
111             $label =~ s/_affinity$//;
112             $table->add_row({
113             affinity => $label,
114             level => $level,
115             my_level => $my_affinity->{$affinity},
116             delta => $level - $my_affinity->{$affinity},
117             });
118             }
119            
120             say $table->render_text;
121             }
122              
123             sub empire_body {
124             my ($self,$empire) = @_;
125            
126             my $planet_stats = $self->my_body_status($self->home_planet_id);
127            
128             my $sth_body = $self->storage_prepare('SELECT
129             body.id,
130             body.x,
131             body.y,
132             body.orbit,
133             body.size,
134             body.name,
135             body.type,
136             body.empire,
137             star.name AS star,
138             distance_func(body.x,body.y,?,?) AS distance
139             FROM body
140             INNER JOIN star ON (body.star = star.id)
141             WHERE empire = ?
142             ORDER BY distance ASC');
143            
144             $sth_body->execute($planet_stats->{x},$planet_stats->{y},$empire->{id});
145            
146             my $table = Games::Lacuna::Task::Table->new({
147             headline => 'Body report',
148             columns => ['Name','X','Y','Type','Orbit','Size','Star','Distance'],
149             });
150            
151             while (my $body = $sth_body->fetchrow_hashref) {
152             $table->add_row({
153             (map { ($_ => $body->{$_}) } qw(name x y orbit type orbit size star distance)),
154             });
155             }
156            
157             say $table->render_text;
158             }
159              
160             __PACKAGE__->meta->make_immutable;
161             no Moose;
162             1;