from otree.api import * import random import csv author = 'Lingguo XU' doc = """ Prolific Pilot--Part 3 """ class Constants(BaseConstants): name_in_url = 'Part3-Prolific' players_per_group = None num_rounds = 1 WTP_endowment = 200 ECUconversion = 100 participationfee = 3 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass def make_field(): return models.IntegerField( choices=[[5, 'Top 10'], [4, 'Rank 11-20'], [3, 'Rank 21-30'], [2, 'Rank 31-40'], [1, 'Rank 41-50'], [0, 'No information']], label="", widget=widgets.RadioSelect, ) def make_field_WTP(): return models.IntegerField(initial=0, min=0, max=200, label="Please fill in a number between 0-200") def make_field_WTP_new(): return models.IntegerField(blank=True, initial=0, min=0, max=200, label="Please fill in a number between 0-200") class Player(BasePlayer): # Measuring self-efficacy efficacy_1 = models.IntegerField(min=0, max=30, label="Please fill in a number between 0 and 30.") efficacy_2 = models.IntegerField(min=0, max=100, label="Please fill in a number between 0 and 100.") #Attentioncheckquestions attncheck1 = models.StringField( blank=True, label="This is to check your attention. Please select the color 'Green'.", initial="", choices=[ 'Red', 'Green', 'Blue', 'Purple', ] ) attncheck1success = models.BooleanField(initial=False) attncheck2 = models.StringField( blank=True, label="This is to check your attention. Please select the color 'Purple'.", initial="", choices=[ 'Red', 'Green', 'Blue', 'Purple', ] ) attncheck2success = models.BooleanField(initial=False) #Rankit3 Pref1 = make_field() Pref2 = make_field() Pref3 = make_field() Pref4 = make_field() Pref5 = make_field() Pref6 = make_field() #WTP_MPL_1-10 WTP1 = make_field_WTP() WTP2 = make_field_WTP() WTP3 = make_field_WTP() WTP4 = make_field_WTP() WTP5 = make_field_WTP() WTP6 = make_field_WTP() WTP1_new = make_field_WTP_new() WTP2_new = make_field_WTP_new() WTP3_new = make_field_WTP_new() WTP4_new = make_field_WTP_new() WTP5_new = make_field_WTP_new() WTP6_new = make_field_WTP_new() # For the order of appearance of intervals when eliciting WTP WTP_order_1 = make_field() WTP_order_2 = make_field() WTP_order_3 = make_field() WTP_order_4 = make_field() WTP_order_5 = make_field() # Check if there is duplicate in WTP for five intervals WTP_duplicate = models.BooleanField() # For the final preference order that is based on WTP WTP_Pref_1 = make_field() WTP_Pref_2 = make_field() WTP_Pref_3 = make_field() WTP_Pref_4 = make_field() WTP_Pref_5 = make_field() WTP_Pref_6 = make_field() #Variables for selecting the one WTP and implementing BDM WTP_X = models.IntegerField() WTP_select = models.IntegerField() WTP_selected = models.IntegerField() payoff_part3 = models.IntegerField() feedback_interval = models.IntegerField() payforfeedback = models.BooleanField() # Receivefeedback = models.BooleanField() # FUNCTIONS def creating_session(subsession): for player in subsession.get_players(): # player.PaymentPart = random.choice([1, 2, 3]) WTP_list = range(0, 200, 10) WTP_choice = range(0, 5) player.WTP_select = random.choice(WTP_choice) player.WTP_X = random.choice(WTP_list) # print('set player.PaymentPart to', player.PaymentPart) print('set player.WTP_X to', player.WTP_X) # PAGES class Part3_0(Page): pass class Rankit_Instructions(Page): form_model = 'player' form_fields = ['attncheck1'] def js_vars(player): return dict( rel_belief=player.participant.relativebelief, ) def before_next_page(player, timeout_happened): if player.attncheck1 == 'Green': player.attncheck1success = True class Pref_1(Page): form_model = 'player' form_fields = ['Pref1'] class Pref_2(Page): form_model = 'player' form_fields = ['Pref2'] def js_vars(player): return dict( Pref1=player.Pref1, ) class Pref_3(Page): form_model = 'player' form_fields = ['Pref3'] def js_vars(player): return dict( Pref1=player.Pref1, Pref2=player.Pref2, ) class Pref_4(Page): form_model = 'player' form_fields = ['Pref4'] def js_vars(player): return dict( Pref1=player.Pref1, Pref2=player.Pref2, Pref3=player.Pref3, ) class Pref_5(Page): form_model = 'player' form_fields = ['Pref5'] def js_vars(player): return dict( Pref1=player.Pref1, Pref2=player.Pref2, Pref3=player.Pref3, Pref4=player.Pref4, ) # Create a random list that will decide the appearance of 5 intervals in WTP elicitation @staticmethod def before_next_page(player, timeout_happened): WTP_order = random.sample(range(1, 6), 5) player.WTP_order_1 = WTP_order[0] player.WTP_order_2 = WTP_order[1] player.WTP_order_3 = WTP_order[2] player.WTP_order_4 = WTP_order[3] player.WTP_order_5 = WTP_order[4] Pref_list = {player.Pref1, player.Pref2, player.Pref3, player.Pref4, player.Pref5} Pref_list_whole = {0, 1, 2, 3, 4, 5, 6} player.Pref6 = list(set(Pref_list_whole) - set(Pref_list))[0] class WTP_Instructions(Page): form_model = 'player' form_fields = ['attncheck2'] @staticmethod def before_next_page(player, timeout_happened): if player.attncheck2 == 'Purple': player.attncheck2success = True class WTP_MPL_1(Page): form_model = 'player' form_fields = ['WTP1'] class WTP_MPL_2(Page): form_model = 'player' form_fields = ['WTP2'] class WTP_MPL_3(Page): form_model = 'player' form_fields = ['WTP3'] class WTP_MPL_4(Page): form_model = 'player' form_fields = ['WTP4'] class WTP_MPL_5(Page): form_model = 'player' form_fields = ['WTP5'] class WTP_Confirm(Page): form_model = 'player' form_fields = ['WTP1_new', 'WTP2_new', 'WTP3_new', 'WTP4_new', 'WTP5_new'] # 'WTP_Pref_1', 'WTP_Pref_2', 'WTP_Pref_3', 'WTP_Pref_4', 'WTP_Pref_5'] def js_vars(player): return dict( PPref1=player.Pref1, PPref2=player.Pref2, PPref3=player.Pref3, PPref4=player.Pref4, PPref5=player.Pref5, WTP1=player.WTP1, WTP2=player.WTP2, WTP3=player.WTP3, WTP4=player.WTP4, WTP5=player.WTP5, ) @staticmethod def before_next_page(player, timeout_happened): # Compute player payoff in Part 3: implementing BDM; # Only pick 1 from the 10 intervals, not the WTP wtp1 = player.WTP1_new wtp2 = player.WTP2_new wtp3 = player.WTP3_new wtp4 = player.WTP4_new wtp5 = player.WTP5_new WTP_list = [wtp1, wtp2, wtp3, wtp4, wtp5] # #WTP_list = [wtp1, wtp2, wtp3, wtp4, wtp5, wtp] # if player.Pref1 == 0: # WTP_list = [wtp2, wtp3, wtp4, wtp5, wtp6] # elif player.Pref2 == 0: # WTP_list = [wtp1, wtp3, wtp4, wtp5, wtp6] # elif player.Pref3 == 0: # WTP_list = [wtp1, wtp2, wtp4, wtp5, wtp6] # elif player.Pref4 == 0: # WTP_list = [wtp1, wtp2, wtp3, wtp5, wtp6] # elif player.Pref5 == 0: # WTP_list = [wtp1, wtp2, wtp3, wtp4, wtp6] # else: # WTP_list = [wtp1, wtp2, wtp3, wtp4, wtp5] # Compute Part 3 payoff, save as participant variable player.WTP_selected = WTP_list[player.WTP_select] if WTP_list[player.WTP_select] == 0: player.payforfeedback = False player.payoff_part3 = Constants.WTP_endowment elif WTP_list[player.WTP_select] != 0 and WTP_list[player.WTP_select] >= player.WTP_X: player.payoff_part3 = Constants.WTP_endowment - player.WTP_X player.payforfeedback = True else: player.payoff_part3 = Constants.WTP_endowment player.payforfeedback = False player.participant.part3payoff = player.payoff_part3 # Get the feedback that was selected feedbacklist = [player.WTP_order_1, player.WTP_order_2, player.WTP_order_3, player.WTP_order_4, player.WTP_order_5] player.feedback_interval = feedbacklist[player.WTP_select] # Compute the final preference order that is based on WTP # If there is multiple intervals with the same order, then the first one to appear will have a higher preference # First check if there is duplicates in WTP_List WTP_list_set = set(WTP_list) player.WTP_duplicate = len(WTP_list_set) != len(WTP_list) player.WTP_Pref_1=feedbacklist[WTP_list.index(max(WTP_list))] WTP_list.remove(max(WTP_list)) feedbacklist.remove(player.WTP_Pref_1) player.WTP_Pref_2=feedbacklist[WTP_list.index(max(WTP_list))] WTP_list.remove(max(WTP_list)) feedbacklist.remove(player.WTP_Pref_2) player.WTP_Pref_3=feedbacklist[WTP_list.index(max(WTP_list))] WTP_list.remove(max(WTP_list)) feedbacklist.remove(player.WTP_Pref_3) player.WTP_Pref_4=feedbacklist[WTP_list.index(max(WTP_list))] WTP_list.remove(max(WTP_list)) feedbacklist.remove(player.WTP_Pref_4) player.WTP_Pref_5=feedbacklist[WTP_list.index(max(WTP_list))] player.WTP_Pref_6 = 0 # #Check if subject will receive feedback using payforfeedback and paymentpart # # This line of code depend on the final payment scheme for Pilot 2(TBD) # if player.PaymentPart == 3 and player.payforfeedback == True: # player.Receivefeedback = True # else: # player.Receivefeedback = False class Efficacy_1(Page): form_model = 'player' form_fields = ['efficacy_1', 'efficacy_2'] class Feedback(Page): pass class FinalPage(Page): pass # page_sequence = [Part3_0, Pref_1, Pref_2, Pref_3, Pref_4, Pref_5, # WTP_Instructions, WTP_MPL_1, WTP_MPL_2, WTP_MPL_3, WTP_MPL_4, WTP_MPL_5, WTP_MPL_6, # WTP_Confirm, FinalPage] #Eessential part for testing page_sequence = [Part3_0, WTP_Instructions, Pref_1, WTP_MPL_1, Pref_2, WTP_MPL_2, Pref_3, WTP_MPL_3, Pref_4, WTP_MPL_4, Pref_5, WTP_MPL_5, WTP_Confirm, Efficacy_1, Feedback]