| 1 |
root |
1.22 |
#! perl # depends=irc mandatory |
| 2 |
root |
1.1 |
|
| 3 |
root |
1.17 |
# statistics-package |
| 4 |
|
|
|
| 5 |
root |
1.12 |
use Fcntl; |
| 6 |
|
|
use Coro::AIO; |
| 7 |
|
|
|
| 8 |
|
|
our $UPDATE_LOGINS = EV::idle sub { |
| 9 |
root |
1.11 |
$_[0]->stop; |
| 10 |
|
|
|
| 11 |
root |
1.12 |
cf::async { |
| 12 |
|
|
my ($status, @pl) = ext::commands::who_listing; |
| 13 |
root |
1.1 |
|
| 14 |
root |
1.12 |
my $fh = aio_open "$LOCALDIR/usercount", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
| 15 |
|
|
or return; |
| 16 |
|
|
aio_write $fh, 0, undef, scalar @pl, 0; |
| 17 |
|
|
aio_close $fh; |
| 18 |
|
|
|
| 19 |
|
|
my $who; |
| 20 |
|
|
for ($status, @pl) { |
| 21 |
|
|
s/[<&]//g; |
| 22 |
|
|
$who .= "$_<br />\n"; |
| 23 |
|
|
} |
| 24 |
|
|
|
| 25 |
|
|
my $fh = aio_open "$LOCALDIR/userlisting.html", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
| 26 |
|
|
or return; |
| 27 |
|
|
aio_write $fh, 0, undef, $who, 0; |
| 28 |
|
|
aio_close $fh; |
| 29 |
|
|
}; |
| 30 |
root |
1.11 |
}; |
| 31 |
root |
1.1 |
|
| 32 |
root |
1.14 |
cf::object->attach ( |
| 33 |
|
|
on_kill => sub { |
| 34 |
|
|
my ($ob, $hitter) = @_; |
| 35 |
|
|
|
| 36 |
|
|
return unless $hitter; |
| 37 |
|
|
$hitter = $hitter->outer_owner; |
| 38 |
|
|
my $pl = $hitter->contr |
| 39 |
|
|
or return; |
| 40 |
|
|
|
| 41 |
root |
1.16 |
++$hitter->{stats_kill}{$ob->name}; |
| 42 |
root |
1.14 |
}, |
| 43 |
|
|
); |
| 44 |
|
|
|
| 45 |
root |
1.3 |
cf::player->attach ( |
| 46 |
root |
1.1 |
on_login => sub { |
| 47 |
|
|
my ($pl) = @_; |
| 48 |
root |
1.19 |
$pl->ob->kv_set (schmorplog_last_login => time); |
| 49 |
root |
1.1 |
|
| 50 |
root |
1.19 |
$pl->ob->kv_set (schmorplog_login_count => |
| 51 |
|
|
1 + $pl->ob->kv_get ("schmorplog_login_count")); |
| 52 |
root |
1.1 |
|
| 53 |
root |
1.24 |
$pl->ob->kv_set (schmorplog_client => $pl->ns->{who_version}); |
| 54 |
root |
1.1 |
|
| 55 |
root |
1.9 |
ext::irc::do_notice (sprintf "%s logged in", $pl->ob->name); |
| 56 |
root |
1.1 |
|
| 57 |
|
|
$UPDATE_LOGINS->start; |
| 58 |
root |
1.5 |
|
| 59 |
|
|
warn "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host; |
| 60 |
root |
1.1 |
}, |
| 61 |
|
|
on_logout => sub { |
| 62 |
root |
1.6 |
my ($pl, $cleanly) = @_; |
| 63 |
root |
1.19 |
$pl->ob->kv_set (schmorplog_last_logout => time); |
| 64 |
root |
1.9 |
ext::irc::do_notice (sprintf "%s left", $pl->ob->name); |
| 65 |
root |
1.1 |
|
| 66 |
|
|
$UPDATE_LOGINS->start; |
| 67 |
root |
1.5 |
|
| 68 |
root |
1.6 |
warn "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)"; |
| 69 |
root |
1.1 |
}, |
| 70 |
|
|
on_birth => sub { |
| 71 |
|
|
my ($pl) = @_; |
| 72 |
root |
1.9 |
ext::irc::do_notice (sprintf "%s was just born", $pl->ob->name); |
| 73 |
root |
1.19 |
$pl->ob->kv_set (schmorplog_birthdate => time); |
| 74 |
root |
1.5 |
|
| 75 |
|
|
warn "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host; |
| 76 |
root |
1.1 |
}, |
| 77 |
|
|
on_quit => sub { |
| 78 |
|
|
my ($pl) = @_; |
| 79 |
root |
1.9 |
ext::irc::do_notice (sprintf "%s quit the game", $pl->ob->name); |
| 80 |
root |
1.5 |
|
| 81 |
|
|
warn "QUIT: ", $pl->ob->name, " from ", $pl->ns->host; |
| 82 |
root |
1.1 |
}, |
| 83 |
|
|
on_death => sub { |
| 84 |
|
|
my ($pl) = @_; |
| 85 |
root |
1.22 |
|
| 86 |
|
|
my $msg = $pl->expand_cfpod (sprintf "%s was killed by %s.", $pl->ob->name, $pl->killer_name); |
| 87 |
|
|
ext::irc::do_notice ($msg); |
| 88 |
root |
1.1 |
|
| 89 |
root |
1.14 |
++$pl->ob->{stats_death}{$pl->killer_name}; |
| 90 |
|
|
|
| 91 |
root |
1.19 |
$pl->ob->kv_set (schmorplog_death_count => |
| 92 |
|
|
1 + $pl->ob->kv_get ("schmorplog_death_count")); |
| 93 |
root |
1.22 |
|
| 94 |
|
|
$_->send_msg ("" => $msg, cf::NDI_VERBATIM) |
| 95 |
|
|
for cf::player::list; |
| 96 |
root |
1.1 |
}, |
| 97 |
|
|
on_load => sub { |
| 98 |
|
|
my ($pl, $path) = @_; |
| 99 |
root |
1.19 |
$pl->ob->kv_set (schmorplog_last_load => time); |
| 100 |
root |
1.1 |
}, |
| 101 |
|
|
on_save => sub { |
| 102 |
|
|
my ($pl, $path) = @_; |
| 103 |
root |
1.19 |
$pl->ob->kv_set (schmorplog_last_save => time); |
| 104 |
root |
1.1 |
}, |
| 105 |
root |
1.3 |
); |
| 106 |
root |
1.1 |
|
| 107 |
root |
1.17 |
cf::register_script_function "statistician::talk" => sub { |
| 108 |
|
|
my ($who, $msg, $npc) = @_; |
| 109 |
|
|
|
| 110 |
|
|
my ($cmd, $args) = split /\s+/, $msg, 2; |
| 111 |
|
|
|
| 112 |
|
|
$args ||= $who->name; |
| 113 |
|
|
|
| 114 |
|
|
if ($cmd eq "deaths") { |
| 115 |
|
|
cf::async { |
| 116 |
|
|
my $pl = cf::player::find $args |
| 117 |
|
|
or return $who->reply ($npc, "I don't know any person named '$args'."); |
| 118 |
|
|
|
| 119 |
|
|
my $s = $pl->ob->{stats_death} |
| 120 |
|
|
or return $who->reply ($npc, "$args didn't die even once."); |
| 121 |
|
|
|
| 122 |
root |
1.23 |
my $rep = "$args died a total of " . $pl->ob->kv_get ("schmorplog_death_count") . " times, among them:\n\n"; |
| 123 |
root |
1.17 |
|
| 124 |
|
|
for (sort { $s->{$b} <=> $s->{$a} } keys %$s) { |
| 125 |
root |
1.23 |
$rep .= sprintf " C<%4d> time(s) due to %s.\n", $s->{$_}, $_; |
| 126 |
root |
1.17 |
} |
| 127 |
|
|
|
| 128 |
|
|
$who->reply ($npc, $rep); |
| 129 |
|
|
}; |
| 130 |
|
|
} elsif ($cmd eq "kills") { |
| 131 |
|
|
cf::async { |
| 132 |
|
|
my $pl = cf::player::find $args |
| 133 |
|
|
or return $who->reply ($npc, "I don't know any person named '$args'."); |
| 134 |
|
|
|
| 135 |
|
|
my $s = $pl->ob->{stats_kill} |
| 136 |
|
|
or return $who->reply ($npc, "I don't know of I<anything> that $args has killed so far."); |
| 137 |
|
|
|
| 138 |
root |
1.23 |
my $rep = sprintf "Here is a list of all the kills I know about:\n\n"; |
| 139 |
|
|
for (sort { $s->{$b} <=> $s->{$a} } keys %$s) { |
| 140 |
|
|
$rep .= sprintf " C<%6s> %s\n", $s->{$_}, $_; |
| 141 |
root |
1.17 |
} |
| 142 |
|
|
|
| 143 |
|
|
$who->reply ($npc, $rep); |
| 144 |
|
|
}; |
| 145 |
|
|
} elsif ($cmd eq "hi") { |
| 146 |
|
|
$who->reply ($npc, "Hello!\n\n" |
| 147 |
|
|
. "I am a statistician, I keep statistics about all people here.\n\n" |
| 148 |
root |
1.23 |
. " - To know how often somebody died, ask C<deaths> I<playername>\n" |
| 149 |
|
|
. " - To know how many kills somebody scored, ask C<kills> I<playername>"); |
| 150 |
root |
1.17 |
} else { |
| 151 |
|
|
$who->reply ($npc, "No idea what you want of me, how about saying 'hi' first?"); |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
|
$cmd = lc $cmd; |
| 155 |
|
|
}; |
| 156 |
|
|
|
| 157 |
root |
1.21 |
# log "crash" messages, i.e. client exit reasons |
| 158 |
|
|
cf::register_exticmd clientlog => sub { |
| 159 |
|
|
my ($ns, $msg) = @_; |
| 160 |
|
|
|
| 161 |
|
|
my $name = $ns->pl && $ns->pl->ob ? $ns->pl->ob->name : "<unknown>"; |
| 162 |
|
|
|
| 163 |
|
|
$msg =~ y/\x0a\x20-\x7f//cd; |
| 164 |
|
|
$msg =~ s/\s+$//; |
| 165 |
|
|
|
| 166 |
|
|
warn sprintf "clientlog [%s/%s]: %s\n", $ns->host, $name, $msg; |
| 167 |
|
|
|
| 168 |
|
|
() |
| 169 |
|
|
}; |
| 170 |
|
|
|