-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_buildable_info.pl
executable file
·75 lines (66 loc) · 2.45 KB
/
get_buildable_info.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
68
69
70
71
72
73
74
75
#!/usr/bin/perl
use strict;
use Client;
use Getopt::Long;
use JSON::PP;
my $config_name = "config.json";
my $body_name;
GetOptions(
"config=s" => \$config_name,
"body=s" => \$body_name,
) or die "$0 --config=foo.json --body=Bar\n";
my $client = Client->new(config => $config_name);
my $body_id;
if ($body_name) {
my $planets = $client->empire_status->{planets};
for my $id (keys(%$planets)) {
$body_id = $id if $planets->{$id} =~ /$body_name/;
}
die "No matching planet for name $body_name\n" unless $body_id;
} else {
$body_id = $client->empire_status->{home_planet_id};
}
my %buildings;
my $result = $client->body_buildable($body_id);
for my $name (keys %{$result->{buildable}}) {
my $building = $result->{buildable}{$name};
$buildings{$name}{1}{cost} = $building->{build}{cost};
my %production;
my %capacity;
for my $resource (qw(food ore water energy waste happiness)) {
$production{$resource} = $building->{production}{"${resource}_hour"};
$capacity{$resource} = $building->{production}{"${resource}_capacity"};
}
$buildings{$name}{1}{production} = { %production };
$buildings{$name}{1}{capacity} = { %capacity };
}
my $result = $client->body_buildings($body_id);
for my $id (keys %{$result->{buildings}}) {
my $name = $result->{buildings}{$id}{name};
my $url = $result->{buildings}{$id}{url};
next if $buildings{$name}{2};
for my $level (1..10) {
warn "Fetching stats for $level $name\n";
my $info = $client->building_stats_for_level($url, $id, $level);
unless ($buildings{$name}{$level}{production}) {
my %production;
my %capacity;
for my $resource (qw(food ore water energy waste happiness)) {
$production{$resource} = $info->{building}{"${resource}_hour"};
$capacity{$resource} = $info->{building}{"${resource}_capacity"};
}
$buildings{$name}{$level}{production} = { %production };
$buildings{$name}{$level}{capacity} = { %capacity };
}
my %production;
my %capacity;
for my $resource (qw(food ore water energy waste happiness)) {
$production{$resource} = $info->{building}{upgrade}{production}{"${resource}_hour"};
$capacity{$resource} = $info->{building}{upgrade}{production}{"${resource}_capacity"};
}
$buildings{$name}{$level}{upgrade} = $info->{building}{upgrade}{cost};
$buildings{$name}{$level+1}{production} = { %production };
$buildings{$name}{$level+1}{capacity} = { %capacity };
}
}
print encode_json({%buildings})."\n";