from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class PEI(Page): form_model = 'player' form_fields = ['estimate1', 'prob_top50_1', 'read_scoringrule1', 'click_time1'] def before_next_page(self): player = self.player player.final_score = player.participant.vars['score'] # if 'read_scoringrule1' in player.participant.vars: # if player.participant.vars['read_scoringrule1'] == 'True': # player.read_scoringrule1 = True # else: # print("Found nothing in participant vars.") if 'forgot_slider' in player.participant.vars: if player.participant.vars['forgot_slider'] == True: print("Player forgot to use the slider.") player.has_read_scoringrule1 = player.participant.vars['read_scoringrule1'] if player.has_read_scoringrule1: player.read_scoringrule1 = True player.participant.vars['forgot_slider'] = False # Reset to false before the next slider def error_message(self, value): return self.player.set_error_message_slider(value) class Pairing(Page): def vars_for_template(self): x = 100 - Constants.percentile return { 'x': x } class MatchingProcess(Page): form_model = 'player' timeout_seconds = 4 def before_next_page(self): # Randomly pick counterpart such that there is 60% prob that counterpart is better. # print("Choosing other participant.") # import random # import statistics # player = self.player # score = player.final_score # oss = Constants.old_session_scores # better = [num for num in oss if num >= score] # worse = [num for num in oss if num < score] # if len(better) == 0: # other = random.choice(worse) # elif len(worse) == 0: # other = random.choice(better) # else: # x = random.uniform(0, 1) # if x <= 0.6: # other = random.choice(better) # else: # other = random.choice(worse) # print("Counterpart score:", other) # player.score_other = other # if statistics.median(oss) < other: # print("Counterpart is in top 50% :)") # player.top50_other = True # else: # print("Counterpart is not in top 50% :(") # player.top50_other = False # Randomly pick counterpart from the top X% of prior participants print("Choosing other participant.") import random import numpy import statistics player = self.player percentile = Constants.percentile oss = Constants.old_session_scores p = numpy.percentile(oss, percentile) # compute Xth percentile of previous scores topX = [num for num in oss if num >= p] other = random.choice(topX) print("Counterpart score:", other) player.score_other = other if statistics.median(oss) < other: print("Counterpart is in top 50% :)") player.top50_other = True else: print("Counterpart is not in top 50% :(") player.top50_other = False class Matched(Page): form_model = 'player' def vars_for_template(self): player = self.player treatment = player.treatment estimate1 = player.estimate1 return { 'treatment': treatment, 'estimate1': estimate1, } class Matched2(Page): form_model = 'player' def vars_for_template(self): player = self.player treatment = player.treatment estimate1 = player.estimate1 return { 'treatment': treatment, 'estimate1': estimate1, } class PEII(Page): form_model = 'player' if Constants.percentile < 50: form_fields = ['estimate_other', 'prob_top50_other', 'read_scoringrule2', 'click_time2', 'estimate2', 'prob_top50_2', 'read_scoringrule3', 'click_time3' ] else: form_fields = ['estimate_other', 'click_time2', 'estimate2', 'prob_top50_2', 'read_scoringrule3', 'click_time3' ] # def before_next_page(self): # player = self.player # if 'forgot_slider' in player.participant.vars: # if player.participant.vars['forgot_slider'] == True: # print("Player forgot to use the slider.") # player.has_read_scoringrule2 = player.participant.vars['read_scoringrule2'] # if player.has_read_scoringrule2: # player.read_scoringrule2 = True # player.participant.vars['forgot_slider'] = False # Reset to false before the next slider # def before_next_page(self): player = self.player if 'forgot_slider' in player.participant.vars: if player.participant.vars['forgot_slider'] == True: print("Player forgot to use the slider.") player.has_read_scoringrule3 = player.participant.vars['read_scoringrule3'] if player.has_read_scoringrule3: player.read_scoringrule3 = True player.participant.vars['forgot_slider'] = False # Reset to false before the next slider def error_message(self, value): return self.player.set_error_message_slider(value) def vars_for_template(self): player = self.player estimate1 = player.estimate1 show_other_slider = Constants.percentile < 50 return { 'estimate1': estimate1, 'show_other_slider': show_other_slider, } # class PEIII(Page): # form_model = 'player' # form_fields = ['estimate2', # 'prob_top50_2', # 'read_scoringrule3', # 'click_time3'] # # def before_next_page(self): # player = self.player # if 'forgot_slider' in player.participant.vars: # if player.participant.vars['forgot_slider'] == True: # print("Player forgot to use the slider.") # player.has_read_scoringrule3 = player.participant.vars['read_scoringrule3'] # if player.has_read_scoringrule3: # player.read_scoringrule3 = True # player.participant.vars['forgot_slider'] = False # Reset to false before the next slider # # def error_message(self, value): # return self.player.set_error_message_slider(value) class Instructions(Page): form_model = 'player' form_fields = ['choice'] def before_next_page(self): player = self.player if player.choice == 1: player.delegate = True else: player.delegate = False # Dertermine bonus payment: import random payoff_question = player.payoff_question score = player.final_score score_other = player.score_other prob_top50_1 = player.prob_top50_1 prob_top50_other = player.prob_top50_other prob_top50_2 = player.prob_top50_2 estimate1 = player.estimate1 estimate_other = player.estimate_other if payoff_question == 1: t = score * 0.05 x = random.uniform(0, 1) if x <= t: player.payoff = 1 print("Lucky.") else: print("Unlucky.") elif payoff_question == 2: if estimate1 == score: player.payoff = 0.5 print("Good guess.") else: print("Bad guess.") elif payoff_question == 3: # Karni scoring rule x = int(round(random.uniform(0, 100))) player.threshold = x if x <= prob_top50_1: player.determine_payoff_later = True print("Player's payoff needs to be determined later, when all data is available.") else: if x <= random.uniform(0, 100): print("Lucky!") player.payoff = 0.5 else: print("Unlucky!") elif payoff_question == 4: if estimate_other == player.score_other: player.payoff = 0.5 print("Good guess.") else: print("Bad guess.") elif payoff_question == 5: x = int(round(random.uniform(0, 100))) player.threshold = x if x <= prob_top50_other: player.determine_payoff_later = True print("Player's payoff needs to be determined later, when all data is available.") else: if x <= random.uniform(0, 100): print("Lucky!") player.payoff = 0.5 else: print("Unlucky!") elif payoff_question == 6: if player.estimate2 == score: player.payoff = 0.5 print("Good guess.") else: print("Bad guess.") elif payoff_question == 7: x = int(round(random.uniform(0, 100))) player.threshold = x if x <= prob_top50_2: player.determine_payoff_later = True print("Player's payoff needs to be determined later, when all data is available.") else: if x <= random.uniform(0, 100): print("Lucky!") player.payoff = 0.5 else: print("Unlucky!") elif payoff_question == 8: if player.delegate: t = score_other * 0.05 else: t = score * 0.05 x = random.uniform(0, 1) if x <= t: player.payoff = 1 print("Lucky.") else: print("Unlucky.") # elif payoff_question in [3, 5, 7]: # player.determine_payoff_later = True # print("Player's payoff needs to be determined later.") class OverallResults(Page): form_model = 'player' def vars_for_template(self): player = self.player determine_payoff_later = player.determine_payoff_later total = player.participant.payoff_plus_participation_fee() bonus = player.payoff payoff_question = player.payoff_question return { 'determine_payoff_later': determine_payoff_later, 'total': total, 'bonus': bonus, 'payoff_question': payoff_question } class Questionnaire(Page): form_model = 'player' form_fields = [ 'q1', 'q16', 'q17', 'q18', 'q19', 'q20', 'q21', 'q22', 'q23', 'q24', 'q25', 'q26', 'q2', 'q27', 'q15', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q10', 'q11', 'q12', 'q13', 'q14', ] class ThankYou(Page): pass page_sequence = [PEI, Pairing, MatchingProcess, Matched, Matched2, PEII, # PEIII, Instructions, Questionnaire, OverallResults, ThankYou, ]