Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 101 lines (86 sloc) 2.893 kb
63c5d3f Adding in a bunch of scripts that I didn't realize I wasn't sharing
T. Alexander Popiel authored
1 #!/usr/bin/perl
2
3 use strict;
4
5 use Carp;
6 use Client;
7 use Getopt::Long;
8 use IO::Handle;
9 use JSON::PP;
10 use List::Util qw(first min max sum);
11
12 autoflush STDOUT 1;
13 autoflush STDERR 1;
14
15 my $config_name = "config.json";
16 my @body_names;
17 my $ship_name;
18 my $equalize = 0;
19 my $themepark = 0;
20 my $debug = 0;
21 my $quiet = 0;
22 my $noaction = 0;
23 my $do_waste = 1;
24 my $use_chain = 0;
25
26 GetOptions(
27 "config=s" => \$config_name,
28 "body=s" => \@body_names,
29 "ship|name=s" => \$ship_name,
30 "debug" => \$debug,
31 "quiet" => \$quiet,
32 "noaction!" => \$noaction,
33 "waste!" => \$do_waste,
34 "use_chain|chain!" => \$use_chain,
35 ) or die "$0 --config=foo.json --body=Bar\n";
36
37 die "Must specify at least two bodies\n" unless @body_names >= 2;
38 $ship_name ||= join(" ", @body_names);
39
40 my $client = Client->new(config => $config_name);
41 my $planets = $client->empire_status->{planets};
42
43 my @order;
44 my %plan_count;
45 my $plan_type;
46 my $size_per_plan;
47 for my $name (@body_names) {
48 my $body_id = first { $planets->{$_} =~ /$name/ } keys %$planets;
49 die "No matching planet for name $name\n" unless $body_id;
50 push(@order, $body_id);
51 my $plans = $client->body_plans($body_id);
52 my $halls = first { $_->{name} eq "Halls of Vrbansk" } @{$plans->{plans}};
53 $plan_count{$body_id} = $halls ? $halls->{quantity} : 0;
54 $plan_type ||= $halls->{plan_type};
55 $size_per_plan = $plans->{cargo_space_used_each};
56 }
57
58 my $average = int(sum(values %plan_count) / @order);
59
60 for my $j (0..$#order) {
61 my $body_id = $order[$j];
62 my $next_id = $order[($j + 1) % @order];
63
64 my $amount = $plan_count{$body_id} - $average;
65 next if $amount <= 0;
66
67 my @ships = sort { $b->{hold_size} <=> $a->{hold_size} }
68 grep { $_->{name} =~ /$ship_name/ && $_->{task} eq "Docked" }
69 @{$client->port_all_ships($body_id)->{ships}};
70 die "No ships available for name $ship_name on planet $planets->{$body_id}\n" unless @ships;
71 for my $ship (@ships) {
72 last if $amount <= 0;
73 my $actual = min(int($ship->{hold_size} / $size_per_plan), $amount);
74 emit(($noaction ? "Would send" : "Sending")." $actual halls to $planets->{$next_id} on $ship->{name}", $planets->{$body_id});
75 eval {
76 $client->trade_push($client->find_building($body_id, "Trade Ministry")->{id}, $next_id,
77 [ { type => "plan",
78 plan_type => $plan_type,
79 level => 1,
80 extra_build_level => 0,
81 quantity => $actual
82 } ], { ship_id => $ship->{id} }) unless $noaction;
83 };
84 $amount -= $actual;
85 }
86 }
87
88 sub emit {
89 my $message = shift;
90 my $name = shift;
91 print Client::format_time(time())." $name: $message\n";
92 }
93
94 sub emit_json {
95 return unless $debug;
96 my $message = shift;
97 my $hash = shift;
98 print Client::format_time(time())." $message:\n";
99 print JSON::PP->new->allow_nonref->canonical->pretty->encode($hash);
100 }
Something went wrong with that request. Please try again.