from otree.api import * doc = """ This is sequential "Prisoner's Dilemma". P2 knows P1's decision before making their own. """ class Constants(BaseConstants): name_in_url = 'sequential_prisoner' players_per_group = 2 num_rounds = 1 instructions_template = 'sequential_prisoner/instructions.html' # payoff if 1 player denys and the other Denys""", betray_payoff = cu(5) betrayed_payoff = cu(0) # payoff if both players Deny or both deny both_deny_payoff = cu(3) both_confess_payoff = cu(1) decision_alias = { 'Deny': 'Deny', 'Confess': 'Confess' } class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): decision = models.StringField( choices=[['Deny', 'Deny'], ['Confess', 'Confess']], doc="""This player's decision""", widget=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 = dict( Deny=dict( Deny=Constants.both_deny_payoff, Confess=Constants.betrayed_payoff ), Confess=dict( Deny=Constants.betray_payoff, Confess=Constants.both_confess_payoff ), ) player.payoff = payoff_matrix[player.decision][other_player(player).decision] # PAGES class Introduction(Page): # timeout_seconds = 100 pass class DecisionWaitPage(WaitPage): pass # @staticmethod # def is_displayed(player: Player): # return player.id_in_group == 2 class DecisionA(Page): form_model = 'player' form_fields = ['decision'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 class DecisionB(Page): form_model = 'player' form_fields = ['decision'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 2 @staticmethod def vars_for_template(player: Player): return { 'decision_A': Constants.decision_alias[player.group.get_player_by_id(1).decision] } class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): @staticmethod def vars_for_template(player: Player): me = player opponent = other_player(me) return dict( my_decision=Constants.decision_alias[me.decision], opponent_decision=Constants.decision_alias[opponent.decision], same_choice=me.decision == opponent.decision ) page_sequence = [Introduction, DecisionA, DecisionWaitPage, DecisionB, ResultsWaitPage, Results]