from otree.api import Currency as c, currency_range from . import models from otreeutils.pages import AllGroupsWaitPage, ExtendedPage, UnderstandingQuestionsPage, APPS_DEBUG from otreeutils.surveys import SurveyPage, setup_survey_pages from ._builtin import Page, WaitPage from .models import Constants import settings import time def vars_for_all_templates(self): pmat = Constants.payoff_matrix[self.player.treatment] quiz_attempts = self.session.config.get('quiz_attempts', 0) payment_currency = self.session.config.get('payment_currency', '$') payment_per_decision = self.session.config['payment_per_decision_UNEQ'] if self.player.treatment == 'UNEQ' else self.session.config['payment_per_decision_EQ'] return { 'pmat': pmat, 'quiz_attempts': quiz_attempts, 'payment_per_decision': payment_per_decision, 'payment_currency':payment_currency, } class StartPage(Page): def vars_for_template(self): return { 'payment_currency': self.session.config.get('payment_currency','$'), 'showup_fee': self.session.config.get('showup_fee',5), 'max_payment': self.session.config['max_payment_UNEQ'] if self.participant.vars['treatment']=='UNEQ' else self.session.config['max_payment_EQ'], } def before_next_page(self): # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) self.participant.vars['experiment_starting_time'] = time.time() class Instructions(Page): def before_next_page(self): # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) self.participant.vars['quiz_starting_time'] = time.time() class SomeUnderstandingQuestions(UnderstandingQuestionsPage): page_title = '' extra_info = Constants.instructions_template quiz_info = Constants.quiz_template set_correct_answers = True # this is the default setting # set_correct_answers = False # do not fill out the correct answers in advance (this is for fast skipping through pages) form_model = 'player' form_field_n_wrong_attempts = 'wrong_attempts' live_method = 'live_attempt' # def get_timeout_seconds(self): # return self.session.config.get('quiz_minutes', 5)*60 def get_questions(self): pmat = Constants.payoff_matrix[self.player.treatment] possible_payoffs = list(set(sum(pmat,[]))) options = [ 'Player 1 chose C, Player 2 chose C', 'Player 1 chose C, Player 2 chose D', 'Player 1 chose D, Player 2 chose C', 'Player 1 chose D, Player 2 chose D', ] questions = [ { 'question': "What was Player 2's payoff, if Player 1 chose D and Player 2 chose C?", 'options': possible_payoffs, 'correct': 0, }, { 'question': "What was the highest possible payoff that Player 2 could get?", 'options': possible_payoffs, 'correct': 10, }, { 'question': "When would the two players receive the highest joint payoff?", 'options': options, 'correct': 'Player 1 chose C, Player 2 chose C', }, { 'question': "When would the two players receive the lowest joint payoff?", 'options': options, 'correct': 'Player 1 chose D, Player 2 chose D', }, { 'question': "After each round, what was the probability that the game continued for another round?", 'options': ['0%','16%','20%','50%','80%','100%'], 'correct': '80%', }, ] return questions def extra_vars_for_template(self): pmat = Constants.payoff_matrix[self.player.treatment] # print('Payoff matrix: ',pmat) return { 'pmat': pmat, } def before_next_page(self): # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) self.player.quiz_time = time.time() - self.participant.vars['quiz_starting_time'] quiz_attempts = self.session.config.get('quiz_attempts', 0) self.player.qualified = (quiz_attempts == 0) or (self.player.wrong_attempts < quiz_attempts) self.participant.vars['qualified'] = self.player.qualified class QuizResults(Page): def before_next_page(self): # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) self.participant.vars['decision_starting_time'] = time.time() def vars_for_template(self): return { 'qualified': self.player.qualified, } def is_displayed(self): return self.player.qualified class BeliefDecisionEQ1(Page): form_model = 'player' form_fields = ['percentage11'] def vars_for_template(self): return { 'payment_currency': getattr(settings, 'REAL_WORLD_CURRENCY_CODE', '$'), } def is_displayed(self): print(self.player,self.player.treatment) if self.player.qualified and ((self.player.treatment == 'EQ-H') or (self.player.treatment == 'EQ-L')): self.participant.vars['decision_starting_time1'] = time.time() return True else: return False def before_next_page(self): # this will be skipped if not displayed self.player.dropout = self.timeout_happened if not self.player.dropout: tolerance = self.session.config.get('guess_tolerence',2) self.player.p1correct1 = abs(self.player.percentage11-Constants.percentages1[self.player.treatment])<=tolerance self.participant.vars['paymentI'] = self.player.p1correct1*self.session.config.get('payment_per_decision_EQ',2) self.player.payoff += self.participant.vars['paymentI'] self.player.p1_time = time.time() - self.participant.vars['decision_starting_time1'] class BeliefDecisionEQ(Page): form_model = 'player' form_fields = ['percentage1'] def vars_for_template(self): return { 'payment_currency': getattr(settings, 'REAL_WORLD_CURRENCY_CODE', '$'), } def is_displayed(self): print(self.player,self.player.treatment) if self.player.qualified and ((self.player.treatment == 'EQ-H') or (self.player.treatment == 'EQ-L')): self.participant.vars['decision_starting_time'] = time.time() return True else: return False def before_next_page(self): # this will be skipped if not displayed self.player.dropout = self.timeout_happened if not self.player.dropout: tolerance = self.session.config.get('guess_tolerence',2) self.player.p1correct = abs(self.player.percentage1-Constants.percentages[self.player.treatment])<=tolerance self.participant.vars['paymentII'] = self.player.p1correct*self.session.config.get('payment_per_decision_EQ',2) self.player.payoff += self.participant.vars['paymentII'] self.player.p_time = time.time() - self.participant.vars['decision_starting_time'] class BeliefDecisionUNEQ1(Page): form_model = 'player' form_fields = ['percentage11','percentage21'] def vars_for_template(self): return { 'payment_currency': getattr(settings, 'REAL_WORLD_CURRENCY_CODE', '$'), } def is_displayed(self): if self.player.qualified and (self.player.treatment == 'UNEQ'): self.participant.vars['decision_starting_time1'] = time.time() return True else: return False def before_next_page(self): self.player.dropout = self.timeout_happened if not self.player.dropout: tolerance = self.session.config.get('guess_tolerence',2) self.player.p1correct1 = abs(self.player.percentage11-Constants.percentages1['UNEQ'][0])<=tolerance self.player.p2correct1 = abs(self.player.percentage21-Constants.percentages1['UNEQ'][1])<=tolerance self.participant.vars['paymentI'] = (self.player.p1correct1*self.session.config.get('payment_per_decision_UNEQ',2) + self.player.p2correct1*self.session.config.get('payment_per_decision_UNEQ',2)) self.player.payoff += self.participant.vars['paymentI'] self.player.p1_time = time.time() - self.participant.vars['decision_starting_time1'] class BeliefDecisionUNEQ(Page): form_model = 'player' form_fields = ['percentage1','percentage2'] def vars_for_template(self): return { 'payment_currency': getattr(settings, 'REAL_WORLD_CURRENCY_CODE', '$'), } def is_displayed(self): if self.player.qualified and (self.player.treatment == 'UNEQ'): self.participant.vars['decision_starting_time'] = time.time() return True else: return False def before_next_page(self): self.player.dropout = self.timeout_happened if not self.player.dropout: tolerance = self.session.config.get('guess_tolerence',2) self.player.p1correct = abs(self.player.percentage1-Constants.percentages['UNEQ'][0])<=tolerance self.player.p2correct = abs(self.player.percentage2-Constants.percentages['UNEQ'][1])<=tolerance self.participant.vars['paymentII'] = (self.player.p1correct*self.session.config.get('payment_per_decision_UNEQ',2) + self.player.p2correct*self.session.config.get('payment_per_decision_UNEQ',2)) self.player.payoff += self.participant.vars['paymentII'] self.player.p_time = time.time() - self.participant.vars['decision_starting_time'] class Reason1(Page): form_model = 'player' form_fields = ['inequality_aversion1', 'other_defection1', 'higher_payoff1'] def is_displayed(self): self.participant.vars['reason1_starting_time'] = time.time() return self.player.firstfirst and self.player.qualified and (self.participant.vars['treatment'] == 'UNEQ') def before_next_page(self): self.player.reason1_time = time.time() - self.participant.vars['reason1_starting_time'] class Reason1g(Page): form_model = 'player' form_fields = ['inequality_aversion1g', 'other_defection1g', 'higher_payoff1g'] def is_displayed(self): self.participant.vars['reason1g_starting_time'] = time.time() return self.player.firstfirst and self.player.qualified and (self.participant.vars['treatment'] == 'UNEQ') def before_next_page(self): self.player.reason1g_time = time.time() - self.participant.vars['reason1g_starting_time'] class Reason(Page): form_model = 'player' form_fields = ['inequality_aversion', 'other_defection', 'higher_payoff'] def is_displayed(self): self.participant.vars['reason_starting_time'] = time.time() return self.player.qualified and (self.participant.vars['treatment'] == 'UNEQ') def before_next_page(self): self.player.reason_time = time.time() - self.participant.vars['reason_starting_time'] def vars_for_template(self): return { 'task_number': 4 if self.player.firstfirst else 3 } class Reasong(Page): form_model = 'player' form_fields = ['inequality_aversiong', 'other_defectiong', 'higher_payoffg'] def is_displayed(self): self.participant.vars['reasong_starting_time'] = time.time() return self.player.qualified and (self.participant.vars['treatment'] == 'UNEQ') def before_next_page(self): self.player.reasong_time = time.time() - self.participant.vars['reasong_starting_time'] def vars_for_template(self): return { 'task_number': 4 if self.player.firstfirst else 3 } class Reason1_copy(Page): form_model = 'player' form_fields = ['inequality_aversion1', 'other_defection1', 'higher_payoff1'] def is_displayed(self): self.participant.vars['reason1_starting_time'] = time.time() return (not self.player.firstfirst) and self.player.qualified and (self.participant.vars['treatment'] == 'UNEQ') def before_next_page(self): self.player.reason1_time = time.time() - self.participant.vars['reason1_starting_time'] class Reason1g_copy(Page): form_model = 'player' form_fields = ['inequality_aversion1g', 'other_defection1g', 'higher_payoff1g'] def is_displayed(self): self.participant.vars['reason1g_starting_time'] = time.time() return (not self.player.firstfirst) and self.player.qualified and (self.participant.vars['treatment'] == 'UNEQ') def before_next_page(self): self.player.reason1g_time = time.time() - self.participant.vars['reason1g_starting_time'] class ResultsWaitPage(WaitPage): template_name = 'belief_decision/ResultsWaitPage.html' def after_all_players_arrive(self): players = [p for p in self.subsession.get_players() if (p.participant.vars['treatment'] == 'UNEQ') & (p.dropout == 0)] ltemp1 = [p.inequality_aversion1 for p in players] inequality_aversion1 = (max(set(ltemp1), key=ltemp1.count)) ltemp1 = [p.other_defection1 for p in players] other_defection1 = (max(set(ltemp1), key=ltemp1.count)) ltemp1 = [p.higher_payoff1 for p in players] higher_payoff1 = (max(set(ltemp1), key=ltemp1.count)) ltemp = [p.inequality_aversion for p in players] inequality_aversion = (max(set(ltemp), key=ltemp.count)) ltemp = [p.other_defection for p in players] other_defection = (max(set(ltemp), key=ltemp.count)) ltemp = [p.higher_payoff for p in players] higher_payoff = (max(set(ltemp), key=ltemp.count)) print(players) print(inequality_aversion, other_defection, higher_payoff) for p in players: p.correct_guesses1 = ((p.inequality_aversion1g == inequality_aversion1) + (p.other_defection1g == other_defection1) + (p.higher_payoff1g == higher_payoff1)) p.correct_guesses = ((p.inequality_aversiong == inequality_aversion) + (p.other_defectiong == other_defection) + (p.higher_payoffg == higher_payoff)) if p.firstfirst: p.participant.vars['paymentIII'] = p.correct_guesses1 * self.session.config.get('payment_per_reason_UNEQ', 1) p.participant.vars['paymentIV'] = p.correct_guesses * self.session.config.get('payment_per_reason_UNEQ', 1) else: p.participant.vars['paymentIV'] = p.correct_guesses1 * self.session.config.get('payment_per_reason_UNEQ', 1) p.participant.vars['paymentIII'] = p.correct_guesses * self.session.config.get('payment_per_reason_UNEQ', 1) p.payoff += p.participant.vars['paymentIII'] + p.participant.vars['paymentIV'] print('ResultsWaitPage', p, p.correct_guesses1, p.participant.vars.get('paymentIII', 0)) print('ResultsWaitPage', p, p.correct_guesses, p.participant.vars.get('paymentIV', 0)) def is_displayed(self): return self.player.qualified and (self.participant.vars['treatment'] == 'UNEQ') def vars_for_template(self): paymentI = self.participant.vars.get('paymentI', 0) paymentII = self.participant.vars.get('paymentII', 0) participation_fee = self.session.config['participation_fee'] return { 'participation_fee': participation_fee, 'paymentI': paymentI, 'paymentII': paymentII, 'real_currency': getattr(settings, 'REAL_WORLD_CURRENCY_CODE', '$'), } class PaymentInfo(Page): def vars_for_template(self): paymentI = self.participant.vars.get('paymentI', 0) paymentII = self.participant.vars.get('paymentII', 0) paymentIII = self.participant.vars.get('paymentIII', 0) paymentIV = self.participant.vars.get('paymentIV', 0) participation_fee = self.session.config['participation_fee'] final_payment = paymentI + paymentII + paymentIV + paymentIV + participation_fee treatment = self.participant.vars.get('treatment', 'EQ-H') p = self.player print('PaymentInfo', p, p.correct_guesses, p.participant.vars.get('paymentIII', 0), paymentIII) return { 'qualified': p.qualified, 'participation_fee': participation_fee, 'paymentI': paymentI, 'paymentII': paymentII, 'paymentIII': paymentIII, 'paymentIV': paymentIV, 'real_currency': getattr(settings, 'REAL_WORLD_CURRENCY_CODE', '$'), 'final_payment': final_payment, 'treatment': treatment, } page_sequence = [ StartPage, Instructions, SomeUnderstandingQuestions, QuizResults, BeliefDecisionEQ1, BeliefDecisionEQ, BeliefDecisionUNEQ1, BeliefDecisionUNEQ, Reason1, Reason1g, Reason, Reasong, Reason1_copy, Reason1g_copy, ResultsWaitPage, PaymentInfo ]