from otree.api import (
models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer,
Currency as c, currency_range, Page, WaitPage
)
cu = c
doc = '\nIn a common value auction game, players simultaneously bid on the item being\nauctioned.
\nPrior to bidding, they are given an estimate of the actual value of the item.\nThis actual value is revealed after the bidding.
\nBids are private. The player with the highest bid wins the auction, but\npayoff depends on the bid amount and the actual value.
\n'
class Constants(BaseConstants):
name_in_url = 'common_value_auction'
players_per_group = None
num_rounds = 1
min_allowable_bid = c(0)
max_allowable_bid = c(10000)
instructions_template = 'common_value_auction/instructions.html'
class Subsession(BaseSubsession):
pass
def generate_value_estimate(group):
import random
minimum = 0
maximum = 1000
estimate = random.uniform(minimum, maximum)
estimate = round(estimate, 1)
if estimate < Constants.min_allowable_bid:
estimate = Constants.min_allowable_bid
if estimate > Constants.max_allowable_bid:
estimate = Constants.max_allowable_bid
return estimate
def set_winner(group):
import random
players = group.get_players()
group.highest_bid = max([p.bid_amount for p in players])
players_with_highest_bid = [p for p in players if p.bid_amount == group.highest_bid]
winner = random.choice(
players_with_highest_bid
) # if tie, winner is chosen at random
winner.is_winner = True
for p in players:
set_payoff(p)
class Group(BaseGroup):
highest_bid = models.CurrencyField()
generate_value_estimate = generate_value_estimate
set_winner = set_winner
def set_payoff(player):
group = player.group
import random
if player.is_winner:
players = group.get_players()
group.highest_bid = max([p.bid_amount for p in players])
all_other_bids = [p.bid_amount for p in players if p.bid_amount != group.highest_bid]
winner_must_pay = random.choice(
all_other_bids
)
player.payoff = winner_must_pay
else:
player.payoff = 0
class Player(BasePlayer):
item_value_estimate = models.CurrencyField(doc='Estimate of the common value may be different for each player')
bid_amount = models.CurrencyField(doc='Amount bidded by the player', label='Bid amount', max=Constants.max_allowable_bid, min=Constants.min_allowable_bid)
is_winner = models.BooleanField(doc='Indicates whether the player is the winner', initial=False)
player_number = models.IntegerField(label='Your ID')
set_payoff = set_payoff