from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'Task2' PLAYERS_PER_GROUP = None NUM_ROUNDS = 6 class Subsession(BaseSubsession): FARMLAND1 = models.IntegerField() FARMLAND2 = models.IntegerField() FARMLAND3 = models.IntegerField() FARMLAND4 = models.IntegerField() def creating_session(subsession: Subsession): session = subsession.session import random trial1=[90,80,70,60,50,40] trial2=[80,70,60,50,40,30] trial3=[70,60,50,40,30,0] trial4=[0,0,0,0,0,0] subsession.FARMLAND1=trial1[subsession.round_number-1] subsession.FARMLAND2=trial2[subsession.round_number-1] subsession.FARMLAND3=trial3[subsession.round_number-1] subsession.FARMLAND4=trial4[subsession.round_number-1] for g in subsession.get_groups(): if subsession.FARMLAND1==100: farmland1return= [230 , 370, 495, 578, 675, 813] farmland1possible= random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==90: farmland1return= [208, 338, 453, 532, 621, 760] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==80: farmland1return=[184, 303, 408, 486, 574, 710] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==70: farmland1return=[159, 267, 365, 441, 525, 662] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==60: farmland1return=[133, 228, 319, 396, 485, 619] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==50: farmland1return=[105, 187, 271, 345, 440, 579] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==40: farmland1return=[78, 146, 225, 304, 408, 547] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==30: farmland1return=[59, 117, 194, 279, 394, 538] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==20: farmland1return=[9, 49, 146, 262, 417, 597] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==10: farmland1return=[9, 49, 146, 262, 417, 597] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] if subsession.FARMLAND1==0: farmland1return=[9, 49, 146, 262, 417, 597] farmland1possible = random.choices(farmland1return, weights =[5,20,25,25,20,5],k=1 ) g.farmland1value=farmland1possible[0] for g in subsession.get_groups(): if subsession.FARMLAND2==100: farmland2return= [230 , 370, 495, 578, 675, 813] farmland2possible= random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==90: farmland2return= [208, 338, 453, 532, 621, 760] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==80: farmland2return=[184, 303, 408, 486, 574, 710] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==70: farmland2return=[159, 267, 365, 441, 525, 662] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==60: farmland2return=[133, 228, 319, 396, 485, 619] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==50: farmland2return=[105, 187, 271, 345, 440, 579] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==40: farmland2return=[78, 146, 225, 304, 408, 547] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==30: farmland2return=[59, 117, 194, 279, 394, 538] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==20: farmland2return=[9, 49, 146, 262, 417, 597] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==10: farmland2return=[9, 49, 146, 262, 417, 597] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] if subsession.FARMLAND2==0: farmland2return=[9, 49, 146, 262, 417, 597] farmland2possible = random.choices(farmland2return, weights =[5,20,25,25,20,5],k=1 ) g.farmland2value=farmland2possible[0] for g in subsession.get_groups(): if subsession.FARMLAND3==100: farmland3return= [230 , 370, 495, 578, 675, 813] farmland3possible= random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==90: farmland3return= [208, 338, 453, 532, 621, 760] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==80: farmland3return=[184, 303, 408, 486, 574, 710] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==70: farmland3return=[159, 267, 365, 441, 525, 662] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==60: farmland3return=[133, 228, 319, 396, 485, 619] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==50: farmland3return=[105, 187, 271, 345, 440, 579] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==40: farmland3return=[78, 146, 225, 304, 408, 547] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==30: farmland3return=[59, 117, 194, 279, 394, 538] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==20: farmland3return=[9, 49, 146, 262, 417, 597] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==10: farmland3return=[9, 49, 146, 262, 417, 597] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND3==0: farmland3return=[9, 49, 146, 262, 417, 597] farmland3possible = random.choices(farmland3return, weights =[5,20,25,25,20,5],k=1 ) g.farmland3value=farmland3possible[0] if subsession.FARMLAND4==0: farmland4return=[9, 49, 146, 262, 417, 597] farmland4possible = random.choices(farmland4return, weights =[5,20,25,25,20,5],k=1 ) g.farmland4value=farmland4possible[0] for p in subsession.get_players(): if subsession.round_number==1: p.A=trial1[0] p.B=trial2[0] p.C=trial3[0] p.D=trial4[0] p.saturate=80 if subsession.round_number==2: p.A=trial1[1] p.B=trial2[1] p.C=trial3[1] p.D=trial4[1] p.saturate=70 if subsession.round_number==3: p.A=trial1[2] p.B=trial2[2] p.C=trial3[2] p.D=trial4[2] p.saturate=60 if subsession.round_number==4: p.A=trial1[3] p.B=trial2[3] p.C=trial3[3] p.D=trial4[3] p.saturate=50 if subsession.round_number==5: p.A=trial1[4] p.B=trial2[4] p.C=trial3[4] p.D=trial4[4] p.saturate=40 if subsession.round_number==6: p.A=trial1[5] p.B=trial2[5] p.C=trial3[5] p.D=trial4[5] p.saturate=30 class Group(BaseGroup): auction_end = models.FloatField() highest_bidder1 = models.IntegerField(initial=1) highest_bid1 = models.CurrencyField(initial=0) highest_bidder2 = models.IntegerField(initial=1) highest_bid2 = models.CurrencyField(initial=0) farmland1value = models.CurrencyField() farmland2value = models.CurrencyField() highest_bidder3 = models.IntegerField(initial=1) highest_bidder4 = models.IntegerField(initial=1) highest_bid3 = models.CurrencyField(initial=0) highest_bid4 = models.CurrencyField(initial=0) farmland3value = models.CurrencyField() farmland4value = models.CurrencyField() def after_all_players_arrive(group: Group): import time group.auction_end = time.time() + 240 players = group.get_players() for p in players: p.limit=p.participant.temp_total p.limit1=p.participant.temp_total p.limit2=p.participant.temp_total p.limit3=p.participant.temp_total p.limit4=p.participant.temp_total p.dummyA=0 p.dummyB=0 p.dummyC=0 p.dummyD=0 def after_all_players_arrive1(group: Group): # set payoffs winner1 = group.get_player_by_id(group.highest_bidder1) winner1.is_winner1 = True winner1.payoff1 = group.farmland1value-group.highest_bid1 winner1.pay1=group.highest_bid1 winner2 = group.get_player_by_id(group.highest_bidder2) winner2.is_winner2 = True winner2.payoff2 = group.farmland2value-group.highest_bid2 winner2.pay2=group.highest_bid2 winner3 = group.get_player_by_id(group.highest_bidder3) winner3.is_winner3 = True winner3.payoff3 = group.farmland3value-group.highest_bid3 winner3.pay3=group.highest_bid3 winner4 = group.get_player_by_id(group.highest_bidder4) winner4.is_winner4 = True winner4.payoff4 = group.farmland4value-group.highest_bid4 winner4.pay4=group.highest_bid4 # Get the set of players in the group players = group.get_players() for p in players: if p.is_winner1 is False: p.payoff1=0 p.pay1=0 if p.is_winner2 is False: p.payoff2=0 p.pay2=0 if p.is_winner3 is False: p.payoff3=0 p.pay3=0 if p.is_winner4 is False: p.payoff4=0 p.pay4=0 p.payoff=p.payoff1+p.payoff2+p.payoff3+p.payoff4 p.trial_pre= p.participant.temp_total p.participant.temp_total= p.participant.temp_total+p.payoff p.temp=p.participant.temp_total p.participant.Task2 = p.participant.Task2 + p.payoff class Player(BasePlayer): is_winner1 = models.BooleanField(initial=False) is_winner2 = models.BooleanField(initial=False) bid1 = models.CurrencyField(initial=0) bid2 = models.CurrencyField(initial=0) pay1 = models.CurrencyField(initial=0) pay2 = models.CurrencyField(initial=0) payoff1 = models.CurrencyField(initial=0) payoff2 = models.CurrencyField(initial=0) A = models.IntegerField() B = models.IntegerField() saturate = models.IntegerField() trial_pre = models.CurrencyField() temp = models.CurrencyField() is_winner3 = models.BooleanField(initial=False) is_winner4 = models.BooleanField(initial=False) bid3 = models.CurrencyField(initial=0) bid4 = models.CurrencyField(initial=0) pay3 = models.CurrencyField() pay4 = models.CurrencyField() payoff3 = models.CurrencyField(initial=0) payoff4 = models.CurrencyField() C = models.IntegerField() D = models.IntegerField() limit = models.CurrencyField() limit1 = models.CurrencyField() limit2 = models.CurrencyField() limit3 = models.CurrencyField() limit4 = models.CurrencyField() dummyA = models.CurrencyField() dummyB = models.CurrencyField() dummyC = models.CurrencyField() dummyD = models.CurrencyField() def live_method(player: Player, data): group = player.group my_id = player.id_in_group is_new_high_bid1 = False is_new_high_bid2 = False is_new_high_bid3 = False is_new_high_bid4 = False bid1 = min(data['bid1'],player.limit1,player.limit) if bid1 > group.highest_bid1: player.bid1 = bid1 group.highest_bid1 = bid1 group.highest_bidder1 = my_id is_new_high_bid1 = True bid2 = min(data['bid2'],player.limit2,player.limit-data['bid1']) if bid2 > group.highest_bid2: player.bid2 = bid2 group.highest_bid2 = bid2 group.highest_bidder2 = my_id is_new_high_bid2 = True bid3 = min(data['bid3'],player.limit3,player.limit-data['bid1']-data['bid2']) if bid3 > group.highest_bid3: player.bid3 = bid3 group.highest_bid3 = bid3 group.highest_bidder3 = my_id is_new_high_bid3 = True bid4 = min(data['bid4'],player.limit4,player.limit-data['bid1']-data['bid2']-data['bid3']) if bid4 > group.highest_bid4: player.bid4 = bid4 group.highest_bid4 = bid4 group.highest_bidder4 = my_id is_new_high_bid4 = True if group.highest_bidder1 == my_id: player.dummyA=group.highest_bid1 else: player.dummyA=0 if group.highest_bidder2 == my_id: player.dummyB=group.highest_bid2 else: player.dummyB=0 if group.highest_bidder3 == my_id: player.dummyC=group.highest_bid3 else: player.dummyC=0 if group.highest_bidder4 == my_id: player.dummyD=group.highest_bid4 else: player.dummyD=0 player.limit1=player.limit-player.dummyB-player.dummyC-player.dummyD player.limit2=player.limit-player.dummyA-player.dummyC-player.dummyD player.limit3=player.limit-player.dummyB-player.dummyA-player.dummyD return { 0: dict( is_new_high_bid1=is_new_high_bid1, highest_bid1=group.highest_bid1, highest_bidder1=group.highest_bidder1, is_new_high_bid2=is_new_high_bid2, highest_bid2=group.highest_bid2, highest_bidder2=group.highest_bidder2, is_new_high_bid3=is_new_high_bid3, highest_bid3=group.highest_bid3, highest_bidder3=group.highest_bidder3, is_new_high_bid4=is_new_high_bid4, highest_bid4=group.highest_bid4, highest_bidder4=group.highest_bidder4, ) } class WaitToStart(WaitPage): after_all_players_arrive = after_all_players_arrive title_text = 'Formal Auction' body_text = 'Please wait for others to get ready' class Bidding(Page): form_model = 'player' live_method = 'live_method' @staticmethod def js_vars(player: Player): group = player.group return dict(my_id=player.id_in_group) @staticmethod def get_timeout_seconds(player: Player): group = player.group import time return group.auction_end - time.time() class ResultsWaitPage(WaitPage): after_all_players_arrive = after_all_players_arrive1 class Results(Page): form_model = 'player' class Summary(Page): form_model = 'player' timeout_seconds = 60 @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, ) page_sequence = [WaitToStart, Bidding, ResultsWaitPage, Results, Summary, Payoff]