diff options
Diffstat (limited to 'src/wasp-user.el')
| -rw-r--r-- | src/wasp-user.el | 231 |
1 files changed, 100 insertions, 131 deletions
diff --git a/src/wasp-user.el b/src/wasp-user.el index 08e5c971..de5df6f8 100644 --- a/src/wasp-user.el +++ b/src/wasp-user.el @@ -9,139 +9,108 @@ (defvar w/user-whitelist nil) -(defvar w/user-current-name nil) -(defvar w/user-current nil) - -(defvar w/user-cache (ht-create) - "A read-only cache of user records for the current session.") - -(defun w/user-cache-update (nm d) - "Set the cache entry for user NM to D." - (ht-set! w/user-cache (s-downcase nm) d)) - -(defun w/user-cache-get (nm) - "Get the cache entry for user NM." - (ht-get w/user-cache (s-downcase nm))) - -(defun w/user-cache-populate () - "Populate `w/user-cache' with entries for all users. -\(This is slow, so it happens once at startup.\)" - (ht-clear! w/user-cache) - (w/db-keys - "user:*" - (lambda (users) - (--each users - (let ((nm (cadr (s-split ":" it)))) - (w/user-get - nm - (lambda (_) - (message "Updated cache for %s" nm)))))))) - -(defun w/user-db-key (nm) - "Return the database key for user NM." - (s-concat "user:" (s-downcase nm))) - -(defun w/user-get (nm k) - "Fetch user data for user NM. -Pass the resulting Lisp form to K." - (when (and nm (stringp nm) (functionp k)) - (w/db-get - (w/user-db-key nm) - (lambda (d) - (if-let* - ((d) - (stringp d) - (res (w/read-sexp d))) - (progn - (w/user-cache-update nm res) - (funcall k res)) - (funcall k nil)))))) - -(defun w/user-set (nm d) - "Save the Lisp form D as the user data for NM." - (when (and nm (stringp nm) d) - (w/user-cache-update nm d) - (w/db-set - (w/user-db-key nm) - (format "%S" d)))) +(defun w/user-id-from-name (nm k) + "Pass user ID corresponding to NM to K." + (let ((key (s-concat "user-id:" (s-downcase nm)))) + (w/db-get key + (lambda (uid) + (if (s-present? uid) + (funcall k uid) + (w/twitch-get-user-id nm + (lambda (v) + (w/db-set key v) + (funcall k v)))))))) (defun w/user-bind (nm k) - "Bind the data for user NM to `w/user-current' during K. -Save it back to the database after K returns." - (w/user-get - nm - (lambda (d) - (let ((w/user-current d) - (w/user-current-name nm)) - (funcall k) - (w/user-set nm w/user-current))))) - -(defun w/user-authorized () - "Return non-nil if the current user is authorized to use advanced techniques." - (let ((boost (alist-get :boost w/user-current))) - (or (and boost (> boost 2)) - (and boost (< boost -2)) - (-contains? w/user-whitelist (s-downcase w/user-current-name))))) - -(defun w/user-boost (user) - "Increase USER's boost power by 1." - (w/user-get - user - (lambda (d) - (cl-incf (alist-get :boost d 0)) - (w/user-set user d)))) - -(defun w/user-tsoob (user) - "Decrement USER's boost power by 1." - (w/user-get - user - (lambda (d) - (cl-decf (alist-get :boost d 0)) - (w/user-set user d)))) - -(defun w/user-add-bookrec (user book) - "Add a recommendation for BOOK from USER." - (w/user-get - "__books__" - (lambda (b) - (w/user-set "__books__" (cons (cons book user) b))))) - -(defun w/user-add-quote (user q) - "Add a recommendation for BOOK from USER." - (w/user-get - "__quotes__" - (lambda (qs) - (w/user-set "__quotes__" (cons (cons q user) qs))))) - -(defun w/user-crown (user) - "Increment USER's equity status." - (w/user-get - user - (lambda (u) - (let ((old (or (alist-get :equity u) 0))) - (setf (alist-get :equity u) (+ old 1))) - (print u) - (w/user-set user u)))) - -(defun w/user-decrown (user) - "Decrement USER's equity status." - (w/user-get - user - (lambda (u) - (let ((old (or (alist-get :equity u) 0))) - (setf (alist-get :equity u) (- old 1))) - (print u) - (w/user-set user u)))) - -(defun w/user-boost-compensation (user) - "Give USER a consolation BOOSTPOWER prize." - (w/user-get - user - (lambda (u) - (let ((old (or (alist-get :boost u) 0))) - (setf (alist-get :boost u) (+ old 20))) - (print u) - (w/user-set user u)))) + "Pass the ID and NM for the named user to K." + (w/user-id-from-name nm + (lambda (uid) + (funcall k uid nm)))) + +(defun w/user-get-stat (uid stat k) + "Get STAT from user UID and pass it to K." + (when (stringp uid) + (let ((key (s-concat "user:stats:" uid))) + (w/db-hget key stat + (lambda (v) + (funcall k + (if-let* + ( (_ v) + (_ (s-present? v)) + (cur (string-to-number v)) + (_ (integerp cur))) + cur + nil))))))) + +(defun w/user-set-stat (uid stat val) + "Set STAT for user UID to VAL." + (when (stringp uid) + (let ((key (s-concat "user:stats:" uid))) + (if (integerp val) + (w/db-hset key stat (format "%s" val)) + (message "Attempted to write stat %s to non-integer %s" stat val))))) + +(defun w/user-get-property (uid prop k) + "Get PROP from user UID and pass it to K." + (when (stringp uid) + (let ((key (s-concat "user:properties:" uid))) + (w/db-hget key prop + (lambda (v) + (funcall k (if (s-present? v) v nil))))))) + +(defun w/user-set-property (uid prop val) + "Set PROP for user UID to VAL." + (when (stringp uid) + (let ((key (s-concat "user:properties:" uid))) + (w/db-hset key prop (format "%s" val))))) + +(defun w/user-when-authorized (uid k) + "Call K when user UID is authorized." + (w/user-get-property uid "boost" + (lambda (scur) + (w/user-get-property uid "name" + (lambda (name) + (let ((cur (or (w/read-sexp scur) 0))) + (if (or (> (abs cur) 2) (-contains? w/user-whitelist name)) + (funcall k) + (w/chat-write-event (format "%s is not authorized, boost harder" name))))))))) + +(defun w/user-crown (&optional uid) + "Increase UID's equity by 1." + (when uid + (w/user-get-stat uid "equity" + (lambda (cur) + (message "Former equity was %s, new equity is %s" (or cur 0) (+ (or cur 0) 1)) + (w/user-set-stat uid "equity" (+ (or cur 0) 1)))))) + +(defun w/user-boost (uid) + "Increase UID's boost power by 1." + (when uid + (w/user-get-property uid "boost" + (lambda (scur) + (when-let* ( (cur (or (w/read-sexp scur) 0)) + (_ (integerp cur))) + (w/user-set-property uid "boost" (format "%s" (+ cur 1)))))))) + +(defun w/user-tsoob (uid) + "Decrement UID's boost power by 1." + (when uid + (w/user-get-property uid "boost" + (lambda (scur) + (when-let* ( (cur (or (w/read-sexp scur) 0)) + (_ (integerp cur))) + (w/user-set-property uid "boost" (format "%s" (- cur 1)))))))) + +(defun w/user-avatar (user) + "Return USER's avatar as a unibyte string." + (when (f-exists? (w/twitch-user-avatar-path user)) + (f-read-bytes (w/twitch-user-avatar-path user)))) + +(defun w/user-charsheet (user) + "Browser USER's character sheet." + (w/user-id-from-name user + (lambda (uid) + (browse-url (format "https://api.colonq.computer/charsheet#%s" uid))))) (provide 'wasp-user) ;;; wasp-user.el ends here |
