06 Key H

In this animation, hitting a key could change the color of this world or send a message:

  • “r”: Randomizes the color of the current world.
  • “1” through “5”: Send messages to the corresponding world 1 … 5, telling them to change to the current world’s color.

Sending a Message

Messages can be sent to the server from any handler by returning a Package instead of a model. A Package is a special struct whose only purpose is to allow you to perform two actions from one handler.

Interpretation:

(make-package new-model the-message)
  • new-model is the model that will be used by the current world
  • the-message is a message that will be sent to the server (and hence broadcast to all of the other worlds, with our server).

Behavior of key-h

The check below describes the situation from the point of view of world 2, which is currently “light blue”. When key “1” is hit, a message is sent from world 2 to world 1 with the color “light blue”.

(check-expect (key-h (make-ic 2 "light blue")
                     "1")
              (make-package (make-ic 2 "light blue")
                            (make-ic-msg 2 1 "light blue")))

Exercise: Write key-h

Design and test a key handler with the behavior described above. Use string->number to convert the key to a world id number.

Add in feature: get-color

The “r” key is supposed to randomize the color of the world in which it is pressed. Design and test this add in feature. A helper function to choose the color is suggested.

Annoyance: You cannot send a make-color as part of a message. Use color words (like “orange”) or pick a fixed palette of colors and remember the number of the color.

Helper: pick three or more colors and write a function get-color: number -> color that takes in a number 0, 1, 2, …, and puts out a color like “orange”, “light blue”, “light green”, ….

(define (get-color n)
    (cond [(= n 0) "orange"]
          ...
          ))

Verify in a big-bang that pressing “r” randomizes the world’s color.

Discussion

There are several questions that regularly come up in discussion.

Just updating the model

When you only want to update the model, just return the updated model like every other Big Bang handler.

Just sending a message

It is not possible to have the only action of a handler be to send a message. When a handler does not want to change the model, it needs to just return the model it received as input. Use the same idea to return a package with the unchanged model as the first part.

As an example, the key handler below sends a message claiming to be from world 1 to world 2 no matter what key is hit, and no matter what world is running the code.

(define (key-h model key)
  (make-package model (make-ic-msg 1 2 "yellow")))

Misconceptions

  • Can one world change another world’s model? No. The first world can only send a message to the second world. It is up to the second world to perform the change based on the message it receives.
Last modified August 18, 2023: 2022-2023 End State (7352e87)