/
pattern.py
845 lines (635 loc) · 33.7 KB
/
pattern.py
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
import socketio
import util
import pickle
import traceback
import os
import time
import json
import random
import copy
import numpy as np
import button_menu
from psiturk.db import db_session, init_db
from custom_models import Session, Task, Emission, User
class Modality(object):
pass
class HtmlUnity(Modality):
def __init__(self, sio, teacher, student, num_teaching_tasks=2, num_testing_tasks=2):
#self.pattern = pattern
self.sio = sio
self.teacher = teacher
self.student = student
new_session = Session(pattern=self.__class__.__name__)
db_session.add(new_session)
db_session.commit()
print("inserting session with pattern + " + new_session.pattern + " and id: " + str(new_session.session_id))
self.session_id = new_session.session_id
#self.session = db_session.query(Session).filter(Session.session_id==new_session.session_id).one()
#self.current_task_id = None
self.event_dict = {
'button': self.__class__.button,
'action': self.__class__.action,
'chat': self.__class__.chat,
#'initial_state' : self.__class__.initial_state,
#'game_state' : self.__class__.game_state
}
self.levels={}
for difficulty in ['easy', 'medium', 'hard']:
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "static", "stages", difficulty)
self.levels[difficulty] = [os.path.join(path, file) for file in os.listdir(path)]
self.initialized = [False for _ in range(num_teaching_tasks + num_testing_tasks)]
self.training_levels = [l for l in self.levels['easy']]
self.testing_levels = [l for l in self.levels['medium']]
random.shuffle(self.training_levels), random.shuffle(self.testing_levels)
self.leveltups = [self.load_level(self.training_levels) for _ in range(num_teaching_tasks)]
self.leveltups.extend([self.load_level(self.testing_levels) for _ in range(num_testing_tasks)])
for i in range(num_teaching_tasks + num_testing_tasks):
self.leveltups[i][0]['task_number'] = i
self.action_descriptions = {
'go': "move along current waypoints",
'set_waypoint': "move to point",
'extraAction' : "pick up or put down object",
'reset': "reset current waypoints"
}
self.unity_lock = {teacher : True, student : True}
self.html_lock = {teacher : True, student : True}
self.init_unity_lock = {teacher : True, student : False}
self.init_html_lock = {teacher : True, student : False}
self.training_buttons = {teacher : [], student : []}
self.read_instructions = {teacher : False, student : False}
self.emissions = {teacher : {}, student : {}}
self.finished = {teacher : False, student : False}
self.initial_state = None
self.current_state = None
self.prev_state = None
self.state_stack = []
self.actions = []
self.num_teaching_tasks = num_teaching_tasks
self.num_testing_tasks = num_testing_tasks
self.current_task = -1
self.requesting_finish = False
self.stale = False
self.last_action = None
self.idle = True
#self.prev_task_id = None
self.testing = False
self.last_callback = 0
self.current_task_record = None
self.synchronize_flag = False
self.last_location = {}
self.last_location[self.teacher] = {}
self.last_location[self.student] = {}
self.waiting = False
def revert(self, uid, state):
self.current_state = state
print("emitting revert to prior state")
if len(self.unity_lock) > 1:
self.emit("load", state, room=self.partner(uid))
self.emit("clearWaypoints", room=self.partner(uid))
def partner(self, actor):
if actor==self.student:
return self.teacher
if actor==self.teacher:
return self.student
return ""
def role_string(self, actor):
if self.student == self.teacher:
return "sandbox"
if actor==self.student:
return "student"
if actor==self.teacher:
return "teacher"
return "ROLE NOT DEFINED"
#blocks
def playback(self, session_id, task_number, room, use_student=True, fast=False):
task = db_session.query(Task).filter(Task.session_id==session_id).filter(Task.task_number==task_number).one()
user_room = task.student if use_student else task.teacher
prev_time = task.timestamp
self.sio.emit("reset", room=room)
for emission in task.emissions:
if user_room in emission.room: # == user_room or emission.room == 'playback'+user_room:
if emission.topic == 'action' or emission.topic == 'load' or emission.topic == 'sendTrainingMessage':
dt = (emission.timestamp - prev_time).total_seconds()
prev_time = emission.timestamp
if fast:
time.sleep(.25)
else:
time.sleep(dt)
data = json.loads(emission.data)
print("playback " + emission.topic + " @t " + str(prev_time))
self.sio.emit(emission.topic, data, room=room)
#time.sleep(1)
#self.sio.emit('load', json.loads(task.final_state), room=room)
def stage_file(self, name):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "static", "stages", str(name) + ".p")
def chat(self, actor, message):
if message[:4]=="emit":
try:
s = message.split()
self.emit(s[1], None, room=actor)
except:
print("error loading state:")
traceback.print_exc()
return
if message[:8]=="playback":
try:
s = message.split()
session_id = s[1]
task_number = s[2]
self.playback(session_id=session_id, task_number=task_number, room=actor)
except:
print("error playing back task " + str(s[1] + "::" + s[2]))
traceback.print_exc()
return
if message[:4]=="load":
try:
s = message.split()
path = self.stage_file(s[1])
f = pickle.load(open(path, 'rb'))
self.current_state = f
self.initial_state = f
self.prev_state = f
self.state_stack = []
self.emit("load", self.current_state, room=self.teacher)
self.emit('sendTrainingMessage', "* stage loaded from: " + path, room=self.teacher)
if self.student!=self.teacher:
self.emit("load", self.current_state, room=self.student)
self.emit('sendTrainingMessage', "* stage loaded from: " + path, room=self.student)
except:
print("error loading state from: " + path + " // ")
traceback.print_exc()
return
if message[:4]=="save":
try:
s = message.split()
#path = self.stage_file(s[1] +str(time.time()) )
init_path = self.stage_file(s[1]+str(time.time()) + "_init")
#pickle.dump(self.current_state, open(path, 'wb+'))
pickle.dump(self.initial_state, open(init_path, 'wb+'))
self.emit('sendTrainingMessage', "* stage saved to: " + init_path, room=actor)
except:
print("error saving state:")
traceback.print_exc()
return
if message=="refresh":
if self.current_state is not None:
self.emit("load", self.current_state, room=actor)
self.update_ui(actor)
return
if message=="new":
self.new_task()
return
if message=='reset':
self.initial_state = None
self.current_state = None
self.emit("reset", room=actor)
self.emit('sendTrainingMessage', 'YOU: '+ message, room=actor)
self.emit('sendTrainingMessage',
self.role_string(actor) + ': ' + message,
room=self.partner(actor))
def reconnect(self, actor):
print('attempting to reconnect ' + self.role_string(actor))
#arg = {"role" : self.role_string(actor), "pattern" : self.__class__.__name__}
#self.emit("instructions", arg, room=actor)
if self.current_state is not None:
self.emit('load', self.current_state, room=actor)
else:
print("current state is none, no state to load")
self.waiting = False
self.update_ui(actor)
#self.emit('sendTrainingMessage', "* Reconnected as "+self.role_string(actor) + " in " + self.__class__.__name__, room=actor)
def event(self, actor, event_type, event_data):
self.event_dict[event_type](self, actor, event_data)
self.update_ui()
def emit(self, topic, argument=None, room=None):
#log emissions to current task in database for future analysis/playback
if topic == 'load':
if len(argument.keys())<3:
print("small load: " + str(argument))
return
argument['waypoints'] = {}
print("attempting to load " + str(hash(frozenset(argument))) + " to " + str(room))
if self.current_task >= 0 and topic != "sleep_callback":
try:
argument_string = json.dumps(argument)
record = Emission(topic, argument_string, room) #self.session_id, self.current_task,
task = db_session.query(Task).filter(Task.session_id==self.session_id).filter(Task.task_number==self.current_task).first()
if task is not None:
task.emissions.append(record)
db_session.commit()
except Exception as e:
print("problem logging emission: " + str(topic) + ": " + str(e) + " on task " + str(self.session_id) + "::" + str(self.current_task))
if argument is None:
self.sio.emit(topic, room=room)
else:
self.sio.emit(topic, argument, room=room)
def update_ui(self, uid=None):
if uid is None:
for uid in self.finished.keys():
if not self.finished[uid]:
self.update_ui(uid)
return
""" update the ui to reflect the current state of the game """
if self.unity_lock[uid] or self.waiting:
self.emit('lock', room=uid)
else:
self.emit('unlock', room=uid)
if self.html_lock[uid] or self.waiting:
self.emit('lockButtons', room=uid)
else:
self.emit('unlockButtons', room=uid)
if uid in self.training_buttons:
if self.training_buttons[uid] is not None:
self.emit('getTrainingButtons', self.training_buttons[uid], room=uid)
def synchronize_state(self, actor, data, lead):
if 'xWp' in data:
xWp, yWp = float(data.get('xWp', 0)), float(data.get('yWp', 0))
x, y = float(data.get('xPos', None)), float(data.get('yPos', None))
self.last_location[actor][(xWp, yWp)] = np.array([x, y])
#ps = "synchronizing actor " + str(actor) + " wp " + str((xWp, yWp))
a_loc = self.last_location[actor].get((xWp, yWp), None)
b_loc = self.last_location[self.partner(actor)].get((xWp, yWp), None)
if a_loc is not None and b_loc is not None:
distance = np.linalg.norm(a_loc-b_loc)
#ps += " dist " + str(distance)
if distance > 1.5 and actor==lead:
self.synchronize_flag = True
self.emit("getGameState", lead)
print("distance: " + str(distance) + ", attempting to synchronize")
#print(ps)
def button(self, actor, action):
raise NotImplementedError
def ended_action(self, actor):
self.waiting = False
self.update_ui()
def game_state(self, actor, state):
raise NotImplementedError
# can be overwritten
def set_initial_state(self, actor, state, level_path=None):
""" handles an initial_state coming from an actor, or call with predefined level to """
if self.current_task_record is not None:
try:
db_session.add(self.current_task_record)
except Exception:
pass
if self.current_task_record.task_number == self.current_task:
return #already set initial state for this task
#if self.initial_state is None:
self.initial_state = state
self.current_state = state
print("level loaded for task " + str(self.current_task) + " current state set: " + str(hash(frozenset(state))) + ", size: " + str(len(state)))
new_task = Task(session_id=self.session_id, task_number=self.current_task, init_state=state)
new_task.level_path = level_path
self.current_task_record = new_task
session_record = db_session.query(Session).filter(Session.session_id==self.session_id).one()
session_record.tasks.append(new_task)
new_task.student = self.student #db_session.query(User).filter(User.user_id == self.student).one()
new_task.teacher = self.teacher #db_session.query(User).filter(User.user_id == self.teacher).one()
new_task.init_state = json.dumps(state)
db_session.commit()
print("creating task with id: " + str(new_task.session_id) + "::" + str(new_task.task_number))
#self.current_task_id = (BLAH)
# first initial state submitted is chosen
#self.current_state = state
self.prev_state = state
#self.state_stack=[state]
self.unity_lock = self.init_unity_lock
self.html_lock = self.init_html_lock
self.emit('load', self.current_state, room=actor)
self.update_ui(actor)
if len(self.unity_lock) > 1:
self.emit('load', self.current_state, room=self.partner(actor))
self.update_ui(self.partner(actor))
#else:
#self.emit('load', self.current_state, room=actor)
def load_level(self, levels):
levelobj = None
while levelobj is None:
#self.level = random_pop(levels)
self.level = random.choice(levels)
path = self.level
#path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static", "stages", self.level)
#print("chose level path " + str(path) + " " + str(os.path.exists(path)))
levelobj = pickle.load(open(path, 'rb'))
#print("levelobj: " + str(levelobj))
#print("hash " + str(hash(frozenset(levelobj))))
return levelobj, path
def sleep_callback(self, actor, seconds_remaining=3):
""" terrible, terrible workaround for being able to sleep """
def random_pop(l):
return l.pop(random.randrange(len(l)))
if seconds_remaining <= 0:
if self.current_task == self.num_teaching_tasks:
self.emit("sendTrainingMessage", "* Done with the training phase of the experiment. Now you will be tested on your knowledge. Complete the tasks as before, but you will receive no feedback or assistance. ", room=self.student)
self.waiting=False
if not self.initialized[self.current_task]:
self.initialized[self.current_task] = True
levelobj, path = self.leveltups[self.current_task]
self.set_initial_state(actor, levelobj, level_path=path)
#print("no levels to sample from, resetting instead")
#self.emit("reset", room=actor)
self.update_ui(actor)
else:
self.emit("sendTrainingMessage", "* " + str(seconds_remaining) + "..", room=actor)
self.emit("sleep_callback", seconds_remaining-1, room=actor)
def final_state(self, data):
try:
task_number = data.get('task_number', self.prev_task)
prev_task_record = db_session.query(Task).filter(Task.session_id==self.session_id).filter(Task.task_number==task_number).first()
if prev_task_record is not None:
#print("dumping final task info to: " + str(self.session_id) + "::" + str(task_number))
prev_task_record.final_state = json.dumps(data)
db_session.commit()
#print("saved game state to " + str(self.prev_task_id))
except Exception as e:
print("error storing final state" + str(e))
def new_task(self):
self.idle = False
self.prev_task = self.current_task
self.current_task += 1
#self.initial_state = None
#self.current_state = None
actors = []
print("starting new task " + str(self.current_task))
if self.current_task > 0:
for key in self.finished:
if not self.finished[key]:
print("requesting final game state")
#self.prev_task_id = self.current_task_id
self.emit("getGameState", room=key)
if self.current_task >= self.num_teaching_tasks + self.num_testing_tasks:
print("student finished")
self.emit("complete_hit", "args", room=self.student)
self.stale = True
self.finished[self.student] = True
# todo: cleanup game here
else:
actors.append(self.student)
if self.current_task >= self.num_teaching_tasks:
if self.current_task == self.num_teaching_tasks:
print("teacher finished")
self.emit("complete_hit", "args", room=self.teacher)
self.finished[self.teacher] = True
#self.test_mode(self.student)
HtmlUnityTest.init.__func__(self, self.student)
#x = HtmlUnityTest().init(self.student)
#x.init()
#self.init(self.student)
self.testing = True
else:
actors.append(self.teacher)
self.waiting = True
self.html_lock = self.init_html_lock
self.unity_lock = self.init_unity_lock
actors = list(set(actors))
for actor in actors:
self.emit("new_task", "testing" if len(actors)==1 else "training", room=actor)
if self.current_task > 0:
self.emit("sendTrainingMessage", "* Current task has been marked complete. Starting a new task in:", room=actor)
self.emit("sleep_callback", 3, room=actor)
else:
self.emit("sendTrainingMessage", "* Starting your first task.", room=actor)
self.emit("sleep_callback", 0, room=actor)
self.update_ui(actor)
def test_mode(self, user):
pass
class HtmlUnityTest(HtmlUnity):
def __init__(self, sio, user, num_teaching_tasks=2, num_testing_tasks=2):
super(HtmlUnityTest, self).__init__(sio=sio, teacher=user, student=user, num_teaching_tasks=num_teaching_tasks, num_testing_tasks=num_testing_tasks)
self.init(user)
def init(self, user):
self.event_dict['action'] = HtmlUnityTest.action.__func__
self.event_dict['button'] = HtmlUnityTest.button.__func__
print("init called on " + str(type(self)))
self.init_unity_lock = {user : False}
self.init_html_lock = {user : False}
self.unity_lock = self.init_unity_lock
self.html_lock = self.init_html_lock
self.training_buttons[user] = button_menu.test
self.update_ui(user)
def action(self, actor, action):
self.training_buttons[actor] = button_menu.test
if action['info']['function'] == 'set_waypoint':
self.emit("action", action, room='playback'+actor)
action['info']['function'] = 'go'
action['arguments']['alreadyPlayed'] = False
if not action['arguments']['alreadyPlayed']:
self.emit("action", action, room=actor)
self.update_ui(actor)
def button(self, actor, button_id):
if actor==self.student:
if button_id == "undo":
if len(self.state_stack) > 0:
self.emit("load", self.state_stack.pop(), room=actor)
if button_id == "instructions":
self.emit("instructions", "student", room=actor)
if button_id == "finish":
self.new_task()
class HtmlUnityReward(HtmlUnity):
def __init__(self, sio, teacher, student, num_teaching_tasks, num_testing_tasks):
super(HtmlUnityReward, self).__init__(sio=sio, teacher=teacher, student=student, num_teaching_tasks=num_teaching_tasks, num_testing_tasks=num_testing_tasks)
#self.unity_lock[self.student] = False
#self.html_lock[self.student] = False
self.training_buttons[self.student] = button_menu.reward_student
self.update_ui()
def action(self, actor, action):
self.last_action = action
if actor==self.teacher:
assert False
if actor==self.student:
question_str = "The student wants to " + self.action_descriptions.get(action['info']['function'], 'DESCRIPTION NOT FOUND') + ", allow it?"
if action['arguments']['alreadyPlayed']:
self.emit('action', action, room=self.teacher)
#if self.prev_state is None:
#self.prev_state = action['prior state']
self.emit('sendTrainingMessage', "* " + question_str, room=self.teacher)
self.training_buttons[self.teacher] = button_menu.reward_teacher
self.training_buttons[self.student] = []
self.unity_lock[self.student] = True
self.html_lock[self.student] = True
self.html_lock[self.teacher] = False
self.update_ui()
def button(self, actor, button_id):
if actor==self.student:
if button_id == "finish:request":
self.unity_lock[self.student] = True
self.html_lock[self.teacher] = False
self.html_lock[self.student] = True
self.emit('sendTrainingMessage', "* The student believes they are finished, allow progress to the next task?", room=self.teacher)
self.training_buttons[self.teacher] = button_menu.confirm_finished
self.training_buttons[self.student] = []
if actor==self.teacher:
self.control = self.student
self.unity_lock[self.student] = False
self.html_lock[self.teacher] = True
self.html_lock[self.student] = False
if button_id == "finish:yes":
self.new_task()
if button_id == "finish:no":
self.emit('sendTrainingMessage', r"* The teacher does not think you're finished, try to figure out what you've forgotten.", room=self.student)
self.emit('getPrevStateAndRevert', room=self.student)
#self.emit('load', self.prev_state, room=self.student)
#self.emit('load', self.prev_state, room=self.teacher)
if button_id == "action:yes":
if self.last_action['info']['function'] == 'set_waypoint':
self.emit("action", self.last_action, room='playback'+actor)
self.last_action['arguments']['alreadyPlayed'] = False
self.last_action['info']['function'] = 'go'
if not self.last_action['arguments']['alreadyPlayed']:
if self.last_action['info']['function'] == 'go':
self.waiting = True
self.emit('action', self.last_action, room=self.teacher)
self.emit('action', self.last_action, room=self.student)
self.last_action['arguments']['alreadyPlayed'] = True
self.emit('sendTrainingMessage', '* The teacher allowed your action.', room=self.student)
if button_id == "action:no":
#self.emit('load', self.prev_state, room=self.student)
#self.emit('load', self.prev_state, room=self.teacher)
#self.emit('getPrevStateAndRevert', room=self.student)
self.emit('revertState', room=self.student)
self.emit('revertState', room=self.teacher)
self.emit('sendTrainingMessage', '* The teacher disallowed your action. Try again.', room=self.student)
self.emit("clearWaypoints", room=self.student)
#self.prev_state = None
self.training_buttons[self.teacher] = []
self.training_buttons[self.student] = button_menu.reward_student
self.update_ui()
class HtmlUnityDemonstrate(HtmlUnity):
def __init__(self, sio, teacher, student, num_teaching_tasks, num_testing_tasks):
super(HtmlUnityDemonstrate, self).__init__(sio=sio, teacher=teacher, student=student, num_teaching_tasks=num_teaching_tasks, num_testing_tasks=num_testing_tasks)
self.training_buttons[self.teacher] = button_menu.demonstrate_teacher
self.init_unity_lock = {teacher : False, student : True}
self.init_html_lock = {teacher : False, student : True}
self.update_ui(teacher)
def action(self, actor, action):
if actor==self.student:
print("student shouldn't be able to take actions")
if actor==self.teacher:
if action['info']['function'] == 'set_waypoint':
self.emit("action", action, room='playback'+actor)
self.emit("action", action, room=self.student)
action['arguments']['alreadyPlayed'] = False
action['info']['function'] = 'go'
if not action['arguments']['alreadyPlayed']:
self.emit("action", action, room=actor)
self.emit("action", action, room=self.student)
self.emit('action', action, room=self.student)
#self.prev_state = action['prior state']
#self.state_stack.append(action['prior state'])
def button(self, actor, button_id):
if actor==self.teacher:
if button_id == "finish":
self.new_task()
if button_id == "undo":
self.state_stack.pop()
self.emit('load', self.state_stack[-1], room=self.student)
self.emit('load', self.state_stack[-1], room=self.teacher)
self.update_ui()
class HtmlUnityApprentice(HtmlUnity):
def __init__(self, sio, teacher, student, num_teaching_tasks, num_testing_tasks):
super(HtmlUnityApprentice, self).__init__(sio=sio, teacher=teacher, student=student, num_teaching_tasks=num_teaching_tasks, num_testing_tasks=num_testing_tasks)
self.training_buttons[self.student] = button_menu.apprentice_student
self.update_ui()
self.demonstrate=False
def action(self, actor, action):
if actor==self.teacher:
if self.demonstrate:
if action['info']['function'] == 'set_waypoint':
self.emit("action", action, room='playback'+actor)
self.emit("action", action, room=self.student)
action['arguments']['alreadyPlayed'] = False
action['info']['function'] = 'go'
if not action['arguments']['alreadyPlayed']:
self.emit("action", action, room=actor)
self.emit('action', action, room=self.student)
#if self.prev_state is None:
#self.prev_state = action['prior state']
self.demonstrate=False
self.training_buttons[self.teacher] = []
self.training_buttons[self.student] = button_menu.apprentice_student
self.unity_lock[self.teacher] = True
self.html_lock[self.teacher] = True
self.unity_lock[self.student] = False
self.html_lock[self.student] = False
self.update_ui()
if actor==self.student:
self.last_action = action
question_str = "The student wants to " + self.action_descriptions.get(action['info']['function'], 'DESCRIPTION NOT FOUND') + ", allow it?"
if action['arguments']['alreadyPlayed']:
#print(str(action['info']['function']) + " " + str(action['arguments']))
#self.emit('sendTrainingMessage', "PLAYING ACTION" + action['info']['function'], room=self.teacher)
self.emit('action', action, room=self.teacher)
#if self.prev_state is None:
#self.prev_state = action['prior state']
self.emit('sendTrainingMessage', "* " + question_str, room=self.teacher)
self.training_buttons[self.teacher] = button_menu.apprentice_teacher
self.training_buttons[self.student] = []
self.unity_lock[self.student] = True
self.html_lock[self.student] = True
self.html_lock[self.teacher] = False
self.update_ui()
def button(self, actor, button_id):
if actor==self.student:
if button_id == "finish:request":
self.unity_lock[self.student] = True
self.html_lock[self.teacher] = False
self.html_lock[self.student] = True
self.emit('sendTrainingMessage', "* The student believes they are finished, allow progress to the next task?", room=self.teacher)
self.training_buttons[self.teacher] = button_menu.confirm_finished
self.training_buttons[self.student] = []
if button_id == "help:request":
self.unity_lock[self.student] = True
self.html_lock[self.student] = True
self.emit('sendTrainingMessage', "* The student is unsure of what to do next, take a single move to give them a hint.", room=self.teacher)
self.unity_lock[self.teacher] = False
self.demonstrate = True
self.training_buttons[self.student] = []
self.training_buttons[self.teacher] = button_menu.demonstrate_teacher
if actor==self.teacher:
self.control = self.student
self.unity_lock[self.student] = False
self.html_lock[self.teacher] = True
self.html_lock[self.student] = False
if button_id == "finish":
self.emit('sendTrainingMessage', r"* The teacher indicated you finished with the task", room=self.student)
self.new_task()
if button_id == "finish:yes":
self.emit('sendTrainingMessage', r"* The teacher agreed that you are finished with the task", room=self.student)
self.new_task()
if button_id == "finish:no":
self.emit('sendTrainingMessage', r"* The teacher does not think you're finished, try to figure out what you've forgotten.", room=self.student)
self.emit('revertState', room=self.student)
self.emit('revertState', room=self.teacher)
#self.emit('load', self.prev_state, room=self.student)
#self.emit('load', self.prev_state, room=self.teacher)
if button_id == "action:yes":
if self.last_action['info']['function'] == 'set_waypoint':
self.emit("action", self.last_action, room='playback'+actor)
self.last_action['arguments']['alreadyPlayed'] = False
self.last_action['info']['function'] = 'go'
if not self.last_action['arguments']['alreadyPlayed']:
if self.last_action['info']['function'] == 'go':
self.waiting = True
self.emit('action', self.last_action, room=self.teacher)
self.emit('action', self.last_action, room=self.student)
self.last_action['arguments']['alreadyPlayed'] = True
self.emit('sendTrainingMessage', '* The teacher allowed your action.', room=self.student)
if button_id == "action:no":
#self.emit('load', self.prev_state, room=self.student)
#self.emit('load', self.prev_state, room=self.teacher)
self.emit('revertState', room=self.student)
self.emit('revertState', room=self.teacher)
self.emit('sendTrainingMessage', '* The teacher disallowed your action. Try again.', room=self.student)
self.emit("clearWaypoints", room= self.student)
#self.prev_state = None
self.training_buttons[self.teacher] = []
self.training_buttons[self.student] = button_menu.apprentice_student
self.update_ui()
if __name__ == "__main__":
h = HtmlUnity(sio="sio", teacher="teacher", student="student")
tl = h.training_levels
js = [pickle.load(open(x, 'rb')) for x in tl]
for i in range(len(tl)):
print(tl[i])
print(str(js[i]))
#HtmlUnityDemonstrate(sio="sio", teacher="teacher", student="student")