/
get_buildings.pl
executable file
·160 lines (140 loc) · 4 KB
/
get_buildings.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env perl
#
# A program that just spits out all buildings with location
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Games::Lacuna::Client;
use Getopt::Long qw(GetOptions);
use JSON;
use Exception::Class;
my %opts = (
h => 0,
v => 0,
config => "lacuna.yml",
dumpfile => "data/data_builds.js",
layout => 0,
station => 0,
sleep => 2,
shipyard => 0,
shipfile => "data/shipyards.js",
layfile => "data/layout.js",
);
GetOptions(\%opts,
'h|help',
'v|verbose',
'planet=s@',
'config=s',
'dumpfile=s',
'layout',
'layfile=s',
'sleep',
'station',
'shipyard',
'shipfile=s',
);
usage() if $opts{'h'};
my $glc = Games::Lacuna::Client->new(
cfg_file => $opts{'config'} || "lacuna.yml",
rpc_sleep => $opts{'sleep'},
# debug => 1,
);
my $json = JSON->new->utf8(1);
$json = $json->pretty([1]);
$json = $json->canonical([1]);
if ($opts{shipyard} ne '0') {
open(OUTPUT, ">", $opts{'shipfile'}) || die "Could not open $opts{'shipfile'}";
}
else {
open(OUTPUT, ">", $opts{'dumpfile'}) || die "Could not open $opts{'dumpfile'}";
}
if ($opts{layout}) {
open(LAYOUT, ">", $opts{'layfile'}) || die "Could not open $opts{'layfile'}";
}
my $status;
my $layout;
my $empire = $glc->empire->get_status->{empire};
# Get planets
my %planets = map { $empire->{planets}{$_}, $_ } keys %{$empire->{planets}};
$status->{planets} = \%planets;
for my $pname (keys %planets) {
next if ($opts{planet} and not (grep { $pname eq $_ } @{$opts{planet}}));
verbose("Inspecting $pname\n");
my $planet = $glc->body(id => $planets{$pname});
my $result = $planet->get_buildings;
# if ($result->{status}{body}{type} eq 'space station' && !$opts{'station'}) {
# verbose("Skipping Space Station: $pname\n");
# next;
# }
my $buildings = $result->{buildings};
my @keys = (keys %$buildings);
my @layout;
for my $bldid (@keys) {
$buildings->{$bldid}->{leveled} = $buildings->{$bldid}->{level};
if ($opts{shipyard}) {
if ( $buildings->{$bldid}->{name} ne 'Shipyard' ) {
delete $buildings->{$bldid};
}
else {
$buildings->{$bldid}->{maxq} = $buildings->{$bldid}->{level};
$buildings->{$bldid}->{reserve} = 10;
}
}
if ($opts{layout}) {
push @layout, { id => $bldid,
name => $buildings->{$bldid}->{name},
x => $buildings->{$bldid}->{x},
y => $buildings->{$bldid}->{y},
};
}
}
if ($opts{layout}) {
@layout = sort {$a->{x} <=> $b->{x} || $a->{y} <=> $b->{y}} @layout;
$layout->{$pname} = \@layout;
}
$status->{$pname} = $buildings;
}
if ($opts{layout}) {
print LAYOUT $json->pretty->canonical->encode($layout);
close(LAYOUT);
}
print OUTPUT $json->pretty->canonical->encode($status);
close(OUTPUT);
exit;
sub usage {
diag(<<END);
Usage: $0 [options]
This program just gets an inventory of the buildings on your planets.
Use parse_building.pl to output a csv of the file.
leveled is a field inserted for use by an autobuild program. (still being developed)
Options:
--help - This info.
--verbose - Print out more information
--config <file> - Specify a GLC config file, normally lacuna.yml.
--planet <name> - Specify planet
--dumpfile - data dump for all the info we don't print
--station - include space stations in listing
--shipyard - instead, output a shipyard file for use of build_ships.pl
--shipfile - Default shipyards.js
END
exit 1;
}
sub verbose {
return unless $opts{v};
print @_;
}
sub output {
return if $opts{q};
print @_;
}
sub diag {
my ($msg) = @_;
print STDERR $msg;
}
sub normalize_planet {
my ($pname) = @_;
$pname =~ s/\W//g;
$pname = lc($pname);
return $pname;
}