from otree.api import * doc = """ 这是第四轮的过垫脚石桥的游戏实验。 """ class C(BaseConstants): NAME_IN_URL = 'round4' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 1 INTRODUCTION_TEMPLATE = 'round4/Introduction.html' PAYOFF_A = cu(20) PAYOFF_B = cu(10) PAYOFF_C = cu(5) PAYOFF_D = cu(0) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): first_cross = models.BooleanField( label='先过桥', choices=[[True, '先过桥'], [False, '后过桥']], doc="""This player's decision""", widgets=widgets.RadioSelect, ) # FUNCTIONS def set_payoffs(group: Group): for p in group.get_players(): set_payoff(p) def other_player(player: Player): return player.get_others_in_group()[0] def set_payoff(player: Player): payoff_matrix = { (True, False): C.PAYOFF_A, (True, True): C.PAYOFF_B, (False, False): C.PAYOFF_C, (False, True): C.PAYOFF_D, } other = other_player(player) player.payoff = payoff_matrix[(player.first_cross, other.first_cross)] # PAGES class MyWaitPage(WaitPage): group_by_arrival_time = True class Instruction(Page): timeout_seconds = 100 class GamePlay(Page): form_model = 'player' form_fields = ['first_cross'] timeout_seconds = 30 class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): @staticmethod def vars_for_template(player: Player): opponent = other_player(player) return dict( opponent=opponent, same_choice=player.first_cross == opponent.first_cross, my_decision=player.field_display('first_cross'), opponent_decision=opponent.field_display('first_cross'), ) page_sequence = [MyWaitPage, Instruction, GamePlay, ResultsWaitPage, Results]