ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/schmorplog.ext
Revision: 1.30
Committed: Sun Aug 14 13:19:30 2011 UTC (12 years, 9 months ago) by root
Branch: MAIN
Changes since 1.29: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl # depends=irc mandatory
2
3 # statistics-package
4
5 use Fcntl;
6 use Coro::AIO;
7
8 our %PLAYERSEEN;
9
10 our $UPDATE_LOGINS = EV::idle sub {
11 $_[0]->stop;
12
13 cf::async {
14 my ($status, @pl) = ext::commands::who_listing;
15
16 my $fh = aio_open "$LOCALDIR/usercount", O_WRONLY | O_CREAT | O_TRUNC, 0644
17 or return;
18 aio_write $fh, 0, undef, scalar @pl, 0;
19 aio_close $fh;
20
21 my $who;
22 for ($status, @pl) {
23 s/[<&]//g;
24 $who .= "$_<br />\n";
25 }
26
27 my $fh = aio_open "$LOCALDIR/userlisting.html", O_WRONLY | O_CREAT | O_TRUNC, 0644
28 or return;
29 aio_write $fh, 0, undef, $who, 0;
30 aio_close $fh;
31 };
32 };
33
34 cf::object->attach (
35 on_kill => sub {
36 my ($ob, $hitter) = @_;
37
38 return unless $hitter;
39 $hitter = $hitter->outer_owner;
40 my $pl = $hitter->contr
41 or return;
42
43 ++$hitter->{stats_kill}{$ob->name};
44 },
45 );
46
47 cf::player->attach (
48 on_login => sub {
49 my ($pl) = @_;
50
51 $pl->ob->kv_set (schmorplog_last_login => time);
52
53 $pl->ob->kv_set (schmorplog_login_count =>
54 1 + $pl->ob->kv_get ("schmorplog_login_count"));
55
56 $pl->ob->kv_set (schmorplog_client => $pl->ns->{who_version});
57
58 my $name = $pl->ob->name;
59
60 ext::irc::do_notice $name . " logged in";
61
62 undef $PLAYERSEEN{$name};
63 $UPDATE_LOGINS->start;
64
65 cf::trace "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host;
66 },
67 on_logout => sub {
68 my ($pl, $cleanly) = @_;
69 $pl->ob->kv_set (schmorplog_last_logout => time);
70 ext::irc::do_notice $pl->ob->name . " left";
71
72 $UPDATE_LOGINS->start;
73
74 cf::trace "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)";
75 },
76 on_birth => sub {
77 my ($pl) = @_;
78 $pl->ob->kv_set (schmorplog_birthdate => time);
79 ext::irc::do_notice $pl->ob->name . " was just born";
80
81 cf::trace "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host;
82 },
83 on_quit => sub {
84 my ($pl) = @_;
85 ext::irc::do_notice $pl->ob->name . " quit the game";
86
87 cf::trace "QUIT: ", $pl->ob->name, " from ", $pl->ns->host;
88 },
89 on_death => sub {
90 my ($pl) = @_;
91
92 my $msg = $pl->expand_cfpod ($pl->ob->name . " was killed by " . $pl->killer_name . ".");
93 ext::irc::do_notice $msg;
94
95 ++$pl->ob->{stats_death}{$pl->killer_name};
96
97 $pl->ob->kv_set (schmorplog_death_count =>
98 1 + $pl->ob->kv_get ("schmorplog_death_count"));
99
100 $_->send_msg ("" => $msg, cf::NDI_VERBATIM)
101 for cf::player::list;
102 },
103 on_load => sub {
104 my ($pl, $path) = @_;
105 $pl->ob->kv_set (schmorplog_last_load => time);
106 },
107 on_save => sub {
108 my ($pl, $path) = @_;
109 $pl->ob->kv_set (schmorplog_last_save => time);
110 },
111 );
112
113 cf::register_script_function "statistician::talk" => sub {
114 my ($who, $msg, $npc) = @_;
115
116 my ($cmd, $args) = split /\s+/, $msg, 2;
117
118 $args ||= $who->name;
119
120 if ($cmd eq "deaths") {
121 cf::async {
122 my $pl = cf::player::find $args
123 or return $who->reply ($npc, "I don't know any person named '$args'.");
124
125 my $s = $pl->ob->{stats_death}
126 or return $who->reply ($npc, "$args didn't die even once.");
127
128 my $rep = "$args died a total of " . $pl->ob->kv_get ("schmorplog_death_count") . " times, among them:\n\n";
129
130 for (sort { $s->{$b} <=> $s->{$a} } keys %$s) {
131 $rep .= sprintf " C<%4d> time(s) due to %s.\n", $s->{$_}, $_;
132 }
133
134 $who->reply ($npc, $rep);
135 };
136 } elsif ($cmd eq "kills") {
137 cf::async {
138 my $pl = cf::player::find $args
139 or return $who->reply ($npc, "I don't know any person named '$args'.");
140
141 my $s = $pl->ob->{stats_kill}
142 or return $who->reply ($npc, "I don't know of I<anything> that $args has killed so far.");
143
144 my $rep = sprintf "Here is a list of all the kills I know about:\n\n";
145 for (sort { $s->{$b} <=> $s->{$a} } keys %$s) {
146 $rep .= sprintf " C<%6s> %s\n", $s->{$_}, $_;
147 }
148
149 $who->reply ($npc, $rep);
150 };
151 } elsif ($cmd eq "hi") {
152 $who->reply ($npc, "Hello!\n\n"
153 . "I am a statistician, I keep statistics about all people here.\n\n"
154 . " - To know how often somebody died, ask C<deaths> I<playername>\n"
155 . " - To know how many kills somebody scored, ask C<kills> I<playername>");
156 } else {
157 $who->reply ($npc, "No idea what you want of me, how about saying 'hi' first?");
158 }
159
160 $cmd = lc $cmd;
161 };
162
163 # log "crash" messages, i.e. client exit reasons
164 cf::register_exticmd clientlog => sub {
165 my ($ns, $msg) = @_;
166
167 my $name = $ns->pl && $ns->pl->ob ? $ns->pl->ob->name : "<unknown>";
168
169 $msg =~ y/\x0a\x20-\x7f//cd;
170 $msg =~ s/\s+$//;
171
172 cf::error sprintf "clientlog [%s/%s]: %s\n", $ns->host, $name, $msg;
173
174 ()
175 };
176
177 # log max playercount every minute
178 #############################################################################
179
180 our $STATSDIR = "$LOCALDIR/maxplayers";
181
182 mkdir $STATSDIR;
183
184 our $WRITE_MAXPLAYERS = EV::periodic 0, 60, undef, sub {
185 my $now = AE::now;
186 my $cnt = scalar keys %PLAYERSEEN;
187
188 %PLAYERSEEN = map +($_->ob->name => undef), grep $_->ns, cf::player::list;
189
190 my @time = gmtime $now;
191
192 my $path = sprintf "%s/%04d-%02d-%02d", $STATSDIR, $time[5] + 1900, $time[4] + 1, $time[3];
193 my $offs = $time [2] * 60 + $time [1];
194
195 $cnt++;
196 $cnt = 254 if $cnt > 254;
197 $cnt = chr $cnt;
198
199 IO::AIO::aio_open $path, O_WRONLY | O_CREAT, 0666, sub {
200 my $fh = shift
201 or return;
202
203 # the truncate is 1440 extra syscalls, but saves 1439
204 # slow metadata updates.
205 IO::AIO::aio_truncate $fh, 1440, sub {
206 IO::AIO::aio_write $fh, $offs, 1, $cnt, 0, sub {
207 IO::AIO::aio_close $fh;
208 };
209 };
210 };
211 };
212