from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'english_trial' PLAYERS_PER_GROUP = None NUM_ROUNDS = 3 class Subsession(BaseSubsession): FARMLAND1 = models.IntegerField() FARMLAND2 = models.IntegerField() def creating_session(subsession: Subsession): session = subsession.session import random trial1=[70,70,70] trial2=[0,0,0] subsession.FARMLAND1=trial1[subsession.round_number-1] subsession.FARMLAND2=trial2[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 p in subsession.get_players(): if subsession.round_number==1: p.A=trial1[0] p.B=trial2[0] p.saturate=50 if subsession.round_number==2: p.A=trial1[1] p.B=trial2[1] p.saturate=50 if subsession.round_number==3: p.A=trial1[2] p.B=trial2[2] p.saturate=50 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() def after_all_players_arrive(group: Group): import time group.auction_end = time.time() + 180 players = group.get_players() for p in players: p.limit=p.participant.temp_total1 p.limit1=p.participant.temp_total1 p.limit2=p.participant.temp_total1 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 # 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 p.payoff=p.payoff1+p.payoff2 p.trial_pre=p.participant.temp_total1 p.participant.temp_total1= p.participant.temp_total1+p.payoff1+p.payoff2 p.temp=p.participant.temp_total1 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() limit = models.CurrencyField() limit1 = models.CurrencyField() limit2 = 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 bid1 = min(data['bid1'],player.limit1) if bid1 > group.highest_bid1: player.bid1 = bid1 group.highest_bid1 = bid1 group.highest_bidder1 = my_id is_new_high_bid1 = True if group.highest_bidder1 == my_id: bid2 = min(data['bid2'],player.limit-data['bid1']) else: bid2 = min(data['bid2'],player.limit2) if bid2 > group.highest_bid2: player.bid2 = bid2 group.highest_bid2 = bid2 group.highest_bidder2 = my_id is_new_high_bid2 = True if group.highest_bidder1 == my_id: player.limit1=player.limit player.limit2=player.limit-group.highest_bid1 if group.highest_bidder1 != my_id: if group.highest_bidder2 != my_id: player.limit1=player.limit player.limit2=player.limit if group.highest_bidder2 == my_id: player.limit1=player.limit-group.highest_bid2 player.limit2=player.limit 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, limit1=player.limit1, limit2=player.limit2, ) } class WaitToStart(WaitPage): after_all_players_arrive = after_all_players_arrive 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 MyPage(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS return True page_sequence = [WaitToStart, Bidding, ResultsWaitPage, Results, Summary, MyPage]