/
push_ships.pl
executable file
·232 lines (178 loc) · 6.02 KB
/
push_ships.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use List::Util (qw(first));
use Games::Lacuna::Client ();
use Getopt::Long (qw(GetOptions));
use POSIX (qw(floor));
my $cfg_file;
if ( @ARGV && $ARGV[0] !~ /^--/) {
$cfg_file = shift @ARGV;
}
else {
$cfg_file = '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 $from;
my $to;
my $push_type;
my $trade_type;
my $min_push = 1;
my $fastest;
my $max_push;
my $ship_name;
my $stay = 0;
GetOptions(
'from=s' => \$from,
'to=s' => \$to,
'push_type=s' => \$push_type,
'trade_type=s' => \$trade_type,
'ship_name=s' => \$ship_name,
'min_push=i' => \$min_push,
'max_push=i' => \$max_push,
'fastest' => \$fastest,
'stay' => \$stay,
);
usage() if !$from || !$to || !$push_type;
my $client = Games::Lacuna::Client->new(
cfg_file => $cfg_file,
#debug => 1,
);
my $empire = $client->empire->get_status->{empire};
my $planets = $empire->{planets};
# reverse hash, to key by name instead of id
my %planets_by_name = map { $planets->{$_}, $_ } keys %$planets;
my $to_id = $planets_by_name{$to}
or die "--to planet not found";
# Load planet data
my $body = $client->body( id => $planets_by_name{$from} );
my $buildings = $body->get_buildings->{buildings};
# Check dock space on target planet
my $to_dock_count;
{
my $to_body = $client->body( id => $planets_by_name{$to} );
my $to_buildings = $to_body->get_buildings->{buildings};
my $space_port_id = first {
$to_buildings->{$_}->{url} eq '/spaceport'
}
grep { $to_buildings->{$_}->{level} > 0 and $to_buildings->{$_}->{efficiency} == 100 }
keys %$to_buildings;
die "No spaceport found on target planet\n"
if !$space_port_id;
my $space_port = $client->building( id => $space_port_id, type => 'SpacePort' );
$to_dock_count = $space_port->view->{docks_available};
die "No docks available in target SpacePort\n"
if !$to_dock_count;
}
die "Fewer free docks in target SpacePort than --min_push value\n"
if $min_push > $to_dock_count;
# Find the TradeMin
my $trade_min_id = first {
$buildings->{$_}->{name} eq 'Trade Ministry'
} keys %$buildings;
my $trade_min = $client->building( id => $trade_min_id, type => 'Trade' );
my @trade_ships = @{ $trade_min->get_trade_ships($to_id)->{ships} };
if ($trade_type) {
@trade_ships = grep {
$_->{type} =~ m/$trade_type/
} @trade_ships;
}
if ($ship_name) {
@trade_ships = grep {
$_->{name} =~ m/$ship_name/i
} @trade_ships;
}
my $get_ships_result = $trade_min->get_ships;
my $hold_required = $get_ships_result->{cargo_space_used_each};
@trade_ships = grep {
$_->{hold_size} >= $min_push*$hold_required;
} @trade_ships;
die "No available ships to push with"
if !@trade_ships;
@trade_ships = sort {
$fastest ? $b->{speed} <=> $a->{speed} : $b->{hold_size} <=> $a->{hold_size}
} @trade_ships;
my @push_ships = @{ $get_ships_result->{ships} };
@push_ships = grep {
$_->{type} =~ m/$push_type/;
} @push_ships;
die "No ships available to be pushed\n"
if !@push_ships;
die "Less than --min_push ships available to be pushed\n"
if $min_push > scalar @push_ships;
if ( $max_push && scalar @push_ships > $max_push) {
splice @push_ships, $max_push;
}
for my $trade_ship (@trade_ships) {
last if $min_push > scalar @push_ships;
last if $min_push > $to_dock_count;
last if $to_dock_count < 1;
my $max_ships = floor( $trade_ship->{hold_size} / $hold_required );
my $ship_count = $max_ships > scalar @push_ships ? scalar @push_ships
: $max_ships;
$ship_count = $to_dock_count
if $ship_count > $to_dock_count;
my @push_ships = splice @push_ships, 0, $ship_count;
my @items;
for my $push_ship (@push_ships) {
push @items, {
type => 'ship',
ship_id => $push_ship->{id},
};
}
my $return = $trade_min->push_items(
$to_id,
\@items,
{
ship_id => $trade_ship->{id},
stay => $stay,
},
);
printf "Pushed %d: %s; ", scalar @push_ships, $push_ships[0]->{name};
printf "Using '%s', arriving %s\n", $return->{ship}{name}, $return->{ship}{date_arrives};
$to_dock_count -= $ship_count;
}
exit;
sub usage {
die <<"END_USAGE";
Usage: $0 CONFIG_FILE
--from PLANET_NAME
--to PLANET_NAME
--push_type SHIP_TYPE
--trade_type SHIP_TYPE
--min_push MIN_PUSH
--max_push MAX_PUSH
--ship_name SHIP_NAME
--fastest
--largest
CONFIG_FILE defaults to 'lacuna.yml'
--push_type SHIP_TYPE is a regex used to decide which ships to push. Required.
If this is set to a trade-type ship, then you must also set --trade_type to a
different type of ship, so that a ship doesn't try pushing itself.
--trade_type SHIP_TYPE is a regex used to decide which ships to use to push the
selected ships.
By default, is not set, so all trade ships are candidates.
MIN_PUSH is the minimum number of matching ships that are required per-push.
Each pushing-ship requires a 50K cargo-hold per ship being pushed.
Defaults to 1, so any trade ship above 50K is a candidate.
MAX_PUSH is the maximum number of matching ships that will be pushed.
SHIP_NAME is the name of the ship to be used. Partial matches are fine.
--fastest or --largest may be set, to decide which ships to use to push.
If neither is set, --largest will be the default behaviour.
END_USAGE
}