from otree.api import * import random class C(BaseConstants): NAME_IN_URL = 'G1' PLAYERS_PER_GROUP = None NUM_ROUNDS = 6 CHOICES = ['Finances', 'Family', 'Politics', 'Study'] ONE = 1 TWO = 1 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass def make_rank_field(label): return models.StringField(choices=C.CHOICES, label=label) class Player(BasePlayer): cubicle_number = models.IntegerField(label='',) ##Part 1 buy_sell = models.IntegerField( choices=[[0, 'Short sell'], [1, 'Buy']], label='', widget=widgets.RadioSelect, ) anxiety = models.IntegerField( choices=[[1,'disagree very strongly'], [2, 'disagree strongly'], [3, 'disagree'], [4, 'agree'], [5, 'agree strongly'], [6, 'agree very strongly']], widget=widgets.RadioSelect, label='', ) point_part1 = models.IntegerField() point_part2 = models.IntegerField() point = models.IntegerField() prob_picked = models.IntegerField() L = models.IntegerField() Y = models.FloatField() ##Part 2 color1 = models.StringField() color2 = models.StringField() color3 = models.StringField() color4 = models.StringField() color5 = models.StringField() prob0 = models.IntegerField(min=0, max=100, initial=False, label='') prob_high0 = models.IntegerField(min=0, max=100, initial=False, label='') prob_low0 = models.IntegerField(min=0, max=100, initial=False, label='') prob1 = models.IntegerField(min=0, max=100, initial=False, label='') prob_high1 = models.IntegerField(min=0, max=100, initial=False, label='') prob_low1 = models.IntegerField(min=0, max=100, initial=False, label='') prob2 = models.IntegerField(min=0, max=100, initial=False, label='') prob_high2 = models.IntegerField(min=0, max=100, initial=False, label='') prob_low2 = models.IntegerField(min=0, max=100, initial=False, label='') prob3 = models.IntegerField(min=0, max=100, initial=False, label='') prob_high3 = models.IntegerField(min=0, max=100, initial=False, label='') prob_low3 = models.IntegerField(min=0, max=100, initial=False, label='') prob4 = models.IntegerField(min=0, max=100, initial=False, label='') prob_high4 = models.IntegerField(min=0, max=100, initial=False, label='') prob_low4 = models.IntegerField(min=0, max=100, initial=False, label='') prob5 = models.IntegerField(min=0, max=100, initial=False, label='') prob_high5 = models.IntegerField(min=0, max=100, initial=False, label='') prob_low5 = models.IntegerField(min=0, max=100, initial=False, label='') ##Questionnaire age = models.IntegerField(label='', min=13, max=40) gender = models.StringField( choices=[['Male', 'Male'], ['Female', 'Female'], [0, 'Prefer not to answer']], label='', widget=widgets.RadioSelect, ) ethnicity = models.StringField( choices=[[1, 'European'], [2, 'Non-European'], [3, 'Asian'], [4, 'Prefer not to answer']], label='', widget=widgets.RadioSelect, ) rank1 = make_rank_field("Top choice") rank2 = make_rank_field("Second choice") rank3 = make_rank_field("Third choice") rank4 = make_rank_field("Fourth choice") education1 = models.StringField( choices=[[1, 'Bachelor'], [2, 'Master'], [3, 'Research Master'], [4, 'PhD']], label="", widget=widgets.RadioSelect, ) education2 = models.StringField( choices=[[1, '1st Year'], [2, '2st Year'], [3, '3rd Year'], [4, 'None of the above']], label="", widget=widgets.RadioSelect, ) education3 = models.StringField( choices=[['TiSEM', 'TiSEM'], ['TLS', 'TLS'], ['TSB', 'TSB'], ['TSHD', 'TSHD'], [5, 'School of Catholic Theology']], label="", widget=widgets.RadioSelect, ) work1 = models.StringField( choices=[[1, 'Yes'], [0, 'No']], label='', widget=widgets.RadioSelect, ) work2 = models.FloatField(label='', blank=True) earning = models.StringField( choices=[['Grants', 'Grants'], ['Scholarship', 'Scholarship'], ['Loans', 'Loans'], ['Family support', 'Family support'], ['Self support', 'Self support']], label='', widget=widgets.RadioSelect, ) IBAN = models.LongStringField(label='') IBAN1 = models.LongStringField(label='') # FUNCTIONS # PAGES class CubicleNum(Page): form_model = 'player' form_fields = ['cubicle_number'] class GeneralInstruction(Page): pass class Part1Instruction(Page): pass class Part1Decision(Page): form_model = 'player' form_fields = ['buy_sell'] @staticmethod def before_next_page(player: Player, timeout_happened): if player.buy_sell == 1: player.point_part1 = 200 else: player.point_part1 = 0 class Anxiety(Page): form_model = 'player' form_fields = ['anxiety'] class Part2Instruction(Page): pass class Part2Decision0(Page): form_model = 'player' form_fields = ['prob0'] @staticmethod def js_vars(player: Player): return dict(ONE=C.ONE) @staticmethod def before_next_page(player, timeout_happened): player.color1 = random.choice(['b', 'b', 'b', 'b', 'b', 'r', 'r', 'r']) player.color2 = random.choice(['b', 'b', 'b', 'b', 'b', 'r', 'r', 'r']) player.color3 = random.choice(['b', 'b', 'b', 'b', 'b', 'r', 'r', 'r']) player.color4 = random.choice(['b', 'b', 'b', 'b', 'b', 'r', 'r', 'r']) player.color5 = random.choice(['b', 'b', 'b', 'b', 'b', 'r', 'r', 'r']) if player.buy_sell == 1: player.prob_high0 = player.prob0 player.prob_low0 = 100 - player.prob_high0 else: player.prob_low0 = player.prob0 player.prob_high0 = 100 - player.prob_low0 class Part2Decision1(Page): form_model = 'player' form_fields = ['prob1'] @staticmethod def js_vars(player: Player): return dict(ONE=C.ONE) @staticmethod def before_next_page(player, timeout_happened): if player.buy_sell == 1: player.prob_high1 = player.prob1 player.prob_low1 = 100 - player.prob_high1 else: player.prob_low1 = player.prob1 player.prob_high1 = 100 - player.prob_low1 class Part2Decision2(Page): form_model = 'player' form_fields = ['prob2'] @staticmethod def js_vars(player: Player): return dict(ONE=C.ONE) @staticmethod def before_next_page(player, timeout_happened): if player.buy_sell == 1: player.prob_high2 = player.prob2 player.prob_low2 = 100 - player.prob_high2 else: player.prob_low2 = player.prob2 player.prob_high2 = 100 - player.prob_low2 class Part2Decision3(Page): form_model = 'player' form_fields = ['prob3'] @staticmethod def js_vars(player: Player): return dict(ONE=C.ONE) @staticmethod def before_next_page(player, timeout_happened): if player.buy_sell == 1: player.prob_high3 = player.prob3 player.prob_low3 = 100 - player.prob_high3 else: player.prob_low3 = player.prob3 player.prob_high3 = 100 - player.prob_low3 class Part2Decision4(Page): form_model = 'player' form_fields = ['prob4'] @staticmethod def js_vars(player: Player): return dict(ONE=C.ONE) @staticmethod def before_next_page(player, timeout_happened): player.L = random.randint(1, 100) if player.buy_sell == 1: player.prob_high4 = player.prob4 player.prob_low4 = 100 - player.prob_high4 else: player.prob_low4 = player.prob4 player.prob_high4 = 100 - player.prob_low4 class Part2Decision5(Page): form_model = 'player' form_fields = ['prob5'] @staticmethod def js_vars(player: Player): return dict(ONE=C.ONE) @staticmethod def before_next_page(player, timeout_happened): if player.buy_sell == 1: player.prob_high5 = player.prob5 player.prob_low5 = 100 - player.prob_high5 else: player.prob_low5 = player.prob5 player.prob_high5 = 100 - player.prob_low5 player.prob_picked = random.choice([player.prob_high0, player.prob_high1, player.prob_high2, player.prob_high3, player.prob_high4, player.prob_high5]) if player.prob_picked >= player.L: player.point_part2 = 200 else: # player.point_part2 = 0 all = [0] * (100-player.L) non_zero = [200]*player.L all.append(non_zero) player.point_part2 = random.choice(all) player.point = random.choice([player.point_part1, player.point_part2]) player.payoff = player.point/40 player.Y = (player.point/40)+5 class Payment(Page): pass class Questionnaire(Page): form_model = 'player' form_fields = ['age', 'gender', 'ethnicity', 'rank1', 'rank2', 'rank3', 'rank4', 'work1', 'work2', 'education1', 'education2', 'education3', 'earning', 'IBAN', 'IBAN1'] @staticmethod def error_message(player: Player, values): choices = [values['rank1'], values['rank2'], values['rank3'], values['rank4']] # set() gives you distinct elements. if a list's length is different from its # set length, that means it must have duplicates. if len(set(choices)) != len(choices): return "You cannot choose the same item twice for the major problems in life question" class ThankYou(Page): pass page_sequence = [ CubicleNum, GeneralInstruction, Part1Instruction, Part1Decision, Anxiety, Part2Instruction, Part2Decision0, Part2Decision1, Part2Decision2, Part2Decision3, Part2Decision4, Part2Decision5, Payment, Questionnaire, ThankYou]