from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, Page, WaitPage, ) import random doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'second_seal_bid' players_per_group = 3 num_rounds = 3 num_group = 2 class Subsession(BaseSubsession): pass class Group(BaseGroup): highest_bid = models.CurrencyField() second_highest_bid = models.CurrencyField() winner = models.IntegerField() pass class Player(BasePlayer): value = models.CurrencyField() bid = models.CurrencyField() is_winner = models.BooleanField() winnerprice = models.CurrencyField() pass #Function def creating_session(subsession): print('in creating session') for p in subsession.get_players(): p.value = random.random()*100 p.is_winner = False if subsession.round_number == 1: p.participant.vars['TotalEarning'] = 0 pass def auction_outcome(g: Group): players = g.get_players() bids = [p.bid for p in players] bids.sort(reverse=True) g.highest_bid = bids[0] g.second_highest_bid = bids[1] #TIE highest_bidders = [p.id_in_group for p in players if p.bid == g.highest_bid] g.winner = random.choice(highest_bidders) winning_player = g.get_player_by_id(g.winner) winning_player.is_winner = True #set payoff for p in players: p.winnerprice = g.highest_bid if p.is_winner: p.payoff = p.value - g.second_highest_bid pass # PAGES class bid(Page): form_model = 'player' form_fields = ['bid'] pass class ResultsWaitPage(WaitPage): after_all_players_arrive = 'auction_outcome' body_text = "please wait for everyone to submit bids." pass class ShuffleWaitPage(WaitPage): wait_for_all_groups = True body_text = "we are shuffling your group for last 2 rounds." def after_all_players_arrive(self): for subsession in self.subsession.in_rounds(2, Constants.num_rounds): subsession.group_like_round(1) def is_displayed(self): return self.round_number == 1 pass class Result(Page): pass page_sequence = [bid, ResultsWaitPage, Result, ShuffleWaitPage]