from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import logging import json # from otreeutils.pages import ExtendedPage logger = logging.getLogger(__name__) class Decision_red(Page): form_model = 'player' form_fields = ['chose_orange'] # custom_name_in_url = 'f' timeout_seconds = 300 def vars_for_template(self): if self.round_number == 1: if 'page_number' not in self.player.participant.vars: self.player.participant.vars['page_number'] = self.player.round_number player_type = self.player.participant.vars['player_type'] # Open the JSON file in read/write mode with open('app2_red/treatment_index_red.json', 'r+') as reader: data = json.loads(reader.read()) logger.info(f'Original data: {data}') # Get the index values for this player type indexes = data[player_type] # # Read the last treatment # treatment = indexes['treatment'] # And the last start index for that specific treatment treatment = self.player.participant.vars['treatment'] index = indexes[treatment] self.player.participant.vars['paradox_index'] = index # to document the starting position in the data (1st position -> starting_position = 1): self.player.my_starting_position = index + 1 logger.info(f"This player as a red type starts in position: {self.player.my_starting_position}") # Now increment the values and write them back to the file - info for next player, not current one. ########### index += 1 if index == self.player.get_paradox_size(): index = 0 logger.info(f"") # if you want subjects to start always in a specific position, replace above with # (e.g. always first position --> index =0): # index = 0 ########## data[player_type][treatment] = index logger.info(f'Updated data: {data}') reader.seek(0) # Rewind cursor back to the start of the file reader.write(json.dumps(data, indent=2)) else: if self.player.participant.vars['page_number'] != self.player.round_number: self.player.update_paradox_index() self.player.participant.vars['page_number'] = self.player.round_number logger.info(f"This player really is in treatment: {self.player.participant.vars['treatment']}") logger.info(f"Participant: {self.participant.id}, round: {self.round_number}. paradox index: {self.player.participant.vars['paradox_index']}") if self.player.participant.vars['paradox_index'] == 0: self.player.participant.vars['position'] = "1st" self.player.my_position = 1 if self.player.participant.vars['paradox_index'] == 1: self.player.participant.vars['position'] = "2nd" self.player.my_position = 2 if self.player.participant.vars['paradox_index'] == 2: self.player.participant.vars['position'] = "3rd" self.player.my_position = 3 if self.player.participant.vars['paradox_index'] == 3: self.player.participant.vars['position'] = "4th" self.player.my_position = 4 if self.player.participant.vars['paradox_index'] == 4: self.player.participant.vars['position'] = "5th" self.player.my_position = 5 if self.player.participant.vars['paradox_index'] == 5: self.player.participant.vars['position'] = "6th" self.player.my_position = 6 if self.player.participant.vars['paradox_index'] == 6: self.player.participant.vars['position'] = "7th" self.player.my_position = 7 if self.player.participant.vars['paradox_index'] == 7: self.player.participant.vars['position'] = "8th" self.player.my_position = 8 self.player.size = self.player.participant.vars['size'] self.player.blue_balls = self.player.participant.vars['blue_balls'] def before_next_page(self): if self.timeout_happened: self.player.timeout = 1 # participant is excluded from study if time runs out def app_after_this_page(self, upcoming_apps): if self.round_number == self.player.get_paradox_size(): logger.info(f"Moving on to next app; finished as type red") return upcoming_apps[0] class TimeOutNew(Page): # shown if timeout # custom_name_in_url = 'time_out' def is_displayed(self): return self.player.timeout == 1 page_sequence = [Decision_red, TimeOutNew]