ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/board.ext
Revision: 1.1
Committed: Wed Aug 9 11:54:02 2006 UTC (17 years, 9 months ago) by elmex
Branch: MAIN
Log Message:
the ipo and board are now converted to JSON and the board works now too.

File Contents

# User Rev Content
1 elmex 1.1 #! perl
2    
3     use Data::Dumper;
4     use Carp;
5     cf::register_script_function "board::help" => sub {
6     # eval { Carp::confess; }; print "[[$@$!]]\n"; require Data::Dumper;
7     my ($board, $who, $npc) = @_;
8     $who->reply ($npc,
9     "Help for $board\n"
10     ."List of commands:\n\n"
11     ."- list\n"
12     ."- write <message>\n"
13     ."- remove <id>\n"
14     );
15     };
16    
17     cf::register_script_function "board::write" => sub {
18     my ($board, $msg, $who, $npc) = @_;
19     if ($msg =~ /\S/) {
20     CFBoard::write ($board, $who->name, $msg);
21     $who->reply ($npc, "Added to $board");
22     } else {
23     $who->reply ($npc, "Usage: write <message>\n");
24     }
25     1
26     };
27    
28     cf::register_script_function "board::list" => sub {
29     my ($board, $who, $npc) = @_;
30     my $cont = CFBoard::get ($board);
31     if (@$cont) {
32     $who->reply ($npc, "Content of $board:");
33     my $idx = 0;
34     for (@$cont) {
35     $who->reply ($npc, "<$idx> $_->[0]: $_->[1]");
36     $idx++;
37     }
38     } else {
39     $who->reply ($npc, "$board is empty.");
40     }
41     1
42     };
43    
44     cf::register_script_function "board::remove" => sub {
45     my ($board, $idx, $who, $npc) = @_;
46    
47     my $entry = CFBoard::get_idx ($board, $idx);
48     unless (defined $entry) {
49     $who->reply ($npc, "No such entry.");
50     return 1;
51     }
52    
53     if ($entry->[0] eq $who->name or $who->flag (cf::FLAG_WIZ)) {
54     my $e = CFBoard::remove_idx ($board, $idx);
55     $who->reply ($npc, "Removed entry $idx ($e->[0]: $e->[1]) from $board.");
56     } else {
57     $who->reply ($npc, "Access denied.");
58     }
59     1
60     };
61    
62    
63     package CFBoard;
64     use POSIX qw/strftime/;
65     use CFDB;
66    
67     my $BOARDDB = CFDB->new (db_file => cf::localdir . "/crossfireboard.perl");
68    
69     sub get {
70     my ($board) = @_;
71     $BOARDDB->get ($board);
72     }
73    
74     sub get_idx {
75     my ($board, $idx) = @_;
76     $board = $BOARDDB->get ($board);
77     return $board->[$idx];
78     }
79    
80     sub remove_idx {
81     my ($boardname, $idx) = @_;
82     my $board = $BOARDDB->get ($boardname);
83     my $e = splice @$board, $idx, 1;
84     $BOARDDB->set ($boardname, $board);
85     $e
86     }
87    
88     sub clear {
89     my ($board) = @_;
90     $BOARDDB->clear ($board);
91     }
92    
93     sub write {
94     my ($board, $from, $message) = @_;
95     my $boardentrys = $BOARDDB->get ($board);
96     push @$boardentrys, [$from, $message];
97     $BOARDDB->set ($board, $boardentrys);
98     }
99    
100     1;