from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random class ParticipantInfo(Page): def is_displayed(self): return self.round_number == 1 class ParticipantConsent(Page): def is_displayed(self): return self.round_number == 1 form_model = "player" form_fields = ["consent"] class DemographicData(Page): def is_displayed(self): return self.round_number == 1 form_model = "player" form_fields = ["age", "gender", "ethnicity"] class Intro(Page): """ Show the player the rules, and allow them to start the task """ def is_displayed(self): # Only show this on the first round # Need to show only to effort group participants # (and to add IntroLuckGroup IntroMixedGroup page) up here and in page sequence return self.round_number == 1 def before_next_page(self): # Set participant score in the game to zero self.player.participant.vars['effort_score'] = 0 # Start the timer import time # Set each participant's "expiry" time, when the task timer runs out self.player.participant.vars['expiry'] = time.time() + Constants.task_timer class AddNumbers(Page): """ The player has to add some numbers together using mental arithmetic If they get it correct, they get a point They can keep playing until the countdown timer runs out """ form_model = "player" form_fields = ["number_entered"] def vars_for_template(self): number_1 = random.randint(1,100) number_2 = random.randint(1,100) number_3 = random.randint(1,100) number_4 = random.randint(1,100) number_5 = random.randint(1,100) self.player.sum_of_numbers = number_1 + number_2 + number_3 + number_4 + number_5 return{ "number_1": number_1, "number_2": number_2, "number_3": number_3, "number_4": number_4, "number_5": number_5, "current_score": self.player.participant.vars['effort_score'] } def before_next_page(self): # Give the participant a point if they get it correct if self.player.sum_of_numbers == self.player.number_entered: self.player.participant.vars['effort_score'] += Constants.marks_per_correct_answer timer_text = "Total time left" # Handles the timer def get_timeout_seconds(self): import time # Check how long the participant has left return self.player.participant.vars['expiry'] - time.time() # Stop showing this page when the timer runs out (or is about to run out) def is_displayed(self): return self.get_timeout_seconds() > 3 class Results(Page): """ Show the results of the effort task after the player has finished """ # Only show this on the last round def is_displayed(self): return self.round_number == Constants.num_rounds def vars_for_template(self): return{ "current_score": self.player.participant.vars['effort_score'] } page_sequence = [ParticipantInfo, ParticipantConsent, DemographicData, Intro, AddNumbers, Results]