from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Siyu' doc = """ multiple remedies contracts """ class Constants(BaseConstants): name_in_url = 'c_Multiple_OD600chat' players_per_group = 2 num_rounds = 30 num_rounds2 = num_rounds/2 num_rounds21 = num_rounds/2+1 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): x = models.CurrencyField() p = models.CurrencyField(min=0, max=500) d = models.CurrencyField(min=0, max=500) value = models.CurrencyField() cost = models.CurrencyField() accept_reject = models.IntegerField( choices=[ [1, 'Accept'], [0, 'Reject'], ] ) deliver = models.IntegerField( choices=[ [1, 'Yes'], [0, 'No'], ], initial= 1 ) veto = models.IntegerField( choices=[ [1, 'Veto'], [0, 'Not Veto'], ] ) message1 = models.LongStringField() message2 = models.LongStringField() def random_numbers(self): self.cost = random.randint(0, 600) self.value = random.randint(0, 1000) print('cost is', self.cost) print('value is', self.value) def set_payoffs(self): p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) if self.accept_reject == 0: p1.payoff1 = 0 p1.payoff2 = 0 p2.payoff1 = 0 p2.payoff2 = 0 else: if self.deliver == 1: p1.payoff1 = self.value-self.p p1.payoff2 = self.p - self.cost p2.payoff1 = self.p - self.cost p2.payoff2 = self.value-self.p else: if self.veto == 1: p1.payoff1 = self.value - self.p p1.payoff2 = self.p - self.cost p2.payoff1 = self.p - self.cost p2.payoff2 = self.value - self.p else: p1.payoff1 = self.d p1.payoff2 = -self.d p2.payoff1 = -self.d p2.payoff2 = self.d 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.02 p2.participant.vars['game_payoff1'] = p2.payoff1 * 0.02 if self.subsession.round_number==self.session.vars['paying_round2']: p1.participant.vars['game_payoff2'] = p1.payoff2 * 0.02 p2.participant.vars['game_payoff2'] = p2.payoff2 * 0.02 class Player(BasePlayer): payoff1 = models.CurrencyField() payoff2 = models.CurrencyField()