from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'quiz' PLAYERS_PER_GROUP = None NUM_ROUNDS = 4 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session import random if subsession.round_number == 1: for player in subsession.get_players(): participant = player.participant participant.control = random.choice([True, False, False, False, False]) class Group(BaseGroup): pass class Player(BasePlayer): info_source1 = models.StringField(choices=[['High', 'High'], ['Low', 'Low']], label='Please choose one information source. You will then get information from it.') info_source2 = models.StringField(choices=[['High', 'High'], ['Low', 'Low']], label='Please choose one information source. You will then get information from it.') dice_list1 = models.StringField(initial='') dice_list2 = models.StringField(initial='') guess1 = models.FloatField(label='Please guess the average of all six dices:') guess2 = models.FloatField(label='Please guess the average of all six dices:') control = models.BooleanField() def random_init(player: Player): participant = player.participant import random signal_space = [50, 70, 90, 110, 130, 150] dices_12 = [] for i in range(12): dices_12.append(random.choice(signal_space)) if player.dice_list1 == "": player.dice_list1 = ",".join(map(str, dices_12[:6])) if player.dice_list2 == "": player.dice_list2 = ",".join(map(str, dices_12[6:])) # to facilitate export player.control = participant.control class Stage1(Page): form_model = 'player' form_fields = ['info_source1', 'info_source2'] @staticmethod def vars_for_template(player: Player): random_init(player) dice_list1 = [int(d) for d in player.dice_list1.split(",")] dice_list2 = [int(d) for d in player.dice_list2.split(",")] return dict( first_dice1 = dice_list1[0], first_dice2 = dice_list2[0] ) class Stage2(Page): form_model = 'player' form_fields = ['guess1', 'guess2'] @staticmethod def vars_for_template(player: Player): dice_list1 = [int(d) for d in player.dice_list1.split(",")] dice_list2 = [int(d) for d in player.dice_list2.split(",")] high1 = list(filter(lambda x : x > 100, dice_list1[1:])) high2 = list(filter(lambda x : x > 100, dice_list2[1:])) low1 = list(filter(lambda x : x < 100, dice_list1[1:])) low2 = list(filter(lambda x : x < 100, dice_list2[1:])) next_three1 = (high1 + low1)[:3] if player.info_source1 == "High" else (low1 + high1)[:3] next_three2 = (high2 + low2)[:3] if player.info_source2 == "High" else (low2 + high2)[:3] return dict( first_dice1 = dice_list1[0], first_dice2 = dice_list2[0], next_three1 = next_three1, next_three2 = next_three2, graphics = player.round_number == 2 or player.round_number == 3 ) @staticmethod def js_vars(player: Player): dice_list1 = [int(d) for d in player.dice_list1.split(",")] dice_list2 = [int(d) for d in player.dice_list2.split(",")] high1 = list(filter(lambda x : x > 100, dice_list1[1:])) high2 = list(filter(lambda x : x > 100, dice_list2[1:])) low1 = list(filter(lambda x : x < 100, dice_list1[1:])) low2 = list(filter(lambda x : x < 100, dice_list2[1:])) next_three1 = (high1 + low1)[:3] if player.info_source1 == "High" else (low1 + high1)[:3] next_three2 = (high2 + low2)[:3] if player.info_source2 == "High" else (low2 + high2)[:3] return dict( data1 = [dice_list1[0]] + next_three1, data2 = [dice_list2[0]] + next_three2 ) @staticmethod def before_next_page(player: Player, timeout_happened): import random dice_list1 = [int(d) for d in player.dice_list1.split(",")] dice_list2 = [int(d) for d in player.dice_list2.split(",")] true_state1 = sum(dice_list1) / 6.0 true_state2 = sum(dice_list2) / 6.0 payoff1 = max(18 - 0.2*(true_state1-player.guess1)**2, 0) payoff2 = max(18 - 0.2*(true_state2-player.guess2)**2, 0) player.payoff = random.choice([payoff1, payoff2]) page_sequence = [Stage1, Stage2]