from otree.api import * import random doc = """ second main task """ class C(BaseConstants): NAME_IN_URL = 'main2' PLAYERS_PER_GROUP = None NUM_ROUNDS = 24 random.seed(34534534) treatment_order = random.sample([1, 2, 3, 4]*int(NUM_ROUNDS/4), NUM_ROUNDS) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): proceed_code = models.IntegerField(label="If you have been told the proceed code, please enter it here: ") bid = models.IntegerField( min=0, max=10, label="How much would you like to bid?" ) rand_draw = models.IntegerField() current_treatment = models.IntegerField() reduced_treatment = models.IntegerField() coord_prob_high = models.IntegerField() coord_prob_low = models.IntegerField() # PAGES class PauseMain2(Page): @staticmethod def is_displayed(player): if player.round_number == 1: return True else: return False form_model = 'player' form_fields = ['proceed_code'] @staticmethod def error_message(player: Player, values): ans = dict(proceed_code=901) errors = {f: 'The code you have entered is incorrect.' for f in ans if values[f] != ans[f]} if errors: return errors class Intro(Page): @staticmethod def is_displayed(player): if player.round_number == 1: return True else: return False class Decision(Page): form_model = 'player' form_fields = ['bid', 'coord_prob_high', 'coord_prob_low'] @staticmethod def vars_for_template(player: Player): treatment_now = C.treatment_order[player.round_number - 1] possible_draws_upper = [] possible_draws_lower = [] for i in player.session.vars['symmetric_round_outcomes']: if player.participant.id_in_session not in i['Players']: # exclude rounds where this player played if i['Treatment'] == treatment_now: possible_draws_upper.append(i) elif i['Treatment'] == treatment_now - 1: possible_draws_lower.append(i) player.participant.upper_t = random.choice(possible_draws_upper) player.participant.lower_t = random.choice(possible_draws_lower) player.current_treatment = treatment_now player.reduced_treatment = player.current_treatment - 1 @staticmethod def before_next_page(player: Player, timeout_happened): player.rand_draw = random.randint(0, 10) if player.round_number == 1: player.participant.main_2_choices = [] player.participant.main_2_outcomes = [] player.participant.main_2_probs = [] class Results(Page): @staticmethod def vars_for_template(player: Player): if player.bid >= player.rand_draw: net_budget = 10 - player.rand_draw actual_round = player.participant.lower_t else: net_budget = 10 actual_round = player.participant.upper_t protesters = 0 for i in range(4): protesters += actual_round['Choices'][i] if protesters == 4: regime = False net = net_budget else: regime = True net = 20 + net_budget # store player's choice player.participant.main_2_choices.append(player.bid) player.participant.main_2_outcomes.append(net) player.participant.main_2_probs.append({'high': player.coord_prob_high, 'low': player.coord_prob_low}) # return variables for template return dict(protesters=protesters, budget=net_budget, net_gain=net, regime=regime) page_sequence = [PauseMain2, Intro, Decision, Results]