from ._builtin import Page, WaitPage import time, random from .models import Constants # variables for all templates # -------------------------------------------------------------------------------------------------------------------- def vars_for_all_templates(self): return { 'total_letters': Constants.total_letters, 'd_total': Constants.d_total, 'b_total': Constants.d_total, 'num_rounds': Constants.num_rounds, 'fixed_payoff': Constants.fixed_payoff, 'bonus_payoff': Constants.bonus_payoff } # ******************************************************************************************************************** # # *** Welcome *** # # ******************************************************************************************************************** # class Welcome(Page): # only display Welcome Page in round 1 # ---------------------------------------------------------------------------------------------------------------- def is_displayed(self): return self.round_number == 1 # ******************************************************************************************************************** # # *** Consent *** # # ******************************************************************************************************************** # class Consent(Page): form_model = 'player' form_fields = ['consent', 'submit_noConsent'] # only display Consent Page in round 1 # ---------------------------------------------------------------------------------------------------------------- def is_displayed(self): return self.round_number == 1 def before_next_page(self): self.participant.vars['consent'] = self.player.consent # ******************************************************************************************************************** # # *** Demographics *** # # ******************************************************************************************************************** # class Demographics(Page): form_model = 'player' form_fields = ['gender', 'gender_others', 'age'] # only display Demographics Page in round 1 # ---------------------------------------------------------------------------------------------------------------- def is_displayed(self): return self.round_number == 1 # ******************************************************************************************************************** # # *** Instructions *** # # ******************************************************************************************************************** # class Instructions(Page): # only display instruction in round 1 # ---------------------------------------------------------------------------------------------------------------- def is_displayed(self): return self.round_number == 1 # start measuring time of a trial round # ---------------------------------------------------------------------------------------------------------------- def before_next_page(self): self.participant.vars['expiry_timestamp'] = time.time() + self.player.task_timer # ******************************************************************************************************************** # # *** TASK PAGE *** # # ******************************************************************************************************************** # class TaskTrial(Page): # form model # ---------------------------------------------------------------------------------------------------------------- form_model = 'player' # time text # ---------------------------------------------------------------------------------------------------------------- timer_text = 'Time left to complete as many matrices as possible:' # form fields # ---------------------------------------------------------------------------------------------------------------- def get_form_fields(self): # unzip list of form_fields from list random.shuffle(self.participant.vars['letters_choices']) form_fields = [list(t) for t in zip(*self.participant.vars['letters_choices'])][1] print(form_fields) return form_fields def vars_for_template(self): print(self.player.participant.vars['letters_choices']) return{ 'choices1': self.player.participant.vars['letters_choices'][:][0:9], 'choices2': self.player.participant.vars['letters_choices'][:][10:19], 'choices3': self.player.participant.vars['letters_choices'][:][20:29], 'choices4': self.player.participant.vars['letters_choices'][:][30:39], 'choices5': self.player.participant.vars['letters_choices'][:][40:49], 'choices6': self.player.participant.vars['letters_choices'][:][50:59], 'choices7': self.player.participant.vars['letters_choices'][:][60:69], 'choices8': self.player.participant.vars['letters_choices'][:][70:79], 'round_number': self.subsession.round_number } # set timer for the Trial Task # ---------------------------------------------------------------------------------------------------------------- def get_timeout_seconds(self): return self.participant.vars['expiry_timestamp'] - time.time() def is_displayed(self): return self.participant.vars['expiry_timestamp'] - time.time() > 0 # get the choice made in a given round # ---------------------------------------------------------------------------------------------------------------- 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['letters_choices'])][1] indices = [list(t) for t in zip(*self.participant.vars['letters_choices'])][0] for j, choice in zip(indices, form_fields): choice_i = getattr(self.player, choice) self.participant.vars['letters_choices_made'][j - 1] = choice_i if self.participant.vars['letters_choices_made'] is not None: self.player.count_effort() self.player.check_correctness() if self.player.round_number == 1: self.player.total_correct = self.player.correct elif self.player.round_number > 1: self.player.total_correct = self.player.in_round( self.round_number - 1).total_correct + self.player.in_round(self.round_number).correct class ResultsWaitPage(WaitPage): pass class ResultsTrial(Page): def is_displayed(self): return self.participant.vars['expiry_timestamp'] - time.time() < 0 \ and self.player.participant.vars['endgame'] != 1 def before_next_page(self): self.player.update_endgame() def vars_for_template(self): table_rows = [] for prev_player in self.player.in_all_rounds(): row = { 'round_number': prev_player.round_number, 'd_crossed': round(prev_player.d_crossed), 'b_crossed': round(prev_player.b_crossed), 'is_correct': round(prev_player.is_correct), } table_rows.append(row) self.participant.vars['t1_results'] = table_rows return { 'table_rows': table_rows, 'total_payoff': self.player.total_correct, } page_sequence = [Welcome, Consent, Demographics, Instructions, TaskTrial, ResultsTrial, ]