from otree.api import * import random doc = """ This is iterated "Prisoner's Dilemma". """ random.seed() class Constants(BaseConstants): name_in_url = 'iterated_prisoner' players_per_group = 2 num_rounds = random.randint(3,10) instructions_template = 'iterated_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 @staticmethod def is_displayed(player: Player): return player.subsession.round_number == 1 class Decision(Page): form_model = 'player' form_fields = ['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) total_score = sum([player.payoff for player in player.in_all_rounds()]) return dict( my_decision=Constants.decision_alias[me.decision], opponent_decision=Constants.decision_alias[opponent.decision], same_choice=me.decision == opponent.decision, total_score=total_score, ) page_sequence = [Introduction, Decision, ResultsWaitPage, Results]