2023-02-03 13:18:17 +00:00
|
|
|
(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)
|
|
|
|
#:use-module (oop goops)
|
2023-02-06 07:30:12 +00:00
|
|
|
#:export (<rps-host> <rps-client> join-rps pick-rps rps-winner rock-paper-scissors))
|
2023-02-03 13:18:17 +00:00
|
|
|
|
2023-02-05 12:51:13 +00:00
|
|
|
;;
|
|
|
|
;; Host logic
|
|
|
|
;;
|
2023-02-03 13:18:17 +00:00
|
|
|
(define-class <rps-host> ()
|
2023-02-05 12:49:01 +00:00
|
|
|
(vat #:accessor vat #:init-thunk spawn-vat)
|
|
|
|
(lobby #:accessor lobby)
|
|
|
|
(user-name #:accessor user-name #:init-keyword #:user-name))
|
2023-02-03 13:18:17 +00:00
|
|
|
|
2023-02-05 12:49:01 +00:00
|
|
|
(define-method (initialize (host <rps-host>) initargs)
|
|
|
|
(next-method)
|
2023-02-03 13:18:17 +00:00
|
|
|
(with-vat (vat host)
|
2023-02-05 12:49:01 +00:00
|
|
|
(set! (lobby host) (spawn ^game-lobby))
|
2023-02-05 13:26:35 +00:00
|
|
|
(define mycapn (spawn-mycapn (new-testuds-netlayer)))
|
2023-02-05 12:49:01 +00:00
|
|
|
(define lobby-sref ($ mycapn 'register (lobby host) 'testuds))
|
|
|
|
(format #t "Connect to: ~a\n" (ocapn-id->string lobby-sref))))
|
2023-02-03 13:18:17 +00:00
|
|
|
|
|
|
|
(define (new-testuds-netlayer)
|
2023-02-03 14:19:04 +00:00
|
|
|
(define tmp "/tmp/netlayers")
|
|
|
|
(unless (access? tmp X_OK) (mkdir tmp))
|
|
|
|
(spawn ^testuds-netlayer tmp))
|
2023-02-03 13:18:17 +00:00
|
|
|
|
2023-02-05 12:51:13 +00:00
|
|
|
;;
|
|
|
|
;; Client logic
|
|
|
|
;;
|
2023-02-03 13:18:17 +00:00
|
|
|
(define-class <rps-client> ()
|
2023-02-05 14:20:04 +00:00
|
|
|
(vat #:accessor vat #:init-thunk spawn-vat)
|
|
|
|
(user-name #:accessor user-name #:init-keyword #:user-name)
|
2023-02-05 12:49:01 +00:00
|
|
|
(addr #:accessor addr #:init-keyword #:addr)
|
2023-02-05 14:20:04 +00:00
|
|
|
(lobby #:accessor lobby))
|
2023-02-03 13:18:17 +00:00
|
|
|
|
2023-02-05 12:49:01 +00:00
|
|
|
(define-method (initialize (client <rps-client>) initargs)
|
|
|
|
(next-method)
|
|
|
|
(with-vat (vat client)
|
|
|
|
(define lobby-sref (string->ocapn-id (addr client)))
|
2023-02-05 13:26:35 +00:00
|
|
|
(define mycapn (spawn-mycapn (new-testuds-netlayer)))
|
2023-02-05 12:49:01 +00:00
|
|
|
(set! (lobby client)
|
2023-02-05 14:20:04 +00:00
|
|
|
($ mycapn 'enliven lobby-sref))))
|
2023-02-03 13:18:17 +00:00
|
|
|
|
2023-02-05 12:49:01 +00:00
|
|
|
(define (join-rps user-name addr)
|
|
|
|
(define client (make <rps-client> #:user-name user-name #:addr addr))
|
2023-02-03 13:18:17 +00:00
|
|
|
(with-vat (vat client)
|
2023-02-05 12:49:01 +00:00
|
|
|
(define client-picker (spawn ^client-picker))
|
|
|
|
(on (<- (lobby client) 'register-opponent user-name client-picker ($ client-picker 'get-sealed-pick))
|
|
|
|
(lambda (_)
|
2023-02-06 07:34:59 +00:00
|
|
|
(format #t "~a finished the game.\n" user-name)))))
|
2023-02-03 13:18:17 +00:00
|
|
|
|
2023-02-05 12:51:13 +00:00
|
|
|
;;
|
2023-02-05 12:49:01 +00:00
|
|
|
;; Standard rock paper scissors logic follows!
|
2023-02-05 12:51:13 +00:00
|
|
|
;;
|
2023-02-03 13:18:17 +00:00
|
|
|
(define rock-paper-scissors (list 'rock 'paper 'scissors))
|
|
|
|
|
2023-02-03 14:19:04 +00:00
|
|
|
(define (pick-rps)
|
|
|
|
(list-ref rock-paper-scissors (random (length rock-paper-scissors))))
|
|
|
|
|
2023-02-03 13:18:17 +00:00
|
|
|
(define (rps-winner a b)
|
|
|
|
(if (and (memq a rock-paper-scissors) (memq b rock-paper-scissors))
|
2023-02-05 14:20:04 +00:00
|
|
|
(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)))
|