-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.lisp
405 lines (355 loc) · 12.5 KB
/
web.lisp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
(defpackage :13l-web
(:documentation "HTML/JS/CSS generator for Thirteen Letters on the web")
(:nicknames :13lw)
(:use :cl)
(:import-from #:parenscript #:ps #:@)
(:local-nicknames (#:sp #:spinneret)))
(in-package :13lw)
;;; JavaScript
;;; TODO: Path should be shared with back-end code
(defvar *web-socket-url* "wss://api.schemescape.com/ws/13l")
(defvar *web-socket-url-override* nil)
(ps:defpsmacro get-id (id)
`((@ document get-element-by-id) ,id))
(ps:defpsmacro watch (&body body)
`(ps:try (progn ,@body) (:catch (e) (error (+ (@ e message) " (" (@ e line-number) ")")))))
(ps:defpsmacro clear-children (node)
`(setf (@ ,node inner-h-t-m-l) ""))
(ps:defpsmacro append-text (node text)
`((@ ,node append-child) ((@ document create-text-node) ,text)))
(defun make-script (&key web-socket-url)
(ps
(defparameter *web-socket-url* (ps:lisp web-socket-url))
(defparameter *name-key* "13l_name")
(defparameter *a* ((@ "a" char-code-at) 0))
(defparameter *z* ((@ "z" char-code-at) 0))
(defun debug (message)
(let ((debug-div ((@ document get-element-by-id) "debug"))
(node ((@ document create-element) "p")))
(append-text node message)
((@ debug-div append-child) node)
nil))
(defun error (error-string)
(debug (+ "ERROR: " error-string))
nil)
(defun get-letter-counts (word)
(let ((counts (ps:create))
(code))
(loop for i from 0 to 25 do (setf (elt counts i) 0))
(loop for i from 0 to (1- (@ word length))
do (setf code ((@ word char-code-at) i))
(if (and (>= code *a*)
(<= code *z*))
(incf (elt counts (- code *a*)))
(return-from get-letter-counts nil)))
counts))
(defun is-valid (expected-counts word)
(let ((actual-counts (get-letter-counts word)))
(if (not actual-counts) (return-from is-valid nil))
(loop for i from 0 to 25
do (if (> (elt actual-counts i) (elt expected-counts i))
(return-from is-valid nil)))
t))
(watch
(let ((socket)
(scrambled)
(scrambled-letter-counts (get-letter-counts ""))
(timer-id)
(validation-timer-id)
(countdown-end-timestamp)
(intro-div (get-id "intro"))
(main-div (get-id "main"))
(time-div (get-id "time"))
(time-title-span (get-id "time-title"))
(time-left-span (get-id "time-left"))
(top-div (get-id "top"))
(player-count-span (get-id "player-count"))
(tbody (get-id "tbody"))
(result-div (get-id "result"))
(debug-div (get-id "debug"))
(news-div (get-id "news"))
(name-input (get-id "name"))
(start-button (get-id "start"))
(guess-input (get-id "guess"))
(winner-span (get-id "winner"))
(winning-word-span (get-id "winning-word")))
(defun hide (element) ((@ element class-list add) "hidden"))
(defun show (element)((@ element class-list remove) "hidden"))
(defun update-letters (word)
(dotimes (i (@ word length))
(let ((letter-span (get-id (+ "l" i))))
(setf (@ letter-span inner-text) (aref word i)))))
(defun round-started (message)
(setf scrambled (@ message scrambled))
(setf scrambled-letter-counts (get-letter-counts scrambled))
(show guess-input)
(show time-div)
(setf (@ time-title-span inner-text) "Time")
(hide result-div)
(setf (@ guess-input value) "")
((@ guess-input focus))
(update-letters scrambled))
(defun round-ended (message)
(show result-div)
(show time-div)
(setf (@ time-title-span inner-text) "Next")
(hide guess-input)
(let* ((entries (@ message leaderboard))
(winning-entry (and entries (aref entries 0))))
(update-letters (@ message solution))
(setf (@ winner-span inner-text) (if winning-entry (@ winning-entry name) "(no one)"))
(setf (@ winning-word-span inner-text) (if winning-entry (@ winning-entry word) "---"))))
(defun unregister-timer ()
(if timer-id (clear-timeout timer-id)))
(defun reset-remaining ()
(unregister-timer)
(setf (@ time-left-span inner-text) "")
nil)
(defun update-time-left ()
(reset-remaining)
(if countdown-end-timestamp
(let* ((remaining-ms (max 0 (- countdown-end-timestamp ((@ -date now)))))
(remaining-seconds (/ remaining-ms 1000))
(floored ((@ -math floor) remaining-seconds))
(text (+ "" floored))
(fraction-ms (max 0 ((@ -math floor) (* (- remaining-seconds
floored)
1000)))))
(setf (@ time-left-span inner-text) text)
(if (> remaining-ms 0)
(setf timer-id (set-timeout update-time-left fraction-ms)))))
nil)
(defun update-remaining (remaining)
(setf countdown-end-timestamp (+ ((@ -date now))
((@ -math floor) (* remaining 1000))))
(update-time-left)
nil)
(defun update-leaderboard (message)
;;; Update leaderboard
(let ((entries (@ message leaderboard)))
(clear-children tbody)
(if entries
(dolist (entry entries)
(let ((row ((@ document create-element) "tr"))
(col-name ((@ document create-element) "td"))
(col-word ((@ document create-element) "td")))
(append-text col-name (@ entry name))
((@ row append-child) col-name)
(append-text col-word (@ entry word))
((@ col-word class-list add) "right")
((@ col-word class-list add) "caps")
((@ row append-child) col-word)
((@ tbody append-child) row)))))
;;; Also update player count, if needed
(let ((client-count (@ message clients)))
(if (>= client-count 0)
(setf (@ player-count-span inner-text)
(max 1 client-count))))
;;; Also update time, if needed
(let ((remaining (@ message remaining)))
(if remaining
(update-remaining remaining)
(reset-remaining))))
(defun handle-error ()
(error "WebSocket error!"))
(defun update-news (message)
(let ((fragment (@ message fragment)))
(setf (@ news-div inner-h-t-m-l) fragment)))
(defun handle-update (event)
(watch
(let* ((json (@ event data))
(message ((@ *JSON* parse) json)))
(case (@ message type)
("state"
(if (not (= scrambled (@ message scrambled))) (round-started message))
(update-leaderboard message))
("result"
(update-leaderboard message)
(round-ended message))
("news"
(update-news message)))
nil)))
(defun send (message)
(if socket
(let ((json ((@ *json* stringify) message)))
((@ socket send) json))))
(defun send-rename ()
(let* ((name-raw (@ name-input value))
(name ((@ name-raw trim))))
(if name
(progn
(send (ps:create type "rename"
name name))
((@ local-storage set-item) *name-key* name))))
nil)
(defun handle-visibility-changed ()
(if (and (not socket)
(= (@ document visibility-state) "visible"))
(connect-socket))
nil)
(defun connect-socket ()
(setf socket (ps:new (-web-socket *web-socket-url*)))
(setf (@ socket onopen) send-rename)
(setf (@ socket onmessage) handle-update)
(setf (@ socket onclose) #'(lambda () (setf socket nil))))
(defun handle-start ()
(watch
(hide intro-div)
(show main-div)
(show top-div)
(if (not socket) (connect-socket))
((@ document add-event-listener) "visibilitychange" #'(lambda ()
(set-timeout handle-visibility-changed 100)))
nil))
(defun get-guess ()
((@ ((@ (@ guess-input value) to-lower-case)) trim)))
(defun send-guess ()
(let* ((word (get-guess))
(message (ps:create type "guess"
word word)))
(send message)
(setf (@ guess-input value) "")
((@ guess-input class-list remove) "invalid")))
(defun validate-current-guess ()
(setf validation-timer-id nil)
(let ((word (get-guess)))
(if (is-valid scrambled-letter-counts word)
((@ guess-input class-list remove) "invalid")
((@ guess-input class-list add) "invalid")))
nil)
(defun schedule-validation-if-needed ()
(if (not validation-timer-id)
(setf validation-timer-id (set-timeout validate-current-guess 50))))
(defun handle-key-down (event)
(watch
(let ((key (@ event key)))
(if (= key "Enter")
(progn (send-guess))
(schedule-validation-if-needed)))))
(defun handle-name-key-down (event)
(watch
(let ((key (@ event key)))
(if (= key "Enter") (handle-start)))))
(setf (@ start-button onclick) handle-start)
(setf (@ name-input onkeydown) handle-name-key-down)
(setf (@ start-button onerror) handle-error)
(setf (@ guess-input onkeydown) handle-key-down)
;;; Try to restore name from localStorage
(let ((saved-name ((@ local-storage get-item) *name-key*) ""))
(if saved-name (setf (@ name-input value) saved-name)))
nil))))
;;; CSS
(defparameter *css*
(cl-css:css
'(("html, body" :margin 0)
("body" :background-color "#eee"
:color "#222")
("body, button, input" :font-family "'Cascadia Mono', 'Consolas', monospace")
("table" :width "100%")
("input" :font-size "100%")
(".left" :text-align "left")
(".center" :text-align "center")
(".right" :text-align "right")
(".caps" :text-transform "uppercase")
(".invalid" :outline "solid 2px red")
(".main" :display "flex"
:flex-direction "column"
:align-items "center")
(".time" :display "flex"
:flex-direction "column"
:align-items "center"
:position "absolute"
:padding "0.25em"
:top 0
:right 0)
(".top" :width "100%"
:display "flex"
:flex-direction "column"
:align-items "center"
:margin-top "1em")
("#root" :display "flex"
:flex-direction "column"
:align-items "center"
:max-width "40em"
:margin "auto"
:padding "1em")
("#guess, #result" :font-size "150%"
:margin-top "1em")
("#start" :margin "1em"
:padding "0.25em 1em"
:font-size "150%"
:font-weight "bold")
("#letters" :margin "1em"
:text-align "center")
("#letters > div" :display "inline-block"
:padding "0.125em"
:font-weight "bold"
:font-size "150%"
:text-transform "uppercase")
("#time-title" :font-weight "bold")
("#time-left" :font-size "125%")
(".time > p" :padding "0.125em"
:margin 0)
(.invisible :display "hidden")
(.hidden :display "none"))))
;;; HTML
(defparameter *intro* (sp:with-html-string ("**Thirteen Letters** is a game where players compete to find the longest word that can be constructed from the given set of letters.
Enter a name and click **Start** to get started.
During each round, a set of thirteen letters will be shown. Type in the longest word you can find and hit Enter/Return to submit your guess. You may continue guessing until the timer in the upper-right counts down to zero.")))
(defmacro page ((&key title) &body body)
"Outputs an HTML page"
`(sp:with-html
(:doctype)
(:html
(:head
(:title ,title)
(:meta :name "viewport"
:content "width=device-width, initial-scale=1, shrink-to-fit=no"))
(:style (:raw *css*))
(:body ,@body))))
(defun make-index-html (&key web-socket-url)
"Outputs root HTML page"
(page (:title "(thirteen-letters)")
(:div :id "root"
(:h1 "(thirteen-letters)")
(:div :id "intro"
(:h2 "Welcome!")
(:raw *intro*)
(:p "Name: " (:input :id "name" :type "text"))
(:p :class "center"
(:button :id "start" "Start")))
(:div :id "main" :class "hidden main"
(:div :id "letters"
(dotimes (n 13)
(:div :id (format nil "l~d" n) "?")))
(:input :id "guess" :type "text"))
(:div :id "time" :class "hidden time"
(:p :id "time-title" "Time")
(:p :id "time-left"))
(:div :id "result" :class "hidden"
"Winner: "
(:span :id "winner")
" ("
(:span :id "winning-word" :class "caps")
")")
(:div :id "top" :class "top hidden"
(:h3 :class "center" "Leaderboard")
(:table (:tbody :id "tbody"))
(:p :class "center"
"(player count: "
(:span :id "player-count" "1")
")"))
(:div :id "news" :class "top")
(:div :id "debug"))
(:script (:raw (make-script :web-socket-url web-socket-url)))))
;;; Generate index.html
(defun write-index-html (&key (web-socket-url (or *web-socket-url-override* *web-socket-url*)))
(setf sp:*suppress-inserted-spaces* t)
(let ((*print-pretty* nil)
(sp:*html-style* :tree))
(with-open-file (out "index.html" :direction :output
:if-exists :supersede)
(setf sp:*html* out)
(make-index-html :web-socket-url web-socket-url))
(format t "Wrote index.html with WebSocket URL: ~a~%" web-socket-url)))
(write-index-html)