--- deliantra/server/ext/schmorplog.ext 2008/04/30 10:31:04 1.14 +++ deliantra/server/ext/schmorplog.ext 2008/09/01 09:12:42 1.21 @@ -1,5 +1,7 @@ #! perl # depends=irc +# statistics-package + use Fcntl; use Coro::AIO; @@ -43,14 +45,14 @@ cf::player->attach ( on_login => sub { my ($pl) = @_; - $pl->ob->set_ob_key_value (schmorplog_last_login => time); + $pl->ob->kv_set (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_login_count => + 1 + $pl->ob->kv_get ("schmorplog_login_count")); (my $client = $pl->ns->version) =~ s/\n/\\n/g; - $pl->ob->set_ob_key_value (schmorplog_client => $client); + $pl->ob->kv_set (schmorplog_client => $client); ext::irc::do_notice (sprintf "%s logged in", $pl->ob->name); @@ -60,7 +62,7 @@ }, on_logout => sub { my ($pl, $cleanly) = @_; - $pl->ob->set_ob_key_value (schmorplog_last_logout => time); + $pl->ob->kv_set (schmorplog_last_logout => time); ext::irc::do_notice (sprintf "%s left", $pl->ob->name); $UPDATE_LOGINS->start; @@ -70,7 +72,7 @@ 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); warn "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host; }, @@ -86,16 +88,80 @@ ++$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")); + $pl->ob->kv_set (schmorplog_death_count => + 1 + $pl->ob->kv_get ("schmorplog_death_count")); }, 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\n\n"; + + for (sort { $s->{$b} <=> $s->{$a} } keys %$s) { + $rep .= "$s->{$_} time(s) 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", "Kills", "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; +}; + +# 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+$//; + + warn sprintf "clientlog [%s/%s]: %s\n", $ns->host, $name, $msg; + + () +}; +