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.15 by root, Wed Apr 30 10:36:01 2008 UTC vs.
Revision 1.32 by root, Sat Feb 4 00:41:42 2012 UTC

1#! perl # depends=irc 1#! perl # depends=irc mandatory
2
3# statistics-package
2 4
3use Fcntl; 5use Fcntl;
4use Coro::AIO; 6use Coro::AIO;
5 7
8CONF EXPORT_RECENTLOG = undef
9CONF EXPORT_RECENTLOG_INTERVAL = 300
10
11#############################################################################
12# stuffs
13
14our %PLAYERSEEN;
15
16# EV, because we call ->start
6our $UPDATE_LOGINS = EV::idle sub { 17our $UPDATE_LOGINS = EV::idle sub {
7 $_[0]->stop; 18 $_[0]->stop;
8 19
9 cf::async { 20 cf::async {
10 my ($status, @pl) = ext::commands::who_listing; 21 my ($status, @pl) = ext::commands::who_listing;
34 return unless $hitter; 45 return unless $hitter;
35 $hitter = $hitter->outer_owner; 46 $hitter = $hitter->outer_owner;
36 my $pl = $hitter->contr 47 my $pl = $hitter->contr
37 or return; 48 or return;
38 49
39 ++$hitter->{stats_kill}{$ob->name_pl}; 50 ++$hitter->{stats_kill}{$ob->name};
40 }, 51 },
41); 52);
42 53
43cf::player->attach ( 54cf::player->attach (
44 on_login => sub { 55 on_login => sub {
45 my ($pl) = @_; 56 my ($pl) = @_;
57
46 $pl->ob->set_ob_key_value (schmorplog_last_login => time); 58 $pl->ob->kv_set (schmorplog_last_login => time);
47 59
48 $pl->ob->set_ob_key_value (schmorplog_login_count => 60 $pl->ob->kv_set (schmorplog_login_count =>
49 1 + $pl->ob->get_ob_key_value ("schmorplog_login_count")); 61 1 + $pl->ob->kv_get ("schmorplog_login_count"));
50 62
51 (my $client = $pl->ns->version) =~ s/\n/\\n/g; 63 $pl->ob->kv_set (schmorplog_client => $pl->ns->{who_version});
52 64
53 $pl->ob->set_ob_key_value (schmorplog_client => $client); 65 my $name = $pl->ob->name;
54 66
55 ext::irc::do_notice (sprintf "%s logged in", $pl->ob->name); 67 ext::irc::do_notice $name . " logged in";
56 68
69 undef $PLAYERSEEN{$name};
57 $UPDATE_LOGINS->start; 70 $UPDATE_LOGINS->start;
58 71
59 warn "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host; 72 cf::trace "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host;
60 }, 73 },
61 on_logout => sub { 74 on_logout => sub {
62 my ($pl, $cleanly) = @_; 75 my ($pl, $cleanly) = @_;
63 $pl->ob->set_ob_key_value (schmorplog_last_logout => time); 76 $pl->ob->kv_set (schmorplog_last_logout => time);
64 ext::irc::do_notice (sprintf "%s left", $pl->ob->name); 77 ext::irc::do_notice $pl->ob->name . " left";
65 78
66 $UPDATE_LOGINS->start; 79 $UPDATE_LOGINS->start;
67 80
68 warn "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)"; 81 cf::trace "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)";
69 }, 82 },
70 on_birth => sub { 83 on_birth => sub {
71 my ($pl) = @_; 84 my ($pl) = @_;
72 ext::irc::do_notice (sprintf "%s was just born", $pl->ob->name);
73 $pl->ob->set_ob_key_value (schmorplog_birthdate => time); 85 $pl->ob->kv_set (schmorplog_birthdate => time);
86 ext::irc::do_notice $pl->ob->name . " was just born";
74 87
75 warn "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host; 88 cf::trace "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host;
76 }, 89 },
77 on_quit => sub { 90 on_quit => sub {
78 my ($pl) = @_; 91 my ($pl) = @_;
79 ext::irc::do_notice (sprintf "%s quit the game", $pl->ob->name); 92 ext::irc::do_notice $pl->ob->name . " quit the game";
80 93
81 warn "QUIT: ", $pl->ob->name, " from ", $pl->ns->host; 94 cf::trace "QUIT: ", $pl->ob->name, " from ", $pl->ns->host;
82 }, 95 },
83 on_death => sub { 96 on_death => sub {
84 my ($pl) = @_; 97 my ($pl) = @_;
85 ext::irc::do_notice (sprintf "%s was killed by %s", $pl->ob->name, $pl->killer_name); 98
99 my $msg = $pl->expand_cfpod ($pl->ob->name . " was killed by " . $pl->killer_name . ".");
100 ext::irc::do_notice $msg;
86 101
87 ++$pl->ob->{stats_death}{$pl->killer_name}; 102 ++$pl->ob->{stats_death}{$pl->killer_name};
88 103
89 $pl->ob->set_ob_key_value (schmorplog_death_count => 104 $pl->ob->kv_set (schmorplog_death_count =>
90 1 + $pl->ob->get_ob_key_value ("schmorplog_death_count")); 105 1 + $pl->ob->kv_get ("schmorplog_death_count"));
106
107 $_->send_msg ("" => $msg, cf::NDI_VERBATIM)
108 for cf::player::list;
91 }, 109 },
92 on_load => sub { 110 on_load => sub {
93 my ($pl, $path) = @_; 111 my ($pl, $path) = @_;
94 $pl->ob->set_ob_key_value (schmorplog_last_load => time); 112 $pl->ob->kv_set (schmorplog_last_load => time);
95 }, 113 },
96 on_save => sub { 114 on_save => sub {
97 my ($pl, $path) = @_; 115 my ($pl, $path) = @_;
98 $pl->ob->set_ob_key_value (schmorplog_last_save => time); 116 $pl->ob->kv_set (schmorplog_last_save => time);
99 }, 117 },
100); 118);
101 119
120cf::register_script_function "statistician::talk" => sub {
121 my ($who, $msg, $npc) = @_;
122
123 my ($cmd, $args) = split /\s+/, $msg, 2;
124
125 $args ||= $who->name;
126
127 if ($cmd eq "deaths") {
128 cf::async {
129 my $pl = cf::player::find $args
130 or return $who->reply ($npc, "I don't know any person named '$args'.");
131
132 my $s = $pl->ob->{stats_death}
133 or return $who->reply ($npc, "$args didn't die even once.");
134
135 my $rep = "$args died a total of " . $pl->ob->kv_get ("schmorplog_death_count") . " times, among them:\n\n";
136
137 for (sort { $s->{$b} <=> $s->{$a} } keys %$s) {
138 $rep .= sprintf " C<%4d> time(s) due to %s.\n", $s->{$_}, $_;
139 }
140
141 $who->reply ($npc, $rep);
142 };
143 } elsif ($cmd eq "kills") {
144 cf::async {
145 my $pl = cf::player::find $args
146 or return $who->reply ($npc, "I don't know any person named '$args'.");
147
148 my $s = $pl->ob->{stats_kill}
149 or return $who->reply ($npc, "I don't know of I<anything> that $args has killed so far.");
150
151 my $rep = sprintf "Here is a list of all the kills I know about:\n\n";
152 for (sort { $s->{$b} <=> $s->{$a} } keys %$s) {
153 $rep .= sprintf " C<%6s> %s\n", $s->{$_}, $_;
154 }
155
156 $who->reply ($npc, $rep);
157 };
158 } elsif ($cmd eq "hi") {
159 $who->reply ($npc, "Hello!\n\n"
160 . "I am a statistician, I keep statistics about all people here.\n\n"
161 . " - To know how often somebody died, ask C<deaths> I<playername>\n"
162 . " - To know how many kills somebody scored, ask C<kills> I<playername>");
163 } else {
164 $who->reply ($npc, "No idea what you want of me, how about saying 'hi' first?");
165 }
166
167 $cmd = lc $cmd;
168};
169
170# log "crash" messages, i.e. client exit reasons
171cf::register_exticmd clientlog => sub {
172 my ($ns, $msg) = @_;
173
174 my $name = $ns->pl && $ns->pl->ob ? $ns->pl->ob->name : "<unknown>";
175
176 $msg =~ y/\x0a\x20-\x7f//cd;
177 $msg =~ s/\s+$//;
178
179 cf::error sprintf "clientlog [%s/%s]: %s\n", $ns->host, $name, $msg;
180
181 ()
182};
183
184#############################################################################
185# log max playercount every minute
186
187our $STATSDIR = "$LOCALDIR/maxplayers";
188
189mkdir $STATSDIR;
190
191our $WRITE_MAXPLAYERS = EV::periodic 0, 60, undef, sub {
192 my $now = AE::now;
193 my $cnt = scalar keys %PLAYERSEEN;
194
195 %PLAYERSEEN = map +($_->ob->name => undef), grep $_->ns, cf::player::list;
196
197 my @time = gmtime $now;
198
199 my $path = sprintf "%s/%04d-%02d-%02d", $STATSDIR, $time[5] + 1900, $time[4] + 1, $time[3];
200 my $offs = $time [2] * 60 + $time [1];
201
202 $cnt++;
203 $cnt = 254 if $cnt > 254;
204 $cnt = chr $cnt;
205
206 IO::AIO::aio_open $path, O_WRONLY | O_CREAT, 0666, sub {
207 my $fh = shift
208 or return;
209
210 # the truncate is 1440 extra syscalls, but saves 1439
211 # slow metadata updates.
212 IO::AIO::aio_truncate $fh, 1440, sub {
213 IO::AIO::aio_write $fh, $offs, 1, $cnt, 0, sub {
214 IO::AIO::aio_close $fh;
215 };
216 };
217 };
218};
219
220#############################################################################
221# export recentlog
222
223our %RECENT;
224
225our $update_w;
226our %need_update;
227
228sub _update_login {
229 my ($login) = @_;
230
231 my $path = (cf::player::playerdir $login) . "/playerdata";
232
233 if (0 >= aio_load $path, my $data) {
234 delete $RECENT{$login};
235 } else {
236 local $_ = $data;
237
238 my $birthdate = /^schmorplog_birthdate (\S+)$/m ? $1 : undef;
239 my $login_count = /^schmorplog_login_count (\S+)$/m ? $1 : 0;
240 my $death_count = /^schmorplog_death_count (\S+)$/m ? $1 : 0;
241 my $last_save = /^schmorplog_last_save (\S+)$/m ? $1 : undef;
242 my $last_login = /^schmorplog_last_login (\S+)$/m ? $1 : undef;
243 my $last_logout = /^schmorplog_last_logout (\S+)$/m ? $1 : undef;
244 my $client = /^schmorplog_client (.*)$/m ? $1 : "?";
245 my $map = /^map (.*)$/m ? $1 : "?";
246
247 return unless $last_login;
248
249 $last_logout = $last_save if $last_save > $last_logout && $last_login > $last_logout && $last_save < $NOW - 10 * 60;
250 $last_logout = undef if $last_logout < $last_login;
251
252 return unless $last_login > $NOW - 86400 * ($login_count * 7 + 10);
253
254# next if $count < 3 && $login < $NOW - 86400*2;
255 $RECENT{$login} = [$login, $birthdate, $last_login, $login_count, $last_logout, $client, $death_count, $map];
256 }
257}
258
259sub _update {
260 cf::async_ext {
261 my $t0 = EV::now;
262
263 $Coro::current->nice (1);
264 $Coro::current->{desc} = "recentlog updater";
265
266 Coro::AnyEvent::sleep 5; # grace time to allow file-saves
267
268 my $lock = cf::lock_acquire "export_recentlog";
269
270 while (%need_update) {
271 for (keys %need_update) {
272 delete $need_update{$_};
273
274 _update_login $_;
275 }
276 }
277
278 undef $update_w;
279
280 cf::get_slot 0.1, 0, "recentlog serialise";
281
282 my $NOW = $cf::NOW;
283
284 cf::replace_file $EXPORT_RECENTLOG, cf::encode_json {
285 version => 1,
286 date => $NOW,
287 data => [
288 sort { ($b->[4] || $NOW) <=> ($a->[4] || $NOW) }
289 values %RECENT
290 ],
291 } or warn "$EXPORT_RECENTLOG: $!";
292
293 cf::trace "recentlog updated (", EV::now - $t0, "s).\n";
294 };
295}
296
297sub update {
298 return unless defined $EXPORT_RECENTLOG;
299
300 $update_w ||= AE::timer $EXPORT_RECENTLOG_INTERVAL, 0, \&_update;
301}
302
303sub reload {
304 return unless defined $EXPORT_RECENTLOG;
305
306 my $lock = cf::lock_acquire "export_recentlog";
307
308 cf::async_ext {
309 $lock;
310
311 $Coro::current->{desc} = "recentlog reloader";
312
313 undef $need_update{$_}
314 for @{ +cf::player::list_logins };
315
316 _update;
317 };
318}
319
320cf::player->attach (
321 on_login => sub { undef $need_update{$_[0]->ob->name}; update },
322 on_logout => sub { undef $need_update{$_[0]->ob->name}; update },
323 on_birth => sub { undef $need_update{$_[0]->ob->name}; update },
324 on_death => sub { undef $need_update{$_[0]->ob->name}; update },
325# on_load => sub { undef $need_update{$_[0]->ob->name}; update },
326# on_save => sub { undef $need_update{$_[0]->ob->name}; update },
327);
328
329cf::post_init {
330 reload;
331};
332

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines