From 0a2a97ca7786308476c52490a5b842dfc065e1bb Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sat, 22 Feb 2025 23:04:52 +0000 Subject: Extract error generation from `player-exists?` to ensure it's an indicator --- src/player.lisp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/player.lisp b/src/player.lisp index a2116ee..5807ac5 100644 --- a/src/player.lisp +++ b/src/player.lisp @@ -40,30 +40,32 @@ (id err) (balance err) (required err))))) (fn player-exists? (id table) (-> (symbol hash-table) boolean) - (if (or (null (gethash id table)) - (not (player-p (gethash id table)))) - (error 'error-player-nonexistent :id id) - t)) + (and (gethash id table) + (player-p (gethash id table)))) + +(defun error-if-no-player (id table) + (unless (player-exists? id table) + (error 'error-player-nonexistent :id id))) (fn player-can-pay? (id table amount) (-> (symbol hash-table fixnum) boolean) - (player-exists? id table) + (error-if-no-player id table) (->> (gethash id table) player-balance (<= amount))) (fn player-bankrupt? (id table) (-> (symbol hash-table) boolean) - (player-exists? id table) + (error-if-no-player id table) (->> (gethash id table) player-balance (>= 0))) (fn player-debit (id table amount) (-> (symbol hash-table fixnum) fixnum) - (player-exists? id table) - (if (not (player-can-pay? id table amount)) - (error 'error-player-broke - :id id :balance (player-balance (gethash id table)) :required amount)) + (error-if-no-player id table) + (unless (player-can-pay? id table amount) + (error 'error-player-broke + :id id :balance (player-balance (gethash id table)) :required amount)) (decf (player-balance (gethash id table)) amount)) (fn player-credit (id table amount) (-> (symbol hash-table fixnum) fixnum) - (player-exists? id table) + (error-if-no-player id table) (incf (player-balance (gethash id table)) amount)) (fn player-set-cards (id table cards) (-> (symbol hash-table cardset) t) - (player-exists? id table) + (error-if-no-player id table) (setf (player-hand (gethash id table)) cards)) -- cgit v1.2.3-13-gbd6f