from otree.api import * import settings import numpy as np import random import string import json doc = """ This app introduces the experiment """ author = "Mouli Modak" class C(BaseConstants): NAME_IN_URL = 'introduction' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 NUM_TYPES = settings.SESSION_CONFIG_DEFAULTS['num_types'] NUM_PARAMS = settings.SESSION_CONFIG_DEFAULTS['num_parameters'] # PAIR_COMBOS = settings.SESSION_CONFIG_DEFAULTS['pairs_all_combinations'] class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): mySessionID = models.IntegerField() mytype = models.StringField(initial="") ## Subsession method def creating_session(subsession: Subsession): # List of possible player types (A, B, C, ..., Z) types = list(string.ascii_uppercase) # Get the list of all players in the subsession ordered by participant id player_list = sorted(subsession.get_players(), key=lambda p: p.participant.id_in_session) # Num of groups num_groups = len(player_list)/(2*C.NUM_TYPES) # Shuffle list to assign ID shuffled_players = player_list[:] random.shuffle(shuffled_players) for ip, p in enumerate(shuffled_players): p.participant.vars["mySessionID"] = ip+1 p.mySessionID = ip+1 p.participant.vars["treatmentHistory"] = [] p.participant.vars["roundHistory"] = [] p.participant.vars["taskHistory"] = [] p.participant.vars["paramHistory"] = [] p.participant.vars["myGroupID"] = [] p.participant.vars["myIDinGroup"] = [] p.participant.vars["otherID"] = [] p.participant.vars["quantityHistory"] = [] p.participant.vars["typeAPayoffHistory"] = [] p.participant.vars["typeBPayoffHistory"] = [] p.participant.vars["typeCPayoffHistory"] = [] p.participant.vars["appHistory"] = [] p.participant.vars["last_app_round_end"] = 0 temp = int(ip/num_groups) # Assign a type to the player (e.g., A, B, ..., depending on index) p.participant.vars["mytype"] = types[int(temp%C.NUM_TYPES)] p.mytype = p.participant.vars["mytype"] print(p.participant.vars["mySessionID"], temp, p.mytype) # PAGES class P1_Intro(Page): @staticmethod def vars_for_template(player): return dict( mytype = player.mytype, ) class P2_Intro(Page): @staticmethod def vars_for_template(player): return dict( mytype = player.mytype, exp_round = 1, task_number = 2, ) @staticmethod def js_vars(player): if(player.session.config['line_input'] != "None"): equations = json.loads(player.session.config['line_input']) else: equations = player.session.config['line_dict'] return dict( equations=equations, colors=player.session.config['graph_colors'], mytype=player.mytype, num_parameter_sets=len(equations), exp_round = 1, task_number = 2, param_number = 5, ) page_sequence = [P1_Intro, P2_Intro]