from otree.api import * import random class C(BaseConstants): NAME_IN_URL = 'DCE_multi_booking' PLAYERS_PER_GROUP = None NUM_ROUNDS = 4 SHOP_OPTIONS = ["Direkt von der Unterkunft", "Mobility Hub (extern)", "Fahrradverleih (extern)"] PAYMENT_OPTIONS = ["Keine Kreditkarte erforderlich", "Kreditkarte erforderlich"] BIKE_COST_OPTIONS = ["3 €", "6 €", "9 €", "12 €"] REGISTRATION_OPTIONS = ["Keine Registrierung/App erforderlich
", "Registrierung an der Rezeption
(Formular ausfüllen + Ausweis vorzeigen)", "Registrierung über App erforderlich
(App herunterladen + Identitätsprüfung)"] RETURN_OPTIONS = ["Flexibel
(24 h)", "Während der Öffnungszeiten
(9.00 - 18.00)"] class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): # Generated by code; allow blank so model never throws required-field errors # Alternative A: shop_a = models.StringField() payment_a = models.StringField() cost_a = models.StringField() registration_a = models.StringField() return_a = models.StringField() # Alternative A: shop_b = models.StringField() payment_b = models.StringField() cost_b = models.StringField() registration_b = models.StringField() return_b = models.StringField() # Participant choice DC_choice = models.IntegerField( label="", blank=True, choices=[ [1, 'Option A'], [2, 'Option B'], [3, 'None'] ]) class DCE_multi_booking(Page): form_model = 'player' form_fields = ['DC_choice'] def is_displayed(player): return player.participant.vars.get('survey_participate') == 1 @staticmethod def vars_for_template(player): app = player.subsession.session.config.get('DCE_multi_booking') key = f"{app}:dce_attrs_round_{player.round_number}" # 1) If not assigned yet for this participant+round: assign now if key not in player.participant.vars: player.participant.vars[key] = dict( shop_a=random.choice(C.SHOP_OPTIONS), payment_a=random.choice(C.PAYMENT_OPTIONS), cost_a=random.choice(C.BIKE_COST_OPTIONS), registration_a=random.choice(C.REGISTRATION_OPTIONS), return_a=random.choice(C.RETURN_OPTIONS), shop_b=random.choice(C.SHOP_OPTIONS), payment_b=random.choice(C.PAYMENT_OPTIONS), cost_b=random.choice(C.BIKE_COST_OPTIONS), registration_b=random.choice(C.REGISTRATION_OPTIONS), return_b=random.choice(C.RETURN_OPTIONS), ) attrs = player.participant.vars[key] # 2) Copy into model fields (stored in DB) player.shop_a = attrs['shop_a'] player.payment_a = attrs['payment_a'] player.cost_a = attrs['cost_a'] player.registration_a = attrs['registration_a'] player.return_a = attrs['return_a'] player.shop_b = attrs['shop_b'] player.payment_b = attrs['payment_b'] player.cost_b = attrs['cost_b'] player.registration_b = attrs['registration_b'] player.return_b = attrs['return_b'] # 3) Provide to template return attrs class DCE_multi_booking_intro(Page): form_model = 'player' def is_displayed(player): return player.participant.vars.get('survey_participate') == 1 and player.round_number == 1 page_sequence = [DCE_multi_booking_intro, DCE_multi_booking]