line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::Lacuna::Task::Action::EmpireCache; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1647
|
use 5.010; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
68
|
|
4
|
|
|
|
|
|
|
our $VERSION = $Games::Lacuna::Task::VERSION; |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
512
|
use Moose -traits => 'NoAutomatic'; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
extends qw(Games::Lacuna::Task::Action); |
8
|
|
|
|
|
|
|
with qw(Games::Lacuna::Task::Role::Stars); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Games::Lacuna::Task::Utils qw(normalize_name parse_date); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $MAX_EMPIRE_CACHE_TIME = 60 * 60 * 24 * 31 * 3; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub description { |
15
|
|
|
|
|
|
|
return q[Build the empire info cache]; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub run { |
19
|
|
|
|
|
|
|
my ($self) = @_; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$self->query_empire_stats(); |
22
|
|
|
|
|
|
|
$self->query_species_affinity(); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub query_species_affinity { |
26
|
|
|
|
|
|
|
my ($self) = @_; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
foreach my $planet_stats ($self->my_planets) { |
29
|
|
|
|
|
|
|
my $library = $self->find_building($planet_stats,'LibraryOfJith'); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
next |
32
|
|
|
|
|
|
|
unless defined $library; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$self->log('debug','Found Library of Jith at %s',$planet_stats->{name}); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
return $self->query_library($library); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$self->log('debug','Could not find Library of Jith to query species affinity'); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub query_library { |
43
|
|
|
|
|
|
|
my ($self,$library) = @_; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $library_object = $self->build_object($library); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$self->log('info','Fetching species affinities'); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $sth = $self->storage_prepare('SELECT id,name |
50
|
|
|
|
|
|
|
FROM empire |
51
|
|
|
|
|
|
|
WHERE affinity IS NULL |
52
|
|
|
|
|
|
|
OR last_checked < ?'); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
$sth->execute(time - $MAX_EMPIRE_CACHE_TIME); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
while (my ($id,$name) = $sth->fetchrow_array) { |
57
|
|
|
|
|
|
|
my $response = $self->request( |
58
|
|
|
|
|
|
|
object => $library_object, |
59
|
|
|
|
|
|
|
method => 'research_species', |
60
|
|
|
|
|
|
|
params => [$id], |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
unless (defined $response->{species}) { |
64
|
|
|
|
|
|
|
$self->log('warn','Empire %s not found',$name); |
65
|
|
|
|
|
|
|
$self->remove_empire($id); |
66
|
|
|
|
|
|
|
next; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$self->storage_do('UPDATE empire SET affinity = ? WHERE id = ?',$response->{species},$id); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub query_empire_stats { |
74
|
|
|
|
|
|
|
my ($self) = @_; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
$self->log('info','Fetching empire stats'); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $sth = $self->storage_prepare('SELECT id,name,level |
79
|
|
|
|
|
|
|
FROM empire |
80
|
|
|
|
|
|
|
WHERE last_checked IS NULL |
81
|
|
|
|
|
|
|
OR last_checked < ?'); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$sth->execute(time - $MAX_EMPIRE_CACHE_TIME); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $empire_object = $self->build_object('Empire'); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
while (my ($id,$name,$level) = $sth->fetchrow_array) { |
88
|
|
|
|
|
|
|
my $response = $self->request( |
89
|
|
|
|
|
|
|
object => $empire_object, |
90
|
|
|
|
|
|
|
method => 'view_public_profile', |
91
|
|
|
|
|
|
|
params => [ $id ], |
92
|
|
|
|
|
|
|
catch => [ |
93
|
|
|
|
|
|
|
[ |
94
|
|
|
|
|
|
|
'1002', |
95
|
|
|
|
|
|
|
'The empire you wish to view does not exist.', |
96
|
|
|
|
|
|
|
sub { |
97
|
|
|
|
|
|
|
$self->remove_empire($id,$name); |
98
|
|
|
|
|
|
|
return 0; |
99
|
|
|
|
|
|
|
}, |
100
|
|
|
|
|
|
|
], |
101
|
|
|
|
|
|
|
], |
102
|
|
|
|
|
|
|
); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
next |
105
|
|
|
|
|
|
|
unless defined $response; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $empire_data = $response->{profile}; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
$level //= 1; |
110
|
|
|
|
|
|
|
foreach my $medal (values %{$empire_data->{medals}}) { |
111
|
|
|
|
|
|
|
if ($medal->{image} =~ m/^building(\d+)$/) { |
112
|
|
|
|
|
|
|
$level = $1 |
113
|
|
|
|
|
|
|
if ($1 > $level); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
# TODO get level based on ships and buildings |
116
|
|
|
|
|
|
|
# TODO calculate threat level based on num of ships |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my %update = ( |
120
|
|
|
|
|
|
|
name => $empire_data->{name}, |
121
|
|
|
|
|
|
|
normalized_name => normalize_name($empire_data->{name}), |
122
|
|
|
|
|
|
|
alliance => (defined $empire_data->{alliance} ? $empire_data->{alliance}{id} : undef), |
123
|
|
|
|
|
|
|
colony_count => $empire_data->{colony_count}, |
124
|
|
|
|
|
|
|
date_founded => parse_date($empire_data->{date_founded}), |
125
|
|
|
|
|
|
|
level => $level, |
126
|
|
|
|
|
|
|
last_checked => time(), |
127
|
|
|
|
|
|
|
); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my (@pairs,@bind,$pairs); |
130
|
|
|
|
|
|
|
foreach my $key (keys %update) { |
131
|
|
|
|
|
|
|
push(@pairs,"$key = ?"); |
132
|
|
|
|
|
|
|
push(@bind,$update{$key}); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
$pairs = join(',',@pairs); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
$self->storage_do("UPDATE empire SET $pairs WHERE id = ?",@bind,$id); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub remove_empire { |
141
|
|
|
|
|
|
|
my ($self,$id,$name) = @_; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$self->log('debug','Removing empire %s from cache',$name); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$self->storage_do('DELETE FROM empire WHERE id = ?',$id); |
146
|
|
|
|
|
|
|
$self->storage_do('UPDATE body SET empire = NULL WHERE empire = ?',$id); |
147
|
|
|
|
|
|
|
} |