from otree.api import ( Page, WaitPage, models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) from random import shuffle x = [ i for i in range(1,11)] shuffle(x) class Constants(BaseConstants): name_in_url = 'common_value_auction' players_per_group = 20 num_rounds = 10 instructions_template = 'dictator_rev/instructions.html' endowment = c(20) class Subsession(BaseSubsession): pass class Group(BaseGroup): highest_bid = models.CurrencyField() offer = models.CurrencyField() class Player(BasePlayer): keep_amount = models.IntegerField(widget=widgets.RadioSelect, choices=[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) is_winner = models.BooleanField( initial=False, doc="""Indicates whether the player is the winner""" ) is_pair = models.BooleanField( initial=False, doc="""Indicates whether the player is the pair""" ) offer1 = models.CurrencyField() # FUNCTIONS def creating_session(subsession: Subsession): pass def set_winner(group: Group): import random players = group.get_players() group.highest_bid = group.round_number-1 players_with_highest_bid = [p for p in players if p.id_in_group == x[group.highest_bid]] players_pair = [p for p in players if p.id_in_group == group.highest_bid + 11] winner = players_with_highest_bid[0] pair = players_pair[0] winner.is_winner = True pair.is_pair = True for p in players: set_payoff(p) def set_payoff(player: Player): group = player.group if player.is_winner: player.payoff = player.keep_amount group.offer = Constants.endowment - player.keep_amount if player.payoff < 0: player.payoff = 0 if player.is_pair: player.payoff = group.offer # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Bid(Page): @staticmethod def is_displayed(player: Player): return player.id_in_group < 11 form_model = 'player' form_fields = ['keep_amount'] class ResultsWaitPage(WaitPage): after_all_players_arrive = 'set_winner' class Results(Page): @staticmethod def is_displayed(player: Player): return player.id_in_group < 11 class Pose(Page): timeout_seconds = 20 @staticmethod def is_displayed(player: Player): if player.round_number == 10: return else: return player.id_in_group < 11 class Payout(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 10 page_sequence = [Introduction, Bid, Results, Pose, ResultsWaitPage, Payout]