from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, Page, WaitPage, ) import random doc = """ Part 3 """ class Constants(BaseConstants): name_in_url = 'my_expr2' players_per_group = 2 # try 5 participants num_rounds = 9 timeout = 4 # small only for testing, 90 Ahi = 85 Alo = 15 Bhi = 50 Blo = 40 t3 = 9 # last round in this part class Subsession(BaseSubsession): pass class Group(BaseGroup): dice_ex3 = models.IntegerField() diceplay3 = models.IntegerField() outcomeG = models.IntegerField() choiceG = models.IntegerField() Bshare = models.IntegerField() Ashare = models.IntegerField() class Player(BasePlayer): lottery = models.IntegerField( choices=[ [0, 'Lottery A'], [1, 'Lottery B'], ], label='Your choice : ', ) # venmo = models.StringField(initial=' ') paypal = models.StringField(initial=' ') take_survey = models.BooleanField( label='Do you wish to add $10 to your payment by completing a short survey?') age = models.IntegerField(label='What is your age?', min=13, max=125) gender = models.StringField( choices=[['Male', 'Male'], ['Female', 'Female'], ['NA', 'Prefer not to say'], ['-9', 'Other']], label='What is your born gender?', widget=widgets.RadioSelectHorizontal, ) education = models.IntegerField( choices=[ [0, 'Less than high school'], [1, 'High school'], [2, 'Some college'], [3, 'College or higher'], ], label='What is the highest level of education you have completed?', ) educationM = models.IntegerField( choices=[ [0, 'Less than high school'], [1, 'High school'], [2, 'Some college'], [3, 'College or higher'], [-9, 'Do not know'], ], label='What is the highest level of education your 1st parent (e.g. mother) has completed?', ) educationF = models.IntegerField( choices=[ [0, 'Less than high school'], [1, 'High school'], [2, 'Some college'], [3, 'College or higher'], [-9, 'Do not know'], ], label='What is the highest level of education your other parent (e.g. father) has completed?', ) usd = models.CurrencyField() # FUNCTIONS def creating_session(subsession): if subsession.round_number == 1: # 1 fixed group here subsession.group_randomly() else: subsession.group_like_round(1) for p in subsession.get_players(): p.age = 0 # establish a dice exercise for g in subsession.get_groups(): if subsession.round_number == 1: g.dice_ex3 = random.randint(1, Constants.t3) # must be same for rounds 1-9 print(g.dice_ex3) elif 1 < subsession.round_number : gOne = g.in_round(1) g.dice_ex3 = gOne.dice_ex3 print(g.dice_ex3) def lottery_outcome(g: Group): # played in the end of lottery_outcome players = g.get_players() choices = [p.lottery for p in players] g.Bshare = sum(choices) g.Ashare = Constants.players_per_group - g.Bshare print(g.Bshare) if g.Bshare > Constants.players_per_group/2: g.choiceG = 1 elif g.Bshare < Constants.players_per_group/2: g.choiceG = 0 else: g.choiceG = random.randint(0, 1) # tie g.diceplay3 = random.randint(1, 10) if g.diceplay3 <= 10 - g.round_number: if g.choiceG == 0: g.outcomeG = Constants.Alo elif g.choiceG == 1: g.outcomeG = Constants.Blo else: g.outcomeG = -999 if g.diceplay3 > 10 - g.round_number: if g.choiceG == 0: g.outcomeG = Constants.Ahi elif g.choiceG == 1: g.outcomeG = Constants.Bhi else: g.outcomeG = -999 for p in g.get_players(): if g.round_number == g.dice_ex3: p.payoff = g.outcomeG else: p.payoff = 0 p.participant.vars['totalEarnings'] += p.payoff p.usd = p.participant.vars['totalEarnings']*0.1 # if g.round_number < Constants.t3: # p.take_survey = False # PAGES class Choice3(Page): form_model = 'player' form_fields = ['lottery'] @staticmethod def get_timeout_seconds(player: Player): return Constants.timeout @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.lottery = random.choice([1, 0]) class ResultsWaitPage(WaitPage): after_all_players_arrive = 'lottery_outcome' body_text = "Please wait for other players to decide" class Results3(Page): form_model = 'group' @staticmethod def get_timeout_seconds(player: Player): return Constants.timeout class PreSurvey(Page): def is_displayed(player: Player): return player.round_number == 9 form_model = 'player' form_fields = ['paypal', 'take_survey'] class Survey(Page): def is_displayed(player: Player): return player.round_number == 9 and player.take_survey == True form_model = 'player' form_fields = ['age', 'gender', 'education', 'educationM', 'educationF'] class FinalPay(Page): def is_displayed(player: Player): return player.round_number == 9 form_model = 'player' page_sequence = [Choice3, ResultsWaitPage, Results3, PreSurvey, Survey, FinalPay]