from otree.api import *
c = Currency
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 = 'final_project'
players_per_group = None
num_rounds = 20
instructions_template = 'final_project/instructions.html'
min_allowable_treatment = c(0)
max_allowable_treatment = c(10)
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
pass
class Player(BasePlayer):
treatment_level = models.CurrencyField(
min=Constants.min_allowable_treatment,
max=Constants.max_allowable_treatment,
doc="""Amount prescribed by the player""",
label="Prescribed treatment level",
)
# FUNCTIONS
def set_winner(group: Group):
players = group.get_players()
group.lowest_treatment = min([p.treatment_level for p in players])
players_with_lowest_treatment = [p for p in players if p.bid_amount == group.lowest_treatment]
winner = players_with_lowest_treatment
winner.is_winner = True
for p in players:
set_payoff(p)
def set_payoff(player: Player):
if player.is_winner:
player.payoff = (2*player.treatment_level-0.1*(player.treatment_level)^2)/len(winner)
else:
player.payoff = 0
# PAGES
class Introduction(Page):
pass
class Prescribe(Page):
form_model = 'player'
form_fields = ['treatment_level']
class ResultsWaitPage(WaitPage):
after_all_players_arrive = 'set_payoff'
class Results(Page):
pass
page_sequence = [Introduction, Prescribe, ResultsWaitPage, Results]