from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, Page, WaitPage, ) import random doc = """ Risky Decision making in Large Groups """ class Constants(BaseConstants): name_in_url = 'my_expr' players_per_group = 4 # try 5 participants num_rounds = 18 timeout = 4 # small only for testing , 120 Ahi = 85 Alo = 15 Bhi = 50 Blo = 40 t1 = 9 t2 = 18 class Subsession(BaseSubsession): pass class Group(BaseGroup): summ = models.IntegerField() summ2 = models.IntegerField() Bshare = models.FloatField() Ashare = models.FloatField() class Player(BasePlayer): dice_ex = models.IntegerField() dice_ex2 = models.IntegerField() diceplay = models.IntegerField() outcome = models.IntegerField() lottery = models.IntegerField( choices=[ [0, 'Lottery A'], [1, 'Lottery B'], ], label='Your choice : ', ) round = models.IntegerField() # FUNCTIONS def creating_session(subsession): if subsession.round_number == 1: # 1 fixed group here subsession.group_randomly() else: subsession.group_like_round(1) # establish a total earnings var for p in subsession.get_players(): if subsession.round_number == 1: p.participant.vars['totalEarnings'] = 0 # must loop for each player p.dice_ex = random.randint(1, Constants.t1) # must be same for rounds 1-9 print(p.dice_ex) p.dice_ex2 = random.randint(Constants.t1 + 1, Constants.t2) # must be same for rounds 10-18 print(p.dice_ex2) elif 1 < subsession.round_number: p.round = subsession.round_number - 9 playerOne = p.in_round(1) p.dice_ex = playerOne.dice_ex print(p.dice_ex) p.dice_ex2 = playerOne.dice_ex2 # must be same for rounds10-18 print(p.dice_ex2) def public_info(g: Group): players = g.get_players() for p in players: if p.round_number >= Constants.t1: base_group = g.in_round(p.round_number - Constants.t1 + 1) g.Bshare = base_group.summ/Constants.players_per_group*100 print(base_group.summ) print(g.Bshare) g.Ashare = 100 - g.Bshare else: g.Bshare = 0 g.Ashare = 1 def end_of_round(g: Group): # played in the end of lottery_outcome players = g.get_players() choices = [p.lottery for p in players] g.summ = sum(choices) g.summ2 = g.session.num_participants - g.summ print(g.session.num_participants) def lottery_outcome(g: Group): public_info(g) # call other functions players = g.get_players() for p in players: p.diceplay = random.randint(1, 10) if p.round_number < Constants.t1 + 1: if p.diceplay <= Constants.t1 + 1 - p.round_number: if p.lottery == 0: p.outcome = Constants.Alo elif p.lottery == 1: p.outcome = Constants.Blo else: p.outcome = -999 if p.diceplay > Constants.t1 + 1 - p.round_number: if p.lottery == 0: p.outcome = Constants.Ahi elif p.lottery == 1: p.outcome = Constants.Bhi else: p.outcome = -999 else: if p.diceplay <= Constants.t2 + 1 - p.round_number: if p.lottery == 0: p.outcome = Constants.Alo elif p.lottery == 1: p.outcome = Constants.Blo else: p.outcome = -999 else: if p.lottery == 0: p.outcome = Constants.Ahi elif p.lottery == 1: p.outcome = Constants.Bhi else: p.outcome = -999 if p.round_number <= Constants.t1 and p.round_number == p.dice_ex: p.payoff = p.outcome elif p.round_number <= Constants.t1 and p.round_number != p.dice_ex: p.payoff = 0 elif p.round_number > Constants.t1 and p.round_number == p.dice_ex2: p.payoff = p.outcome else: p.payoff = 0 p.participant.vars['totalEarnings'] += p.payoff end_of_round(g) # PAGES class Intro(Page): def is_displayed(player: Player): return player.round_number == 1 class Choice(Page): form_model = 'player' form_fields = ['lottery'] def vars_for_template(p: Player): if p.round_number > 1: p_old = p.in_round(p.round_number - 1) print('p.group:', p.group) print('p.round_number:', p.round_number) b = p_old.group.Bshare a = p_old.group.Ashare c = p.round if p.round_number > Constants.t1: return dict( a=a, b=b, c=c, ) @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 Results(Page): form_model = 'player' @staticmethod def get_timeout_seconds(player: Player): return Constants.timeout page_sequence = [Intro, Choice, ResultsWaitPage, Results]