from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) doc = """ Discount Choice Game """ class Constants(BaseConstants): name_in_url = 'Andalib_switching_cost' players_per_group = 2 num_rounds = 10 P1_stag_hare_payoff = 20 P1_hare_stag_payoff = 60 P1_both_hare_payoff = 45 P1_both_stag_payoff = 30 P2_stag_hare_payoff = 60 P2_hare_stag_payoff = 20 P2_both_hare_payoff = 45 P2_both_stag_payoff = 30 InstructionsP1_template = 'Andalib_project/InstructionsP1.html' InstructionsP2_template = 'Andalib_project/InstructionsP2.html' class Subsession(BaseSubsession): def creating_session(self): import random if self.round_number == 1: self.group_randomly() if random.randint(1,100)<=55: self.session.vars['max_rounds']=5 else: if random.randint(1,100)<=55: self.session.vars['max_rounds'] = 6 else: if random.randint(1, 100) <= 55: self.session.vars['max_rounds'] = 7 else: if random.randint(1, 100) <= 55: self.session.vars['max_rounds'] = 8 else: if random.randint(1, 100) <= 55: self.session.vars['max_rounds'] = 9 else: self.session.vars['max_rounds'] = 10 paying_round = random.randint(1,self.session.vars['max_rounds']) self.session.vars['paying_round'] = paying_round class Group(BaseGroup): def set_payoffs(self): for p in self.get_players(): p.set_payoff() class Player(BasePlayer): decision = models.StringField( choices=[['Low Discount', 'Low Discount'], ['High Discount', 'High Discount']], doc="""This player's decision""", widget=widgets.RadioSelect, ) def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): if self.id_in_group == 1: payoff_matrix = dict( Low_Discount=dict( Low_Discount=Constants.P1_both_stag_payoff, High_Discount=Constants.P1_stag_hare_payoff ), High_Discount=dict( Low_Discount=Constants.P1_hare_stag_payoff, High_discount=Constants.P1_both_hare_payoff ), ) self.payoff = payoff_matrix[self.decision][self.other_player().decision] if self.round_number == self.session.vars['paying_round']: total_payoff_1 = self.payoff self.session.vars['total_payoff_P1'] = total_payoff_1 else: payoff_matrix = dict( Low_Discount=dict( Low_Discount =Constants.P2_both_stag_payoff, High_discount=Constants.P2_hare_stag_payoff ), High_Discount=dict( Low_Discount=Constants.P2_stag_hare_payoff, High_Discount=Constants.P2_both_hare_payoff ), ) self.payoff = payoff_matrix[self.decision][self.other_player().decision] if self.round_number == self.session.vars['paying_round']: total_payoff_2 = self.payoff self.session.vars['total_payoff_P2'] = total_payoff_2 age = models.IntegerField(label='What is your age?', min=18, max=125) major=models.StringField( choices=[['Math', 'Math'], ['Science', 'Science'], ['Economics', 'Economics'], ['Other', 'Other']], label='What is you major', widget=widgets.RadioSelect, ) gender = models.StringField( choices=[['Male', 'Male'], ['Female', 'Female'], ['Other', 'Other']], label='What is your gender?', widget=widgets.RadioSelect, ) race = models.StringField( choices=[['Asian', 'Asian'], ['Black', 'Black'], ['Hispanic', 'Hispanic'], ['White', 'White'], ['Other', 'Other']], label='What is your race/ethnicity?', widget=widgets.RadioSelect, )