from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'auction' PLAYERS_PER_GROUP = None NUM_ROUNDS = 12 STYLE_BACKGROUND_COLOR = '#F0F0F0' STYLE_TEXT_COLOR = '#1F297E' MIN_ALLOWABLE_BID = cu(0) MY_CONSTANT = () class Subsession(BaseSubsession): FARMLAND = models.IntegerField() NO1 = models.IntegerField() NO2 = models.IntegerField() NO3 = models.IntegerField() NO4 = models.IntegerField() saturate = models.IntegerField() speriod = models.IntegerField() def creating_session(subsession: Subsession): session = subsession.session import random FARMLANDS=[90,70,50,0,70,50,30,0,50,30,0,0] subsession.FARMLAND=FARMLANDS[subsession.round_number-1] print(FARMLANDS) for g in subsession.get_groups(): if subsession.FARMLAND==100: farmlandreturn= [230 , 370, 495, 578, 675, 813] Possible= random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==90: farmlandreturn= [208, 338, 453, 532, 621, 760] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==80: farmlandreturn=[184, 303, 408, 486, 574, 710] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==70: farmlandreturn=[159, 267, 365, 441, 525, 662] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==60: farmlandreturn=[133, 228, 319, 396, 485, 619] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==50: farmlandreturn=[105, 187, 271, 345, 440, 579] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==40: farmlandreturn=[78, 146, 225, 304, 408, 547] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==30: farmlandreturn=[59, 117, 194, 279, 394, 538] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==20: farmlandreturn=[9, 49, 146, 262, 417, 597] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==10: farmlandreturn=[9, 49, 146, 262, 417, 597] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] if subsession.FARMLAND==0: farmlandreturn=[9, 49, 146, 262, 417, 597] Possible = random.choices(farmlandreturn, weights =[5,20,25,25,20,5],k=1 ) g.value=Possible[0] for p in subsession.get_players(): p.is_winner = False p.myfarmland=subsession.FARMLAND for p in subsession.in_rounds(1, 4): p.NO1=FARMLANDS[0] p.NO2=FARMLANDS[1] p.NO3=FARMLANDS[2] p.NO4=FARMLANDS[3] p.speriod=1 p.saturate=70 for p in subsession.in_rounds(5, 8): p.NO1=FARMLANDS[4] p.NO2=FARMLANDS[5] p.NO3=FARMLANDS[6] p.NO4=FARMLANDS[7] p.speriod=2 p.saturate=50 for p in subsession.in_rounds(9,12): p.NO1=FARMLANDS[8] p.NO2=FARMLANDS[9] p.NO3=FARMLANDS[10] p.NO4=FARMLANDS[11] p.speriod=3 p.saturate=30 class Group(BaseGroup): highest_bid = models.CurrencyField() second_highest_bid = models.CurrencyField() winner = models.IntegerField() myfarmland = models.IntegerField() value = models.CurrencyField() period = models.IntegerField() def auction_outcome(group: Group): import random # Get the set of players in the group players = group.get_players() # Get the set of bids from the players bids = [p.bid for p in players] # Sort the bids in descending order bids.sort(reverse=True) # Set the highest and second highest bids to the appropriate group variables # (Python uses 0-based arrays...) group.highest_bid = bids[0] group.second_highest_bid = bids[1] # Tie break # We always do this even when there is not a tie... ##### # first get the set of player IDs who bid the highest highest_bidders = [p.id_in_group for p in players if p.bid == group.highest_bid] # next randomly select one of these player IDs to be the winner group.winner = random.choice(highest_bidders) # Finally get the player model of the winning bidder and flag as winner winning_player = group.get_player_by_id(group.winner) winning_player.is_winner = True # set payoffs ##### for p in players: if p.is_winner: p.paid=group.second_highest_bid p.payoff = group.value - group.second_highest_bid p.finalreturn = group.value else: p.paid=0 p.payoff = 0 p.finalreturn=group.value for p in players: p.participant.Task2 = p.participant.Task2 + p.payoff p.price = group.second_highest_bid p.participant.Total=p.participant.Total+p.payoff for p in players: if p.round_number == 4 or p.round_number == 8 or p.round_number == 12: p.pre= p.participant.temp_total p.participant.temp_total= p.participant.Total p.temp=p.participant.temp_total else: p.pre=p.participant.temp_total p.participant.temp_total=p.participant.temp_total-p.paid p.temp=p.participant.temp_total for p in players: for p in group.in_rounds(1, 4): p.period=1 for p in group.in_rounds(5, 8): p.period=2 for p in group.in_rounds(9, 12): p.period=3 class Player(BasePlayer): bid = models.CurrencyField(max=850, min=0) is_winner = models.BooleanField() price = models.CurrencyField() myfarmland = models.IntegerField() paid = models.CurrencyField(initial=0) finalreturn = models.CurrencyField() temp = models.CurrencyField() pre = models.CurrencyField() class MyWaitPage(WaitPage): wait_for_all_groups = True title_text = 'Formal auction' body_text = 'please wait for the others to join in the next period auction ' @staticmethod def is_displayed(player: Player): return player.round_number == 1 or player.round_number == 5 or player.round_number == 9 return True class Warning(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == 1 or player.round_number == 5 or player.round_number == 9 return True class Bid(Page): form_model = 'player' form_fields = ['bid'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.bid>participant.temp_total: player.bid=participant.temp_total class ResultsWaitPage(WaitPage): after_all_players_arrive = auction_outcome body_text = '"Please wait for everyone to submit bids"' class Result(Page): form_model = 'player' class Summary(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS return True class Payoff(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS return True @staticmethod def vars_for_template(player: Player): participant = player.participant return dict( c=max(-200,participant.Task2), a=participant.Task1+max(-200,participant.Task2) +1000, b=(participant.Task1+max(-200,participant.Task2) +1000)/100, ) class MyPage(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS return True page_sequence = [MyWaitPage, Warning, Bid, ResultsWaitPage, Result, Summary, Payoff, MyPage]