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.7 by root, Wed Jan 24 16:29:10 2007 UTC vs.
Revision 1.22 by root, Fri Sep 19 01:39:45 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines