1 |
pippijn |
1.1 |
#! perl |
2 |
|
|
|
3 |
|
|
# Access levels are: |
4 |
|
|
# 0 = read |
5 |
|
|
# 1 = write/remove |
6 |
|
|
sub access { |
7 |
|
|
my ($ob, $guild) = @_; |
8 |
|
|
|
9 |
|
|
return 1 |
10 |
|
|
if $ob->flag (cf::FLAG_WIZ); |
11 |
|
|
|
12 |
|
|
List::Util::max |
13 |
pippijn |
1.3 |
map $_->type == cf::FORCE && $_->slaying eq "$guild master" ? 1 : 0, |
14 |
pippijn |
1.1 |
$ob->inv |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
sub do_help { |
18 |
|
|
my ($guildrules, $who, $npc) = @_; |
19 |
|
|
$who->reply ($npc, |
20 |
|
|
"Help for $guildrules\n" |
21 |
|
|
."List of commands:\n\n" |
22 |
|
|
."- list\n" |
23 |
|
|
."- write <message>\n" |
24 |
|
|
."- remove <id>\n" |
25 |
|
|
); |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
sub do_write { |
29 |
|
|
my ($guildrules, $msg, $who, $npc) = @_; |
30 |
|
|
my $guild = $guildrules; |
31 |
|
|
$guild =~ s/Rules for //; |
32 |
|
|
return $who->reply ($npc, "Access denied.") |
33 |
|
|
unless access $who, $guild; |
34 |
|
|
|
35 |
|
|
if ($msg =~ /\S/) { |
36 |
|
|
CFguildrules::put_entry ($guildrules, $who->name, $msg); |
37 |
|
|
$who->reply ($npc, "Added entry."); |
38 |
|
|
} else { |
39 |
|
|
$who->reply ($npc, "Usage: write <message>\n"); |
40 |
|
|
} |
41 |
|
|
1 |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
sub do_list { |
45 |
|
|
my ($guildrules, $who, $npc) = @_; |
46 |
|
|
my $cont = CFguildrules::get ($guildrules); |
47 |
|
|
if (@{$cont || []}) { |
48 |
|
|
$who->reply ($npc, "My content:"); |
49 |
|
|
my $idx = 0; |
50 |
|
|
for (@$cont) { |
51 |
|
|
$who->reply ($npc, "<$idx> $_->[0]: $_->[1]"); |
52 |
|
|
$idx++; |
53 |
|
|
} |
54 |
|
|
} else { |
55 |
|
|
$who->reply ($npc, "There are no rules."); |
56 |
|
|
} |
57 |
|
|
1 |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
sub do_remove { |
61 |
|
|
my ($guildrules, $idx, $who, $npc) = @_; |
62 |
|
|
|
63 |
|
|
my $entry = CFguildrules::get_entry ($guildrules, $idx); |
64 |
|
|
unless (defined $entry) { |
65 |
|
|
$who->reply ($npc, "No such entry."); |
66 |
|
|
return 1; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
if ($entry->[0] eq $who->name or $who->flag (cf::FLAG_WIZ)) { |
70 |
|
|
my $e = CFguildrules::remove_entry ($guildrules, $idx); |
71 |
|
|
$who->reply ($npc, "Removed entry $idx ($e->[0]: $e->[1])."); |
72 |
|
|
} else { |
73 |
|
|
$who->reply ($npc, "Access denied."); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
1 |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
# this is the main command interface for the IPO NPC |
80 |
|
|
cf::register_script_function "guildrules::command" => sub { |
81 |
|
|
my ($who, $msg, $npc) = @_; |
82 |
|
|
my $guildrules = $npc->name; |
83 |
|
|
|
84 |
|
|
if ($msg =~ /^list$/i) { |
85 |
|
|
do_list ($guildrules, $who, $npc); |
86 |
|
|
} elsif ($msg =~ /^write (.+)$/i) { |
87 |
|
|
do_write ($guildrules, $1, $who, $npc); |
88 |
|
|
} elsif ($msg =~ /^remove (\d+)$/i) { |
89 |
|
|
do_remove ($guildrules, $1, $who, $npc); |
90 |
|
|
} else { |
91 |
|
|
do_help ($guildrules, $who, $npc); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
1 |
95 |
|
|
}; |
96 |
|
|
|
97 |
|
|
cf::object::attachment guildrules => |
98 |
|
|
on_apply => sub { |
99 |
|
|
my ($npc, $who) = @_; |
100 |
|
|
$who->reply ($npc, "Hello, I'm a rules sign. Say 'help' for a list of commands."); |
101 |
|
|
do_list ($npc->name, $who, $npc); |
102 |
|
|
cf::override; |
103 |
|
|
}, |
104 |
|
|
; |
105 |
|
|
|
106 |
|
|
package CFguildrules; |
107 |
|
|
|
108 |
|
|
use POSIX qw/strftime/; |
109 |
|
|
use CFDB; |
110 |
|
|
|
111 |
root |
1.2 |
my $GUILDRULESDB = cf::db_get guildrules => "data"; |
112 |
pippijn |
1.1 |
|
113 |
|
|
sub get { |
114 |
|
|
my ($guildrules) = @_; |
115 |
|
|
|
116 |
|
|
$GUILDRULESDB->{"msg_$guildrules"} ||= [] |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
sub get_entry { |
120 |
|
|
my ($guildrules, $idx) = @_; |
121 |
|
|
|
122 |
|
|
$GUILDRULESDB->{"msg_$guildrules"}[$idx] |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
sub remove_entry { |
126 |
|
|
my ($guildrules, $idx) = @_; |
127 |
|
|
|
128 |
|
|
my $entry = splice @{ $GUILDRULESDB->{"msg_$guildrules"} ||= [] }, $idx, 1; |
129 |
root |
1.2 |
cf::db_put guildrules => data => $GUILDRULESDB; |
130 |
pippijn |
1.1 |
$entry |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
sub clear { |
134 |
|
|
my ($guildrules) = @_; |
135 |
|
|
|
136 |
|
|
delete $GUILDRULESDB->{"msg_$guildrules"}; |
137 |
root |
1.2 |
cf::db_put guildrules => data => $GUILDRULESDB; |
138 |
pippijn |
1.1 |
} |
139 |
|
|
|
140 |
|
|
sub put_entry { |
141 |
|
|
my ($guildrules, $from, $message) = @_; |
142 |
|
|
|
143 |
|
|
my $entries = $GUILDRULESDB->{"msg_$guildrules"} ||= []; |
144 |
|
|
push @$entries, [$from, $message]; |
145 |
root |
1.2 |
cf::db_put guildrules => data => $GUILDRULESDB; |
146 |
pippijn |
1.1 |
} |
147 |
|
|
|
148 |
|
|
1; |