--- deliantra/server/ext/schmorplog.ext 2006/12/15 19:29:18 1.1 +++ deliantra/server/ext/schmorplog.ext 2008/05/01 06:47:03 1.17 @@ -1,19 +1,48 @@ -#! perl +#! perl # depends=irc -our $UPDATE_LOGINS = Event->idle (repeat => 0, min => 1, data => cf::WF_AUTOCANCEL, cb => sub { - my ($status, @pl) = ext::commands::who_listing; +# statistics-package - open my $fh, ">", cf::localdir . "/usercount"; - print $fh scalar @pl; +use Fcntl; +use Coro::AIO; - open my $fh, ">", cf::localdir . "/userlisting.html"; - for ($status, @pl) { - s/[<&]//g; - print $fh "$_
\n"; - } -}); +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) = @_; -cf::attach_to_players + 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); @@ -21,33 +50,43 @@ $pl->ob->set_ob_key_value (schmorplog_login_count => 1 + $pl->ob->get_ob_key_value ("schmorplog_login_count")); - (my $client = $pl->socket->client) =~ s/\n/\\n/g; + (my $client = $pl->ns->version) =~ s/\n/\\n/g; $pl->ob->set_ob_key_value (schmorplog_client => $client); - ext::schmorp_irc::do_notice (sprintf "%s logged in", $pl->ob->name); + ext::irc::do_notice (sprintf "%s logged in", $pl->ob->name); $UPDATE_LOGINS->start; + + warn "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host; }, on_logout => sub { - my ($pl) = @_; + my ($pl, $cleanly) = @_; $pl->ob->set_ob_key_value (schmorplog_last_logout => time); - ext::schmorp_irc::do_notice (sprintf "%s left", $pl->ob->name); + ext::irc::do_notice (sprintf "%s left", $pl->ob->name); $UPDATE_LOGINS->start; + + warn "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)"; }, on_birth => sub { my ($pl) = @_; - ext::schmorp_irc::do_notice (sprintf "%s was just born", $pl->ob->name); + ext::irc::do_notice (sprintf "%s was just born", $pl->ob->name); $pl->ob->set_ob_key_value (schmorplog_birthdate => time); + + warn "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host; }, on_quit => sub { my ($pl) = @_; - ext::schmorp_irc::do_notice (sprintf "%s quit the game", $pl->ob->name); + ext::irc::do_notice (sprintf "%s quit the game", $pl->ob->name); + + warn "QUIT: ", $pl->ob->name, " from ", $pl->ns->host; }, on_death => sub { my ($pl) = @_; - ext::schmorp_irc::do_notice (sprintf "%s was killed by %s", $pl->ob->name, $pl->killer); + ext::irc::do_notice (sprintf "%s was killed by %s", $pl->ob->name, $pl->killer_name); + + ++$pl->ob->{stats_death}{$pl->killer_name}; $pl->ob->set_ob_key_value (schmorplog_death_count => 1 + $pl->ob->get_ob_key_value ("schmorplog_death_count")); @@ -60,5 +99,55 @@ my ($pl, $path) = @_; $pl->ob->set_ob_key_value (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->get_ob_key_value ("schmorplog_death_count") . " times, among them:\n\n\n\n"; + + for (sort { $s->{$b} <=> $s->{$a} } keys %$s) { + $rep .= "$s->{$_} times due to $_.\n\n"; + } + + $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 "\n\nC<%6s> %s\n\n", "#", "Name"; + for (sort { $s->{$a} <=> $s->{$b} } keys %$s) { + $rep .= sprintf "C<%6s> %s\n\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\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; +};