from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import logging import json logger = logging.getLogger(__name__) class Page1(Page): form_model = 'player' form_fields = ['my_choice'] def before_next_page(self): # Store the data so we can access it in app2 self.player.participant.vars['choice'] = self.player.my_choice # Seed the session with the first players index of 0 if 'player_type' not in self.session.vars: self.player.participant.vars['player_type'] = self.player.my_choice player_type = self.player.participant.vars['player_type'] # Open the JSON file in read/write mode with open('app1/treatment_index.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 # index = indexes[treatment] index = 0 self.player.participant.vars['paradox_index'] = index self.player.participant.vars['treatment'] = treatment # Now increment the values and write them back to the file treatment_index = Constants.treatments.index(treatment) + 1 if treatment_index == len(Constants.treatments): treatment_index = 0 # LIA ADDED: after sub-sample for a certain SVO type * treatment is full, skip it # count manually and then remove comment out manually # ** skip treatment "a" in SVO type Pro-Social: # if treatment_index == 0 and player_type == "Pro-Social": # treatment_index = 1 # ** skip treatment "b" in SVO type Pro-Social: # if treatment_index == 1 and player_type == "Pro-Social": # treatment_index = 2 # ** skip treatment "c" in SVO type Pro-Social: # if treatment_index == 2 and player_type == "Pro-Social": # treatment_index = 3 # ** skip treatment "d" in SVO type Pro-Social: # if treatment_index == 3 and player_type == "Pro-Social": # treatment_index = 0 data[player_type]['treatment'] = Constants.treatments[treatment_index] index += 1 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)) class Results(Page): pass page_sequence = [Page1, Results]