summaryrefslogtreecommitdiff
path: root/src/gizmo/wasp-scratchpad.el
blob: 39111529465f35673d7ef6a193dc3f67ea1875c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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