from otree.api import Currency as c, currency_range from otree.models import subsession from .models import Subsession from ._builtin import Page, WaitPage from .models import Constants class Instructions(Page): def is_displayed(self): return self.round_number == 1 # this definition defines the variable is_displayed which is only relevant for the # instruction page. Then when the complier loops over this paticular page, it checks whether # at the same time the round number is equal to 1 and if it is it returns the page, that is # it shows the page. Otherwise, it won't def vars_for_template(self): return dict( dbg_seq=self.participant.vars.get('discount_seq'), dbg_tg=self.participant.vars.get('treatment_group') ) class Task1_Quiz(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' # form fields # ---------------------------------------------------------------------------------------------------------------- def get_form_fields(self): return ['q1', 'q2',] ans1 = 6.50 ans2 = 2 def error_message(self, values): # count number of incorrect attempts. if self.player.incorrect_attempts == None: self.player.incorrect_attempts = 0 if values['q1'] != self.ans1 or values['q2'] != self.ans2: self.player.incorrect_attempts += 1 # define error message that is displayed. if values['q1'] != self.ans1: return 'Your answer to Question 1 is incorrect!' if values['q2'] != self.ans2: return 'Your answer to Question 2 is incorrect!' def vars_for_template(self): return { 'answer1': self.ans1, 'answer2': self.ans2, } class Middle_Screen(Page): def is_displayed(self): return self.round_number == 1 class Decision(Page): # form model # ---------------------------------------------------------------------------------------------------------------- form_model = 'player' # form fields # ---------------------------------------------------------------------------------------------------------------- def get_form_fields(self): # unzip list of form_fields from list form_fields = self.participant.vars['form_fields'] return form_fields def vars_for_template(self): mpl = self.player.participant.vars['mpl_sequence'][self.round_number - 1] mpl_transp = [list(i) for i in zip(*mpl)] return { 'mpl': mpl_transp, 'mpl2': mpl, 'first': mpl[0][0], 'lot_hi': mpl[0][0], 'lot_lo': mpl[1][0], 'prob_hi': mpl[2][0], 'prob_lo': mpl[3][0], 'num_choices': Constants.num_choices, 'round_number': self.round_number, } def before_next_page(self): #set list number self.player.set_list_number() # extract choices from form_fields before we move on form_fields = self.participant.vars['form_fields'] # replace choices in for j, choice in zip(Constants.index_choices, form_fields): choice_i = getattr(self.player, choice) self.participant.vars['mpl_choices_made'][j - 1] = choice_i # set consistency and switching row self.player.set_consistency() self.player.set_switching_row() # set round to pay self.player.set_payoffs() # determine certainty equivalent (which is important for the time preference task that follows this one) # 1. determine CE for every MPL mpl = self.player.participant.vars['mpl_sequence'][self.round_number - 1] mpl_transp = [list(i) for i in zip(*mpl)] if self.player.inconsistent == 0 and self.player.switching_row != 1 and self.player.switching_row != Constants.num_choices +1: cert_equi = mpl_transp[self.player.switching_row - 2][4] - ((mpl_transp[self.player.switching_row-2][4] - mpl_transp[self.player.switching_row-1][4])/2) elif self.player.inconsistent == 0 and self.player.switching_row == 1 and self.player.switching_row != Constants.num_choices +1: cert_equi = mpl_transp[0][4] else: cert_equi = mpl_transp[Constants.num_choices - 1][4] cert_equi_rounded = c(round(cert_equi + 0.01,1)) # we have to add this very small number since otherwise the rounding in python is strange self.player.participant.vars['cert_equi'].append(cert_equi) # execute respective command in models sheet self.player.set_certainty_equivalents() # 2. determine CE for MPL that shall be used in Time_Results preference task if mpl[0][0] == Constants.list_2[0][0] and mpl[1][0] == Constants.list_2[1][0] and mpl[2][0] == '50%': #we have to type in this last condition manually because its a string variable. So, make sure to change it in case you want to change the list. self.player.participant.vars['cert_equi_list2'].append(cert_equi_rounded) elif mpl[0][0] == Constants.list_9[0][0] and mpl[1][0] == Constants.list_9[1][0] and mpl[2][0] == '50%': self.player.participant.vars['cert_equi_list9'].append(cert_equi_rounded) elif mpl[0][0] == Constants.list_12[0][0] and mpl[1][0] == Constants.list_12[1][0] and mpl[2][0] == '50%': self.player.participant.vars['cert_equi_list12'].append(cert_equi_rounded) #以下是我添加的代码为了根据discount_seq随机分组 import random if 'discount_seq' not in self.participant.vars: self.participant.vars['discount_seq'] = random.choice([1, 2]) print('Assigned discount_seq:', self.participant.vars['discount_seq']) class Thank_You(Page): def is_displayed(self): return self.round_number == Constants.num_rounds def before_next_page(self): self.player.set_settings_for_next_task() #def app_after_this_page(self, upcoming_apps): # if self.player.participant.vars['discount_seq'] == 1: # return 'Time_Adaptive_Lot_1' # if self.player.participant.vars['discount_seq'] == 2: # return 'Time_Adaptive_CE_84' #def app_after_this_page(self, upcoming_apps): # seq = self.participant.vars['seq'] # # first_app = { # 1: 'Time_Adaptive_Lot_1', # seq=1: Lot1_48 # 2: 'Time_Adaptive_CE', # seq=2: CE_48 # 3: 'Time_Adaptive_Lot_1_84', # seq=3: Lot1_84 # 4: 'Time_Adaptive_CE_84', # seq=4: CE_84 # }[seq] # # return first_app def app_after_this_page(self, upcoming_apps): seq = self.participant.vars.get('discount_seq') # 48: seq 1/2 if seq == 1: return 'Time_Adaptive_Lot_1' # Lot1_48 if seq == 2: return 'Time_Adaptive_CE' # CE_48 # 84: seq 3/4 if seq == 3: return 'Time_Adaptive_Lot_1_84' # Lot1_84 if seq == 4: return 'Time_Adaptive_CE_84' # CE_84 def vars_for_template(self): pv = self.participant.vars return dict( dbg_seq=pv.get('discount_seq'), dbg_path=pv.get('path'), dbg_order=pv.get('order'), dbg_treat=pv.get('treatment_group'), dbg_session_code=self.session.code, dbg_pid=self.participant.id_in_session, ) page_sequence = [ Instructions, Task1_Quiz, Middle_Screen, Decision, Thank_You, ]