From 29b3eea131d7af25a5863677f6fe5b3931caef27 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Thu, 12 Dec 2019 23:16:10 +0100 Subject: [PATCH] market/products: reuse existing entry --- plugins/market | 24 ++++++++++++++++++++---- plugins/products | 12 +++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/plugins/market b/plugins/market index 0078a98..3c4a0b8 100644 --- a/plugins/market +++ b/plugins/market @@ -37,10 +37,26 @@ sub command :Tab(market,&tab) { my $space = parse_amount($product->{ space }) or return NEXT; my $description = $product->{description}; - $cart - ->add(-($seller + $space), "$description (sold by $username)", {product_id=>$command}) - ->add_contra($username, 0+$seller, "\$you bought $description") - if 0+$seller; + 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); + return ACCEPT; + } + + $cart->add( + -($seller + $space), + "$description (sold by $username)", + { product_id => $command, plugin => $self->id } + )->add_contra( + $username, + $seller, + "\$you bought $description" + ); + return ACCEPT; } diff --git a/plugins/products b/plugins/products index ba8c10c..bee1599 100644 --- a/plugins/products +++ b/plugins/products @@ -34,10 +34,20 @@ sub command :Tab(edit,&tab) { my $price = parse_amount( $product->{price} ) or return NEXT; + my @existing = grep { + $_->attribute('plugin') eq $self->id and + $_->attribute('product_id') eq $product->{id} + } $cart->entries('plugin'); + + if (@existing) { + $existing[0]->quantity($existing[0]->quantity + 1); + return ACCEPT; + } + $cart->add( -$price, $product->{description}, - { product_id => $product->{id} } + { product_id => $product->{id}, plugin => $self->id } ); return ACCEPT; }