-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathentertainment.pl
executable file
·67 lines (58 loc) · 1.99 KB
/
entertainment.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
#!/usr/bin/perl
use strict;
use Client;
use Getopt::Long;
use JSON::PP;
use List::Util;
my $config_name = "config.json";
my $sleep = 1;
GetOptions(
"config=s" => \$config_name,
"sleep!" => \$sleep,
"all-links" => \my $all_links,
) or die "$0 --config=foo.json --body=Bar\n";
sleep(900 + rand() * 900) if $sleep;
my $client = Client->new(config => $config_name);
my $planets = $client->empire_status->{planets};
my %zones;
for my $id (List::Util::shuffle(keys(%$planets))) {
emit("cheking buildings on planet $id");
my $buildings = $client->body_buildings($id);
my @buildings = map { { %{$buildings->{buildings}{$_}}, id => $_, body_id => $id } } keys(%{$buildings->{buildings}});
my $ed = (grep($_->{name} eq "Entertainment District", @buildings))[0];
if ($ed) {
emit("found an entertainment district in zone $buildings->{status}{body}{zone}\n");
$zones{$buildings->{status}{body}{zone}} = $ed;
}
}
my $tries = 3;
for my $ed (List::Util::shuffle(values(%zones))) {
my $result;
do {
$result = eval { $client->call(entertainment => get_lottery_voting_options => $ed->{id}) };
if ($tries > 0 && $result && !@{$result->{options}}) {
emit("No entertainment links, sleeping", $ed->{body_id});
sleep(900 + rand() * 900);
}
} while ($tries-- > 0 && (!$result || !@{$result->{options}}));
if ($result) {
emit("Got ".scalar(@{$result->{options}})." entertainment links", $ed->{body_id});
my @links = ($all_links) ? @{$result->{options}} : ((List::Util::shuffle(@{$result->{options}}))[0]);
for my $link ( @links ) {
if ($link) {
emit("Visiting $link->{name} at $link->{url}", $ed->{body_id});
`GET '$link->{url}'`;
sleep(10 + rand() * 15);
}
}
}
else {
emit("I did not get any results for lottery voting options: $!\n");
}
}
sub emit {
my $message = shift;
my $body_id = shift;
my $body_name = $planets->{$body_id};
print Client::format_time(time())." entertainment: $body_name: $message\n";
}