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.17 by root, Thu May 1 06:47:03 2008 UTC vs.
Revision 1.30 by root, Sun Aug 14 13:19:30 2011 UTC

1#! perl # depends=irc 1#! perl # depends=irc mandatory
2 2
3# statistics-package 3# statistics-package
4 4
5use Fcntl; 5use Fcntl;
6use Coro::AIO; 6use Coro::AIO;
7
8our %PLAYERSEEN;
7 9
8our $UPDATE_LOGINS = EV::idle sub { 10our $UPDATE_LOGINS = EV::idle sub {
9 $_[0]->stop; 11 $_[0]->stop;
10 12
11 cf::async { 13 cf::async {
43); 45);
44 46
45cf::player->attach ( 47cf::player->attach (
46 on_login => sub { 48 on_login => sub {
47 my ($pl) = @_; 49 my ($pl) = @_;
50
48 $pl->ob->set_ob_key_value (schmorplog_last_login => time); 51 $pl->ob->kv_set (schmorplog_last_login => time);
49 52
50 $pl->ob->set_ob_key_value (schmorplog_login_count => 53 $pl->ob->kv_set (schmorplog_login_count =>
51 1 + $pl->ob->get_ob_key_value ("schmorplog_login_count")); 54 1 + $pl->ob->kv_get ("schmorplog_login_count"));
52 55
53 (my $client = $pl->ns->version) =~ s/\n/\\n/g; 56 $pl->ob->kv_set (schmorplog_client => $pl->ns->{who_version});
54 57
55 $pl->ob->set_ob_key_value (schmorplog_client => $client); 58 my $name = $pl->ob->name;
56 59
57 ext::irc::do_notice (sprintf "%s logged in", $pl->ob->name); 60 ext::irc::do_notice $name . " logged in";
58 61
62 undef $PLAYERSEEN{$name};
59 $UPDATE_LOGINS->start; 63 $UPDATE_LOGINS->start;
60 64
61 warn "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host; 65 cf::trace "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host;
62 }, 66 },
63 on_logout => sub { 67 on_logout => sub {
64 my ($pl, $cleanly) = @_; 68 my ($pl, $cleanly) = @_;
65 $pl->ob->set_ob_key_value (schmorplog_last_logout => time); 69 $pl->ob->kv_set (schmorplog_last_logout => time);
66 ext::irc::do_notice (sprintf "%s left", $pl->ob->name); 70 ext::irc::do_notice $pl->ob->name . " left";
67 71
68 $UPDATE_LOGINS->start; 72 $UPDATE_LOGINS->start;
69 73
70 warn "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)"; 74 cf::trace "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)";
71 }, 75 },
72 on_birth => sub { 76 on_birth => sub {
73 my ($pl) = @_; 77 my ($pl) = @_;
74 ext::irc::do_notice (sprintf "%s was just born", $pl->ob->name);
75 $pl->ob->set_ob_key_value (schmorplog_birthdate => time); 78 $pl->ob->kv_set (schmorplog_birthdate => time);
79 ext::irc::do_notice $pl->ob->name . " was just born";
76 80
77 warn "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host; 81 cf::trace "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host;
78 }, 82 },
79 on_quit => sub { 83 on_quit => sub {
80 my ($pl) = @_; 84 my ($pl) = @_;
81 ext::irc::do_notice (sprintf "%s quit the game", $pl->ob->name); 85 ext::irc::do_notice $pl->ob->name . " quit the game";
82 86
83 warn "QUIT: ", $pl->ob->name, " from ", $pl->ns->host; 87 cf::trace "QUIT: ", $pl->ob->name, " from ", $pl->ns->host;
84 }, 88 },
85 on_death => sub { 89 on_death => sub {
86 my ($pl) = @_; 90 my ($pl) = @_;
87 ext::irc::do_notice (sprintf "%s was killed by %s", $pl->ob->name, $pl->killer_name); 91
92 my $msg = $pl->expand_cfpod ($pl->ob->name . " was killed by " . $pl->killer_name . ".");
93 ext::irc::do_notice $msg;
88 94
89 ++$pl->ob->{stats_death}{$pl->killer_name}; 95 ++$pl->ob->{stats_death}{$pl->killer_name};
90 96
91 $pl->ob->set_ob_key_value (schmorplog_death_count => 97 $pl->ob->kv_set (schmorplog_death_count =>
92 1 + $pl->ob->get_ob_key_value ("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;
93 }, 102 },
94 on_load => sub { 103 on_load => sub {
95 my ($pl, $path) = @_; 104 my ($pl, $path) = @_;
96 $pl->ob->set_ob_key_value (schmorplog_last_load => time); 105 $pl->ob->kv_set (schmorplog_last_load => time);
97 }, 106 },
98 on_save => sub { 107 on_save => sub {
99 my ($pl, $path) = @_; 108 my ($pl, $path) = @_;
100 $pl->ob->set_ob_key_value (schmorplog_last_save => time); 109 $pl->ob->kv_set (schmorplog_last_save => time);
101 }, 110 },
102); 111);
103 112
104cf::register_script_function "statistician::talk" => sub { 113cf::register_script_function "statistician::talk" => sub {
105 my ($who, $msg, $npc) = @_; 114 my ($who, $msg, $npc) = @_;
114 or return $who->reply ($npc, "I don't know any person named '$args'."); 123 or return $who->reply ($npc, "I don't know any person named '$args'.");
115 124
116 my $s = $pl->ob->{stats_death} 125 my $s = $pl->ob->{stats_death}
117 or return $who->reply ($npc, "$args didn't die even once."); 126 or return $who->reply ($npc, "$args didn't die even once.");
118 127
119 my $rep = "$args died a total of " . $pl->ob->get_ob_key_value ("schmorplog_death_count") . " times, among them:\n\n\n\n"; 128 my $rep = "$args died a total of " . $pl->ob->kv_get ("schmorplog_death_count") . " times, among them:\n\n";
120 129
121 for (sort { $s->{$b} <=> $s->{$a} } keys %$s) { 130 for (sort { $s->{$b} <=> $s->{$a} } keys %$s) {
122 $rep .= "$s->{$_} times due to $_.\n\n"; 131 $rep .= sprintf " C<%4d> time(s) due to %s.\n", $s->{$_}, $_;
123 } 132 }
124 133
125 $who->reply ($npc, $rep); 134 $who->reply ($npc, $rep);
126 }; 135 };
127 } elsif ($cmd eq "kills") { 136 } elsif ($cmd eq "kills") {
130 or return $who->reply ($npc, "I don't know any person named '$args'."); 139 or return $who->reply ($npc, "I don't know any person named '$args'.");
131 140
132 my $s = $pl->ob->{stats_kill} 141 my $s = $pl->ob->{stats_kill}
133 or return $who->reply ($npc, "I don't know of I<anything> that $args has killed so far."); 142 or return $who->reply ($npc, "I don't know of I<anything> that $args has killed so far.");
134 143
135 my $rep = sprintf "\n\nC<%6s> %s\n\n", "#", "Name"; 144 my $rep = sprintf "Here is a list of all the kills I know about:\n\n";
136 for (sort { $s->{$a} <=> $s->{$b} } keys %$s) { 145 for (sort { $s->{$b} <=> $s->{$a} } keys %$s) {
137 $rep .= sprintf "C<%6s> %s\n\n", $s->{$_}, $_; 146 $rep .= sprintf " C<%6s> %s\n", $s->{$_}, $_;
138 } 147 }
139 148
140 $who->reply ($npc, $rep); 149 $who->reply ($npc, $rep);
141 }; 150 };
142 } elsif ($cmd eq "hi") { 151 } elsif ($cmd eq "hi") {
143 $who->reply ($npc, "Hello!\n\n" 152 $who->reply ($npc, "Hello!\n\n"
144 . "I am a statistician, I keep statistics about all people here.\n\n" 153 . "I am a statistician, I keep statistics about all people here.\n\n"
145 . "To know how often somebody died, ask C<deaths> I<playername>\n\n" 154 . " - To know how often somebody died, ask C<deaths> I<playername>\n"
146 . "To know how many kills somebody scored, ask C<kills> I<playername>"); 155 . " - To know how many kills somebody scored, ask C<kills> I<playername>");
147 } else { 156 } else {
148 $who->reply ($npc, "No idea what you want of me, how about saying 'hi' first?"); 157 $who->reply ($npc, "No idea what you want of me, how about saying 'hi' first?");
149 } 158 }
150 159
151 $cmd = lc $cmd; 160 $cmd = lc $cmd;
152}; 161};
153 162
163# log "crash" messages, i.e. client exit reasons
164cf::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
180our $STATSDIR = "$LOCALDIR/maxplayers";
181
182mkdir $STATSDIR;
183
184our $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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines