… | |
… | |
3 | # statistics-package |
3 | # statistics-package |
4 | |
4 | |
5 | use Fcntl; |
5 | use Fcntl; |
6 | use Coro::AIO; |
6 | use Coro::AIO; |
7 | |
7 | |
|
|
8 | CONF EXPORT_RECENTLOG = undef |
|
|
9 | CONF EXPORT_RECENTLOG_INTERVAL = 300 |
|
|
10 | |
|
|
11 | ############################################################################# |
|
|
12 | # stuffs |
|
|
13 | |
8 | our %PLAYERSEEN; |
14 | our %PLAYERSEEN; |
9 | |
15 | |
|
|
16 | # EV, because we call ->start |
10 | our $UPDATE_LOGINS = AE::idle sub { |
17 | our $UPDATE_LOGINS = EV::idle sub { |
11 | $_[0]->stop; |
18 | $_[0]->stop; |
12 | |
19 | |
13 | cf::async { |
20 | cf::async { |
14 | my ($status, @pl) = ext::commands::who_listing; |
21 | my ($status, @pl) = ext::commands::who_listing; |
15 | |
22 | |
… | |
… | |
172 | cf::error sprintf "clientlog [%s/%s]: %s\n", $ns->host, $name, $msg; |
179 | cf::error sprintf "clientlog [%s/%s]: %s\n", $ns->host, $name, $msg; |
173 | |
180 | |
174 | () |
181 | () |
175 | }; |
182 | }; |
176 | |
183 | |
|
|
184 | ############################################################################# |
177 | # log max playercount every minute |
185 | # log max playercount every minute |
178 | ############################################################################# |
|
|
179 | |
186 | |
180 | our $STATSDIR = "$LOCALDIR/maxplayers"; |
187 | our $STATSDIR = "$LOCALDIR/maxplayers"; |
181 | |
188 | |
182 | mkdir $STATSDIR; |
189 | mkdir $STATSDIR; |
183 | |
190 | |
… | |
… | |
208 | }; |
215 | }; |
209 | }; |
216 | }; |
210 | }; |
217 | }; |
211 | }; |
218 | }; |
212 | |
219 | |
|
|
220 | ############################################################################# |
|
|
221 | # export recentlog |
213 | |
222 | |
|
|
223 | our %RECENT; |
|
|
224 | |
|
|
225 | our $update_w; |
|
|
226 | our %need_update; |
|
|
227 | |
|
|
228 | sub _update_login { |
|
|
229 | my ($login) = @_; |
|
|
230 | |
|
|
231 | my $path = (cf::player::playerdir $login) . "/playerdata"; |
|
|
232 | |
|
|
233 | if (0 >= aio_load $path, my $data) { |
|
|
234 | delete $RECENT{$login}; |
|
|
235 | } else { |
|
|
236 | local $_ = $data; |
|
|
237 | |
|
|
238 | my $birthdate = /^schmorplog_birthdate (\S+)$/m ? $1 : undef; |
|
|
239 | my $login_count = /^schmorplog_login_count (\S+)$/m ? $1 : 0; |
|
|
240 | my $death_count = /^schmorplog_death_count (\S+)$/m ? $1 : 0; |
|
|
241 | my $last_save = /^schmorplog_last_save (\S+)$/m ? $1 : undef; |
|
|
242 | my $last_login = /^schmorplog_last_login (\S+)$/m ? $1 : undef; |
|
|
243 | my $last_logout = /^schmorplog_last_logout (\S+)$/m ? $1 : undef; |
|
|
244 | my $client = /^schmorplog_client (.*)$/m ? $1 : "?"; |
|
|
245 | my $map = /^map (.*)$/m ? $1 : "?"; |
|
|
246 | |
|
|
247 | return unless $last_login; |
|
|
248 | |
|
|
249 | $last_logout = $last_save if $last_save > $last_logout && $last_login > $last_logout && $last_save < $NOW - 10 * 60; |
|
|
250 | $last_logout = undef if $last_logout < $last_login; |
|
|
251 | |
|
|
252 | return unless $last_login > $NOW - 86400 * ($login_count * 7 + 10); |
|
|
253 | |
|
|
254 | # next if $count < 3 && $login < $NOW - 86400*2; |
|
|
255 | $RECENT{$login} = [$login, $birthdate, $last_login, $login_count, $last_logout, $client, $death_count, $map]; |
|
|
256 | } |
|
|
257 | } |
|
|
258 | |
|
|
259 | sub _update { |
|
|
260 | cf::async_ext { |
|
|
261 | my $t0 = EV::now; |
|
|
262 | |
|
|
263 | $Coro::current->nice (1); |
|
|
264 | $Coro::current->{desc} = "recentlog updater"; |
|
|
265 | |
|
|
266 | Coro::AnyEvent::sleep 5; # grace time to allow file-saves |
|
|
267 | |
|
|
268 | my $lock = cf::lock_acquire "export_recentlog"; |
|
|
269 | |
|
|
270 | while (%need_update) { |
|
|
271 | for (keys %need_update) { |
|
|
272 | delete $need_update{$_}; |
|
|
273 | |
|
|
274 | _update_login $_; |
|
|
275 | } |
|
|
276 | } |
|
|
277 | |
|
|
278 | undef $update_w; |
|
|
279 | |
|
|
280 | cf::get_slot 0.1, 0, "recentlog serialise"; |
|
|
281 | |
|
|
282 | my $NOW = $cf::NOW; |
|
|
283 | |
|
|
284 | cf::replace_file $EXPORT_RECENTLOG, cf::encode_json { |
|
|
285 | version => 1, |
|
|
286 | date => $NOW, |
|
|
287 | data => [ |
|
|
288 | sort { ($b->[4] || $NOW) <=> ($a->[4] || $NOW) } |
|
|
289 | values %RECENT |
|
|
290 | ], |
|
|
291 | } or warn "$EXPORT_RECENTLOG: $!"; |
|
|
292 | |
|
|
293 | cf::trace "recentlog updated (", EV::now - $t0, "s).\n"; |
|
|
294 | }; |
|
|
295 | } |
|
|
296 | |
|
|
297 | sub update { |
|
|
298 | return unless defined $EXPORT_RECENTLOG; |
|
|
299 | |
|
|
300 | $update_w ||= AE::timer $EXPORT_RECENTLOG_INTERVAL, 0, \&_update; |
|
|
301 | } |
|
|
302 | |
|
|
303 | sub reload { |
|
|
304 | return unless defined $EXPORT_RECENTLOG; |
|
|
305 | |
|
|
306 | my $lock = cf::lock_acquire "export_recentlog"; |
|
|
307 | |
|
|
308 | cf::async_ext { |
|
|
309 | $lock; |
|
|
310 | |
|
|
311 | $Coro::current->{desc} = "recentlog reloader"; |
|
|
312 | |
|
|
313 | undef $need_update{$_} |
|
|
314 | for @{ +cf::player::list_logins }; |
|
|
315 | |
|
|
316 | _update; |
|
|
317 | }; |
|
|
318 | } |
|
|
319 | |
|
|
320 | cf::player->attach ( |
|
|
321 | on_login => sub { undef $need_update{$_[0]->ob->name}; update }, |
|
|
322 | on_logout => sub { undef $need_update{$_[0]->ob->name}; update }, |
|
|
323 | on_birth => sub { undef $need_update{$_[0]->ob->name}; update }, |
|
|
324 | on_death => sub { undef $need_update{$_[0]->ob->name}; update }, |
|
|
325 | # on_load => sub { undef $need_update{$_[0]->ob->name}; update }, |
|
|
326 | # on_save => sub { undef $need_update{$_[0]->ob->name}; update }, |
|
|
327 | ); |
|
|
328 | |
|
|
329 | cf::post_init { |
|
|
330 | reload; |
|
|
331 | }; |
|
|
332 | |