-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinvalidate.pl
executable file
·73 lines (60 loc) · 1.62 KB
/
invalidate.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
#!/usr/bin/perl
use strict;
use Carp;
use Client;
use DBI;
use Getopt::Long;
use IO::Handle;
use JSON::XS;
use List::Util qw(min max sum first);
use File::Path;
use POSIX qw(strftime);
autoflush STDOUT 1;
autoflush STDERR 1;
my $config_name = "config.json";
my @body_names;
my @types;
my $quiet;
GetOptions(
"config=s" => \$config_name,
"body|b=s" => \@body_names,
"type|name=s" => \@types,
"quiet" => \$quiet,
) or die "$0 --config=foo.json --body=Bar\n";
my $client = Client->new(config => $config_name);
my $empire_name = $client->empire_status->{name};
my $planets = $client->empire_status->{planets};
@body_names = values(%$planets) unless @body_names;
my @body_ids = map { $client->match_planet($_) } @body_names;
if ((@body_ids != @body_names)) {
emit("Aborting due to identification errors", $empire_name);
exit 1;
}
@body_ids = sort { $planets->{$a} cmp $planets->{$b} } @body_ids;
@body_names = map { $planets->{$_} } @body_ids;
@types = qw(buildable buildings) unless @types;
my @valid = qw(
body_status
buildings
buildable
spy_list
spaceport_view_all_ships
session
plans
glyphs
);
@types = map { my $t = $_; $t =~ s/\W//g; first { $_ =~ /$t/ } @valid } @types;
for my $body_id (@body_ids) {
for my $type (@types) {
emit("Invalidating $type", $body_id) if !$quiet;
$client->cache_invalidate(type => $type, id => $body_id);
}
}
sub emit {
my $message = shift;
my $prefix = shift;
$prefix ||= $empire_name;
my $planets = $client->empire_status->{planets};
$prefix = $planets->{$prefix} if $planets->{$prefix};
print Client::format_time(time())." explorer: $prefix: $message\n";
}