-
Notifications
You must be signed in to change notification settings - Fork 1
/
Template-Python.nlogo
280 lines (250 loc) · 4.34 KB
/
Template-Python.nlogo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
extensions [hubnet-proxy]
breed [ students student ]
students-own [ user-id step-size ]
to startup
hubnet-reset
hubnet-proxy:start 9999
end
to setup
clear-patches
clear-drawing
clear-output
ask turtles [
set step-size 1
hubnet-send user-id "step-size" step-size
]
reset-ticks
end
to go
listen-clients
every 0.1 [ tick ]
end
to listen-clients
while [ hubnet-message-waiting? ]
[
hubnet-fetch-message
ifelse hubnet-enter-message?
[ create-new-student ]
[
ifelse hubnet-exit-message?
[ remove-student ]
[ ask students with [user-id = hubnet-message-source]
[ execute-command hubnet-message-tag ]
]
]
end
to create-new-student
create-students 1 [
set user-id hubnet-message-source
set label user-id
set step-size 1
send-info-to-clients
]
end
to remove-student
ask students with [user-id = hubnet-message-source] [ die ]
end
to execute-command [command]
if command = "step-size" [ 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
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
@#$#@#$#@
GRAPHICS-WINDOW
231
10
661
461
10
10
20.0
1
10
1
1
1
0
0
0
1
-10
10
-10
10
1
1
0
ticks
30.0
BUTTON
34
51
105
84
NIL
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
107
51
178
84
NIL
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
@#$#@#$#@
## WHAT IS IT?
This template contains code that can serve as a starting point for creating new HubNet activities. It shares many of the basic procedures used by other HubNet activities, which are required to connect to and communicate with clients in Disease-like activities.
## HOW IT WORKS
In activities like Disease, each client controls a single turtle on the server. These turtles are a breed called STUDENTS. When a client logs in we create a new student turtle and set it up with the default attributes. Students own a variable for every widget on the client that holds a state, that is, sliders, switches, choosers, and input boxes. Whenever a user changes one of these elements on the client, a message is sent to the server. The server catches the message and stores the result. In this example a slider is used to demonstrate this behavior. You can also send messages to the client-side widgets using hubnet-send. Monitors on clients must be updated manually by the model, that is you must send a message to a monitor every time you want the value displayed to change. For example, if you have a monitor that displays the current location of the client's avatar, you must send a message to the client like this:
hubnet-send "location" (word xcor " " ycor)
whenever the client moves. Buttons on the client side send but do not receive messages. When a user presses a button, a message is sent to the server. The server catches the message and executes the appropriate commands. In this case, the commands should always be turtle commands since the clients control only a single turtle.
## HOW TO USE IT
To start the activity press the GO button. Ask students to login using the HubNet client or you can test the activity locally by pressing the LOCAL button in the HubNet Control Center. To see the view in the client interface check the Mirror 2D view on clients checkbox. The clients can use the UP, DOWN, LEFT, and RIGHT buttons to move their avatar and change the amount they move each step by changing the STEP-SIZE slider.
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250
@#$#@#$#@
NetLogo 5.0RC2
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
VIEW
252
10
682
440
0
0
0
1
1
1
1
1
0
1
1
1
-10
10
-10
10
BUTTON
85
121
147
154
up
NIL
NIL
1
T
OBSERVER
NIL
I
BUTTON
85
187
150
220
down
NIL
NIL
1
T
OBSERVER
NIL
K
BUTTON
147
154
210
187
right
NIL
NIL
1
T
OBSERVER
NIL
L
BUTTON
23
154
85
187
left
NIL
NIL
1
T
OBSERVER
NIL
J
SLIDER
39
78
189
111
step-size
step-size
1
5
2
1
1
NIL
HORIZONTAL
MONITOR
70
21
157
70
location
NIL
0
1
@#$#@#$#@
default
0.0
-0.2 0 1.0 0.0
0.0 1 1.0 0.0
0.2 0 1.0 0.0
link direction
true
0
Line -7500403 true 150 150 90 180
Line -7500403 true 150 150 210 180
@#$#@#$#@
0
@#$#@#$#@