from otree.api import ( Page, WaitPage, models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) doc = """ In a common value auction game, players simultaneously bid on the item being auctioned.
Prior to bidding, they are given an estimate of the actual value of the item. This actual value is revealed after the bidding.
Bids are private. The player with the highest bid wins the auction, but payoff depends on the bid amount and the actual value.
""" 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): bid_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 players_with_highest_bid = [p for p in players if p.id_in_group == group.highest_bid] players_pair = [p for p in players if p.id_in_group == group.highest_bid + 10] winner = random.choice( players_with_highest_bid ) # if tie, winner is chosen at random pair = random.choice( players_pair ) # if tie, winner is chosen at random 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.bid_amount group.offer = Constants.endowment - player.bid_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]