This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.rkt
195 lines (174 loc) · 6.78 KB
/
backend.rkt
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#lang racket/base
(require
racket/class
racket/contract
(only-in racket/format ~a)
(only-in racket/function const identity thunk)
(only-in racket/sequence sequence->list)
(only-in racket/string string-join)
threading
"common.rkt"
(prefix-in ev: "evaluator.rkt")
"interface.rkt"
(prefix-in db: "trick-db.rkt"))
(provide r16%)
(define r16%
(class* object% [r16-backend<%>]
(init-field db)
(field [start-time (current-seconds)])
(define (evaluation-context enrich-context context-id trick name args parent-ctx)
(define real-args (string->immutable-string args))
(define this-context #f)
(define/contract (call-subtrick name arguments)
(-> (or/c symbol? string?) any/c any)
(define strname (~a name))
(define trick-obj (db:get-trick db context-id strname))
(if trick-obj
(let ()
(define rr
(ev:run
(trick-body trick-obj)
(evaluation-context
enrich-context
context-id
trick-obj
strname
(if arguments (~a arguments) "")
this-context)
(const #t)))
(write-string (ev:run-result-stdout rr))
(cond [(ev:run-result-stderr rr)
=> (lambda (stderr) (write-string stderr (current-error-port)))])
(apply values (ev:run-result-results rr)))
(raise (make-exn:fail:contract (~a "Trick " name " doesn't exist!")))))
(define (read-args)
(with-handlers ([exn:fail:read? (const #f)])
(sequence->list (in-producer read eof (open-input-string real-args)))))
(define base
`(((string-args . ,real-args)
(read-args . ,read-args)
(call-trick . ,call-subtrick)
(trick-name . ,(string->immutable-string name))
(parent-context . ,(and parent-ctx (make-immutable-hash (car parent-ctx)))))
threading))
(set! this-context (enrich-context base trick real-args parent-ctx))
this-context)
(define (response? x)
(send (current-frontend) response? x))
(define/public (evaluate code)
(define enrich-context (send (current-frontend) get-enrich-context))
(define ev-ctx (evaluation-context
enrich-context
(current-context-id)
#f "" "" #f))
(ev:run code ev-ctx response?))
(define/public (call name args)
(define ctx-id (current-context-id))
(define trick-obj (db:get-trick db ctx-id name))
(cond
[trick-obj
(db:update-trick! db ctx-id name
(lambda (t)
(~> (trick-invocations t) add1
(set-trick-invocations! t _))
t)
(const #t))
(define enrich-context (send (current-frontend) get-enrich-context))
(define ev-ctx (evaluation-context
enrich-context
(current-context-id)
trick-obj name args #f))
(define code (trick-body trick-obj))
(ev:run code ev-ctx response?)]
[else
(~a "Trick " name " doesn't exist!")]))
(define/public (delete name)
(define ctx-id (current-context-id))
(define trick-obj (db:get-trick db ctx-id name))
(define frontend (current-frontend))
(cond
[(not trick-obj)
(~a "Trick " name " doesn't exist!")]
[(db:remove-trick!
db ctx-id name
(lambda (t) (send frontend can-modify? t)))
(~a "Successfully removed trick " name "!")]
[else
(~a "You cannot modify trick " name "!")]))
(define/public (register name code author timestamp)
(cond
[(zero? (string-length code))
(~a "Trick " name " needs a body!")]
[(db:add-trick!
db (current-context-id) name
(thunk (trick author code timestamp (make-hash) 0)))
(~a "Successfully registered trick " name "!")]
[else (update name code)]))
(define/public (update name code)
(define ctx-id (current-context-id))
(define trick-obj (db:get-trick db ctx-id name))
(define frontend (current-frontend))
(cond
[(not trick-obj)
(~a "Trick " name " doesn't exist!")]
[(zero? (string-length code))
(~a "Trick " name " needs a body!")]
[(db:update-trick!
db ctx-id name
(lambda (trick-obj)
(trick (trick-author trick-obj)
code
(trick-created trick-obj)
(trick-storage trick-obj)
(trick-invocations trick-obj)))
(lambda (t)
(send frontend can-modify? t)))
(~a "Successfully updated trick " name "!")]))
(define/public (lookup name)
(db:get-trick db (current-context-id) name))
(define/public (popular)
(sort (db:all-tricks db (current-context-id)) cmp-tricks))
(define/public (save)
(with-handlers ([exn:fail? identity])
(if (db:commit-db! db trick->json)
'success
'unchanged)))
(define/public (about)
(string-join
`("R16 -- A Racket Trick Bot"
,(~a "Running on Racket " (version))
"Brought to you by williewillus, Alwinfy, and Eutro"
"Project Homepage: https://sr.ht/~williewillus/r16")
"\n"))
(define/public (stats)
(define all-tricks (db:all-tricks db (current-context-id)))
(define total-invocations
(for/sum ([pair (in-list all-tricks)])
(trick-invocations (cdr pair))))
(string-join
(list (~a "Uptime (dd:hh:mm:ss): " (uptime))
(~a "Bytes in use: " (current-memory-use))
(~a "Total trick invocations (for your guild): " total-invocations))
"\n"))
(define (uptime)
(define seconds-in-minute 60)
(define seconds-in-hour (* 60 60))
(define seconds-in-day (* 24 60 60))
(let*-values
([(v) (- (current-seconds) start-time)]
[(days v) (quotient/remainder v seconds-in-day)]
[(hours v) (quotient/remainder v seconds-in-hour)]
[(minutes seconds) (quotient/remainder v seconds-in-minute)])
(~>> (list days hours minutes seconds)
(map (lambda (x)
(~a #:min-width 2
#:align 'right
#:pad-string "0"
x)))
(string-join _ ":"))))
(super-new)))
(define (cmp-tricks lt rt)
(let ([l (cdr lt)] [r (cdr rt)])
(if (= (trick-invocations l) (trick-invocations r))
(string>? (trick-created l) (trick-created r))
(> (trick-invocations l) (trick-invocations r)))))