-
Notifications
You must be signed in to change notification settings - Fork 48
/
happiness_report.pl
executable file
·67 lines (54 loc) · 1.67 KB
/
happiness_report.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Number::Format qw( format_number );
use List::Util qw( max );
use Games::Lacuna::Client ();
my $cfg_file = shift(@ARGV) || 'lacuna.yml';
unless ( $cfg_file and -e $cfg_file ) {
$cfg_file = eval{
require File::HomeDir;
require File::Spec;
my $dist = File::HomeDir->my_dist_config('Games-Lacuna-Client');
File::Spec->catfile(
$dist,
'login.yml'
) if $dist;
};
unless ( $cfg_file and -e $cfg_file ) {
die "Did not provide a config file";
}
}
my $client = Games::Lacuna::Client->new(
cfg_file => $cfg_file,
# debug => 1,
);
# Load the planets
my $empire = $client->empire->get_status->{empire};
# reverse hash, to key by name instead of id
my %planets = reverse %{ $empire->{planets} };
my $max_length = max map { length } keys %planets;
my @results;
# Scan each planet
foreach my $name ( sort keys %planets ) {
# Load planet data
my $planet = $client->body( id => $planets{$name} );
my $body = $planet->get_status->{body};
next if $body->{type} eq 'space station';
push @results, {
name => $name,
happy => format_number( $body->{happiness} ),
happyhour => format_number( $body->{happiness_hour} ),
};
}
my $max_name = max map { length $_->{name} } @results;
my $max_happy = max map { length $_->{happy} } @results;
my $max_happyhour = max map { length $_->{happyhour} } @results;
for my $planet (@results) {
printf "%${max_name}s: %${max_happy}s @ %${max_happyhour}s/hr\n",
$planet->{name},
$planet->{happy},
$planet->{happyhour};
}