diff options
Diffstat (limited to 'src/gizmo/wasp-scratchpad.el')
| -rw-r--r-- | src/gizmo/wasp-scratchpad.el | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/src/gizmo/wasp-scratchpad.el b/src/gizmo/wasp-scratchpad.el new file mode 100644 index 00000000..39111529 --- /dev/null +++ b/src/gizmo/wasp-scratchpad.el @@ -0,0 +1,152 @@ +;;; wasp-scratchpad --- assorted junk (not loaded) -*- lexical-binding: t; -*- +;;; Commentary: +;;; Code: + +(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-authorized () + "Return non-nil if the current user is authorized to use advanced techniques." + t + ;; (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-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 quote) + "Add a QUOTE from USER." + (w/user-get + "__quotes__" + (lambda (qs) + (w/user-set "__quotes__" (cons (cons quote 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)))) + +(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)))) + +(defvar w/user-migrated nil) +(defun w/user-migrate-redis (user &optional k) + "Put USER's basic statistics in Redis." + (w/user-get + user + (lambda (u) + (w/twitch-get-user-id user + (lambda (uid) + (w/twitch-get-user-avatar user + (lambda () + (when (and u uid) + (message "Migrating user: %s" user) + (w/db-set (format "user-id:%s" (s-downcase user)) uid) + (w/db-hset (format "user:stats:%s" uid) + "equity" (format "%s" (or (alist-get :equity u) 0)) + "talentpoints" "0" + "power" (format "%s" (+ 9 (random 3))) + "speed" (format "%s" (+ 9 (random 3))) + "majjyka" (format "%s" (+ 9 (random 3))) + "wisdom" (format "%s" (+ 9 (random 3)))) + (w/db-hset (format "user:properties:%s" uid) + "boost" (format "%s" (alist-get :boost u)) + "faction" (format "%s" (alist-get :faction u)) + "element" (format "%s" (alist-get :element u)) + "color" (format "%s" (alist-get :color u)) + "name" (s-downcase user)) + (when-let* ((avatar (w/user-avatar user))) + (w/db-set (format "user:avatar:%s" uid) avatar)) + (push (cons user uid) w/user-migrated) + ;; (message "http://localhost:8080/charsheet#%s" uid) + ))))))) + (when k (funcall k))) + +(defun w/user-migrate-all-helper (xs) + (w/user-migrate-redis (s-chop-prefix "user:" (car xs)) + (lambda () + (w/user-migrate-all-helper (cdr xs))))) + +(defun w/user-migrate-all () + (w/db-keys + "user:*" + (lambda (users) + (w/user-migrate-all-helper users)))) + +;; (defvar w/users-to-migrate nil) +;; (w/db-keys +;; "user:*" +;; (lambda (users) +;; (setf w/users-to-migrate (--map (s-chop-prefix "user:" it) (--filter (= 1 (s-count-matches (rx ":") it)) users))))) +;; +;; (--each w/users-to-migrate +;; (w/user-migrate-redis it) +;; (sleep-for 0.05)) +;; +;; (--each (-difference w/users-to-migrate (-map #'car w/user-migrated)) +;; ;; (message "Trying: %s" it) +;; ;; (w/user-migrate-redis it) +;; (w/twitch-get-user-id it +;; (lambda (uid) +;; (when uid +;; (message "BAD: %s exists!: %s" it uid)))) +;; (sleep-for 0.50)) + +(provide 'wasp-scratchpad) +;;; wasp-scratchpad.el ends here |
