ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/schmorplog.ext
(Generate patch)

Comparing deliantra/server/ext/schmorplog.ext (file contents):
Revision 1.29 by root, Tue May 4 21:45:42 2010 UTC vs.
Revision 1.35 by root, Tue Aug 11 02:20:13 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines