from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from otreeutils.surveys import create_player_model_for_survey # Additional libraries import random import json class Constants(BaseConstants): name_in_url = 'Shop' players_per_group = None tasks = ['ES01', 'ES02', 'ES03', 'ES04', 'ES05', 'ES06', 'LS01', 'LS02', 'LS03', 'LS04', ] num_rounds = len(tasks) sale_pages = [1, 2, 3, 4, 5, 6] class Subsession(BaseSubsession): def creating_session(self): for i, p in enumerate(self.get_players()): ''' Each player will be assigned one of six extra shop pages at random. That page will show an ongoing sale! ''' if self.round_number == 1: p.sale_page = random.choice(Constants.sale_pages) else: p.sale_page = p.in_round(1).sale_page ''' Change page sequence. Instead of a single round with all pages, X rounds will be played with each round consisting of a single page. This allows us to control which page to show and randomize it on a per player basis. ''' round_numbers = list(range(1, Constants.num_rounds+1)) random.shuffle(round_numbers) p.participant.vars['round'] = dict(zip(Constants.tasks, round_numbers)) class Group(BaseGroup): pass # define survey questions per page # for each page define a page title and a list of questions # the questions have a field name, a question text (input label), and a field type (model field class) SURVEY_DEFINITIONS = ( ) OTHER_FIELDS = { 'sale_page': models.IntegerField(), 'list_01': models.IntegerField(), 'list_02': models.IntegerField(), 'list_03': models.IntegerField(), 'list_04': models.IntegerField(), 'extra_01': models.IntegerField(), 'extra_02': models.IntegerField(), 'extra_03': models.IntegerField(), 'extra_04': models.IntegerField(), 'extra_05': models.IntegerField(), 'extra_06': models.IntegerField(), } # now dynamically create the Player class from the survey definitions # we can also pass additional (non-survey) fields via `other_fields` Player = create_player_model_for_survey('shop.models', SURVEY_DEFINITIONS, other_fields=OTHER_FIELDS)