ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/schmorplog.ext
Revision: 1.22
Committed: Fri Sep 19 01:39:45 2008 UTC (15 years, 8 months ago) by root
Branch: MAIN
Changes since 1.21: +7 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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.4 (my $client = $pl->ns->version) =~ s/\n/\\n/g;
54 root 1.1
55 root 1.19 $pl->ob->kv_set (schmorplog_client => $client);
56 root 1.1
57 root 1.9 ext::irc::do_notice (sprintf "%s logged in", $pl->ob->name);
58 root 1.1
59     $UPDATE_LOGINS->start;
60 root 1.5
61     warn "LOGIN: ", $pl->ob->name, " from ", $pl->ns->host;
62 root 1.1 },
63     on_logout => sub {
64 root 1.6 my ($pl, $cleanly) = @_;
65 root 1.19 $pl->ob->kv_set (schmorplog_last_logout => time);
66 root 1.9 ext::irc::do_notice (sprintf "%s left", $pl->ob->name);
67 root 1.1
68     $UPDATE_LOGINS->start;
69 root 1.5
70 root 1.6 warn "LOGOUT: ", $pl->ob->name, " from ", $pl->ns->host, " ($cleanly)";
71 root 1.1 },
72     on_birth => sub {
73     my ($pl) = @_;
74 root 1.9 ext::irc::do_notice (sprintf "%s was just born", $pl->ob->name);
75 root 1.19 $pl->ob->kv_set (schmorplog_birthdate => time);
76 root 1.5
77     warn "BIRTH: ", $pl->ob->name, " from ", $pl->ns->host;
78 root 1.1 },
79     on_quit => sub {
80     my ($pl) = @_;
81 root 1.9 ext::irc::do_notice (sprintf "%s quit the game", $pl->ob->name);
82 root 1.5
83     warn "QUIT: ", $pl->ob->name, " from ", $pl->ns->host;
84 root 1.1 },
85     on_death => sub {
86     my ($pl) = @_;
87 root 1.22
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 root 1.1
91 root 1.14 ++$pl->ob->{stats_death}{$pl->killer_name};
92    
93 root 1.19 $pl->ob->kv_set (schmorplog_death_count =>
94     1 + $pl->ob->kv_get ("schmorplog_death_count"));
95 root 1.22
96     $_->send_msg ("" => $msg, cf::NDI_VERBATIM)
97     for cf::player::list;
98 root 1.1 },
99     on_load => sub {
100     my ($pl, $path) = @_;
101 root 1.19 $pl->ob->kv_set (schmorplog_last_load => time);
102 root 1.1 },
103     on_save => sub {
104     my ($pl, $path) = @_;
105 root 1.19 $pl->ob->kv_set (schmorplog_last_save => time);
106 root 1.1 },
107 root 1.3 );
108 root 1.1
109 root 1.17 cf::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 root 1.20 my $rep = "$args died a total of " . $pl->ob->kv_get ("schmorplog_death_count") . " times, among them:\n\n\n\n";
125 root 1.17
126     for (sort { $s->{$b} <=> $s->{$a} } keys %$s) {
127 root 1.18 $rep .= "$s->{$_} time(s) due to $_.\n\n";
128 root 1.17 }
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 root 1.18 my $rep = sprintf "\n\nC<%6s> %s\n\n", "Kills", "Name";
141 root 1.17 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 root 1.21 # log "crash" messages, i.e. client exit reasons
160     cf::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