ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/board.ext
Revision: 1.6
Committed: Sun Aug 27 15:23:30 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.5: +9 -7 lines
Log Message:
further conversion

File Contents

# Content
1 #! perl
2
3 sub do_help {
4 my ($board, $who, $npc) = @_;
5 $who->reply ($npc,
6 "Help for $board\n"
7 ."List of commands:\n\n"
8 ."- list\n"
9 ."- write <message>\n"
10 ."- remove <id>\n"
11 );
12 }
13
14 sub do_write {
15 my ($board, $msg, $who, $npc) = @_;
16 if ($msg =~ /\S/) {
17 CFBoard::write ($board, $who->name, $msg);
18 $who->reply ($npc, "Added entry.");
19 } else {
20 $who->reply ($npc, "Usage: write <message>\n");
21 }
22 1
23 }
24
25 sub do_list {
26 my ($board, $who, $npc) = @_;
27 my $cont = CFBoard::get ($board);
28 if (@{$cont || []}) {
29 $who->message ("$board content:", cf::NDI_BROWN | cf::NDI_UNIQUE);
30 my $idx = 0;
31 for (@$cont) {
32 $who->message ("<$idx> $_->[0]: $_->[1]", cf::NDI_BROWN | cf::NDI_UNIQUE);
33 $idx++;
34 }
35 } else {
36 $who->message ("$board is empty.", cf::NDI_BROWN | cf::NDI_UNIQUE);
37 }
38 1
39 }
40
41 sub do_remove {
42 my ($board, $idx, $who, $npc) = @_;
43
44 my $entry = CFBoard::get_idx ($board, $idx);
45 unless (defined $entry) {
46 $who->reply ($npc, "No such entry.");
47 return 1;
48 }
49
50 if ($entry->[0] eq $who->name or $who->flag (cf::FLAG_WIZ)) {
51 my $e = CFBoard::remove_idx ($board, $idx);
52 $who->reply ($npc, "Removed entry $idx ($e->[0]: $e->[1]).");
53 } else {
54 $who->reply ($npc, "Access denied.");
55 }
56
57 1
58 }
59
60 # this is the main command interface for the IPO NPC
61 cf::register_script_function "board::command" => sub {
62 my ($who, $msg, $npc) = @_;
63 my $board = $npc->name;
64
65 if ($msg =~ /^list$/i) {
66 do_list ($board, $who, $npc);
67 } elsif ($msg =~ /^write (.+)$/i) {
68 do_write ($board, $1, $who, $npc);
69 } elsif ($msg =~ /^remove (\d+)$/i) {
70 do_remove ($board, $1, $who, $npc);
71 } else {
72 do_help ($board, $who, $npc);
73 }
74
75 1
76 };
77
78 cf::register_attachment messageboard =>
79 on_apply => sub {
80 my ($npc, $who) = @_;
81 $who->reply ($npc, "Hello, i'm a talking board, 'say help' to get help");
82 do_list ($npc->name, $who, $npc);
83 cf::override;
84 },
85 ;
86
87 package CFBoard;
88
89 use POSIX qw/strftime/;
90 use CFDB;
91
92 my $BOARDDB = CFDB->new (db_file => cf::localdir . "/crossfireboard.perl");
93
94 sub get {
95 my ($board) = @_;
96 $BOARDDB->get ($board);
97 }
98
99 sub get_idx {
100 my ($board, $idx) = @_;
101 $board = $BOARDDB->get ($board);
102 return $board->[$idx];
103 }
104
105 sub remove_idx {
106 my ($boardname, $idx) = @_;
107 my $board = $BOARDDB->get ($boardname);
108 my $e = splice @$board, $idx, 1;
109 $BOARDDB->set ($boardname, $board);
110 $e
111 }
112
113 sub clear {
114 my ($board) = @_;
115 $BOARDDB->clear ($board);
116 }
117
118 sub write {
119 my ($board, $from, $message) = @_;
120 my $boardentrys = $BOARDDB->get ($board);
121 push @$boardentrys, [$from, $message];
122 $BOARDDB->set ($board, $boardentrys);
123 }
124
125 1;