2023-07-10 01:37:52 +00:00
|
|
|
(define-module (gib-gab-gob ui console)
|
2023-07-10 04:40:22 +00:00
|
|
|
#:use-module (gib-gab-gob rps)
|
|
|
|
#:use-module (gib-gab-gob game)
|
2023-07-10 01:37:52 +00:00
|
|
|
#:use-module (gib-gab-gob board)
|
|
|
|
#:use-module (ice-9 rdelim)
|
|
|
|
#:use-module (ice-9 exceptions)
|
2023-07-10 02:35:12 +00:00
|
|
|
#:use-module (srfi srfi-9)
|
2023-07-10 04:40:22 +00:00
|
|
|
#:use-module (srfi srfi-1)
|
2023-07-10 02:35:12 +00:00
|
|
|
#:use-module (fibers)
|
|
|
|
#:use-module (fibers channels)
|
|
|
|
#:use-module (goblins)
|
2023-07-17 02:59:42 +00:00
|
|
|
#:use-module (goblins vat)
|
2023-07-10 05:45:57 +00:00
|
|
|
#:use-module (goblins ocapn captp)
|
|
|
|
#:use-module (goblins ocapn ids)
|
|
|
|
#:use-module (goblins ocapn netlayer onion)
|
|
|
|
#:export (make-initiator make-joiner))
|
2023-07-10 01:37:52 +00:00
|
|
|
|
|
|
|
;; Module for simple console-based UI (no curses)
|
|
|
|
|
|
|
|
;; A single run of the loop for a bespoke REPL for playing the game
|
2023-07-17 02:59:42 +00:00
|
|
|
(define (%loop vat b controller state+)
|
|
|
|
(define state (get-message state+))
|
|
|
|
(%print b state)
|
|
|
|
(if (eq? state 'play)
|
|
|
|
(let ((coords (%read)))
|
|
|
|
(with-vat
|
|
|
|
vat
|
|
|
|
(if coords (%eval vat b controller coords) #f)))
|
|
|
|
#f))
|
2023-07-10 01:37:52 +00:00
|
|
|
|
|
|
|
(define (%read)
|
|
|
|
(define line (read-line (current-input-port)))
|
|
|
|
(if (string-prefix? ",q" line) #f
|
|
|
|
(let ((coords (map string->number (string-tokenize line char-set:digit))))
|
|
|
|
(if (not (= (length coords) 2))
|
|
|
|
(begin (format #t "Must be two numbers, x y; or ,quit to quit. Try again.\n")
|
|
|
|
(%prompt) (%read))
|
|
|
|
coords))))
|
|
|
|
|
2023-07-10 04:40:22 +00:00
|
|
|
(define (%eval vat b controller coordinates)
|
|
|
|
;; weird?
|
|
|
|
(let ((x (first coordinates))
|
|
|
|
(y (second coordinates)))
|
|
|
|
(on (<- controller 'my-turn! x y)
|
|
|
|
(λ (state) (%display b state)))))
|
2023-07-10 01:37:52 +00:00
|
|
|
|
2023-07-10 02:35:12 +00:00
|
|
|
(define (%print b state)
|
|
|
|
(%display b state)
|
2023-07-10 01:37:52 +00:00
|
|
|
(%prompt))
|
|
|
|
|
2023-07-10 02:35:12 +00:00
|
|
|
(define (%prompt) (format #t "Enter your move: "))
|
2023-07-10 01:37:52 +00:00
|
|
|
|
2023-07-10 02:35:12 +00:00
|
|
|
(define (%display b state)
|
|
|
|
(board-display b)
|
2023-07-10 03:23:00 +00:00
|
|
|
(cond
|
2023-07-10 05:59:29 +00:00
|
|
|
((eq? state 'won) (format #t "*** I won! ***\n"))
|
|
|
|
((eq? state 'lost) (format #t "*** I lost! ***\n"))))
|
2023-07-10 02:35:12 +00:00
|
|
|
|
2023-07-17 02:59:42 +00:00
|
|
|
(define (begin-game-loop board controller state+)
|
2023-07-10 03:23:00 +00:00
|
|
|
(define vat (spawn-vat #:name "UI"))
|
2023-07-17 02:59:42 +00:00
|
|
|
(syscaller-free-fiber
|
2023-07-10 02:35:12 +00:00
|
|
|
(λ ()
|
2023-07-17 02:59:42 +00:00
|
|
|
(while (%loop vat board controller state+))
|
2023-07-10 02:35:12 +00:00
|
|
|
(format #t "bye-bye!\n")
|
|
|
|
#f)))
|
2023-07-10 04:40:22 +00:00
|
|
|
|
|
|
|
(define (make-initiator)
|
|
|
|
(with-vat
|
|
|
|
(spawn-vat #:name "Initiator Game")
|
2023-07-17 02:59:42 +00:00
|
|
|
(define state+ (make-channel))
|
2023-07-10 04:40:22 +00:00
|
|
|
(define board (make-board))
|
2023-07-17 02:59:42 +00:00
|
|
|
(define initiator (spawn ^game-initiator ^ggg-controller board state+))
|
2023-07-10 04:40:22 +00:00
|
|
|
(initiator/connect initiator)
|
2023-07-17 02:59:42 +00:00
|
|
|
(begin-game-loop board initiator state+)))
|
2023-07-10 05:45:57 +00:00
|
|
|
|
|
|
|
(define (make-joiner ^game-controller addr)
|
|
|
|
(with-vat
|
|
|
|
(spawn-vat #:name "Joiner Game")
|
|
|
|
(define mycapn (spawn-mycapn (new-onion-netlayer)))
|
2023-07-17 02:59:42 +00:00
|
|
|
(define state+ (make-channel))
|
2023-07-10 05:45:57 +00:00
|
|
|
(define board (make-board))
|
|
|
|
(let* ((init-sref (string->ocapn-id addr))
|
|
|
|
(initiator ($ mycapn 'enliven init-sref))
|
2023-07-17 02:59:42 +00:00
|
|
|
(joiner (spawn ^game-joiner initiator ^game-controller board state+))
|
2023-07-10 05:45:57 +00:00
|
|
|
(sealed ($ joiner 'get-sealed-pick)))
|
|
|
|
(on (<- initiator 'register-opponent joiner sealed)
|
|
|
|
(λ (_)
|
|
|
|
(on ($ joiner 'initialize!)
|
2023-07-17 02:59:42 +00:00
|
|
|
(λ (status) (begin-game-loop board joiner state+) #f)))))))
|