from otree.api import * import json import random import itertools import time from random import choice doc = """ """ author = "Zeyu Qiu" class C(BaseConstants): NAME_IN_URL = 'baseline' PLAYERS_PER_GROUP = None NUM_ROUNDS = 20 default_chart_max = 10 chart_padding = 5 class Subsession(BaseSubsession): pass def creating_session(subsession): for p in subsession.get_players(): pressures = itertools.cycle([1, 2]) for player in subsession.get_players(): player.code_treatment = next(pressures) if p.code_treatment == 1: p.treatment = "1" elif p.code_treatment == 2: p.treatment = "2" class Group(BaseGroup): pass class Player(BasePlayer): prediction1 = models.FloatField(min=0, max=500) prediction2 = models.FloatField(min=0, max=500) code_treatment = models.IntegerField() treatment = models.StringField() name = models.StringField( label="1. What is your name?" ) gender = models.StringField( choices=[ ['1', 'Male'], ['2', 'Female'], ['3', 'Other'], ], label="2. What is your gender?", widget=widgets.RadioSelect ) access = models.LongStringField( label="3. Do you think having access to the historical time series in the game will help to improve your forecast? Please briefly explain." ) other_1 = models.LongStringField( label="4. What other information do you think would be helpful for you to improve your forecast?" ) other_2 = models.LongStringField( label="3. What other information do you think would be helpful for you to improve your forecast?" ) class Instructions(Page): def is_displayed(player: Player): return player.round_number == 1 class Information(Page): form_model = 'player' def is_displayed(player: Player): return player.treatment == '1' and player.round_number == 1 def vars_for_template(player: Player): chart_min = -1 chart_max = C.default_chart_max historical_prices = [ 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 4, 4, 4, 4, 5, 4, 5, 5, 3, 3, 4, 3, 4, 5, 6, 6, 3, 1, 0, 0, -1, 2, 2, 2, 2, 2, 3, 4, 4 ] chart_series = [ { 'name': 'Historical inflation rate', 'color': '#FF0000', 'data': historical_prices } ] return { 'chart_series': chart_series, 'chart_min': chart_min, 'chart_max': chart_max } class task(Page): form_model = 'player' def get_form_fields(player: Player): if player.round_number == 20: return ['prediction1'] else: return ['prediction1', 'prediction2'] def js_vars(player): return dict( round=player.round_number ) def vars_for_template(player: Player): if player.treatment == '1': roundData = player.in_previous_rounds() chart_min = -1 chart_max = C.default_chart_max historical_prices = [ 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 4, 4, 4, 4, 5, 4, 5, 5, 3, 3, 4, 3, 4, 5, 6, 6, 3, 1, 0, 0, -1, 2, 2, 2, 2, 2, 3, 4, 4 ] # Adjust the view of historical prices to the last four periods new_price = [ 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 1, 0, 0, 0, 2, 1, 2 ] predictions = [] for x in range(0, len((roundData))): historical_prices.append(new_price[x]) chart_min = min(chart_min, roundData[x].prediction1 ) chart_max = max(chart_max, roundData[x].prediction1 ) predictions.append(roundData[x].prediction1) historical_prices = historical_prices[-4:] predictions = predictions[-4:] if predictions: # Check if predictions list is not empty chart_min = min(chart_min, *predictions) # Use * to unpack the list chart_max = max(chart_max, *predictions) + C.chart_padding chart_series = [ { 'name': 'Historical inflation rate', 'color': '#FF0000', 'data': historical_prices }, { 'name': 'Your Prediction in previous rounds', 'color': '#0000FF', 'data': predictions, 'pointStart': max(0,5-player.round_number) } ] period = player.round_number num_periods = C.NUM_ROUNDS return { 'chart_series': chart_series, 'chart_min': chart_min, 'chart_max': chart_max, 'period': period, 'num_periods': num_periods, } elif player.treatment == '2': roundData = player.in_previous_rounds() chart_min = -1 chart_max = C.default_chart_max historical_prices = [ 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 4, 4, 4, 4, 5, 4, 5, 5, 3, 3, 4, 3, 4, 5, 6, 6, 3, 1, 0, 0, -1, 2, 2, 2, 2, 2, 3, 4, 4 ]+ [None] * 20 new_price = [ 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 1, 0, 0, 0, 2, 1, 2 ] predictions = [] for x in range(0, len((roundData))): historical_prices[40 + x] = new_price[x] chart_min = min(chart_min, roundData[x].prediction1 ) chart_max = max(chart_max, roundData[x].prediction1 ) predictions.append(roundData[x].prediction1) if predictions: # Check if predictions list is not empty chart_min = min(chart_min, *predictions) # Use * to unpack the list chart_max = max(chart_max, *predictions) + C.chart_padding chart_series = [ { 'name': 'Historical inflation rate', 'color': '#FF0000', 'data': historical_prices }, { 'name': 'Your Prediction in previous rounds', 'color': '#0000FF', 'data': predictions, 'pointStart': 40 } ] period = player.round_number num_periods = C.NUM_ROUNDS return { 'chart_series': chart_series, 'chart_min': chart_min, 'chart_max': chart_max, 'period': period, 'num_periods': num_periods, } class Questionnaire(Page): form_model = 'player' def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS def get_form_fields(player: Player): if player.treatment == "1": return ['name', 'gender', 'access', 'other_1'] else: return ['name', 'gender', 'other_2'] class Thanks(Page): def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS page_sequence = [Instructions,Information,task,Questionnaire,Thanks]