from otree.api import Currency as c, currency_range from . import models from ._builtin import Page, WaitPage from .models import Constants import pandas as pd # variables for all templates # -------------------------------------------------------------------------------------------------------------------- def vars_for_all_templates(self): return { 'lottery_a_lo': c(Constants.lottery_a_lo), 'lottery_a_hi': c(Constants.lottery_a_hi), 'lottery_b_lo': c(Constants.lottery_b_lo), 'lottery_b_hi': c(Constants.lottery_b_hi) } # ******************************************************************************************************************** # # *** CLASS INSTRUCTIONS *** # # ******************************************************************************************************************** # class general_study_Instructions(Page): form_model = 'player' form_fields = ['P_ID'] def before_next_page(self): # read payment data # ---------------------------------------------------------------------------------------------------- payment_data = pd.read_csv("mpl_private_mzp_2/bonus_payment_data_private.csv", delimiter=",") payment = payment_data.to_dict(orient="records") try: payment_data = list(filter(lambda d: d['P_ID'] == self.player.P_ID, payment))[0] print(payment_data) self.player.implemented_game = payment_data["implemented_game"] self.player.dg_kept = payment_data["dg_kept"] self.player.condition = payment_data["condition"] self.player.exception_occurred = False #self.player.dg_exit_option = payment_data["dg_exit_option"] #self.player.dg_exit_kept = payment_data["dg_exit_kept"] #self.player.guess = payment_data["guess"] #self.player.expectation_recipient = payment_data["expectation_recipient"] #self.player.correct_guess = payment_data["correct_guess"] except IndexError: self.player.exception_occurred = True class Introduction(Page): pass class Instructions(Page): # only display instruction in round 1 # ---------------------------------------------------------------------------------------------------------------- def is_displayed(self): return self.subsession.round_number == 1 # variables for template # ---------------------------------------------------------------------------------------------------------------- def vars_for_template(self): return { 'num_choices': len(self.participant.vars['mpl_choices']), } def before_next_page(self): pass #print("decision1_kept is of type: ", round(self.participant.vars['dictator_payoff_int'], 0)) # ******************************************************************************************************************** # # *** PAGE DECISION *** # # ******************************************************************************************************************** # class Decision(Page): # form model # ---------------------------------------------------------------------------------------------------------------- form_model = 'player' # form fields # ---------------------------------------------------------------------------------------------------------------- def get_form_fields(self): # unzip list of form_fields from list form_fields = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][1] # provide form field associated with pagination or full list if Constants.one_choice_per_page: page = self.subsession.round_number return [form_fields[page - 1]] else: return form_fields # variables for template # ---------------------------------------------------------------------------------------------------------------- def vars_for_template(self): # specify info for progress bar total = len(self.participant.vars['mpl_choices']) page = self.subsession.round_number progress = page / total * 100 if Constants.one_choice_per_page: return { 'page': page, 'total': total, 'progress': progress, 'choices': [self.player.participant.vars['mpl_choices'][page - 1]] } else: return { 'choices': self.player.participant.vars['mpl_choices'], #'decision1_kept': c(10),#self.participant.vars['dictator_payoff'], #'decision1_gave': c(10)#c(10) - self.participant.vars['dictator_payoff'] } # set player's payoff # ---------------------------------------------------------------------------------------------------------------- def before_next_page(self): # unzip indices and form fields from list round_number = self.subsession.round_number form_fields = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][1] indices = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][0] index = indices[round_number - 1] # if choices are displayed sequentially # ------------------------------------------------------------------------------------------------------------ if Constants.one_choice_per_page: # replace current choice in current_choice = getattr(self.player, form_fields[round_number - 1]) self.participant.vars['mpl_choices_made'][index - 1] = current_choice # if current choice equals index to pay ... if index == self.player.participant.vars['mpl_index_to_pay']: # set payoff self.player.set_payoffs() # after final choice if round_number == Constants.num_choices: # determine consistency self.player.set_consistency() # set switching row self.player.set_switching_row() # if choices are displayed in tabular format # ------------------------------------------------------------------------------------------------------------ if not Constants.one_choice_per_page: # replace choices in for j, choice in zip(indices, form_fields): choice_i = getattr(self.player, choice) self.participant.vars['mpl_choices_made'][j - 1] = choice_i # set payoff self.player.set_payoffs() # determine consistency self.player.set_consistency() # set switching row self.player.set_switching_row() # ******************************************************************************************************************** # # *** PAGE RESULTS *** # # ******************************************************************************************************************** # class Results_A(Page): # skip results until last page # ---------------------------------------------------------------------------------------------------------------- def is_displayed(self): return self.player.option_to_pay == 'A' # variables for template # ---------------------------------------------------------------------------------------------------------------- def vars_for_template(self): # unzip into list of lists choices = [list(t) for t in zip(*self.participant.vars['mpl_choices'])] indices = choices[0] # get index, round, and choice to pay index_to_pay = self.player.participant.vars['mpl_index_to_pay'] round_to_pay = indices.index(index_to_pay) + 1 choice_to_pay = self.participant.vars['mpl_choices'][round_to_pay - 1] if Constants.one_choice_per_page: return { 'choice_to_pay': [choice_to_pay], 'option_to_pay': self.player.in_round(round_to_pay).option_to_pay, 'payoff': self.player.in_round(round_to_pay).payoff, } else: return { 'choice_to_pay': [choice_to_pay], 'option_to_pay': self.player.option_to_pay, 'payoff': self.player.payoff, } class Results_B(Page): # skip results until last page # ---------------------------------------------------------------------------------------------------------------- def is_displayed(self): return self.player.option_to_pay == 'B' # variables for template # ---------------------------------------------------------------------------------------------------------------- def vars_for_template(self): # unzip into list of lists choices = [list(t) for t in zip(*self.participant.vars['mpl_choices'])] indices = choices[0] # get index, round, and choice to pay index_to_pay = self.player.participant.vars['mpl_index_to_pay'] round_to_pay = indices.index(index_to_pay) + 1 choice_to_pay = self.participant.vars['mpl_choices'][round_to_pay - 1] if Constants.one_choice_per_page: return { 'choice_to_pay': [choice_to_pay], 'option_to_pay': self.player.in_round(round_to_pay).option_to_pay, 'payoff': self.player.in_round(round_to_pay).payoff, } else: return { 'choice_to_pay': [choice_to_pay], 'option_to_pay': self.player.option_to_pay, 'payoff': self.player.mpl_private_payoff } class dictator_game(Page): def is_displayed(self): return self.player.option_to_pay == 'A' # set payoff if participant opted in def before_next_page(self): self.player.set_payoffs() form_model = 'player' form_fields = ['offer_dg_bdm_private', 'kept_dg_bdm_private','kept_history_dg_bdm_private'] class Results(Page): def is_displayed(self): return self.player.option_to_pay == 'A' def vars_for_template(self): return { 'kept': self.player.kept_dg_bdm_private, 'offer': 10 - self.player.kept_dg_bdm_private, } class bonus_payment_info(Page): def is_displayed(self): return not self.player.exception_occurred def vars_for_template(self): return { 'dg_recipient': 10 - self.player.dg_kept, 'dg_payoff_pound': 0.5 * self.player.dg_kept, 'dg_exit_exit_payoff_pound': 0.5 * self.player.mpl_private_payoff, 'dg_exit_play_recipient_payoff': int(10 - self.player.mpl_private_payoff), 'dg_exit_play_pound': 0.5 * self.player.mpl_private_payoff, } class P_ID_error(Page): def is_displayed(self): return self.player.exception_occurred class study_end(Page): form_model = 'player' form_fields = ['feedback', 'university'] class completion_site(Page): pass # ******************************************************************************************************************** # # *** PAGE SEQUENCE *** # # ******************************************************************************************************************** # page_sequence = [general_study_Instructions, Decision, Results_A, Results_B, dictator_game, Results, bonus_payment_info, P_ID_error, study_end, completion_site] #if Constants.instructions: # page_sequence.insert(0, Instructions) DON'T SHOW INSTRUCTIONS (CHANGED)!!!!!!!!!! #if Constants.results: # page_sequence.append(Results_A) # page_sequence.append(Results_B)