from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class PhoneNumber(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' form_fields = ['phone'] class Introduction(Page): form_model = 'player' form_fields = ['test_1', 'test_2', 'test_3'] def is_displayed(self): return self.round_number == 1 def error_message(self, value): bb = value['test_1'] + value['test_2'] + value['test_3'] if bb != 3: self.participant.vars['test_tries'] = self.participant.vars['test_tries'] + 1 self.player.test_tries = self.participant.vars['test_tries'] self.participant.vars['test_1'] = value['test_1'] self.participant.vars['test_2'] = value['test_2'] self.participant.vars['test_3'] = value['test_3'] return "您给出的答案至少有一个不正确。请您认真阅读下方提示并确定理解任务。" def vars_for_template(self): treatment = self.participant.vars['l_dict'][1]['treatment'] if treatment == 1: payoffs_a_1 = [20, 40, 70] payoffs_b_1 = [60, 30, 50] colors_a_1 = ['red', 'blue', 'orange'] colors_b_1 = ['red', 'blue', 'orange'] exp_a = payoffs_a_1 exp_b = payoffs_b_1 exp_c = colors_a_1 payoffs_a_2 = [20, 150, 30, 10] payoffs_b_2 = [100, 50, 30, 40] colors_a_2 = ['red', 'blue', 'orange', 'green'] colors_b_2 = ['red', 'blue', 'orange', 'green'] exp_a_2 = payoffs_a_2 exp_b_2 = payoffs_b_2 exp_c_2 = colors_a_2 payoff_blue_a = 150 payoff_blue_b = 50 payoff_blue_sc = 30 payoff_red_a = 20 payoff_red_b = 100 payoff_green_a = 10 payoff_green_b = 40 payoff_green_sc = 100 elif treatment == 2: payoffs_a_1 = [20, 40, 70] payoffs_b_1 = [60, 30, 50] colors_a_1 = ['red', 'blue', 'orange'] colors_b_1 = ['orange', 'red', 'blue'] exp_a = payoffs_a_1 exp_b = [30, 50, 60] exp_c = colors_a_1 payoffs_a_2 = [20, 150, 30, 10] payoffs_b_2 = [100, 50, 30, 40] colors_a_2 = ['red', 'blue', 'orange', 'green'] colors_b_2 = ['blue', 'orange', 'green', 'red'] exp_a_2 = payoffs_a_2 exp_b_2 = [40, 100, 50, 30] exp_c_2 = colors_a_2 payoff_blue_a = 150 payoff_blue_b = 100 payoff_blue_sc = 50 payoff_red_a = 20 payoff_red_b = 40 payoff_green_a = 10 payoff_green_b = 30 payoff_green_sc = 40 else: payoffs_a_1 = [20, 40, 70] payoffs_b_1 = [30, 50, 60] colors_a_1 = ['red', 'blue', 'orange'] colors_b_1 = ['blue', 'orange', 'red'] exp_a = payoffs_a_1 exp_b = [60, 30, 50] exp_c = colors_a_1 payoffs_a_2 = [20, 150, 30, 10] payoffs_b_2 = [40, 100, 50, 30] colors_a_2 = ['red', 'blue', 'orange', 'green'] colors_b_2 = ['green', 'red', 'blue', 'orange'] exp_a_2 = payoffs_a_2 exp_b_2 = [100, 50, 30, 40] exp_c_2 = colors_a_2 payoff_blue_a = 150 payoff_blue_b = 50 payoff_blue_sc = 100 payoff_red_a = 20 payoff_red_b = 100 payoff_green_a = 10 payoff_green_b = 40 payoff_green_sc = 30 col_1 = Constants.translate_colors([colors_a_1, colors_b_1]) col_2 = Constants.translate_colors([colors_a_2, colors_b_2]) exp_cols = Constants.translate_colors([exp_c, exp_c_2]) colors_a_1_c, colors_b_1_c = col_1[0], col_1[1] colors_a_2_c, colors_b_2_c = col_2[0], col_2[1] exp_cc, exp_cc_2 = exp_cols[0], exp_cols[1] error = 0 if self.participant.vars['test_tries'] > 0: error = 1 return dict( treatment=treatment, row_a_1=zip(payoffs_a_1, colors_a_1, colors_a_1_c), row_b_1=zip(payoffs_b_1, colors_b_1, colors_b_1_c), explain=zip(exp_c, exp_cc, exp_a, exp_b), explain2=zip(exp_c_2, exp_cc_2, exp_a_2, exp_b_2), explain3=zip(exp_c_2, exp_cc_2, exp_a_2, exp_b_2), row_a_2=zip(payoffs_a_2, colors_a_2, colors_a_2_c), row_b_2=zip(payoffs_b_2, colors_b_2, colors_b_2_c), payoff_blue_a=payoff_blue_a, payoff_blue_b=payoff_blue_b, payoff_blue_sc=payoff_blue_sc, payoff_red_a=payoff_red_a, payoff_red_b=payoff_red_b, payoff_green_a=payoff_green_a, payoff_green_b=payoff_green_b, payoff_green_sc=payoff_green_sc, error=error, test_1=self.participant.vars['test_1'], test_2=self.participant.vars['test_2'], test_3=self.participant.vars['test_3'], ) class passed(Page): def is_displayed(self): return self.round_number == 1 class Feedback(Page): def is_displayed(self): return self.participant.vars['features'][self.round_number][0] == 'Feedback' def vars_for_template(self): game = self.participant.vars['games'][self.round_number] A_skewed = self.participant.vars['choice_dict'][self.round_number]['A_skewed'] pos = self.participant.vars['choice_dict'][self.round_number]['pos'] pp, ppp, oo_1, oo_2 = list(), list(), list(), list() sum_fields = 0 for i in range(len(game)): sum_fields = sum_fields + game[i][0] for i in range(len(game)): pp.append(round((game[i][0]/sum_fields)*100, 1)) ppp.append(game[i][0]) oo_1.append(game[i][pos[0]]) oo_2.append(game[i][pos[1]]) fields_from, fields_to = Constants.fields(ppp)[0], Constants.fields(ppp)[1] skewed = self.participant.vars['choice_dict'][self.round_number]['skewed'] payoff_A, payoff_B = 0, 0 if skewed == 1: n_skewed = 0 else: n_skewed = 1 if A_skewed == skewed: chosen_option = 'A' checked_A = 'checked' checked_B = '' payoff_A = self.participant.vars['possible_payoffs_dict'][self.round_number][skewed] payoff_B = self.participant.vars['possible_payoffs_dict'][self.round_number][n_skewed] else: chosen_option = 'B' checked_B = 'checked' checked_A = '' payoff_B = self.participant.vars['possible_payoffs_dict'][self.round_number][skewed] payoff_A = self.participant.vars['possible_payoffs_dict'][self.round_number][n_skewed] #Put payoffs of lotteries in player vars self.player.payoff_skewed = self.participant.vars['possible_payoffs_dict'][self.round_number][1] self.player.payoff_nonskewed = self.participant.vars['possible_payoffs_dict'][self.round_number][0] return dict( pp=pp, oo_1=zip(oo_1, fields_from, fields_to), oo_2=zip(oo_2, fields_from, fields_to), treatment=self.participant.vars['treatment'], fields=zip(fields_from, fields_to, pp), fields_from=fields_from, fields_to=fields_to, payoff=self.participant.vars['possible_payoffs_dict'][self.round_number][self.participant.vars['choice_dict'][self.round_number]['skewed']], skewed=skewed, chosen_option=chosen_option, realized_field=self.participant.vars['realized_field_dict'][self.round_number], checked_A=checked_A, checked_B=checked_B, payoff_A=payoff_A, payoff_B=payoff_B, ) class help(Page): def vars_for_template(self): games = self.participant.vars['games'] ABC = self.participant.vars['ABC'] h2 = self.participant.vars['h2'] a2 = self.participant.vars['a2'] features = self.participant.vars['features'] return dict( games=games, ABC=ABC, h2=h2, a2=a2, features=features ) class MyPage(Page): form_model = 'player' form_fields = ['skewed'] def vars_for_template(self): this_round = self.participant.vars['l_dict'][self.round_number] # for template payoffs_a = this_round["payoffs"][0] payoffs_b = this_round["payoffs"][1] colors_a = this_round["colors"][0] colors_b = this_round["colors"][1] colors_a_c = this_round['colors_chinese'][0] colors_b_c = this_round['colors_chinese'][1] option_a = this_round['A_skewed'] option_b = 1 - option_a n_fields = len(colors_a) p_field = round((1/n_fields)*100, 2) treatment = this_round['treatment'] self.player.lottery_nr = this_round['lottery_nr'] self.player.type = this_round['type'] self.player.premium = this_round['premium'] self.player.treatment = str(treatment) self.player.payoffs = str(this_round['payoffs']) self.player.states = str(this_round['states']) self.player.colors = str(this_round['colors']) self.player.A_skewed = this_round['A_skewed'] #write vars into variables return dict( n_fields=n_fields, p_field=p_field, relevant=self.participant.vars['relevant'], row_a=zip(payoffs_a, colors_a, colors_a_c), row_b=zip(payoffs_b, colors_b, colors_b_c), option_a=option_a, option_b=option_b, width=60, selected=self.participant.vars['selected_for_payoff'], treatment=treatment, A_skewed=this_round['A_skewed'], type=this_round['type'], lottery_nr=this_round['lottery_nr'], premium=this_round['premium'], colors=this_round['colors'], ) def before_next_page(self): choice = 'A' if (self.player.skewed == 0 and self.player.A_skewed == 1) or (self.player.skewed == 1 and self.player.A_skewed == 0): choice = 'B' self.participant.vars['l_dict'][self.round_number]['choice'] = choice if self.participant.vars["relevant"] == self.round_number: if choice == 'A': payoff = self.participant.vars['l_dict'][self.round_number]['r_payoff_a'] else: payoff = self.participant.vars['l_dict'][self.round_number]['r_payoff_b'] self.participant.vars['payoff'] = payoff self.participant.vars['final_payoff'] = round(self.participant.vars['selected_for_payoff'] * payoff * 0.4 + 9, 1) self.player.payoff = self.participant.vars['final_payoff']*100 # self.participant.vars['choice_dict'][self.round_number]['skewed'] = self.player.skewed # ff = self.participant.vars['features'][self.round_number] # self.player.part = self.participant.vars['features'][self.round_number][0] # self.player.lottery_nr = self.participant.vars['features'][self.round_number][1] # self.player.correlation = self.participant.vars['features'][self.round_number][2] # self.player.z = self.participant.vars['features'][self.round_number][3] # self.player.A_skewed = self.participant.vars['choice_dict'][self.round_number]['A_skewed'] # # if self.round_number == Constants.num_rounds: # possible_payoff = self.participant.vars['possible_payoff'] # payoff_relevant = self.participant.vars['payoff_relevant'] # skewed = self.participant.vars['choice_dict'][payoff_relevant]['skewed'] # payoff = possible_payoff[skewed] # # in participant.vars, I save the payoff a player obtained from the chosen option. In player.payoff, I save # # the final payoff # self.player.payoff = (self.participant.vars['selected_for_payoff'] * payoff * 0.4 + 9)*100 # self.participant.vars['payoff'] = payoff # self.participant.vars['final_payoff'] = round(self.participant.vars['selected_for_payoff'] * payoff * 0.4 + 9, 1) # self.player.choice_dict = str(self.participant.vars['choice_dict']) class FeedbackStart(Page): def is_displayed(self): return self.round_number == Constants.num_rounds_nf + 1 class Survey(Page): def is_displayed(self): return self.round_number == Constants.num_rounds form_model = 'player' form_fields = ['Columns', 'Consider_colors', 'Rows', 'EV', 'open_end'] def vars_for_template(self): treatment = self.participant.vars['l_dict'][1]['treatment'] if treatment == 1: payoffs_a = [20, 40, 70] payoffs_b = [60, 30, 50] colors_a = ['red', 'blue', 'orange'] colors_b = ['red', 'blue', 'orange'] hl_a = 20 hl_b = 60 elif treatment == 2: payoffs_a = [20, 40, 70] payoffs_b = [60, 30, 50] colors_a = ['red', 'blue', 'orange'] colors_b = ['orange', 'red', 'blue'] hl_a = 20 hl_b = 60 else: payoffs_a = [20, 40, 70] payoffs_b = [30, 50, 60] colors_a = ['red', 'blue', 'orange'] colors_b = ['blue', 'orange', 'red'] hl_a = 20 hl_b = 30 col_1 = Constants.translate_colors([colors_a, colors_b]) colors_a_c, colors_b_c = col_1[0], col_1[1] # pp = (35, 25, 40) # ff = (21, 15, 24) # oo_1 = (20, 40, 70) # oo_2 = (30, 70, 50) # fields_from, fields_to = Constants.fields(ff)[0], Constants.fields(ff)[1] # # pp_2 = (15, 80, 5) # ff_2 = (9, 48, 3) # oo_1_2 = (20, 30, 150) # oo_2_2 = (70, 40, 50) # fields_from_2, fields_to_2 = Constants.fields(ff_2)[0], Constants.fields(ff_2)[1] return dict( row_a=zip(payoffs_a, colors_a, colors_a_c), row_b=zip(payoffs_b, colors_b, colors_b_c), hl_a=hl_a, hl_b=hl_b, # pp=pp, # oo_1=oo_1, # oo_2=oo_2, # fields=zip(fields_from, fields_to, pp), # explain=zip(fields_from, fields_to, pp, oo_1, oo_2), # pp_2=pp_2, # oo_1_2=oo_1_2, # oo_2_2=oo_2_2, # fields_2=zip(fields_from_2, fields_to_2, pp_2), # test_1=self.participant.vars['test_1'], # test_2=self.participant.vars['test_2'], # test_3=self.participant.vars['test_3'], ) class Figures(Page): pass page_sequence = [PhoneNumber, Introduction, passed, MyPage, Survey] # page_sequence = [Introduction, MyPage]