from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) doc = '\nThis is a one-shot "Prisoner\'s Dilemma". Two players are asked separately\nwhether they want to cooperate or defect. Their choices directly determine the\npayoffs.\n' class Constants(BaseConstants): name_in_url = 'prisoner' players_per_group = 2 num_rounds = 7 betray_payoff = c(8) betrayed_payoff = c(-2) both_cooperate_payoff = c(4) both_defect_payoff = c(0) instructions_template = 'prisoner/instructions.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): skipapp = models.BooleanField(initial=False) def set_payoffs(self): for p in self.get_players(): p.set_payoff() def help_calculation(self): p2 = self.get_player_by_id(2) import random chance = random.randint(1,100) if p2.help == True and chance <= 90: self.skipapp = False elif p2.help == False and chance <= 10: self.skipapp = False else: self.skipapp = True def endowment(self): for player in self.get_players(): player.payoff = player.payoff + 10 class Player(BasePlayer): decision = models.StringField(choices=[['Cooperate', 'Cooperate'], ['Defect', 'Defect']], doc='This player s decision', widget=widgets.RadioSelect) help = models.BooleanField(choices=[[True, 'Yes'], [False, 'No']], label='', widget=widgets.RadioSelect) ManipulationCheck1 = models.StringField(blank=True, choices=[['Strongly Agree', 'Strongly Agree'], ['Agree', 'Agree'], ['Somewhat Agree', 'Somewhat Agree'], ['Neutral', 'Neutral'], ['Somewhat Disagree', 'Somewhat Disagree'], ['Disagree', 'Disagree'], ['Strongly Disagree', 'Strongly Disagree']], label='What was good for my partner was good for me:', widget=widgets.RadioSelectHorizontal) ManipulationCheck2 = models.StringField(blank=True, choices=[['Strongly Agree', 'Strongly Agree'], ['Agree', 'Agree'], ['Somewhat Agree', 'Somewhat Agree'], ['Neutral', 'Neutral'], ['Somewhat Disagree', 'Somewhat Disagree'], ['Disagree', 'Disagree'], ['Strongly Disagree', 'Strongly Disagree']], label='What was good for my partner was bad for me:', widget=widgets.RadioSelectHorizontal) Sex = models.StringField(blank=True, choices=[['Female', 'Female'], ['Male', 'Male'], ['Other', 'Other']], label='Sex:', widget=widgets.RadioSelectHorizontal) Age = models.IntegerField(blank=True, label='Age:', max=100, min=0) ManipulationCheck3 = models.StringField(blank=True, choices=[['Strongly Agree', 'Strongly Agree'], ['Agree', 'Agree'], ['Somewhat Agree', 'Somewhat Agree'], ['Neutral', 'Neutral'], ['Somewhat Disagree', 'Somewhat Disagree'], ['Disagree', 'Disagree'], ['Strongly Disagree', 'Strongly Disagree']], label='I depend on my partner', widget=widgets.RadioSelectHorizontal) ManipulationCheck4 = models.StringField(blank=True, choices=[['Strongly Agree', 'Strongly Agree'], ['Agree', 'Agree'], ['Somewhat Agree', 'Somewhat Agree'], ['Neutral', 'Neutral'], ['Somewhat Disagree', 'Somewhat Disagree'], ['Disagree', 'Disagree'], ['Strongly Disagree', 'Strongly Disagree']], label='I benefit from having a partner ', widget=widgets.RadioSelectHorizontal) Comprehension = models.StringField(choices=[['Cooperate', 'Cooperate'], ['Defect', 'Defect']], label='In an individual round of the game which option has the potential for the highest payoff?') def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): payoff_matrix = dict( Cooperate=dict( Cooperate=Constants.both_cooperate_payoff, Defect=Constants.betrayed_payoff, ), Defect=dict( Cooperate=Constants.betray_payoff, Defect=Constants.both_defect_payoff ), ) self.payoff = payoff_matrix[self.decision][self.other_player().decision] def help_player2(self): if self.id_in_group == 2 and help == True: self.participant.payoff = self.participant.payoff - 5 def help_player1(self): if self.id_in_group == 1 and help == True: self.participant.payoff = self.participant.payoff + 10