from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Siyu' doc = """ belief formation endogenous """ class Constants(BaseConstants): name_in_url = 'b_belief_formation_Endo' players_per_group = 2 num_rounds = 32 num_rounds2 = num_rounds/2 num_rounds21 = num_rounds/2+1 total_tile = 20 instructions_template = 'b_belief_formation_Endo/instructions.html' class Subsession(BaseSubsession): def creating_session(self): self.group_randomly(fixed_id_in_group=True) import random paying_round1 = random.randint(1, Constants.num_rounds2) paying_round2 = random.randint(Constants.num_rounds21, Constants.num_rounds) self.session.vars['paying_round1'] = paying_round1 print('set the paying round 1 to', paying_round1) self.session.vars['paying_round2'] = paying_round2 print('set the paying round 2 to', paying_round2) class Group(BaseGroup): computer_tile = models.IntegerField() x = models.IntegerField() com_tile_color = models.IntegerField( choices=[ [2, 'Green_Square'], [1, 'Green_Circular'], [0, 'Yellow_Circular'], ] ) hat_color = models.IntegerField( choices=[ [1, 'Green'], [0, 'Yellow'], ] ) color_green = models.IntegerField(widget=widgets.RadioSelectHorizontal, choices=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) look_choice = models.IntegerField( choices=[ [1, 'Look'], [0, 'Not Look'], ] ) def random_numbers(self): self.computer_tile = random.randint(1, Constants.total_tile) self.x = 20 + random.randint(0, 2) * 30 print('computer selects tiles', self.computer_tile) print('payoff x is', self.x) def set_payoffs(self): p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) if self.computer_tile <= 10: self.com_tile_color = 2 elif self.computer_tile <= 10+self.color_green: self.com_tile_color = 1 else: self.com_tile_color = 0 print('tile color is', self.com_tile_color) if self.look_choice == 0: self.hat_color = 0 elif self.com_tile_color == 0: self.hat_color = 0 else: self.hat_color = 1 print('hat color is', self.hat_color) if self.hat_color == 1: p1.payoff1 = 100 p2.payoff2 = 100 else: p1.payoff1 = 0 p2.payoff2 = 0 if self.hat_color == 0 and self.com_tile_color < 2: p2.payoff1 = 100 p1.payoff2 = 100 elif self.hat_color == 1 and self.com_tile_color == 2: p2.payoff1 = self.x p1.payoff2 = self.x else: p2.payoff1 = 0 p1.payoff2 = 0 p1.participant.vars['paying_r1'] = p1.session.vars['paying_round1'] p2.participant.vars['paying_r1'] = p2.session.vars['paying_round1'] p1.participant.vars['paying_r2'] = p1.session.vars['paying_round2'] p2.participant.vars['paying_r2'] = p2.session.vars['paying_round2'] if self.subsession.round_number==self.session.vars['paying_round1']: p1.participant.vars['game_payoff1'] = p1.payoff1 * 0.1 p2.participant.vars['game_payoff1'] = p2.payoff1 * 0.1 if self.subsession.round_number==self.session.vars['paying_round2']: p1.participant.vars['game_payoff2'] = p1.payoff2 * 0.1 p2.participant.vars['game_payoff2'] = p2.payoff2 * 0.1 class Player(BasePlayer): payoff1 = models.CurrencyField() payoff2 = models.CurrencyField() game_payoff1 = models.CurrencyField() game_payoff2 = models.CurrencyField()