from otree.api import * import random doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'tm_ns_b' PLAYERS_PER_GROUP = None NUM_ROUNDS = 20 PAYOFFS = dict( C=(73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 84, 85, 86, 87, 88, 89, 90, 85, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 87, 88, 89, 77, 78, 82, 83, 84, 85, 86, 87, 88, 89, 90, 90, 89, 88, 87, 86), A=(75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 83, 84, 85, 86, 87, 88, 89, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 84, 85, 86, 87), D=(73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 82, 85, 86, 77, 78, 79, 80, 81, 82, 83, 84, 76, 77, 78, 79, 80, 81, 82, 83, 84, 83, 82, 81, 80, 79, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 78, 79, 80, 81, 82, 83, 84, 85, 86, 80, 81), B=(70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 75, 76, 77, 78, 79, 80, 81, 82, 81, 80, 79, 78, 77, 76, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 78, 79), ) RECOMMENDATION_SEQUENCE = [ 'A', 'A', 'C', 'A', 'D', 'C', 'A', 'C', 'A', 'A', 'C', 'D', 'A', 'A', 'C', 'B', 'A', 'C', 'C', 'A', ] class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): first_choice = models.StringField( choices=[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')], label='Which envelope would you like to open?:', ) suggestion = models.StringField() final_choice = models.StringField( choices=[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')], label='What is your final choice?:' ) check_q1 = models.IntegerField( label='I feel confident in the decisions I have made in this game (1: Not at all, 5: Very)', choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) check_q2 = models.IntegerField( label='The recommendations during this game provided useful input for my decisions (1: Strongly Disagree, 5: Strongly Agree)', choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) payoff_round = models.IntegerField() round_time_three = models.FloatField() time_game_three = models.FloatField() game_total_three = models.CurrencyField() active = models.BooleanField() treatment = models.StringField() sequence = models.StringField() stage = models.IntegerField() def get_cumulative_payoff(self): return sum(p.payoff for p in self.in_all_rounds()) def creating_session(subsession): for p in subsession.get_players(): order = p.participant.vars['order'] p.treatment = 'NS' p.stage = 2 p.sequence = order p.active = ( (order == 'B') ) # PAGES class InstructionsTwo(Page): @staticmethod def is_displayed(player): return player.active and player.round_number == 1 class Decision(Page): form_model = 'player' @staticmethod def is_displayed(player): return player.active @staticmethod def get_form_fields(player): fields = ['first_choice', 'final_choice'] if player.round_number == 16: #including trials fields += ['check_q1', 'check_q2'] return fields @staticmethod def vars_for_template(player): from time import time if 'timer_start_three' not in player.participant.vars: player.participant.vars['timer_start_three'] = time() player.participant.vars['round_start_three'] = time() rec = C.RECOMMENDATION_SEQUENCE[player.round_number - 1] return dict( recommended_option=rec ) @staticmethod def before_next_page(player, timeout_happened): dist = C.PAYOFFS[player.final_choice] payoff = random.choice(dist) player.payoff_round = payoff player.payoff = payoff player.suggestion = C.RECOMMENDATION_SEQUENCE[player.round_number - 1] from time import time start = player.participant.vars.get('round_start_three') if start is not None: player.round_time_three = time() - start if player.round_number == C.NUM_ROUNDS: player.time_game_three = ( time() - player.participant.vars['timer_start_three'] ) class Results(Page): @staticmethod def is_displayed(player): return player.active @staticmethod def vars_for_template(player): return { 'round_payoff': player.payoff_round, 'choice': player.final_choice, 'round_number': player.round_number, } class TotalResults(Page): @staticmethod def is_displayed(player): return player.active and player.round_number == C.NUM_ROUNDS @staticmethod def vars_for_template(player): total = sum(p.payoff for p in player.in_all_rounds()) return { 'total_payoff': total } @staticmethod def before_next_page(player, timeout_happened): player.game_total_three = sum(p.payoff for p in player.in_all_rounds()) page_sequence = [InstructionsTwo, Decision, Results, TotalResults]