56 lines
1.8 KiB
Scheme
56 lines
1.8 KiB
Scheme
(define-module (gib-gab-gob rps)
|
|
#:use-module (gib-gab-gob actors)
|
|
#:use-module (ice-9 match)
|
|
#:use-module (goblins)
|
|
#:use-module (goblins ocapn captp)
|
|
#:use-module (goblins ocapn ids)
|
|
#:use-module (goblins ocapn netlayer testuds)
|
|
#:export (make-host make-client pick-rps rps-winner rock-paper-scissors))
|
|
|
|
;;
|
|
;; Host logic
|
|
;;
|
|
(define (make-host)
|
|
(with-vat (spawn-vat)
|
|
(define lobby (spawn ^game-lobby))
|
|
(define mycapn (spawn-mycapn (new-testuds-netlayer)))
|
|
(define lobby-sref ($ mycapn 'register lobby 'testuds))
|
|
(format #t "Connect to: ~a\n" (ocapn-id->string lobby-sref))))
|
|
|
|
(define (new-testuds-netlayer)
|
|
(define tmp "/tmp/netlayers")
|
|
(unless (access? tmp X_OK) (mkdir tmp))
|
|
(spawn ^testuds-netlayer tmp))
|
|
|
|
;;
|
|
;; Client logic
|
|
;;
|
|
(define (make-client user-name addr)
|
|
(with-vat (spawn-vat)
|
|
(define mycapn (spawn-mycapn (new-testuds-netlayer)))
|
|
(define lobby-sref (string->ocapn-id addr))
|
|
(define lobby ($ mycapn 'enliven lobby-sref))
|
|
(define client-picker (spawn ^client-picker))
|
|
|
|
(on (<- lobby 'register-opponent user-name client-picker ($ client-picker 'get-sealed-pick))
|
|
(lambda (_)
|
|
(format #t "~a finished the game.\n" user-name)))))
|
|
|
|
;;
|
|
;; Standard rock paper scissors logic follows!
|
|
;;
|
|
(define rock-paper-scissors (list 'rock 'paper 'scissors))
|
|
|
|
(define (pick-rps)
|
|
(list-ref rock-paper-scissors (random (length rock-paper-scissors))))
|
|
|
|
(define (rps-winner a b)
|
|
(if (and (memq a rock-paper-scissors) (memq b rock-paper-scissors))
|
|
(match (list a b)
|
|
[(x x) 'tie]
|
|
[('rock 'scissors) #t]
|
|
[('rock 'paper) #f]
|
|
[('scissors 'paper) #t]
|
|
[(x y) (not (rps-winner y x))])
|
|
(error "Unexpected item in the shooting area" a b)))
|