from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'trial' PLAYERS_PER_GROUP = None NUM_ROUNDS = 4 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() period = models.IntegerField() saturate = models.IntegerField() def creating_session(subsession: Subsession): session = subsession.session import random trial=[70,50,30,0] subsession.FARMLAND=trial[subsession.round_number-1] 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=trial[0] p.NO2=trial[1] p.NO3=trial[2] p.NO4=trial[3] p.period=1 p.saturate=50 class Group(BaseGroup): highest_bid = models.CurrencyField() second_highest_bid = models.CurrencyField() winner = models.IntegerField() myfarmland = models.IntegerField() value = models.CurrencyField() 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 p.price = group.second_highest_bid p.participant.Total1=p.participant.Total1+p.payoff p.participant.trial= p.participant.trial+p.payoff #temporary payment for p in players: if p.round_number == 4 : p.trial_pre= p.participant.temp_total1 p.participant.temp_total1= p.participant.Total1 p.temp=p.participant.temp_total1 else: p.trial_pre= p.participant.temp_total1 p.participant.temp_total1=p.participant.temp_total1-p.paid p.temp=p.participant.temp_total1 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() trial_pre = models.CurrencyField() class MyWaitPage(WaitPage): wait_for_all_groups = True title_text = 'Please wait for the others to join in the trial auction' @staticmethod def is_displayed(player: Player): return player.round_number == 1 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_total1: player.bid=participant.temp_total1 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.trial), a=max(-200,participant.trial) +800, b=(max(-200,participant.trial)+800)/100, ) page_sequence = [MyWaitPage, Bid, ResultsWaitPage, Result, Summary, Payoff]