allow universe handlers to return state of universe, nt just bundle

This commit is contained in:
Matthias Felleisen 2014-09-27 20:14:17 -06:00
parent 26d1b52140
commit 8cfa68b647
4 changed files with 83 additions and 9 deletions

View File

@ -0,0 +1,70 @@
#lang racket
(module universe racket
(provide
;; Boolean -> [Listof IWorld]
;; run a univsere server, show state if given #true
run)
;; ---------------------------------------------------------------------------
(require 2htdp/universe)
(require 2htdp/image)
(define (run show?)
(universe '()
(on-tick identity 1 15)
(state show?)
(on-new (lambda (u nw) (cons nw u)))
(on-msg (lambda (u sender msg)
(define new-u (remq sender u))
(define throw (list sender))
(make-bundle new-u (mail2 new-u msg) throw)))
(on-disconnect (lambda (u gone)
(remq gone u)))))
;; [Listof IWorld] String -> [Listof Mail]
(define (mail2 lo-iw sender)
(for/list ((iw lo-iw))
(make-mail iw (format "~a says good-bye" sender)))))
(module client racket
(provide
;; Boolean -> ???
run)
;; -----------------------------------------------------------------------------
(require 2htdp/universe)
(require 2htdp/image)
;; String -> Natural
;; run for at most 9 seconds, send your secret to server at t = 1
(define (run secret)
(big-bang (random 10)
(register LOCALHOST)
(on-receive (lambda (x msg)
(displayln msg)
x))
(to-draw (lambda (x)
(overlay (text secret 12 'blue)
(circle (+ 100 (* 10 x)) 'solid 'red))))
(on-tick (lambda (x)
(if (= x 1)
(make-package 0 secret)
(sub1 x)))
1
10)
(stop-when zero?))))
(require (prefix-in server: (submod "." universe))
(prefix-in client: (submod "." client))
2htdp/universe)
(define (main show?)
(launch-many-worlds
(server:run show?)
(client:run "matthias")
(client:run "matthew")
(client:run "robby")
(client:run "shriram")))
(main #f)

View File

@ -11,7 +11,17 @@
;; BallMail = (make-mail IWorld 'go) ;; BallMail = (make-mail IWorld 'go)
;; Result = (make-bundle [Listof IWorld] [Listof BallMail] '()) ;; Result = (make-bundle [Listof IWorld] [Listof BallMail] '())
(define Result0 (make-bundle '() '() '())) (define Result0 '() #;(make-bundle '() '() '()))
;; -----------------------------------------------------------------------------
;; Any -> [Listof IWorld]
(define (run _)
(universe '()
(on-new add-world)
(check-with list?)
(on-msg switch)
(on-disconnect disconnect)))
;; ----------------------------------------------------------------------------- ;; -----------------------------------------------------------------------------
;; [Listof IWorld] -> Result ;; [Listof IWorld] -> Result
@ -79,11 +89,4 @@
(test) (test)
(define (run _)
(universe '()
(on-new add-world)
(check-with list?)
(on-msg switch)
(on-disconnect disconnect)))
; (run 'go) ; (run 'go)

View File

@ -1,6 +1,6 @@
#! /bin/sh #! /bin/sh
#| #|
exec racket -tm "$0" ${1+"$@"} exec racket -t "$0" ${1+"$@"}
|# |#
#lang racket #lang racket

View File

@ -10,6 +10,7 @@ run() {
cd tests cd tests
run universe-disappearing.rkt
run bitmap-as-image-in-universe.rkt run bitmap-as-image-in-universe.rkt
run key-error.rkt run key-error.rkt
run to-draw-error.rkt run to-draw-error.rkt