aboutsummaryrefslogtreecommitdiff
path: root/src/player.lisp
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2025-02-22 23:04:52 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2025-02-22 23:06:57 +0000
commit0a2a97ca7786308476c52490a5b842dfc065e1bb (patch)
treed31944dea7bd5db7dc36f18154d7ba7c5e9b0237 /src/player.lisp
parentee2234e9bbb69fe06cdc241eb1fac75bafa15fda (diff)
downloadcantedraw-0a2a97ca7786308476c52490a5b842dfc065e1bb.tar.gz
cantedraw-0a2a97ca7786308476c52490a5b842dfc065e1bb.tar.bz2
cantedraw-0a2a97ca7786308476c52490a5b842dfc065e1bb.zip
Extract error generation from `player-exists?` to ensure it's an indicator
Diffstat (limited to 'src/player.lisp')
-rw-r--r--src/player.lisp26
1 files 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))