from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'machine_change' PLAYERS_PER_GROUP = None NUM_ROUNDS = 9 SCENARIO_MEANS = (400, 400, 400, 500, 500, 500, 600, 600, 600) SCENARIO_SDS = (100, 200, 300, 100, 200, 300, 100, 200, 300) SCENARIO_IMAGES = ('mean_down_sd_down.png', 'mean_down_sd_same.png', 'mean_down_sd_up.png', 'mean_same_sd_down.png', 'mean_same_sd_same.png', 'mean_same_sd_up.png', 'mean_up_sd_down.png', 'mean_up_sd_same.png', 'mean_up_sd_up.png') REALIZED_DEMAND = (382, 615, 455, 470, 560, 290, 640, 515, 735) class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): import random for player in subsession.get_players(): if subsession.round_number == 1: scenario_order = list(range(1, C.NUM_ROUNDS + 1)) random.shuffle(scenario_order) player.participant.vars['machine_scenario_order'] = scenario_order class Group(BaseGroup): pass class Player(BasePlayer): machine_choice = models.StringField(choices=[['Keep current quantity', 'Keep current quantity'], ['Change quantity', 'Change quantity']], widget=widgets.RadioSelect) machine_new_qty = models.IntegerField(blank=True, label='How much would you like to order? ', max=1000, min=0) scenario_id = models.IntegerField() updated_mean = models.IntegerField() updated_sd = models.IntegerField() final_quantity = models.IntegerField() Student_status = models.BooleanField(blank=True, choices=[[True, 'Yes'], [False, 'No']], label='Are you currently a graduate student studying supply chain management or another related field? ') gender = models.StringField(blank=True, label='If comfortable, please provide your gender below:') Age = models.StringField(blank=True, label='If comfortable, please provide your age below: ') Consent = models.BooleanField(choices=[[True, 'Yes'], [False, 'No']], label='Do you consent to participating in this study? ') class Intro(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Decision(Page): form_model = 'player' form_fields = ['machine_choice', 'machine_new_qty'] @staticmethod def vars_for_template(player: Player): scenario_order = player.participant.vars['machine_scenario_order'] scenario_id = scenario_order[player.round_number - 1] idx = scenario_id - 1 return dict( product_number=player.round_number, scenario_id=scenario_id, updated_mean=C.SCENARIO_MEANS[idx], updated_sd=C.SCENARIO_SDS[idx], scenario_image_path='Change_New/' + C.SCENARIO_IMAGES[idx], ) @staticmethod def before_next_page(player: Player, timeout_happened): scenario_order = player.participant.vars['machine_scenario_order'] scenario_id = scenario_order[player.round_number - 1] idx = scenario_id - 1 player.scenario_id = scenario_id player.updated_mean = C.SCENARIO_MEANS[idx] player.updated_sd = C.SCENARIO_SDS[idx] if player.machine_choice == "Keep current quantity": player.final_quantity = 700 else: player.final_quantity = player.machine_new_qty if player.round_number == C.NUM_ROUNDS: machine_results = [] for p in player.in_all_rounds(): machine_results.append({ "scenario_id": p.scenario_id, "final_quantity": p.final_quantity, "updated_mean": p.updated_mean, "updated_sd": p.updated_sd, }) player.participant.vars['machine_results'] = machine_results @staticmethod def error_message(player: Player, values): if values["machine_choice"] == "Change quantity" and values["machine_new_qty"] is None: return "Please enter a new quantity if you choose to change the current order." if values["machine_choice"] == "Keep current quantity" and values["machine_new_qty"] is not None: return "Please leave the new quantity box blank if you choose to keep the current order quantity." page_sequence = [Intro, Decision]