aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2025-02-15 15:40:56 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2025-02-15 15:40:56 +0000
commit24b07f9166a3c100121143f0d59d3d5d167a82ad (patch)
treeba6a416cc7bb6f685e0d5632222e49a822928d6c
parent4ded442dd47babd9f3c0163ffe274f92d0e153a7 (diff)
downloadcantedraw-24b07f9166a3c100121143f0d59d3d5d167a82ad.tar.gz
cantedraw-24b07f9166a3c100121143f0d59d3d5d167a82ad.tar.bz2
cantedraw-24b07f9166a3c100121143f0d59d3d5d167a82ad.zip
Introduce custom errors for player handling
Custom errors for malformed/nonexistent player IDs as well as a player not having a high enough balance for the money requested. This will allow me to encode more information in the errors, for callers to use. I'm generally of the opinion that callers should be checking error conditions before calling these functions, but per Murphy's law it would be nice to have more information in the error message.
-rw-r--r--packages.lisp2
-rw-r--r--src/player.lisp28
2 files changed, 27 insertions, 3 deletions
diff --git a/packages.lisp b/packages.lisp
index 574da08..4c371c8 100644
--- a/packages.lisp
+++ b/packages.lisp
@@ -55,6 +55,8 @@
(:export
:player
:players
+ :error-player-nonexistent
+ :error-player-broke
:player-id :player-balance :player-hand
:player-exists? :player-bankrupt? :player-can-bet?
:player-pay
diff --git a/src/player.lisp b/src/player.lisp
index 7979b25..a5b1625 100644
--- a/src/player.lisp
+++ b/src/player.lisp
@@ -28,6 +28,25 @@
(deftype players () 'list)
+(define-condition error-player-nonexistent (error)
+ ((id :initarg :id :reader id))
+ (:report
+ (lambda (err stream)
+ (format stream "Player [~a] is non-existent or malformed"
+ (id err)))))
+
+(define-condition error-player-broke (error)
+ ((id :initarg :id :reader id)
+ (balance :initarg :balance :reader balance)
+ (required :initarg :required :reader required))
+ (:report
+ (lambda (err stream)
+ (format stream "Player [~a] has balance $~a but $~a requested."
+ (id err)
+ (balance err)
+ (required err)))))
+
+
(fn player-id (player) (-> (player) fixnum)
(car player))
@@ -50,10 +69,13 @@
(let ((p (assoc id players)))
(cond
((not (typep p 'player))
- (error "Player [~a] does not exist."))
+ (error 'error-player-nonexistent
+ :id id))
((not (player-can-bet? amount p))
- (error "Player [~a] has $~a but needs to pay ~a."
- id (player-balance p) amount))
+ (error 'error-player-broke
+ :id id
+ :balance (player-balance p)
+ :required amount))
(t
(destructuring-bind (id balance cards) p
(setf (cdr (assoc id players))