from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random class Instruct1(Page): def is_displayed(self): return self.subsession.round_number==1 class Instruct2(Page): def is_displayed(self): return self.subsession.round_number==1 class Instruct3(Page): def is_displayed(self): return self.subsession.round_number==1 class Instruct4(Page): def is_displayed(self): return self.subsession.round_number==1 class PayType(WaitPage): def after_all_players_arrive(self): group = self.group if self.session.vars['tx_arm']=='Disc' or self.session.vars['tx_arm']=='NoDisc' or self.session.vars['tx_arm']=='NoRep': group.Agnt1_payA = 1 group.Agnt2_payA = 1 elif self.session.vars['tx_arm']=='Align': group.Agnt1_payA = 0 group.Agnt2_payA = 0 class PayType1(Page): form_model = 'group' form_fields = ['Agnt1_payA'] def Agnt1_payA_choices(self): return [[0, f'{self.session.vars["Flat"]} every time an Investor selects you'], [1, f'{self.session.vars["Base"]} every time an Investor selects you, and {self.session.vars["Comish"]} every time Jar A is chosen']] def vars_for_template(self): return { 'round_number': self.round_number } def is_displayed(self): if self.session.vars['tx_arm']=='Choice' and self.player.role()=='agent1': return True else: return False class PayType2(Page): form_model = 'group' form_fields = ['Agnt2_payA'] def Agnt2_payA_choices(self): return [[0, f'{self.session.vars["Flat"]} every time an Investor selects you'], [1, f'{self.session.vars["Base"]} every time an Investor selects you, and {self.session.vars["Comish"]} every time Jar A is chosen']] def vars_for_template(self): return { 'round_number': self.round_number } def is_displayed(self): if self.session.vars['tx_arm']=='Choice' and self.player.role()=='agent2': return True else: return False class Est1(Page): form_model = 'group' form_fields = ['Agnt1_EstA', 'Agnt1_EstB'] def before_next_page(self): group = self.group p3 = group.get_player_by_id(3) if self.session.vars['tx_arm']!='NoRep' and self.round_number > 1: p3.Agnt_Star_Cum = p3.in_round(p3.round_number - 1).Agnt_Star_Cum p3.Agnt_Star_Pot = p3.in_round(p3.round_number - 1).Agnt_Star_Pot group.Agnt1_stars = round(5* p3.Agnt_Star_Cum / p3.Agnt_Star_Pot, 2) def is_displayed(self): return self.player.role()=='agent1' def vars_for_template(self): return { 'image_pathA': 'Jars/sJar{}a1.png'.format(self.player.jarA_numb), 'image_pathB': 'Jars/sJar{}a1.png'.format(self.player.jarB_numb), 'round_number': self.round_number } class Est2(Page): form_model = 'group' form_fields = ['Agnt2_EstA', 'Agnt2_EstB'] def before_next_page(self): group = self.group p4 = group.get_player_by_id(4) if self.session.vars['tx_arm']!='NoRep' and self.round_number > 1: p4.Agnt_Star_Cum = p4.in_round(p4.round_number - 1).Agnt_Star_Cum p4.Agnt_Star_Pot = p4.in_round(p4.round_number - 1).Agnt_Star_Pot group.Agnt2_stars = round(5* p4.Agnt_Star_Cum / p4.Agnt_Star_Pot, 2) def is_displayed(self): return self.player.role()=='agent2' def vars_for_template(self): return { 'image_pathA': 'Jars/sJar{}a1.png'.format(self.player.jarA_numb), 'image_pathB': 'Jars/sJar{}a1.png'.format(self.player.jarB_numb), 'round_number': self.round_number } class PreSel(WaitPage): pass class SelExp1(Page): form_model = 'group' form_fields = ['Princ1_Agnt2'] def is_displayed(self): if self.player.role()=='princ1' and self.subsession.round_number>1: return True else: return False def before_next_page(self): if self.round_number == 1: self.participant.vars['total_rounds'] = Constants.num_rounds class SelExp2(Page): form_model = 'group' form_fields = ['Princ2_Agnt2'] def is_displayed(self): if self.player.role()=='princ2' and self.subsession.round_number>1: return True else: return False def before_next_page(self): if self.round_number == 1: self.participant.vars['total_rounds'] = Constants.num_rounds class EstWaitPage(WaitPage): def after_all_players_arrive(self): group = self.group if self.round_number == 1: group.Princ1_Agnt2 = 0 group.Princ2_Agnt2 = 1 class SelJar1(Page): form_model = 'group' form_fields = ['Princ1_JarB'] def vars_for_template(self): group = self.group return { 'Princ1_Agnt': group.get_Princ1_Agnt2_display(), 'round_number': self.round_number } def is_displayed(self): return self.player.role()=='princ1' def before_next_page(self): if self.round_number == 1: self.session.vars['total_rounds'] = Constants.num_rounds # print(self.session.total_rounds) # self.participant.vars['paying_round'] = random.randint(1, 2) self.participant.vars['paying_round'] = random.randint(1, 29) self.participant.vars['current_round'] = 0 # print(self.session.vars.paying_round) self.participant.vars['current_round'] = 1 + self.participant.vars['current_round'] # print('p1 pay round', self.participant.vars['paying_round']) class SelJar2(Page): form_model = 'group' form_fields = ['Princ2_JarB'] def vars_for_template(self): group = self.group return { 'Princ2_Agnt': group.get_Princ2_Agnt2_display(), 'round_number': self.round_number } def is_displayed(self): return self.player.role()=='princ2' def before_next_page(self): if self.round_number == 1: self.session.vars['total_rounds'] = Constants.num_rounds # print(self.session.total_rounds) self.participant.vars['paying_round'] = random.randint(1, 29) # self.participant.vars['paying_round'] = random.randint(1, 4) self.participant.vars['current_round'] = 0 # print(self.participant.vars['paying_round'] self.participant.vars['current_round'] = 1 + self.participant.vars['current_round'] class RateWaitPage(WaitPage): def after_all_players_arrive(self): group = self.group p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p3 = group.get_player_by_id(3) p4 = group.get_player_by_id(4) if self.round_number == 1: p3.participant.vars['current_block'] = 1 p4.participant.vars['current_block'] = 1 # only for first app p3.participant.vars['paying_block1'] = random.randint(1, 4) p4.participant.vars['paying_block1'] = random.randint(1, 4) # assures not equal to previous - also only for first app p3.participant.vars['paying_block2'] = p3.participant.vars['paying_block1'] + random.randint(1, 3) p4.participant.vars['paying_block2'] = p4.participant.vars['paying_block1'] + random.randint(1, 3) if p3.participant.vars['paying_block2'] >4: p3.participant.vars['paying_block2'] = p3.participant.vars['paying_block2'] - 4 if p4.participant.vars['paying_block2'] >4: p4.participant.vars['paying_block2'] = p4.participant.vars['paying_block2'] - 4 print('p1 paying round', p1.participant.vars['paying_round']) print('p2 paying round', p2.participant.vars['paying_round']) print('p3 paying blocks', p3.participant.vars['paying_block1'], p3.participant.vars['paying_block2']) print('p4 paying blocks', p4.participant.vars['paying_block1'], p4.participant.vars['paying_block2']) p1.round_payoff = (1-group.Princ1_Agnt2)*(Constants.totals[p3.jarA_numb-1] * (1-group.Princ1_JarB) + Constants.totals[p3.jarB_numb-1] * (group.Princ1_JarB)) + \ (group.Princ1_Agnt2)*(Constants.totals[p4.jarA_numb-1] * (1-group.Princ1_JarB) + Constants.totals[p4.jarB_numb-1] * (group.Princ1_JarB)) p2.round_payoff = (1-group.Princ2_Agnt2)*(Constants.totals[p3.jarA_numb-1] * (1-group.Princ2_JarB) + Constants.totals[p3.jarB_numb-1] * (group.Princ2_JarB)) + \ (group.Princ2_Agnt2)*(Constants.totals[p4.jarA_numb-1] * (1-group.Princ2_JarB) + Constants.totals[p4.jarB_numb-1] * (group.Princ2_JarB)) p3.round_payoff = (1-group.Agnt1_payA)*p3.session.vars['Flat']*((1-group.Princ1_Agnt2)+(1-group.Princ2_Agnt2)) + group.Agnt1_payA*p3.session.vars['Base']*((1-group.Princ1_Agnt2) +\ (1-group.Princ2_Agnt2)) + group.Agnt1_payA*p3.session.vars['Comish']*((1-group.Princ1_JarB)*(1-group.Princ1_Agnt2)+(1-group.Princ2_Agnt2)*(1-group.Princ2_JarB)) p4.round_payoff = (1-group.Agnt2_payA)*p4.session.vars['Flat']*(group.Princ1_Agnt2+group.Princ2_Agnt2) + group.Agnt2_payA*p4.session.vars['Base']*(group.Princ1_Agnt2 + \ group.Princ2_Agnt2) + group.Agnt2_payA*p4.session.vars['Comish']*(group.Princ1_Agnt2*(1-group.Princ1_JarB)+group.Princ2_Agnt2*(1-group.Princ2_JarB)) p1.ncf_payoff = (1-group.Princ1_Agnt2)*(Constants.totals[p3.jarA_numb-1] * (group.Princ1_JarB) + Constants.totals[p3.jarB_numb-1] * (1-group.Princ1_JarB)) + \ (group.Princ1_Agnt2)*(Constants.totals[p4.jarA_numb-1] * (group.Princ1_JarB) + Constants.totals[p4.jarB_numb-1] * (1-group.Princ1_JarB)) +\ round(random.uniform(-p1.session.vars['Noise'], p1.session.vars['Noise']), 2) p2.ncf_payoff = (1-group.Princ2_Agnt2)*(Constants.totals[p3.jarA_numb-1] * (group.Princ2_JarB) + Constants.totals[p3.jarB_numb-1] * (1-group.Princ2_JarB)) + \ (group.Princ2_Agnt2)*(Constants.totals[p4.jarA_numb-1] * (group.Princ2_JarB) + Constants.totals[p4.jarB_numb-1] * (1-group.Princ2_JarB)) + \ round(random.uniform(-p2.session.vars['Noise'], p2.session.vars['Noise']), 2) if p1.participant.vars['current_round'] == p1.participant.vars['paying_round']: p1.payoff = p1.round_payoff if p2.participant.vars['current_round'] == p2.participant.vars['paying_round']: p2.payoff = p2.round_payoff if p3.participant.vars['current_block'] == p3.participant.vars['paying_block1']: p3.payoff = p3.round_payoff if p4.participant.vars['current_block'] == p4.participant.vars['paying_block1']: p4.payoff = p4.round_payoff if p3.participant.vars['current_block'] == p3.participant.vars['paying_block2']: p3.payoff = p3.round_payoff if p4.participant.vars['current_block'] == p4.participant.vars['paying_block2']: p4.payoff = p4.round_payoff print('p1 block 1 rnd', p1.participant.vars['current_round'], 'pay', p1.round_payoff, 'total = ', p1.payoff) print('p2 block 1 rnd', p2.participant.vars['current_round'], 'pay', p2.round_payoff, 'total = ', p2.payoff) print('p3 block 1 rnd', p1.participant.vars['current_round'], 'pay', p3.round_payoff, 'total = ', p3.payoff) print('p4 block 1 rnd', p1.participant.vars['current_round'], 'pay', p4.round_payoff, 'total = ', p4.payoff) class Rate1(Page): def vars_for_template(self): if self.session.vars['tx_arm']!='NoRep': form_model = 'group' form_fields = ['Prnc1_Rate'] group = self.group return { 'Princ1_Agnt': group.get_Princ1_Agnt2_display(), 'Princ1_Jar': group.get_Princ1_JarB_display(), 'round_number': self.round_number } def is_displayed(self): return self.player.role()=='princ1' class Rate2(Page): def vars_for_template(self): if self.session.vars['tx_arm']!='NoRep': form_model = 'group' form_fields = ['Prnc1_Rate'] group = self.group return { 'Princ2_Agnt': group.get_Princ2_Agnt2_display(), 'Princ2_Jar': group.get_Princ2_JarB_display(), 'round_number': self.round_number } def is_displayed(self): return self.player.role()=='princ2' class EndWaitPage(WaitPage): pass class ResAgnt1(Page): def vars_for_template(self): group = self.group return{ 'choosen': 2 - group.Princ1_Agnt2 - group.Princ2_Agnt2, 'jarAs': 2 - group.Princ1_Agnt2 - group.Princ2_Agnt2 - (1-group.Princ1_Agnt2)*group.Princ1_JarB - (1-group.Princ2_Agnt2)*group.Princ2_JarB, 'round_number': self.round_number } def is_displayed(self): return self.player.role()=='agent1' class ResAgnt2(Page): def vars_for_template(self): group = self.group return{ 'choosen': group.Princ1_Agnt2 + group.Princ2_Agnt2, 'jarAs': group.Princ1_Agnt2 + group.Princ2_Agnt2 - group.Princ1_Agnt2*group.Princ1_JarB - group.Princ2_Agnt2*group.Princ2_JarB, 'round_number': self.round_number } def is_displayed(self): return self.player.role()=='agent2' class EndWaitPage(WaitPage): def after_all_players_arrive(self): if self.session.vars['tx_arm']!='NoRep': group = self.group p3 = group.get_player_by_id(3) p4 = group.get_player_by_id(4) if self.round_number == 1: p3.Agnt_Star_Cum = (1-group.Princ1_Agnt2)*group.Prnc1_Rate + (1-group.Princ2_Agnt2)*group.Prnc2_Rate p3.Agnt_Star_Pot = (1-group.Princ1_Agnt2)*5 + (1-group.Princ2_Agnt2)*5 p4.Agnt_Star_Cum = group.Princ1_Agnt2*group.Prnc1_Rate + group.Princ2_Agnt2*group.Prnc2_Rate p4.Agnt_Star_Pot = group.Princ1_Agnt2*5 + group.Princ2_Agnt2*5 if self.round_number > 1: p3.Agnt_Star_Cum = p3.Agnt_Star_Cum + (1-group.Princ1_Agnt2)*group.Prnc1_Rate + (1-group.Princ2_Agnt2)*group.Prnc2_Rate p3.Agnt_Star_Pot = p3.Agnt_Star_Pot + (1-group.Princ1_Agnt2)*5 + (1-group.Princ2_Agnt2)*5 p4.Agnt_Star_Cum = p4.Agnt_Star_Cum + group.Princ1_Agnt2*group.Prnc1_Rate + group.Princ2_Agnt2*group.Prnc2_Rate p4.Agnt_Star_Pot = p4.Agnt_Star_Pot + group.Princ1_Agnt2*5 + group.Princ2_Agnt2*5 self.vars_dump = str(self.session.vars) class BlockEnd(Page): def is_displayed(self): return self.round_number==Constants.num_rounds page_sequence = [ Instruct1, Instruct2, Instruct3, Instruct4, PayType, PayType1, PayType2, Est1, Est2, PreSel, SelExp1, SelExp2, EstWaitPage, SelJar1, SelJar2, RateWaitPage, Rate1, Rate2, ResAgnt1, ResAgnt2, EndWaitPage, BlockEnd ]