From 1077a12a0156f2c24083067f760a1afda91f99e9 Mon Sep 17 00:00:00 2001 From: El-BG-1970 Date: Fri, 29 Jul 2022 17:03:43 +0200 Subject: split add-item in add-item and update-item in inventory --- src/inventory.lisp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/inventory.lisp b/src/inventory.lisp index 429f666..657585e 100644 --- a/src/inventory.lisp +++ b/src/inventory.lisp @@ -34,22 +34,24 @@ (setq items (remove item items)) (setq filled (- filled (cdr item))))) +(defmethod update-item ((inv inventory) item qty old-item old-qty) + (with-slots (filled items) inv + (delete-item inv (cons old-item old-qty)) + (let ((total-quantity (+ qty old-qty))) + (push (cons item total-quantity) items) + (setq filled (+ filled total-quantity))))) + (defmethod add-item ((inv inventory) item quantity) ;; add of to inventory (when (> quantity 0) (with-slots (size filled items) inv (when (> size (+ filled quantity)) - (let* ((old-item - (find-item inv (commodities:name item))) - (qt - (if old-item - (progn - (delete-item inv old-item) - (+ quantity (cdr old-item))) - quantity))) - (prog1 (setq filled (+ filled qt)) - (push (cons item qt) - items))))))) + (let ((old-item (find-item inv (commodities:name item)))) + (if old-item + (update-item inv item quantity + (car old-item) (cdr old-item)) + (prog1 (setq filled (+ filled quantity)) + (push (cons item quantity) items)))))))) (defmethod remove-item ((inv inventory) name quantity) ;; remove of from inventory -- cgit v1.2.3