Template

Template preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0beta1 • Viewed 165 times • Downloaded 13 times • Run 0 times
Download the 'Template' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;; create a breed of turtles that the students control through the clients
;; there will be one student turtle for each client.
breed [ students student ]

students-own
[
  user-id   ;; students choose a user name when they log in whenever you receive a
            ;; message from the student associated with this turtle hubnet-message-source
            ;; will contain the user-id
  step-size ;; you should have a turtle variable for every widget in the client interface that
            ;; stores a value (sliders, choosers, and switches). You will receive a message
            ;; from the client whenever the value changes. However, you will not be able to
            ;; retrieve the value at will unless you store it in a variable on the server.
]

;; the STARTUP procedure runs only once at the beginning of the model
;; at this point you must initialize the system.

to startup
  hubnet-reset
end 

to setup
  clear-patches
  clear-drawing
  clear-output
  ;; during setup you do not want to kill all the turtles
  ;; (if you do you'll lose any information you have about the clients)
  ;; so reset any variables you want to default values, and let the clients
  ;; know about the change if the value appears anywhere in their interface.
  ask turtles
  [
    set step-size 1
    hubnet-send user-id "step-size" step-size
  ]
  ;; calling reset-ticks enables the 'go' button
  reset-ticks
end 

to go
  ;; process incoming messages and respond to them (if needed)
  ;; listening for messages outside of the every block means that messages
  ;; get processed and responded to as fast as possible
  listen-clients
  every 0.1
  [
    ;; tick (and display) causes world updates messages to be sent to clients
    ;; these types of messages should only be sent inside an every block.
    ;; otherwise, the messages will be created as fast as possible
    ;; and consume your bandwidth.
    tick
  ]
end 

;;
;; HubNet Procedures
;;

to listen-clients
  ;; as long as there are more messages from the clients
  ;; keep processing them.
  while [ hubnet-message-waiting? ]
  [
    ;; get the first message in the queue
    hubnet-fetch-message
    ifelse hubnet-enter-message? ;; when clients enter we get a special message
    [ create-new-student ]
    [
      ifelse hubnet-exit-message? ;; when clients exit we get a special message
      [ remove-student ]
      [ ask students with [user-id = hubnet-message-source]
        [ execute-command hubnet-message-tag ] ;; otherwise the message means that the user has
      ]                                        ;; done something in the interface hubnet-message-tag
                                               ;; is the name of the widget that was changed
    ]
  ]
end 

;; when a new user logs in create a student turtle
;; this turtle will store any state on the client
;; values of sliders, etc.

to create-new-student
  create-students 1
  [
    ;; store the message-source in user-id now
    ;; so when you get messages from this client
    ;; later you will know which turtle it affects
    set user-id hubnet-message-source
    set label user-id
    ;; initialize turtle variables to the default
    ;; value of the corresponding widget in the client interface
    set step-size 1
    ;; update the clients with any information you have set
    send-info-to-clients
  ]
end 

;; when a user logs out make sure to clean up the turtle
;; that was associated with that user (so you don't try to
;; send messages to it after it is gone) also if any other
;; turtles of variables reference this turtle make sure to clean
;; up those references too.

to remove-student
  ask students with [user-id = hubnet-message-source]
  [ die ]
end 

;; Other messages correspond to users manipulating the
;; client interface, handle these individually.

to execute-command [command]
  ;; you should have one if statement for each widget that
  ;; can affect the outcome of the model, buttons, sliders, switches
  ;; choosers and the view, if the user clicks on the view you will receive
  ;; a message with the tag "View" and the hubnet-message will be a
  ;; two item list of the coordinates
  if command = "step-size"
  [
    ;; note that the hubnet-message will vary depending on
    ;; the type of widget that corresponds to the tag
    ;; for example if the widget is a slider the message
    ;; will be a number, of the widget is switch the message
    ;; will be a boolean value
    set step-size hubnet-message
    stop
  ]
  if command = "up"
  [ execute-move 0 stop ]
  if command = "down"
  [ execute-move 180 stop ]
  if command = "right"
  [ execute-move 90 stop ]
  if command = "left"
  [ execute-move 270 stop ]
end 

;; whenever something in world changes that should be displayed in
;; a monitor on the client send the information back to the client

to send-info-to-clients ;; turtle procedure
  hubnet-send user-id "location" (word "(" pxcor "," pycor ")")
end 

to execute-move [new-heading]
  set heading new-heading
  fd step-size
  send-info-to-clients
end 


; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There are 6 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 9 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky almost 10 years ago Updated version tag Download this version
Uri Wilensky over 10 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Template Download this version
Uri Wilensky over 12 years ago Template Download this version

Attached files

File Type Description Last updated
Template.png preview Preview for 'Template' over 9 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.