revbank/plugins/market

68 lines
1.7 KiB
Perl

#!perl
HELP "market" => "Edit market list";
my $filename = 'revbank.market';
sub _read_market() {
my %market;
for (slurp $filename) {
/^\s*#/ and next;
/\S/ or next;
chomp;
my ($user, $id, $seller, $space, $description) = split " ", $_, 5;
$market{$id} = {
user => $user,
seller => $seller,
space => $space,
description => $description,
};
}
return \%market;
}
sub command :Tab(market,&tab) ($self, $cart, $command, @) {
if ($command eq 'market') {
require RevBank::TextEditor;
RevBank::TextEditor::edit($filename);
return ACCEPT;
}
my $product = _read_market->{ $command } or return NEXT;
my $username = parse_user( $product->{ user }) or return NEXT;
my $seller = parse_amount($product->{ seller }) or return NEXT;
my $space = parse_amount($product->{ space }) or return NEXT;
my $description = $product->{description};
my @existing = grep {
$_->attribute('plugin') eq $self->id and
$_->attribute('product_id') eq $command
} $cart->entries('plugin');
if (@existing) {
$existing[0]->quantity($existing[0]->quantity + 1);
$cart->select($existing[0]);
return ACCEPT;
}
$cart->add(
-($seller + $space),
"$description (sold by $username)",
{ product_id => $command, plugin => $self->id }
)->add_contra(
$username,
$seller,
"\$you bought $description"
)->add_contra(
"+sales/market",
$space,
"\$you bought $description from $username"
);
return ACCEPT;
}
sub tab {
return grep /\D/, keys %{ _read_market() };
}