from otree.api import * import random author = 'Mike ZhiRen Wu' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'Emotion_NC' participation_fee = cu(10) players_per_group = 2 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] class Subsession(BaseSubsession): pass class Group(BaseGroup): offer_A = models.IntegerField(min=0, max=100) offer_B = models.IntegerField(min=0, max=100) choice_B1 = models.IntegerField(choices=[[1, '接受方案'], [2, '接受方案并奖励A实验点5点'], [3, '不接受方案']], blank=True, initial=0) choice_B2 = models.IntegerField(choices=[[4, '接受方案'], [5, '不接受方案']], blank=True, initial=0) 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() # PAGES class Welcome(Page): 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 Task1_count11(Page): form_model = 'player' form_fields = ['table11_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table11_entered'] != Constants.table11_answer: return '数量有误,请仔细检查并更正!' class Task1_count12(Page): form_model = 'player' form_fields = ['table12_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table12_entered'] != Constants.table12_answer: return '数量有误,请仔细检查并更正!' class Task1_count13(Page): form_model = 'player' form_fields = ['table13_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table13_entered'] != Constants.table13_answer: return '数量有误,请仔细检查并更正!' class Task1_count14(Page): form_model = 'player' form_fields = ['table14_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table14_entered'] != Constants.table14_answer: return '数量有误,请仔细检查并更正!' class Task1_count15(Page): form_model = 'player' form_fields = ['table15_entered']\ @staticmethod def error_message(player, values): print('values is', values) if values['table15_entered'] != Constants.table15_answer: return '数量有误,请仔细检查并更正!' class WaitForAll(WaitPage): pass class Task2_Instruction(Page): pass class Task2_choice_A(Page): form_model = 'group' form_fields = ['offer_A', 'offer_B']\ @staticmethod def is_displayed(player): return player.id_in_group == 1 @staticmethod def error_message(player, values): print('values is', values) if values['offer_A'] + values['offer_B'] != 100: return '分配的实验点数总和必须为100点' class WaitForA(WaitPage): pass class Task2_choice_B(Page): form_model = 'group' form_fields = ['choice_B1', 'choice_B2'] \ @staticmethod def is_displayed(player): return player.id_in_group == 2 class WaitForB(WaitPage): pass class Task2_result(Page): @staticmethod def vars_for_template(player): group = player.group if group.choice_B1 == 1 or group.choice_B2 == 4: return dict(task2pay_A=group.offer_A, task2pay_B=group.offer_B) elif group.choice_B1 == 2: return dict(task2pay_A=group.offer_A+5, task2pay_B=group.offer_B-5) else: return dict(task2pay_A=0, task2pay_B=0) 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) random.seed() p1.lottery_choice = random.choice(Constants.lottery_draw) p2.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) 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 p1.option_choice == 2 and (p1.within_option_choice <= p1.lottery_choice): p1.lottery_payment = 19.25 elif p1.option_choice == 2 and (p1.within_option_choice > p1.lottery_choice): p1.lottery_payment = 0.5 elif p1.option_choice == 1 and (p1.within_option_choice <= p1.lottery_choice): p1.lottery_payment = 10 else: p1.lottery_payment = 8 # Calculation of actual lottery payment if p2.option_choice == 2 and (p2.within_option_choice <= p2.lottery_choice): p2.lottery_payment = 19.25 elif p2.option_choice == 2 and (p2.within_option_choice > p2.lottery_choice): p2.lottery_payment = 0.5 elif p2.option_choice == 1 and (p2.within_option_choice <= p2.lottery_choice): p2.lottery_payment = 10 else: p2.lottery_payment = 8 # Calculation of actual lottery payment if group.choice_B1 == 1 or group.choice_B2 == 4: p1.payoff = Constants.participation_fee + group.offer_A + p1.lottery_payment p2.payoff = Constants.participation_fee + group.offer_B + p2.lottery_payment elif group.choice_B1 == 2: p1.payoff = Constants.participation_fee + group.offer_A + 5 + p1.lottery_payment p2.payoff = Constants.participation_fee + group.offer_B - 5 + p2.lottery_payment else: p1.payoff = Constants.participation_fee + p1.lottery_payment p2.payoff = Constants.participation_fee + p2.lottery_payment class Results(Page): @staticmethod def vars_for_template(player): group = player.group if group.choice_B1 == 1 or group.choice_B2 == 4: return dict(task2pay_A=group.offer_A, task2pay_B=group.offer_B) elif group.choice_B1 == 2: return dict(task2pay_A=group.offer_A + 5, task2pay_B=group.offer_B - 5) else: return dict(task2pay_A=0, task2pay_B=0) page_sequence = [ Welcome, Task1Instruction, Task1_count1, # Task1_count2, # Task1_count3, # Task1_count4, # Task1_count5, # Task1_count6, # Task1_count7, # Task1_count8, # Task1_count9, # Task1_count10, # Task1_count11, # Task1_count12, # Task1_count13, # Task1_count14, # Task1_count15, WaitForAll, Task2_Instruction, Task2_choice_A, WaitForA, Task2_choice_B, WaitForB, Task2_result, Questionnaire, ResultsWaitPage, Results, ]