Esempio n. 1
0
    def new_trial (self) :
        """ 
        advance to the next trial 
        """
        self.trialNum = self.trialNum + 1
        new_trial = self.trialList[self.trialNum]

        # Swap roles every block of four trials
        speaker_index = math.floor(self.trialNum / self.numRepetitions) % 2
        listener_index = (math.floor(self.trialNum / self.numRepetitions) + 1) % 2        
        roles = {
            'speaker' : self.players[speaker_index],
            'listener' : self.players[listener_index]
        }
        packet = json.dumps({
            'type': 'newTrial',
            'networkid' : self.network_id,
            'roomid' : self.room_id,
            'stim_set_id' : self.stim_set_id,
            'participantids' : self.players,
            'partnerNum' : self.partner_num,
            'prev' : self.prev,
            'trialNum' : self.trialNum,
            'targetURL' : new_trial['targetImg']['url'],
            'currStim' : new_trial['stimuli'],
            'roles' : roles
        })
        
        redis_conn.publish('refgame', packet)
Esempio n. 2
0
 def new_partner(self, room_id, partner_num) :
     """ 
     advance to the next partner on game schedule
     """
     ids = self.rooms[room_id].players
     if partner_num + 1 >= self.num_players :
         self.players.remove(ids[0])
         self.players.remove(ids[1])
         redis_conn.publish(
             'refgame',
             json.dumps({'type' : 'disconnectClient', 'participantids' : ids})
         )
     else :
         self.ready.extend(ids)
         redis_conn.publish(
             'refgame',
             json.dumps({
                 'type' : 'waitForPartner',
                 'participantids' : ids,
                 'partnerNum' : partner_num,
                 'schedule' : self.schedule
             })
         )
         t = threading.Timer(4, lambda : self.assignPartners(partner_num))
         t.start()        
Esempio n. 3
0
 def publish(self):
     """Relay messages from client to redis."""
     while not self.ws.closed:
         # Sleep to prevent *constant* context-switches.
         gevent.sleep(self.lag_tolerance_secs)
         message = self.ws.receive()
         if message is not None:
             channel_name, data = message.split(":", 1)
             redis_conn.publish(channel_name, data)
Esempio n. 4
0
 def handle_disconnect(self, msg) :
     p = Participant.query.get(msg['participantid'])
     network_id = p.all_nodes[0].network_id
     
     # if disconnected participant has not already finished game, disconnect rest of their network
     if msg['participantid'] in self.games[network_id].players :
         p.status = "dropped"
         redis_conn.publish(
             'refgame',
             json.dumps({'type' : 'disconnectClient', 'networkid' : network_id})
         )
Esempio n. 5
0
 def publish(self, msg):
     """Publish a message to all memoryexpt2 clients"""
     redis_conn.publish('memoryexpt2', json.dumps(msg))