summaryrefslogtreecommitdiff
path: root/src/wasp-user.el
blob: de5df6f8cd64020274bb991f82473ad9b10b76d7 (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
;;; 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