revbank/plugins/take
Juerd Waalboer fa60e1081a chmod 644 plugins/*
Undoes 714b337 because github seems to no longer require chmod +x
for syntax highlighting extensionless files.
2019-08-07 15:42:16 +02:00

71 lines
1.8 KiB
Perl

#!perl
HELP "take <account>... <amount> <reason>" => "Transfer money from them to you";
sub command :Tab(take,steal) {
my ($self, $cart, $command) = @_;
$command eq 'take' or $command eq 'steal'
or return NEXT;
$self->{users} = [];
return "User to take from", \&arg;
}
sub arg :Tab(USERS) {
my ($self, $cart, $arg) = @_;
my @users = @{ $self->{users} };
my $amount = parse_amount($arg);
if (@users and $amount) {
my $each = sprintf "%.2f", $amount / @users;
my $total = sprintf "%.2f", @users * $each;
if ($total != $amount) {
print "Adjusted total amount to $total because of rounding.\n";
}
$self->{each} = $each;
$self->{total} = $total;
my $them = @users == 1 ? $users[0] : 'each';
return sprintf("Why are you taking %.2f from %s?", $each, $them),
\&reason;
}
my $user = parse_user($arg);
if ($user) {
push @{ $self->{users} }, $user;
} else {
return REJECT, "$arg: No such user" .
($amount ? "." : ", and not a valid amount.");
}
return "User to take from, or total amount to finish", \&arg;
}
sub reason :Tab(bbq,NOABORT) { # finish
my ($self, $cart, $reason) = @_;
return REJECT, "'$reason' is a username, not a description :)."
if parse_user($reason);
return REJECT, "'$reason' is an amount, not a description :)."
if parse_amount($reason);
my @users = @{ $self->{users} };
my $each = $self->{each};
my $total = $self->{total};
for my $user (@users) {
$cart->add( $user, -$each, "Taken by \$you ($reason)" );
}
my $users = join '/', @users;
$cart->add( undef, $total, "Taken from $users ($reason)" );
return ACCEPT;
}