from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ your app description """ class Constants(BaseConstants): name_in_url = 'my_RPD_2' players_per_group = 2 num_rounds = 20 stopping_probability = 0.25 instructions_template = 'my_RPD_2/Instructions.html' # payoff if 1 player defects and the other cooperates""", betray_payoff = c(50) betrayed_payoff = c(12) # payoff if both players cooperate or both defect both_cooperate_payoff = c(48) both_defect_payoff = c(25) ''' the second version of the game: both_cooperate_payoff = c(32) both_defect_payoff = c(25) ''' ''' the second version of the game: both_cooperate_payoff = c(48) both_defect_payoff = c(30) ''' #class Subsession(BaseSubsession): # pass class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: self.group_randomly() else: self.group_like_round(1) class Group(BaseGroup): is_last_round = models.BooleanField(initial=False) class Player(BasePlayer): decision = models.StringField( choices=['Cooperate', 'Defect'], doc="""This player's decision""", widget=widgets.RadioSelect ) strategy_0 = models.IntegerField(widget=widgets.RadioSelectHorizontal, choices=[[1,'Cooperate'], [2,'Defect']]) strategy_1 = models.IntegerField(widget=widgets.RadioSelectHorizontal, choices=[[1,'Cooperate'], [2,'Defect']]) strategy_2 = models.IntegerField(widget=widgets.RadioSelectHorizontal, choices=[[1,'Cooperate'], [2,'Defect']]) strategy_3 = models.IntegerField(widget=widgets.RadioSelectHorizontal, choices=[[1,'Cooperate'], [2,'Defect']]) strategy_4 = models.IntegerField(widget=widgets.RadioSelectHorizontal, choices=[[1,'Cooperate'], [2,'Defect']]) roundstrategy = models.StringField(widget=widgets.RadioSelect, choices=['Cooperate', 'Defect']) def player_strategy(self): self.participant.vars['strategy0'] = self.strategy_0 self.participant.vars['strategy1'] = self.strategy_1 self.participant.vars['strategy2'] = self.strategy_2 self.participant.vars['strategy3'] = self.strategy_3 self.participant.vars['strategy4'] = self.strategy_4 self.participant.vars['roundstrategy'] = self.roundstrategy def is_displayed(self): pass def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): payoff_matrix = { 'Cooperate': { 'Cooperate': Constants.both_cooperate_payoff, 'Defect': Constants.betrayed_payoff }, 'Defect': { 'Cooperate': Constants.betray_payoff, 'Defect': Constants.both_defect_payoff } } self.payoff = payoff_matrix[self.decision][self.other_player().decision] def set_roundstrategy(self): if self.round_number == 1: self.roundstrategy = self.participant.vars['strategy0'] elif self.in_round(self.round_number - 1).decision == "Cooperate" and self.in_round( self.round_number - 1).other_player().decision == "Cooperate": self.roundstrategy = self.participant.vars['strategy1'] elif self.in_round(self.round_number - 1).decision == "Cooperate" and self.in_round( self.round_number - 1).other_player().decision == "Defect": self.roundstrategy = self.participant.vars['strategy2'] elif self.in_round(self.round_number - 1).decision == "Defect" and self.in_round( self.round_number - 1).other_player().decision == "Cooperate": self.roundstrategy = self.participant.vars['strategy3'] elif self.in_round(self.round_number - 1).decision == "Defect" and self.in_round( self.round_number - 1).other_player().decision == "Defect": self.roundstrategy = self.participant.vars['strategy4']