forked from popiel/Lacuna-Automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevacuate.pl
executable file
·140 lines (119 loc) · 4.02 KB
/
evacuate.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
#!/usr/bin/perl
use strict;
use Carp;
use Client;
use Getopt::Long;
use IO::Handle;
use JSON::PP;
use List::Util;
autoflush STDOUT 1;
autoflush STDERR 1;
my $config_name = "config.json";
my @body_name;
my $ship_name;
my $stay = 0;
my $debug = 0;
my $quiet = 0;
my $do_plans;
my $do_glyphs;
my $do_tyleon;
my $do_sculpture;
GetOptions(
"config=s" => \$config_name,
"body=s" => \@body_name,
"stay!" => \$stay,
"debug" => \$debug,
"glyphs!" => \$do_glyphs,
"plans!" => \$do_plans,
"tyleon!" => \$do_tyleon,
"sculpture!" => \$do_sculpture,
"quiet" => \$quiet,
) or die "$0 --config=foo.json --body=Bar\n";
if (!$do_glyphs && !$do_plans && !$do_tyleon) {
$do_glyphs = 1 unless defined $do_glyphs;
$do_plans = 1 unless defined $do_plans;
$do_tyleon = 1 unless defined $do_tyleon;
$do_sculpture = 1 unless defined $do_sculpture;
}
if (defined($do_plans) && !defined($do_tyleon)) {
$do_tyleon = $do_plans;
}
if (defined($do_plans) && !defined($do_sculpture)) {
$do_sculpture = $do_plans;
}
die "Must specify two bodies\n" unless @body_name == 2;
$ship_name ||= join(" ", @body_name);
my $client = Client->new(config => $config_name);
my $planets = $client->empire_status->{planets};
my @body_id;
for my $id (keys(%$planets)) {
$body_id[0] = $id if $planets->{$id} =~ /$body_name[0]/;
$body_id[1] = $id if $planets->{$id} =~ /$body_name[1]/;
}
exit(1) if !$debug && $quiet && (!$body_id[0] || !$body_id[1]);
die "No matching planet for name $body_name[0]\n" unless $body_id[0];
die "No matching planet for name $body_name[1]\n" unless $body_id[1];
# get trade ministries, space ports, and ships for each planet
my @trade;
my @port;
for my $body_id (@body_id) {
$debug and print "Looking at $planets->{$body_id}\n";
my $buildings = $client->body_buildings($body_id);
my @buildings = map { { %{$buildings->{buildings}{$_}}, id => $_ } } keys(%{$buildings->{buildings}});
my $trade = (grep($_->{name} eq "Trade Ministry", @buildings))[0];
my $port = (grep($_->{name} eq "Space Port", @buildings))[0];
die "No Trade Ministry on $planets->{$body_id}\n" unless $trade;
die "No Space Port ". "on $planets->{$body_id}\n" unless $port;
$debug and print "Got trade $trade->{id}, port $port->{id}\n";
push(@trade, $trade);
push(@port, $port);
}
my @plans;
my @glyphs;
my $plans;
my $glyphs;
if ($do_plans || $do_tyleon || $do_sculpture) {
$plans = $client->call(trade => get_plans => $trade[0]{id});
@plans = @{$plans->{plans}};
@plans = grep { $_->{name} ne "Halls of Vrbansk" } @plans;
@plans = grep { $_->{name} !~ /Tyleon/ } @plans unless $do_tyleon;
@plans = grep { $_->{name} !~ /Sculpture/ } @plans unless $do_sculpture;
@plans = grep { $_->{name} =~ /Tyleon|Sculpture/ } @plans unless $do_plans;
}
if ($do_glyphs) {
$glyphs = $client->call(trade => get_glyphs => $trade[0]{id});
@glyphs = @{$glyphs->{glyphs}};
}
exit(0) unless @plans || @glyphs;
my $ships = $client->call(trade => get_trade_ships => $trade[0]{id}, $body_id[1]);
my @ships = grep($_->{name} !~ /(Alpha|Beta)$/ && $_->{task} eq "Docked" && $_->{hold_size} > 10, @{$ships->{ships}});
for my $ship (@ships) {
my $space = $ship->{hold_size};
my @items;
my $pc = 0;
my $gc = 0;
while (@plans && $space > $plans->{cargo_space_used_each}) {
my $plan = shift @plans;
push(@items, { type => "plan", plan_id => $plan->{id} });
$space -= $plans->{cargo_space_used_each};
$pc++;
}
while (@glyphs && $space > $glyphs->{cargo_space_used_each}) {
my $glyph = shift @glyphs;
push(@items, { type => "glyph", glyph_id => $glyph->{id} });
$space -= $glyphs->{cargo_space_used_each};
$gc++;
}
if (@items) {
emit("Sending $pc plans and $gc glyphs to $planets->{$body_id[1]} on $ship->{name}", $planets->{$body_id[0]});
my $result = $client->trade_push(
$trade[0]->{id}, $body_id[1], [ @items ],
{ ship_id => $ship->{id}, stay => $stay }
);
}
}
sub emit {
my $message = shift;
my $name = shift;
print Client::format_time(time())." $name: $message\n";
}