-
Notifications
You must be signed in to change notification settings - Fork 0
/
myapp.lisp
583 lines (510 loc) · 24.3 KB
/
myapp.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
(defpackage :myapp
(:use :cl :m-macro :utils))
(in-package :myapp)
(defvar *ws* nil)
(defvar *ws-keep-alive-timer* nil)
(defvar *toast-messages* nil)
(defvar *nickname* nil)
(defvar *login-success* nil)
(defvar *other-users* nil)
(defvar *pending-invite* nil)
(defvar *game-board* nil)
(defvar *game-first-player-nickname* nil)
(defvar *game-opponent-nickname* nil)
(defvar *game-current-player* nil)
(defvar *game-my-color* nil)
(defvar *ongoing-games* nil)
(defun send! (message)
(utils::websocket-send
*ws*
(format-message-as-json message)))
(defun handle-message (message)
(case (car message)
(:please-tell-me-who-you-are
(send! `(:login ,*nickname*)))
(:logged-in
(add-toast-message (second message) :delay 1)
(setq *login-success* t)
(send! `(:get-list-of-users)))
(:users-present
(setq *other-users*
(map 'list #'jscl::js-to-lisp (second message))))
(:user-entered
(add-toast-message (format nil "~A came online" (second message)))
(push (second message) *other-users*))
(:game-invitation-from
(setq *pending-invite* (second message))
(add-toast-message
(lambda (remove-message-thunk)
(ms (:div :class "game-invitation"
:data-invitator *pending-invite*)
(ms (:h2 :class "text-xl mb-4")
(format nil "~A invites you for a game!" *pending-invite*))
(ms (:button :class "px-2 py-2 bg-pink-600 hover:bg-pink-700 text-white w-full"
:onclick (lambda (event)
(funcall remove-message-thunk)
(handle-game-invitation-accept event)))
"Accept")))
:no-auto-close t))
(:game-start-with
(setq *game-board* (othello::initial-board)
*game-opponent-nickname* (second message)
*game-first-player-nickname* (third message)
*game-current-player* othello::+black+
*game-my-color* (if (equal *game-first-player-nickname* *nickname*)
othello::+black+
othello::+white+)))
(:couple-starts-game
(destructuring-bind (user partner) (cdr message)
(push (list user partner) *ongoing-games*)))
(:move-to
(destructuring-bind (square) (cdr message)
(handle-opponent-move square)))
(:pong
(clog::clog "pong received!"))
(t
(error "Don't know how to handle-message: ~S"
message)))
(m-redraw))
(defun user-currently-playing-with (user)
(let ((game (find-if (lambda (game)
(or (equal user (first game))
(equal user (second game))))
*ongoing-games*)))
(when game
(if (equal user (first game))
(second game)
(first game)))))
(defun send-ping ()
(clog::clog "Sending ping...")
(send! '(:ping)))
(defun open-websocket-with-handlers ()
(jscl::make-new
#j:Promise
(jscl::lisp-to-js
(lambda (resolve reject)
(setq *ws*
(utils::open-websocket
(ws-url)
:log-each-event t
:on-open
(lambda (event)
(setq *ws-keep-alive-timer*
(#j:setInterval #'send-ping
(* 30 1000)))
(funcall resolve *ws*))
:on-error
(lambda (event)
(funcall reject event))
:on-message
(lambda (event)
(handle-message
(parse-json-message
(jscl::oget event "data"))))
:on-close
(lambda (event)
(clog::clog "websocket was closed!")
(when *ws-keep-alive-timer*
(#j:clearInterval *ws-keep-alive-timer*)
(setq *ws-keep-alive-timer* nil))
(setq *ws* nil))))))))
(defun close-websocket ()
(jscl::make-new
#j:Promise
(jscl::lisp-to-js
(lambda (resolve reject)
(if (not *ws*)
(funcall resolve nil)
(progn
((jscl::oget *ws* "addEventListener") "close"
(lambda (event)
(funcall resolve t)))
(utils::close-websocket *ws*)))))))
(defun reopen-websocket ()
(let ((promise (close-websocket)))
((jscl::oget promise "then")
(lambda (value)
(open-websocket-with-handlers)))))
(defun get-nickname-from-input-field ()
(let* ((elt (jscl::js-inline "document.getElementById('nickname')"))
(nickname (jscl::oget elt "value")))
nickname))
(defun handle-login-submit (event)
((jscl::oget event "preventDefault"))
(let ((nickname (get-nickname-from-input-field)))
(unless (or (equal nickname "")
(every (lambda (char) (char= char #\space)) nickname))
(setq *nickname* nickname)
(let ((promise
;; will assign *ws* and *ws-keep-alive-timer*
(open-websocket-with-handlers)))
(setq promise
((jscl::oget promise "then")
(lambda (value)
(clog::clog "Connected successfully!" value))))
(setq promise
((jscl::oget promise "catch")
(lambda (value)
(clog::cerror "Failed to connect!")
(add-toast-message "Failed to connect!")
(m-redraw))))))))
(defun invite-for-game-handler (invitee)
(lambda (event)
((jscl::oget event "preventDefault"))
(send! `(:invite-for-game ,invitee))))
(defun handle-game-invitation-accept (event)
((jscl::oget event "preventDefault"))
(send! `(:accept-game-invitation ,*pending-invite*)))
(defun move-to-handler (square)
(lambda (event)
((jscl::oget event "preventDefault"))
(when (othello::legal-p square *game-current-player* *game-board*)
(othello::make-move square *game-current-player* *game-board*)
(setf *game-current-player* (othello::opponent *game-current-player*))
(send! `(:move-to ,square)))))
(defun handle-opponent-move (square)
(when (othello::legal-p square *game-current-player* *game-board*)
(othello::make-move square *game-current-player* *game-board*)
(setf *game-current-player* (othello::opponent *game-current-player*))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; clog-handler ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun clog-handler (message)
(lambda (event)
(clog::clog message)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; define-component ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro define-component* (name args state &body body)
`(defun ,name (initial-vnode)
(let ,state
(plist2object
(list
:view
(lambda (vnode)
(let ((children (jscl::oget vnode "children"))
,@(mapcar
(lambda (arg)
(destructuring-bind (name default)
(if (consp arg) arg (list arg nil))
`(,name (or (jscl::oget vnode "attrs" ,(string-downcase name))
,default))))
args))
;; if children are empty, it will be #()
(when (zerop (length children))
(setq children nil))
,@body)))))))
(defmacro define-component (name args &body body)
`(define-component* ,name ,args ()
,@body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; counter ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component* counter ((button-text "Click me") children-fn) ((count 0))
(m "div.text-4xl"
(if children-fn
(funcall children-fn count)
(or children
(m "div" count)))
(m "button" (list :onclick (lambda (event) (incf count)))
button-text)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; initial-wrapper ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component initial-wrapper ()
(ms (:div :class "bg-gray-400 min-h-screen")
children))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; main-container ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component main-container ()
(ms (:main :class "container mx-auto px-4 pt-14")
children))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; navbar ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component navbar ()
(ms (:header :class "fixed w-full text-white text-xl md:text-2xl bg-pink-700 p-2 shadow-lg flex items-center")
(ms :h1 (ms (:a :href "/") "Othello Square"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; toast messages ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun make-toast-message (text &optional (id (princ-to-string (random 100000))))
(list :id id :text text))
(defun add-toast-message (text &key delay no-auto-close)
(labels ((do-it ()
(let* ((message-id (princ-to-string (random 100000)))
(remove-message-thunk (lambda () (remove-toast-message-by-id message-id)))
(message-text (if (functionp text)
(funcall text remove-message-thunk)
text))
(message (make-toast-message message-text message-id)))
(setf *toast-messages*
(append *toast-messages* (list message)))
(unless no-auto-close
(m-set-timeout 3 remove-message-thunk)))))
(if (null delay)
(do-it)
(m-set-timeout delay #'do-it))))
(defun remove-toast-message-by-id (message-id)
(setq *toast-messages*
(remove-if (lambda (x)
(equal (getf x :id) message-id))
*toast-messages*)))
(defun remove-toast-message (message)
(remove-toast-message-by-id (getf message :id)))
(defun toast-message-close-handler (message)
(lambda (event)
(remove-toast-message message)))
(defun toast-message-dom-id (message)
(format nil "toast_~A" (getf message :id)))
(define-component toast-messages (messages)
(ms (:DIV :CLASS "fixed right-4 top-4")
(map 'vector
(lambda (message)
(mc (toast-message
:key (getf message :id)
:message message
:onbeforeremove
(lambda (vnode)
((jscl::oget vnode "dom" "classList" "add") "fade-out")
(jscl::make-new #j:Promise
(lambda (resolve)
((jscl::oget vnode "dom" "addEventListener")
"animationend" resolve)))))))
messages)))
(define-component toast-message-icon ()
(ms (:DIV :CLASS
"inline-flex items-center justify-center flex-shrink-0 w-8 h-8 text-pink-500 bg-pink-100 rounded-lg dark:bg-blue-800 dark:text-blue-200")
(ms (:SVG :ARIA-HIDDEN "true" :CLASS "w-5 h-5" :FILL "currentColor" :VIEWBOX "0 0 20 20")
(ms (:PATH :FILL-RULE "evenodd"
:D "M12.395 2.553a1 1 0 00-1.45-.385c-.345.23-.614.558-.822.88-.214.33-.403.713-.57 1.116-.334.804-.614 1.768-.84 2.734a31.365 31.365 0 00-.613 3.58 2.64 2.64 0 01-.945-1.067c-.328-.68-.398-1.534-.398-2.654A1 1 0 005.05 6.05 6.981 6.981 0 003 11a7 7 0 1011.95-4.95c-.592-.591-.98-.985-1.348-1.467-.363-.476-.724-1.063-1.207-2.03zM12.12 15.12A3 3 0 017 13s.879.5 2.5.5c0-1 .5-4 1.25-4.5.5 1 .786 1.293 1.371 1.879A2.99 2.99 0 0113 13a2.99 2.99 0 01-.879 2.121z"
:CLIP-RULE "evenodd")))
(ms (:SPAN :CLASS "sr-only")
"Fire icon")))
(define-component toast-message-close-button (message)
(let ((dom-id (toast-message-dom-id message)))
(ms (:BUTTON :TYPE "button"
:CLASS "ml-auto -mx-1.5 -my-1.5 bg-gray-100 text-gray-400 hover:text-gray-900 rounded-lg focus:ring-2 focus:ring-gray-300 p-1.5 hover:bg-gray-300 inline-flex h-8 w-8 dark:text-gray-500 dark:hover:text-white dark:bg-gray-800 dark:hover:bg-gray-700"
:DATA-DISMISS-TARGET (format nil "#~A" dom-id)
:ARIA-LABEL "Close"
:onclick (toast-message-close-handler message))
(ms (:SPAN :CLASS "sr-only")
"Close")
(ms (:SVG :ARIA-HIDDEN "true" :CLASS "w-5 h-5" :FILL "currentColor" :VIEWBOX
"0 0 20 20")
(ms (:PATH :FILL-RULE "evenodd" :D
"M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
:CLIP-RULE "evenodd"))))))
(define-component toast-message (message)
(let ((dom-id (toast-message-dom-id message)))
(ms (:DIV
:ID dom-id
:CLASS "toast-message fade-in mb-4 flex w-full max-w-xs p-4 text-black bg-gray-100 rounded-lg shadow dark:text-gray-400 dark:bg-gray-800"
:ROLE "alert")
(mc toast-message-icon)
(ms (:DIV :CLASS "ml-3 text-xl font-normal")
(getf message :text))
(mc (toast-message-close-button :message message)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; default-layout ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component default-layout ()
(mc initial-wrapper
(mc navbar)
(mc (toast-messages :messages *toast-messages*))
(mc main-container
(ms :div
children))))
(defmacro define-page (name args &body body)
`(define-component ,name ,args
(mc default-layout
,@body)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; login-form ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component login-form ()
;; (ms (:div :class "flex justify-center mt-[23vh]")
;; (ms (:div
;; :class "border-2 border-black rounded bg-gray-100 p-4 my-4 text-center md:text-xl")
;; (ms :h2 "Please login")
;; (ms (:form :onsubmit #'handle-login-submit)
;; (ms (:label :for "nickname") "Nickname")
;; (ms (:input :id "nickname"))
;; (ms :button "Login"))))
;; (ms (:div :class "bg-white p-12")
;; (ms (:h2 :class "text-base font-semibold leading-7 text-gray-900")
;; "Please login")
;; (ms (:div :class "mt-6 grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6")
;; (ms (:div :class "col-span-full")
;; (ms (:label :class "block text-sm font-medium leading-6 text-gray-900")
;; "Nickname")
;; (ms (:div :class "mt-2")
;; (ms (:input :class "block outline-none w-full rounded-md border-0 py-1.5 px-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-pink-600 sm:text-sm sm:leading-6"))))
;; (ms (:div :class "mt-6 flex items-center justify-end gap-x-6")
;; (ms (:button :class "cursor-pointer rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600")
;; "Login"))))
(ms (:div :class "flex justify-center mt-[20vh]")
(ms (:div :class "border-2 border-black rounded bg-gray-100 p-4 my-4 md:text-xl")
(ms (:h2 :class "text-base font-semibold leading-7 text-gray-900 text-center")
"Please login")
(ms (:form :onsubmit #'handle-login-submit)
(ms (:div :class "mt-6 grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6")
(ms (:div :class "col-span-full")
(ms (:label
:for "nickname"
:class "block text-sm font-medium leading-6 text-gray-900 text-center")
"Nickname")
(ms (:div :class "mt-2")
(ms (:input
:id "nickname"
:class "block outline-none w-full rounded-md border-0 py-1.5 px-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-pink-600 sm:text-sm sm:leading-6"))))
(ms (:div :class "mt-2 flex items-center justify-center gap-x-6")
(ms (:button :class "cursor-pointer rounded-md bg-pink-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-pink-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-pink-600")
"Login")))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; login-page ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-page login-page ()
(mc login-form))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; users-page ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component users-page-header ()
(ms (:DIV :CLASS "border-2 border-black rounded bg-gray-100 p-4 my-4 text-center md:text-xl")
(ms (:H2 :CLASS "text-2xl") "Players")))
(define-component users-page-invite-for-game-button (on-invite-for-game)
(ms (:button
:CLASS "inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white bg-pink-600 rounded-lg hover:bg-pink-700 focus:ring-4 focus:outline-none focus:ring-pink-300 dark:bg-pink-600 dark:hover:bg-pink-700 dark:focus:ring-pink-800"
:onclick on-invite-for-game)
"Invite for game"
(ms (:SVG :ARIA-HIDDEN "true" :CLASS "w-4 h-4 ml-2 -mr-1" :FILL "currentColor"
:VIEWBOX "0 0 20 20")
(ms (:PATH :FILL-RULE "evenodd" :D
"M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z"
:CLIP-RULE "evenodd")))))
(define-component users-page-user-card (user on-invite-for-game)
(ms (:DIV
:id (format nil "user_~A" user)
:CLASS "max-w-sm p-6 bg-gray-100 border-2 border-black rounded-lg shadow dark:bg-gray-800 dark:border-gray-700")
(ms (:H5 :CLASS "mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white")
user)
(ms (:P :CLASS "mb-3 font-normal text-gray-700 dark:text-gray-400")
"Played games so far: 0")
(let ((partner (user-currently-playing-with user)))
(if partner
(format nil "Currently playing a game with ~A" partner)
(mc (users-page-invite-for-game-button :on-invite-for-game on-invite-for-game))))))
(define-page users-page ()
(ms :div
(mc users-page-header)
(ms (:div :class "grid gap-4 sm:grid-cols-2 md:grid-cols-3")
(map 'vector
(lambda (user)
(mc (users-page-user-card
:key user
:user user
:on-invite-for-game (invite-for-game-handler user))))
*other-users*))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; game-message ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component game-message ()
(ms (:div :id "game_message"
:class "border-2 border-black rounded bg-gray-100 p-4 my-4 text-center md:text-xl")
(if (eql *game-current-player* *game-my-color*)
"It's your turn"
(format nil "Waiting for ~a's turn" *game-opponent-nickname*))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; square ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component square-container (square)
(ms (:button
:id (format nil "square_~A" square)
:class "border border-black flex items-center justify-center"
:onclick (move-to-handler square))
children))
(define-component empty-square (square legal-move-indicator)
(mc (square-container :square square)
(if legal-move-indicator
(ms (:div :class "lm border border-black rounded-full w-5/6 h-5/6"))
(ms (:div :class "ee")))))
(define-component black-square (square)
(mc (square-container :square square)
(ms (:div :class "bp w-5/6 h-5/6"))))
(define-component white-square (square)
(mc (square-container :square square)
(ms (:div :class "wp w-5/6 h-5/6"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; board ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component board ()
(let ((legal-moves (othello::legal-moves *game-current-player* *game-board*)))
(ms (:div :class "border border-black bg-green-600 aspect-square grid grid-cols-8 max-h-screen")
(apply #'js-array
(othello::map-board-squares
(lambda (row col square)
(case (othello::bref *game-board* square)
;; othello::+empty+
(0 (mc (empty-square
:square square
:legal-move-indicator
(and (eql *game-current-player*
*game-my-color*)
(member square legal-moves)))))
;; othello::+black+
(1 (mc (black-square :square square)))
;; othello::+white+
(2 (mc (white-square :square square))))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; game-page ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-page game-page ()
(m "div#board"
(list :data-pieces-balance
(format nil "~A/~A"
(count othello::+black+ *game-board*)
(count othello::+white+ *game-board*)))
(mc game-message)
(mc board)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; page-switcher ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component page-switcher ()
(cond
((not *login-success*)
(mc login-page))
((not *game-board*)
(mc users-page))
(*game-board*
(mc game-page))
(t (error "don't know which page to show"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; app ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-component app-aux ()
(mc page-switcher))
(define-component app ()
(mc app-aux))
(defvar *app-mounted* nil)
(defun mount-app ()
;; you can run this from the browser console:
;; import("./myapp.js").then(x => x.default.exports_for_js().mountApp())
(when *app-mounted*
(unmount-app))
(let ((elt (jscl::js-inline "document.getElementById('app')")))
(m-mount elt #'app)
(setq *app-mounted* t)))
(defun unmount-app ()
(let ((elt (jscl::js-inline "document.getElementById('app')")))
(m-mount elt (jscl::js-inline "null"))
(jscl::oset "" elt "innerHTML")))
(defun exports-for-js ()
(#j:console:log "exports-for-js called")
(obj-literal
:|runAllTestsJest| #'mini-fiveam:run-all-tests-jest
:|mountApp| #'mount-app
:|unmountApp| #'unmount-app
:fact #'fact
:|getBoolFromJs| #'get-bool-from-js
:|maxViaJsInline| #'max-via-js-inline))