from otree.api import * import random author = 'Mike ZhiRen Wu' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'Emotion_CWTP' participation_fee = cu(10) players_per_group = 3 num_rounds = 1 table1_answer = 39 table2_answer = 29 table3_answer = 26 table4_answer = 30 table5_answer = 32 table6_answer = 43 table7_answer = 26 table8_answer = 37 table9_answer = 32 table10_answer = 40 table11_answer = 47 table12_answer = 33 table13_answer = 31 table14_answer = 39 table15_answer = 44 lottery_draw = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] endowment = 50 class Subsession(BaseSubsession): pass class Group(BaseGroup): choice_A = models.IntegerField( min=0, max=Constants.endowment, label="How much does A want to take?" ) choice_B = models.IntegerField(choices=[[1, '同意方案'], [2, '不同意方案']],) CWTP_message = models.LongStringField(blank=True) class Player(BasePlayer): table1_entered = models.IntegerField(min=0, max=75) table2_entered = models.IntegerField(min=0, max=75) table3_entered = models.IntegerField(min=0, max=75) table4_entered = models.IntegerField(min=0, max=75) table5_entered = models.IntegerField(min=0, max=75) table6_entered = models.IntegerField(min=0, max=75) table7_entered = models.IntegerField(min=0, max=75) table8_entered = models.IntegerField(min=0, max=75) table9_entered = models.IntegerField(min=0, max=75) table10_entered = models.IntegerField(min=0, max=75) table11_entered = models.IntegerField(min=0, max=75) table12_entered = models.IntegerField(min=0, max=75) table13_entered = models.IntegerField(min=0, max=75) table14_entered = models.IntegerField(min=0, max=75) table15_entered = models.IntegerField(min=0, max=75) switching_point = models.IntegerField() gender = models.StringField(choices=['男', '女']) age = models.IntegerField() major = models.StringField() comment = models.LongStringField() lottery_choice = models.IntegerField() within_option_choice = models.IntegerField() option_choice = models.IntegerField() lottery_payment = models.FloatField() lottery_payment_RMB = models.FloatField() # PAGES class Welcome(Page): pass class RoleAssign(Page): pass class Wait(WaitPage): pass class Task1Instruction(Page): pass class Task1_count1(Page): form_model = 'player' form_fields = ['table1_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table1_entered'] != Constants.table1_answer: return '数量有误,请仔细检查并更正!' class Task1_count2(Page): form_model = 'player' form_fields = ['table2_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table2_entered'] != Constants.table2_answer: return '数量有误,请仔细检查并更正!' class Task1_count3(Page): form_model = 'player' form_fields = ['table3_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table3_entered'] != Constants.table3_answer: return '数量有误,请仔细检查并更正!' class Task1_count4(Page): form_model = 'player' form_fields = ['table4_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table4_entered'] != Constants.table4_answer: return '数量有误,请仔细检查并更正!' class Task1_count5(Page): form_model = 'player' form_fields = ['table5_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table5_entered'] != Constants.table5_answer: return '数量有误,请仔细检查并更正!' class Task1_count6(Page): form_model = 'player' form_fields = ['table6_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table6_entered'] != Constants.table6_answer: return '数量有误,请仔细检查并更正!' class Task1_count7(Page): form_model = 'player' form_fields = ['table7_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table7_entered'] != Constants.table7_answer: return '数量有误,请仔细检查并更正!' class Task1_count8(Page): form_model = 'player' form_fields = ['table8_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table8_entered'] != Constants.table8_answer: return '数量有误,请仔细检查并更正!' class Task1_count9(Page): form_model = 'player' form_fields = ['table9_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table9_entered'] != Constants.table9_answer: return '数量有误,请仔细检查并更正!' class Task1_count10(Page): form_model = 'player' form_fields = ['table10_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table10_entered'] != Constants.table10_answer: return '数量有误,请仔细检查并更正!' class WaitForAll(WaitPage): pass class Task2_Instruction(Page): pass class WaitForAll2(WaitPage): pass class Task2_choice_A(Page): form_model = 'group' form_fields = ['choice_A']\ @staticmethod def is_displayed(player): return player.id_in_group == 1 @staticmethod def js_vars(player: Player): return dict(endowment=Constants.endowment) class WaitForA(WaitPage): pass class Task2_choice_B(Page): form_model = 'group' form_fields = ['choice_B', 'CWTP_message'] \ @staticmethod def is_displayed(player): return player.id_in_group == 2 @staticmethod def vars_for_template(player): group = player.group return dict(remain_A=Constants.endowment+group.choice_A, remain_B=Constants.endowment-group.choice_A) class WaitForB(WaitPage): pass class Task2_MessageFeedBack(Page): @staticmethod def is_displayed(player): return player.id_in_group == 2 or player.id_in_group == 3 class WaitForResult(WaitPage): pass class Task2_result(Page): @staticmethod def vars_for_template(player): group = player.group if group.choice_B == 1: return dict(task2pay_A=Constants.endowment+group.choice_A, task2pay_B=Constants.endowment-group.choice_A, remain_B=Constants.endowment-group.choice_A) else: return dict(task2pay_A=Constants.endowment, task2pay_B=0, remain_B=Constants.endowment-group.choice_A) class Questionnaire(Page): form_model = 'player' form_fields = ['switching_point', 'gender', 'age', 'major', 'comment'] @staticmethod def vars_for_template(player): situation = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] left = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] right = [90, 80, 70, 60, 50, 40, 30, 20, 10, 0] return dict(a=zip(situation, left, right)) class ResultsWaitPage(WaitPage): @staticmethod def after_all_players_arrive(group: Group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p3 = group.get_player_by_id(3) random.seed() p1.lottery_choice = random.choice(Constants.lottery_draw) p2.lottery_choice = random.choice(Constants.lottery_draw) p3.lottery_choice = random.choice(Constants.lottery_draw) random.seed() p1.within_option_choice = random.choice(Constants.lottery_draw) p2.within_option_choice = random.choice(Constants.lottery_draw) p3.within_option_choice = random.choice(Constants.lottery_draw) if p1.lottery_choice >= p1.switching_point: p1.option_choice = 2 else: p1.option_choice = 1 # The option you pick in the situation drawn: 1=option A,2=option B if p2.lottery_choice >= p2.switching_point: p2.option_choice = 2 else: p2.option_choice = 1 # The option you pick in the situation drawn: 1=option A,2=option B if p3.lottery_choice >= p3.switching_point: p3.option_choice = 2 else: p3.option_choice = 1 # The option you pick in the situation drawn: 1=option A,2=option B if p1.option_choice == 2 and (p1.within_option_choice <= p1.lottery_choice): p1.lottery_payment = 9.625 p1.lottery_payment_RMB = 7.7 elif p1.option_choice == 2 and (p1.within_option_choice > p1.lottery_choice): p1.lottery_payment = 0.25 p1.lottery_payment_RMB = 0.2 elif p1.option_choice == 1 and (p1.within_option_choice <= p1.lottery_choice): p1.lottery_payment = 5 p1.lottery_payment_RMB = 4 else: p1.lottery_payment = 4 # Calculation of actual lottery payment p1.lottery_payment_RMB = 3.2 if p2.option_choice == 2 and (p2.within_option_choice <= p2.lottery_choice): p2.lottery_payment = 9.625 p2.lottery_payment_RMB = 7.7 elif p2.option_choice == 2 and (p2.within_option_choice > p2.lottery_choice): p2.lottery_payment = 0.25 p2.lottery_payment_RMB = 0.2 elif p2.option_choice == 1 and (p2.within_option_choice <= p2.lottery_choice): p2.lottery_payment = 5 p2.lottery_payment_RMB = 4 else: p2.lottery_payment = 4 # Calculation of actual lottery payment p2.lottery_payment_RMB = 3.2 if p3.option_choice == 2 and (p3.within_option_choice <= p3.lottery_choice): p3.lottery_payment = 9.625 p3.lottery_payment_RMB = 7.7 elif p3.option_choice == 2 and (p3.within_option_choice > p3.lottery_choice): p3.lottery_payment = 0.25 p3.lottery_payment_RMB = 0.2 elif p3.option_choice == 1 and (p3.within_option_choice <= p3.lottery_choice): p3.lottery_payment = 5 p3.lottery_payment_RMB = 4 else: p3.lottery_payment = 4 # Calculation of actual lottery payment p3.lottery_payment_RMB = 3.2 if group.choice_B == 1: p1.payoff = Constants.participation_fee + 0.8*(Constants.endowment+group.choice_A + p1.lottery_payment) p2.payoff = Constants.participation_fee + 0.8*(Constants.endowment-group.choice_A + p2.lottery_payment) p3.payoff = Constants.participation_fee + 0.8*(Constants.endowment+p3.lottery_payment) else: p1.payoff = Constants.participation_fee + 0.8*(Constants.endowment + p1.lottery_payment) p2.payoff = Constants.participation_fee + 0.8*p2.lottery_payment p3.payoff = Constants.participation_fee + 0.8*(Constants.endowment+p3.lottery_payment) class Results(Page): @staticmethod def vars_for_template(player): group = player.group if group.choice_B == 1: return dict(task2pay_A=Constants.endowment+group.choice_A, task2pay_B=Constants.endowment-group.choice_A, task2pay_A_RMB=round(0.8*(Constants.endowment+group.choice_A), 2), task2pay_B_RMB=round(0.8*(Constants.endowment-group.choice_A), 2)) else: return dict(task2pay_A=Constants.endowment, task2pay_B=0, task2pay_A_RMB=0.8*Constants.endowment, task2pay_B_RMB=0) page_sequence = [ Welcome, RoleAssign, Wait, Task1Instruction, Task1_count1, Task1_count2, Task1_count3, Task1_count4, Task1_count5, Task1_count6, Task1_count7, Task1_count8, Task1_count9, Task1_count10, WaitForAll, Task2_Instruction, WaitForAll2, Task2_choice_A, WaitForA, Task2_choice_B, WaitForB, Task2_MessageFeedBack, WaitForResult, Task2_result, Questionnaire, ResultsWaitPage, Results, ]