diff options
| -rw-r--r-- | src/inventory.lisp | 24 |
1 files 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 <quantity> of <item> 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 <quantity> of <item> from inventory |
