from otree.api import * import random doc = """ This app presents the task. """ class C(BaseConstants): NAME_IN_URL = 'task' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 TIMEOUT_SECONDS = 180 # Pool options Y_IE = ["to_A", "to_B", "to_C", "to_D"] N_IE = ["to_A", "to_B", "to_C"] class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): to_A = models.IntegerField(label="Contribution to Pool A:", blank=True, min=0, max=10) to_B = models.IntegerField(label="Contribution to Pool B:", blank=True, min=0, max=10) to_C = models.IntegerField(label="Contribution to Pool C:", blank=True, min=0, max=10) # Always set to_D to 0 in n_ie condition to_D = models.IntegerField(label="Contribution to Pool D:", blank=True, min=0, max=10) # FUNCTIONS def creating_session(subsession: Subsession): for p in subsession.get_players(): p.participant.receive_bonus = False # PAGES class GroupingResult(Page): timeout_seconds = C.TIMEOUT_SECONDS @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.participant.over_time = True @staticmethod def app_after_this_page(player: Player, upcoming_apps): if player.participant.over_time: return upcoming_apps[-1] class MakeDecisions(Page): timeout_seconds = C.TIMEOUT_SECONDS form_model = 'player' @staticmethod def get_form_fields(player: Player): return C.Y_IE if player.participant.condition == "y_ie" else C.N_IE @staticmethod def error_message(player: Player, values): fields = C.Y_IE if player.participant.condition == "y_ie" else C.N_IE total_contribution = sum([values[field] for field in fields if values[field] is not None]) if total_contribution != player.session.token_per_round: return 'Please make sure that your contributions add up to {}.'.format(player.session.token_per_round) @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.participant.over_time = True for field in C.Y_IE: if player.field_maybe_none(field) is None: setattr(player, field, 0) class Decisions(Page): timeout_seconds = C.TIMEOUT_SECONDS @staticmethod def before_next_page(player: Player, timeout_happened): if random.choice(list(range(player.session.lottery))) == 0: player.participant.receive_bonus = True page_sequence = [ GroupingResult, MakeDecisions, Decisions ]