--- deliantra/server/ext/schmorplog.ext 2007/05/19 11:04:16 1.9 +++ deliantra/server/ext/schmorplog.ext 2012/02/04 00:14:41 1.31 @@ -1,73 +1,328 @@ -#! perl +#! perl # depends=irc mandatory -our $UPDATE_LOGINS = Event->idle (repeat => 0, min => 1, data => cf::WF_AUTOCANCEL, cb => Coro::unblock_sub { - my ($status, @pl) = ext::commands::who_listing; +# statistics-package - #TODO: write files asynchronously - open my $fh, ">", "$LOCALDIR/usercount"; - print $fh scalar @pl; - - open my $fh, ">", "$LOCALDIR/userlisting.html"; - for ($status, @pl) { - s/[<&]//g; - print $fh "$_
\n"; - } -}); +use Fcntl; +use Coro::AIO; + +CONF EXPORT_RECENTLOG = undef +CONF EXPORT_RECENTLOG_INTERVAL = 0.5 + +############################################################################# +# stuffs + +our %PLAYERSEEN; + +# EV, because we call ->start +our $UPDATE_LOGINS = EV::idle sub { + $_[0]->stop; + + cf::async { + my ($status, @pl) = ext::commands::who_listing; + + my $fh = aio_open "$LOCALDIR/usercount", O_WRONLY | O_CREAT | O_TRUNC, 0644 + or return; + aio_write $fh, 0, undef, scalar @pl, 0; + aio_close $fh; + + my $who; + for ($status, @pl) { + s/[<&]//g; + $who .= "$_
\n"; + } + + my $fh = aio_open "$LOCALDIR/userlisting.html", O_WRONLY | O_CREAT | O_TRUNC, 0644 + or return; + aio_write $fh, 0, undef, $who, 0; + aio_close $fh; + }; +}; + +cf::object->attach ( + on_kill => sub { + my ($ob, $hitter) = @_; + + return unless $hitter; + $hitter = $hitter->outer_owner; + my $pl = $hitter->contr + or return; + + ++$hitter->{stats_kill}{$ob->name}; + }, +); cf::player->attach ( on_login => sub { my ($pl) = @_; - $pl->ob->set_ob_key_value (schmorplog_last_login => time); - $pl->ob->set_ob_key_value (schmorplog_login_count => - 1 + $pl->ob->get_ob_key_value ("schmorplog_login_count")); + $pl->ob->kv_set (schmorplog_last_login => time); - (my $client = $pl->ns->version) =~ s/\n/\\n/g; + $pl->ob->kv_set (schmorplog_login_count => + 1 + $pl->ob->kv_get ("schmorplog_login_count")); - $pl->ob->set_ob_key_value (schmorplog_client => $client); + $pl->ob->kv_set (schmorplog_client => $pl->ns->{who_version}); - ext::irc::do_notice (sprintf "%s logged in", $pl->ob->name); + my $name = $pl->ob->name; + ext::irc::do_notice $name . " logged in"; + + undef $PLAYERSEEN{$name}; $UPDATE_LOGINS->start; - warn "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host; + cf::trace "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host; }, on_logout => sub { my ($pl, $cleanly) = @_; - $pl->ob->set_ob_key_value (schmorplog_last_logout => time); - ext::irc::do_notice (sprintf "%s left", $pl->ob->name); + $pl->ob->kv_set (schmorplog_last_logout => time); + ext::irc::do_notice $pl->ob->name . " left"; $UPDATE_LOGINS->start; - warn "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)"; + cf::trace "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)"; }, on_birth => sub { my ($pl) = @_; - ext::irc::do_notice (sprintf "%s was just born", $pl->ob->name); - $pl->ob->set_ob_key_value (schmorplog_birthdate => time); + $pl->ob->kv_set (schmorplog_birthdate => time); + ext::irc::do_notice $pl->ob->name . " was just born"; - warn "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host; + cf::trace "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host; }, on_quit => sub { my ($pl) = @_; - ext::irc::do_notice (sprintf "%s quit the game", $pl->ob->name); + ext::irc::do_notice $pl->ob->name . " quit the game"; - warn "QUIT: ", $pl->ob->name, " from ", $pl->ns->host; + cf::trace "QUIT: ", $pl->ob->name, " from ", $pl->ns->host; }, on_death => sub { my ($pl) = @_; - ext::irc::do_notice (sprintf "%s was killed by %s", $pl->ob->name, $pl->killer); - $pl->ob->set_ob_key_value (schmorplog_death_count => - 1 + $pl->ob->get_ob_key_value ("schmorplog_death_count")); + my $msg = $pl->expand_cfpod ($pl->ob->name . " was killed by " . $pl->killer_name . "."); + ext::irc::do_notice $msg; + + ++$pl->ob->{stats_death}{$pl->killer_name}; + + $pl->ob->kv_set (schmorplog_death_count => + 1 + $pl->ob->kv_get ("schmorplog_death_count")); + + $_->send_msg ("" => $msg, cf::NDI_VERBATIM) + for cf::player::list; }, on_load => sub { my ($pl, $path) = @_; - $pl->ob->set_ob_key_value (schmorplog_last_load => time); + $pl->ob->kv_set (schmorplog_last_load => time); }, on_save => sub { my ($pl, $path) = @_; - $pl->ob->set_ob_key_value (schmorplog_last_save => time); + $pl->ob->kv_set (schmorplog_last_save => time); }, ); +cf::register_script_function "statistician::talk" => sub { + my ($who, $msg, $npc) = @_; + + my ($cmd, $args) = split /\s+/, $msg, 2; + + $args ||= $who->name; + + if ($cmd eq "deaths") { + cf::async { + my $pl = cf::player::find $args + or return $who->reply ($npc, "I don't know any person named '$args'."); + + my $s = $pl->ob->{stats_death} + or return $who->reply ($npc, "$args didn't die even once."); + + my $rep = "$args died a total of " . $pl->ob->kv_get ("schmorplog_death_count") . " times, among them:\n\n"; + + for (sort { $s->{$b} <=> $s->{$a} } keys %$s) { + $rep .= sprintf " C<%4d> time(s) due to %s.\n", $s->{$_}, $_; + } + + $who->reply ($npc, $rep); + }; + } elsif ($cmd eq "kills") { + cf::async { + my $pl = cf::player::find $args + or return $who->reply ($npc, "I don't know any person named '$args'."); + + my $s = $pl->ob->{stats_kill} + or return $who->reply ($npc, "I don't know of I that $args has killed so far."); + + my $rep = sprintf "Here is a list of all the kills I know about:\n\n"; + for (sort { $s->{$b} <=> $s->{$a} } keys %$s) { + $rep .= sprintf " C<%6s> %s\n", $s->{$_}, $_; + } + + $who->reply ($npc, $rep); + }; + } elsif ($cmd eq "hi") { + $who->reply ($npc, "Hello!\n\n" + . "I am a statistician, I keep statistics about all people here.\n\n" + . " - To know how often somebody died, ask C I\n" + . " - To know how many kills somebody scored, ask C I"); + } else { + $who->reply ($npc, "No idea what you want of me, how about saying 'hi' first?"); + } + + $cmd = lc $cmd; +}; + +# log "crash" messages, i.e. client exit reasons +cf::register_exticmd clientlog => sub { + my ($ns, $msg) = @_; + + my $name = $ns->pl && $ns->pl->ob ? $ns->pl->ob->name : ""; + + $msg =~ y/\x0a\x20-\x7f//cd; + $msg =~ s/\s+$//; + + cf::error sprintf "clientlog [%s/%s]: %s\n", $ns->host, $name, $msg; + + () +}; + +############################################################################# +# log max playercount every minute + +our $STATSDIR = "$LOCALDIR/maxplayers"; + +mkdir $STATSDIR; + +our $WRITE_MAXPLAYERS = EV::periodic 0, 60, undef, sub { + my $now = AE::now; + my $cnt = scalar keys %PLAYERSEEN; + + %PLAYERSEEN = map +($_->ob->name => undef), grep $_->ns, cf::player::list; + + my @time = gmtime $now; + + my $path = sprintf "%s/%04d-%02d-%02d", $STATSDIR, $time[5] + 1900, $time[4] + 1, $time[3]; + my $offs = $time [2] * 60 + $time [1]; + + $cnt++; + $cnt = 254 if $cnt > 254; + $cnt = chr $cnt; + + IO::AIO::aio_open $path, O_WRONLY | O_CREAT, 0666, sub { + my $fh = shift + or return; + + # the truncate is 1440 extra syscalls, but saves 1439 + # slow metadata updates. + IO::AIO::aio_truncate $fh, 1440, sub { + IO::AIO::aio_write $fh, $offs, 1, $cnt, 0, sub { + IO::AIO::aio_close $fh; + }; + }; + }; +}; + +############################################################################# +# export recentlog + +our %RECENT; + +our $update_w; +our %need_update; + +sub _update_login { + my ($login) = @_; + + my $path = (cf::player::playerdir $login) . "/playerdata"; + + if (0 >= aio_load $path, my $data) { + delete $RECENT{$login}; + } else { + local $_ = $data; + + my $birthdate = /^schmorplog_birthdate (\S+)$/m ? $1 : undef; + my $login_count = /^schmorplog_login_count (\S+)$/m ? $1 : 0; + my $death_count = /^schmorplog_death_count (\S+)$/m ? $1 : 0; + my $last_save = /^schmorplog_last_save (\S+)$/m ? $1 : undef; + my $last_login = /^schmorplog_last_login (\S+)$/m ? $1 : undef; + my $last_logout = /^schmorplog_last_logout (\S+)$/m ? $1 : undef; + my $client = /^schmorplog_client (.*)$/m ? $1 : "?"; + my $map = /^map (.*)$/m ? $1 : "?"; + + return unless $last_login; + + $last_logout = $last_save if $last_save > $last_logout && $last_login > $last_logout && $last_save < $NOW - 10 * 60; + $last_logout = undef if $last_logout < $last_login; + + return unless $last_login > $NOW - 86400 * ($login_count * 7 + 10); + +# next if $count < 3 && $login < $NOW - 86400*2; + $RECENT{$login} = [$login, $birthdate, $last_login, $login_count, $last_logout, $client, $death_count, $map]; + } +} + +sub _update { + cf::async_ext { + my $t0 = EV::now; + + $Coro::current->nice (1); + $Coro::current->{desc} = "recentlog updater"; + + my $lock = cf::lock_acquire "export_recentlog"; + + while (%need_update) { + for (keys %need_update) { + delete $need_update{$_}; + + _update_login $_; + } + } + + cf::get_slot 0.1, 0, "recentlog serialise"; + + my $NOW = $cf::NOW; + + cf::replace_file $EXPORT_RECENTLOG, cf::encode_json { + version => 1, + date => $NOW, + data => [ + sort { ($b->[4] || $NOW) <=> ($a->[4] || $NOW) } + values %RECENT + ], + }; + + cf::trace "recentlog updated (", EV::now - $t0, "s).\n"; + }; +} + +sub update { + return unless defined $EXPORT_RECENTLOG; + + $update_w ||= AE::timer $EXPORT_RECENTLOG_INTERVAL, 0, \&_update; +} + +sub reload { + return unless defined $EXPORT_RECENTLOG; + + my $lock = cf::lock_acquire "export_recentlog"; + + cf::async_ext { + $lock; + + $Coro::current->{desc} = "recentlog reloader"; + + undef $need_update{$_} + for @{ +cf::player::list_logins }; + + _update; + }; +} + +cf::player->attach ( + on_login => sub { undef $need_update{$_[0]->ob->name}; _update }, + on_logout => sub { undef $need_update{$_[0]->ob->name}; _update }, + on_birth => sub { undef $need_update{$_[0]->ob->name}; _update }, + on_death => sub { undef $need_update{$_[0]->ob->name}; _update }, + on_load => sub { undef $need_update{$_[0]->ob->name}; _update }, + on_save => sub { undef $need_update{$_[0]->ob->name}; _update }, +); + +cf::post_init { + reload; +}; +