import json from otree.api import * import time doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'grouping_wait_page' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Subsession(BaseSubsession): pass def creating_session(subsession): if subsession.round_number == 1: import csv mini = False if mini: path = '_static/scenarios_100_mini.csv' else: path = '_static/scenarios_100_browser_testn24d6.csv' with open(path, encoding='utf-8') as f: reader = csv.DictReader(f) scenarios_to_play = [] scenarios_from_config = json.loads(subsession.session.config['scenarios']) print(f"Scenarios aus der Config: {scenarios_from_config}") for row in reader: row['status'] = 'available' if int(row['scenario_id']) in scenarios_from_config: scenarios_to_play.append(row) for sc in scenarios_to_play: print("Scenario read in from Scenario Csv:") print(sc['scenario_id']) print(sc['clustering']) print(sc['seeding']) subsession.session.vars['scenario_pool'] = scenarios_to_play class Group(BaseGroup): pass class Player(BasePlayer): pass def group_by_arrival_time_method(subsession, waiting_players): target_size = subsession.session.config['group_size'] if len(waiting_players) >= target_size: import json pool = subsession.session.vars['scenario_pool'] selected_scenario = None for s in pool: if s['status'] == 'available': selected_scenario = s s['status'] = 'active' break if selected_scenario: group_players = waiting_players[:target_size] # Convert JSON strings from the CSV back into Python lists/dictionaries human_nodes = json.loads(selected_scenario['human_nodes']) network_map = json.loads(selected_scenario['node_to_neighbours']) centralities = json.loads(selected_scenario['centralities']) all_dists_to_bot = json.loads(selected_scenario['all_dists_to_bot']) # Shuffle so that the roles are assigned to people at random import random random.shuffle(human_nodes) current_time = time.time() node_map = {} for i, p in enumerate(group_players): node_id = human_nodes[i] # treatment variables p.participant.node_id = node_id p.participant.neighbour_node_ids = json.dumps(network_map[str(node_id)]) p.participant.scenario_id = selected_scenario['scenario_id'] p.participant.network_map = selected_scenario['node_to_neighbours'] p.participant.bot_nodes = selected_scenario['bot_nodes'] p.participant.clustering = selected_scenario['clustering'] p.participant.seeding = selected_scenario['seeding'] # structural variables for key, values_map in centralities.items(): single_val = values_map[str(node_id)] setattr(p.participant, key, single_val) p.participant.dist_to_bots = json.dumps(all_dists_to_bot[str(node_id)]) # game variables p.participant.is_finished = False p.participant.in_phase_2 = False p.participant.phase_1_end_round = None p.participant.experiment_end_round = None p.participant.established_option = None p.participant.social_tipping_success = False p.participant.timeout_strikes = 0 # {node_id: participant} node_map[str(node_id)] = p.id_in_subsession if random.random() < 0.5: p.participant.symbol_for_one = "#" p.participant.symbol_for_zero = "@" else: p.participant.symbol_for_one = "@" p.participant.symbol_for_zero = "#" # tracking start og phase 1 p.participant.p1_start_time = current_time # saves the group map to the session variable dict group_maps { group_key: node_map } if 'group_maps' not in subsession.session.vars: subsession.session.vars['group_maps'] = {} group_key = group_players[0].participant.code subsession.session.vars['group_maps'][group_key] = node_map return group_players # PAGES class GlobalWaitPage(WaitPage): group_by_arrival_time = True page_sequence = [GlobalWaitPage]