from otree.api import * import time doc = """eBay style auction""" global leading_bid leading_bid = 0 class Constants(BaseConstants): name_in_url = 'ebay' players_per_group = 2 num_rounds = 1 max_bid = 100 bid_increment = 1 class Subsession(BaseSubsession): pass class Group(BaseGroup): auction_end = models.FloatField() # start with player 1 by default highest_bidder = models.IntegerField(initial = 1,) highest_bid = models.CurrencyField(initial = 0,) class Player(BasePlayer): is_winner = models.BooleanField(initial=False) bid = models.CurrencyField(initial=0) class WaitToStart(WaitPage): @staticmethod def after_all_players_arrive(group: Group): group.auction_end = time.time() + 60 class Bidding(Page): @staticmethod def live_method(player: Player, data): group = player.group my_id = player.id_in_group is_new_high_bid = False global leading_bid if 'bid' in data: bid = data['bid'] is_new_high_bid = True if my_id == group.highest_bidder: if bid > group.highest_bid: if group.highest_bid == 0: leading_bid = 1 player.bid = bid player.payoff = Constants.max_bid - (leading_bid) group.highest_bid = bid else: player.bid = bid else: if bid > group.highest_bid: player.bid = bid leading_bid = group.highest_bid + 1 if group.highest_bid == 0: leading_bid = 1 group.highest_bidder = my_id group.highest_bid = bid else: player.bid = bid leading_bid = bid + 1 else: group.highest_bid = 0 leading_bid = 0 player.bid = 0 is_new_high_bid = True return { 0: dict( is_new_high_bid = is_new_high_bid, highest_bid = leading_bid, highest_bidder = group.highest_bidder, my_bid = player.bid, my_id = my_id, ) } @staticmethod def js_vars(player: Player): return dict (my_id = player.id_in_group) @staticmethod def get_timeout_seconds(player: Player): import time group = player.group return group.auction_end - time.time() class ResultsWaitPage(WaitPage): @staticmethod def after_all_players_arrive(group: Group): global leading_bid winner = group.get_player_by_id(group.highest_bidder) group.highest_bid = leading_bid winner.is_winner = True winner.payoff = Constants.max_bid - leading_bid # you lose whatever you bid. class Results(Page): pass page_sequence = [WaitToStart, Bidding, ResultsWaitPage, Results]