ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/help.ext
Revision: 1.20
Committed: Tue Nov 20 14:30:22 2012 UTC (11 years, 6 months ago) by root
Branch: MAIN
Changes since 1.19: +10 -9 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.18 #! perl # mandatory depends=doclet
2 pippijn 1.1
3 root 1.2 our $TOPIC;
4 root 1.18 our %DOCLET;
5 root 1.2
6 root 1.12 our $HELP_CHANNEL = {
7     id => "help",
8     title => "Help",
9     reply => "help ",
10     tooltip => "Online Help",
11     };
12    
13 root 1.18 # considerable duplication between load_doclets and load_topics
14     sub load_doclets {
15     %DOCLET = ();
16    
17     my @command_list;
18    
19     for (
20     [0, "command_help"],
21     [1, "emote_help"],
22     [2, "dmcommand_help"],
23     ) {
24     my ($type, $path) = @$_;
25    
26     my $paragraphs = cf::pod::load_pod "$PODDIR/$path.pod"
27     or die "unable to load $path";
28    
29     my $level = 1e9;
30     my $rpar;
31    
32     for my $par (@$paragraphs) {
33     if ($par->{type} eq "head2") {
34     # this code taken almost verbatim from DC/Protocol.pm
35    
36     if ($par->{markup} =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x) {
37     my $cmd = $1;
38     my @args = split /\|/, $2;
39     @args = (".*") unless @args;
40    
41     $_ = $_ eq ".*" ? "" : " $_"
42     for @args;
43    
44     my @variants = map "$cmd$_", sort { (length $a) <=> (length $b) } @args;
45    
46     $rpar = \($DOCLET{$cmd} = &cf::pod::as_cfpod ([$par]));
47    
48 root 1.20 push @{ $command_list[$type] }, @variants;
49 root 1.18 $level = $par->{level};
50     } else {
51     cf::error "$par->{markup}: unparsable command heading";
52     }
53     } elsif ($par->{level} > $level) {
54     $$rpar .= &cf::pod::as_cfpod ([$par]);
55     }
56    
57     cf::cede_to_tick;
58     }
59     }
60    
61 root 1.20 cf::cede_to_tick;
62 root 1.18
63 root 1.20 use Data::Dump; ddx \@command_list;#d#
64 root 1.18
65 root 1.20 cf::client::set_command_faces
66     map {
67     cf::face::set
68     "command_list/$_" => cf::FT_RSRC,
69     JSON::XS->new->utf8->encode ([ sort @{ $command_list[$_] } ])
70     }
71     0..$#command_list;
72 root 1.18 }
73    
74     our $DOCLET_HANDLER = ext::doclet::register command => sub {
75     my ($pl, $category, $command) = @_;
76    
77     my $guard = cf::lock_acquire "ext::help::loading";
78    
79     $DOCLET{$command}
80     || "B<No documentation available for '$category/$command'>"
81     };
82    
83 root 1.3 sub load_topics($$) {
84     my ($type, $path) = @_;
85 root 1.2
86     my $paragraphs = cf::pod::load_pod "$PODDIR/$path.pod"
87     or die "unable to load $path";
88    
89     my @topics;
90     my $level = 1e9;
91    
92     for my $par (@$paragraphs) {
93 root 1.11 cf::cede_to_tick;
94 root 1.2 if ($par->{type} eq "head2") {
95     if ($par->{markup} =~ /^(\S+)/) {
96 root 1.3 push @topics, $1 => [$type => $par];
97 root 1.2 $level = $par->{level};
98     }
99     } elsif ($par->{level} > $level) {
100     push @{ $topics[-1] }, $par;
101     }
102     }
103    
104     @topics
105     }
106    
107     sub reload() {
108 root 1.18 my $guard1 = cf::lock_acquire "ext::help::loading";
109     my $guard2 = cf::lock_acquire "ext::resource";
110 root 1.15
111     local $Coro::current->{desc} = "help loader";
112    
113 root 1.2 $TOPIC = {
114 root 1.3 (load_topics "DM Commands" => "dmcommand_help"),
115     (load_topics "Emotes" => "emote_help"),
116     (load_topics "Commands" => "command_help"),
117     (load_topics "Generic Help Topics" => "generic_help"),
118 root 1.2 };
119 root 1.18
120     load_doclets;
121 root 1.13
122     ()
123 root 1.2 }
124    
125 root 1.15 cf::post_init {
126 root 1.17 cf::async_ext { reload };
127 root 1.18 Coro::cede; # make sure reload acquires the lock(s)
128 root 1.2 };
129    
130 root 1.3 cf::register_command help => sub {
131 root 1.2 my ($pl, $topic) = @_;
132    
133     if (cf::lock_active "ext::help::loading") {
134 root 1.12 $pl->send_msg ($HELP_CHANNEL => "help files are being loaded currently, try again in a few seconds.", cf::NDI_REPLY | cf::NDI_CLEAR);
135 root 1.2 return;
136     }
137 root 1.3
138     $topic = $1 if $topic =~ /(\S+)/;
139    
140     if (!length $topic) {
141     # sort..
142    
143     my %topics;
144     while (my ($k, $v) = each %$TOPIC) {
145     push @{$topics{$v->[0]}}, $k;
146     }
147    
148     my $res;
149     while (my ($k, $v) = each %topics) {
150 root 1.14 $res .= "T<$k:>\n\n" . (join " ", sort @$v) . "\n\n";
151 root 1.3 }
152    
153 root 1.12 $pl->send_msg ($HELP_CHANNEL => $res, cf::NDI_REPLY | cf::NDI_CLEAR);
154 root 1.3
155 root 1.7 } elsif (my $item = $TOPIC->{$topic}) {
156     my ($type, @pars) = @$item;
157 root 1.12 $pl->send_msg ($HELP_CHANNEL => (cf::pod::as_cfpod \@pars), cf::NDI_REPLY | cf::NDI_CLEAR);
158 root 1.3
159     } else {
160 root 1.12 $pl->send_msg ($HELP_CHANNEL => "'$topic' no such help topic, try just 'help' to get a list of topics.", cf::NDI_REPLY);
161 root 1.3 }
162 root 1.2 };
163 root 1.3