from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants from random import randint class ShuffleWaitPage(WaitPage): wait_for_all_groups = True class instruction(Page): def is_displayed(self): return self.subsession.round_number == 1 class control_1(Page): def is_displayed(self): return self.subsession.round_number == 1 form_model = 'player' form_fields = ['expert_q1', 'client_q1'] class control_2(Page): def is_displayed(self): return self.subsession.round_number == 1 form_model = 'player' form_fields = ['expert_q2', 'client_q2'] class control_3(Page): def is_displayed(self): return self.subsession.round_number == 1 form_model = 'player' form_fields = ['expert_q3_1', 'expert_q3_2', 'client_q3'] class control_4(Page): def is_displayed(self): return self.subsession.round_number == 1 form_model = 'player' form_fields = ['expert_q4', 'expert_q5', 'client_q4'] class get_problems(WaitPage): #wait_for_all_groups = True after_all_players_arrive = 'set_problems_and_signals' # def get_signal(self): # for p in self.subsession.get_players(): # if p.id_in_group == 2: # random_result = randint(1, 100) # if random_result <= 60: # p.private_signal = p.problem # else: # p.private_signal = abs(p.problem - 1) # def after_all_players_arrive(self): # for p in self.subsession.get_players(): # if p.id_in_group == 2: # num_problem = randint(1, 100) # if num_problem <= 50: # p.problem = 0 # else: # p.problem = 1 # player1 = self.group.get_player_by_role('player1') # player2 = self.group.get_player_by_role('player2') # player2.problem = player1.problem # # for p in self.subsession.get_players(): # if p.id_in_group == 2: # random_result = randint(1, 100) # if random_result <=60: # p.private_signal = p.problem # else: # p.private_signal = abs(p.problem - 1) class after_control_questions(Page): def is_displayed(self) -> bool: return self.subsession.round_number == 1 class WTP(Page): def is_displayed(self): return self.player.id_in_group == 2 def vars_for_template(self): return { 'round_number': self.round_number, 'private_signal_letter': self.player.private_signal_letter } # def get_disease(self): # for p in self.subsession.get_players(): # if p.id_in_group == 2: # num_problem = randint(1, 100) # if num_problem <= 50: # self.player.problem = 0 # else: # self.player.problem = 1 # # def get_signal(self): # for p in self.subsession.get_players(): # if p.id_in_group == 2: # random_result = randint(1, 100) # if random_result <= 60: # p.private_signal = p.problem # else: # p.private_signal = abs(p.problem - 1) form_model = "player" form_fields = [ 'active1', 'active2', # 'active3', # 'active4', # 'active5', # 'active6', # 'active7' ] def before_next_page(self): # Determine which row is chosen for WTP table for p in self.subsession.get_players(): if p.id_in_group == 2: random_num = randint(1, 2) if random_num ==1: p.activeness = p.active1 p.activeness_chosen = 1 if random_num ==2: p.activeness = p.active2 p.activeness_chosen = 2 # if random_num ==3: # p.activeness = p.active3 # p.activeness_chosen = 3 # if random_num ==4: # p.activeness = p.active4 # p.activeness_chosen = 4 # if random_num ==5: # p.activeness = p.active5 # p.activeness_chosen = 5 # if random_num ==6: # p.activeness = p.active6 # p.activeness_chosen = 6 # if random_num ==7: # p.activeness = p.active7 # p.activeness_chosen = 7 class AWaitPage(WaitPage): def after_all_players_arrive(self): player1 = self.group.get_player_by_role('player1') player2 = self.group.get_player_by_role('player2') player1.activeness = player2.activeness class precision_choice(Page): def is_displayed(self): return self.player.id_in_group == 1 form_model = "player" form_fields = ['precision_choice'] def vars_for_template(self): return { 'round_number': self.round_number, } def precision_choice_error_message(self, value): if value == None and self.player.id_in_group == 1: return 'Please make a choice' # def before_next_page(self): # #这里需要loop吗?对每个人都loop一次?? # for p in self.subsession.get_players(): # if p.id_in_group == 1: # num_problem = randint(1, 100) # if num_problem <= 50: # self.player.problem = 0 # else: # self.player.problem = 1 #import pdb; pdb.set_trace() class diagnosis_wait_page_pre_wait(WaitPage): wait_for_all_groups = True class diagnosis_wait_page(WaitPage): #wait_for_all_groups = True def after_all_players_arrive(self): for p in self.group.get_players(): if p.id_in_group == 1: p.diagnostic_cost = round(100*(p.precision_choice - 0.5) ** 2) p.diagnostic_result = p.problem random_result = randint(1, 100) if p.precision_choice == 0.5: if random_result <= 50: p.diagnostic_result = p.problem else: p.diagnostic_result = abs(p.problem - 1) if p.precision_choice == 0.6: if random_result <= 60: p.diagnostic_result = p.problem else: p.diagnostic_result = abs(p.problem - 1) if p.precision_choice == 0.7: if random_result <= 70: p.diagnostic_result = p.problem else: p.diagnostic_result = abs(p.problem - 1) if p.precision_choice == 0.8: if random_result <= 80: p.diagnostic_result = p.problem else: p.diagnostic_result = abs(p.problem - 1) if p.precision_choice == 0.9: if random_result <= 90: p.diagnostic_result = p.problem else: p.diagnostic_result = abs(p.problem - 1) if p.precision_choice == 1: p.diagnostic_result = p.problem player1 = self.group.get_player_by_role('player1') player2 = self.group.get_player_by_role('player2') #player2.problem = player1.problem player2.precision_choice = player1.precision_choice player2.diagnostic_result = player1.diagnostic_result player1.diagnosis_letter = "A"*(player1.diagnostic_result == 0) + "B"*(player1.diagnostic_result == 1) player2.diagnosis_letter = player1.diagnosis_letter player1.recommend_soln_letter = "A*"*(player1.diagnostic_result == 0) + "B*"*(player1.diagnostic_result == 1) player2.recommend_soln_letter = player1.recommend_soln_letter # for p in self.subsession.get_players(): # if p.id_in_group == 2: # random_result = randint(1, 100) # if random_result <= 60: # p.private_signal = p.problem # else: # p.private_signal = abs(p.problem-1) class diagnostic_result(Page): wait_for_all_groups = True def vars_for_template(self): return { 'diagnostic_result': self.player.diagnosis_letter, 'recommended_solution': self.player.recommend_soln_letter, 'private_signal': self.player.private_signal } class choose_treatment(Page): def is_displayed(self): return self.player.id_in_group == 2 def vars_for_template(self): return { 'precision': self.player.precision_choice, 'diagnostic_result': self.player.diagnosis_letter, 'private_signal': self.player.private_signal_letter, 'recommend_solution': self.player.recommend_soln_letter } form_model = "player" form_fields = ['belief_precision','treatment_choice'] def treatment_choice_error_message(self, value): if value == None and self.player.id_in_group == 2: return 'Please make a choice' def belief_precision_error_message(self, value): if value == None and self.player.id_in_group == 2: return 'Please make a choice' class payoff_wait_page(WaitPage): after_all_players_arrive = 'set_payoffs' class payoff(Page): # def after_all_players_arive(self): # for p in self.subsession.get_players(): # if p.id_in_group == 2: # if p.activeness == 1: # p.treatment_final = p.treatment_choice # if p.treatment_final == 0: # p.treatment_final_letter = "A*" # else: # p.treatment_final_letter = "B*" # else: # p.treatment_final == p.diagnostic_result # p.treatment_final_letter = p.recommend_soln_letter # player1 = self.group.get_player_by_role('player1') # player2 = self.group.get_player_by_role('player2') # player2.treatment_final = player1.treatment_final # player2.treatment_final_letter = player1.treatment_final_letter #after_all_players_arrive = ' set_final_treatments' def vars_for_template(self): return { 'round_number': self.round_number, 'payment_expert': self.player.payment, 'payment_patient': self.player.payment_temp, 'recommended_solution': self.player.recommend_soln_letter, 'actual_solution':self.player.treatment_final_letter, 'problem':self.player.problem_letter, 'cost':self.player.diagnostic_cost, 'randomization_result':self.player.active_result_display, 'randomization_cost': self.player.active_cost_display } class FinalPaymentWaitPage(WaitPage): wait_for_all_groups = True def is_displayed(self): return self.round_number == Constants.num_rounds def after_all_players_arrive(self): for p in self.subsession.get_players(): # for pr in p.in_all_rounds(): # p.final_payment = p.final_payment + pr.payment # # p.final_payment = Constants.initial_earning + (p.final_payment*Constants.exchange_rate) # p.participant.vars['final_payment'] = p.final_payment # p.payoff = p.final_payment p.final_payment = Constants.initial_earning + p.in_round(Constants.round_selected).payment * Constants.exchange_rate p.participant.vars['final_payment'] = p.final_payment p.participant.vars["round_selected"] = Constants.round_selected page_sequence = [ShuffleWaitPage, instruction, control_1, control_2, control_3, control_4, after_control_questions, get_problems, WTP, AWaitPage, precision_choice, diagnosis_wait_page_pre_wait, diagnosis_wait_page, diagnostic_result, choose_treatment, payoff_wait_page, payoff, FinalPaymentWaitPage]