;;; wasp-user --- User data -*- lexical-binding: t; -*- ;;; Commentary: ;;; Code: (require 's) (require 'ht) (require 'wasp-utils) (require 'wasp-db) (defvar w/user-whitelist nil) (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) "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