from . import models from ._builtin import Page, WaitPage from otree.api import Currency as c, currency_range from .models import Constants, Match import time import boto3 import settings class MatchingWaitPage(WaitPage): template_name = Constants.location+'/MatchingWaitPage.html' group_by_arrival_time = True def vars_for_template(self): wait_minutes = self.session.config.get('matching_wait_minutes', 5) # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) # user has 5 minutes to complete as many pages as possible; waitpage_expiry_time is measured in milliseconds self.participant.vars['waitpage_expiry_time'] = int( (self.participant.vars['waitpage_arrival_time'] + wait_minutes*60 - 2)*1000) return { 'wait_minutes': wait_minutes, 'expiry_timestamp': self.participant.vars['waitpage_expiry_time'], } def is_displayed(self): # only use the custom wait page at the start of this part p = self.player if p.participant.vars.get('matched',0): # if already matched, set the parameters p.type = p.id_in_group if p.round_number != 1: pp = p.in_round(p.round_number-1) # unless in the first round, set the type the same as in the previous round p.pmat = pp.pmat if p.type == 1: p.P11,p.P12,p.P21,p.P22 = Constants.payoff_matrix[p.pmat][0] p.P11o,p.P12o,p.P21o,p.P22o = Constants.payoff_matrix[p.pmat][1] elif p.type == 2: p.P11,p.P12,p.P21,p.P22 = Constants.payoff_matrix[p.pmat][1] p.P11o,p.P12o,p.P21o,p.P22o = Constants.payoff_matrix[p.pmat][0] return (not self.participant.vars.get('finished',0)) class Introduction(Page): timeout_seconds = 60 def is_displayed(self): # the function is always run, can be used to set parameters return (self.participant.vars.get('matched',0) and (not self.participant.vars.get('finished',0))) # return self.round_number == 1 def before_next_page(self): self.participant.vars['decision_start_time'] = time.time() self.participant.vars['time_used'] = 0 class Decision(Page): live_method = 'live_method' def js_vars(self): debug = self.session.config.get('debug',0) return dict( timer_display_seconds=Constants.timer_display_seconds, timeout_seconds=3 if debug else Constants.timeout_seconds, # time allowed for making decision debug=debug, # debug=0, ) def vars_for_template(self): return { 'button': self.session.config.get('button',True), } def is_displayed(self): return (self.participant.vars.get('matched',0) and (not self.participant.vars.get('finished',0))) class Summary(Page): # timeout_seconds = 300 def is_displayed(self): if self.participant.vars.get('matched',0): self.player.decision_time = round(time.time() - self.participant.vars['decision_start_time']) ## setting qualification value if not self.participant.vars.get('matched', 0): v = 1 elif self.player.status == 'dropout': v = 2 elif self.player.status == 'other_dropout': v = 3 else: v = 0 print(self.participant.id_in_session, 'Current status:', v) if v and (not self.session.config.get('debug', False)): access_key_id = self.session.config['access_key_id'] secret_access_key = self.session.config['secret_access_key'] if self.session.config.get('sandbox', False): # sandbox Qid = self.session.config['Qid_sandbox'] mturk = boto3.client( service_name='mturk', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name='us-east-1', endpoint_url="https://mturk-requester-sandbox.us-east-1.amazonaws.com", ) else: # live production Qid = self.session.config['Qid'] mturk = boto3.client( service_name='mturk', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name='us-east-1', ) try: mturk.associate_qualification_with_worker( QualificationTypeId=Qid, WorkerId=self.participant.label, IntegerValue=v, SendNotification=False ) print('Qualification %d granted for player '%v, self.participant.id_in_session) except Exception as e: print(e) return (self.participant.vars.get('matched',0) and not self.participant.vars.get('finished',0)) def before_next_page(self): self.participant.vars['game_payoff'] = self.player.payoff def vars_for_template(self): return { 'history': self.participant.vars['history'][::-1], } class EndInfo(Page): def vars_for_template(self): return { 'mturk': self.session.config.get('mturk',False), 'matched': self.participant.vars.get('matched',0), 'qualified': self.participant.vars.get('qualified',0), 'dropout': self.participant.vars.get('dropout',0), } def is_displayed(self): return (self.participant.vars.get('finished',0) and not self.participant.vars.get('qualified',0)) page_sequence = [ MatchingWaitPage, Introduction, Decision, Summary, # EndInfo, ]