from otree.api import * c = Currency doc = """ Your app description """ def read_csv(): import csv f = open(__name__ + '/MarketData.csv') rows = list(csv.DictReader(f)) return rows class Constants(BaseConstants): name_in_url = 'Game' players_per_group = None num_rounds = 20 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): if subsession.round_number == 1: import random for p in subsession.get_players(): participant = p.participant data = read_csv() random_id = random.randint(1, 175) filtered_data = [row for row in data if row["ID"] == str(random_id)] participant.algorithm = not bool(int(filtered_data[1]['Human'])) participant.historical = bool(int(filtered_data[1]['Historical'])) participant.betray = bool(int(filtered_data[1]['Betray'])) p.algorithm = participant.algorithm p.historical = participant.historical p.betray = participant.betray for j in range(1, Constants.num_rounds+1): exec(f"participant.ac{j} = float(filtered_data[{j-1}]['AssetChange'])") exec(f"participant.sa{j} = float(filtered_data[{j-1}]['SuggAmount'])") exec(f"participant.ap{j} = float(filtered_data[{j-1}]['AssetPrice'])") else: for p in subsession.get_players(): participant = p.participant p.algorithm = participant.algorithm p.historical = participant.historical p.betray = participant.betray class Group(BaseGroup): pass class Player(BasePlayer): invest_amount = models.FloatField() sugg_amount = models.FloatField() asset_change = models.FloatField() asset_price = models.FloatField() invest_return = models.FloatField() starting_endow = models.FloatField() ending_endow = models.FloatField() c_return = models.FloatField() pos = models.BooleanField() asset_dict = models.FloatField() algorithm = models.BooleanField() historical = models.BooleanField() title_text = models.StringField() body_text = models.StringField() decision = models.BooleanField() betray = models.BooleanField() # r1 = models.FloatField() # r2 = models.FloatField() # r3 = models.FloatField() # r4 = models.FloatField() # r5 = models.FloatField() # r6 = models.FloatField() # r7 = models.FloatField() # r8 = models.FloatField() # r9 = models.FloatField() # r10 = models.FloatField() # r11 = models.FloatField() # r12 = models.FloatField() # r13 = models.FloatField() # r14 = models.FloatField() # r15 = models.FloatField() # r16 = models.FloatField() # r17 = models.FloatField() # r18 = models.FloatField() # r19 = models.FloatField() # r20 = models.FloatField() # s1 = models.FloatField() # s2 = models.FloatField() # s3 = models.FloatField() # s4 = models.FloatField() # s5 = models.FloatField() # s6 = models.FloatField() # s7 = models.FloatField() # s8 = models.FloatField() # s9 = models.FloatField() # s10 = models.FloatField() # s11 = models.FloatField() # s12 = models.FloatField() # s13 = models.FloatField() # s14 = models.FloatField() # s15 = models.FloatField() # s16 = models.FloatField() # s17 = models.FloatField() # s18 = models.FloatField() # s19 = models.FloatField() # s20 = models.FloatField() # p1 = models.FloatField() # p2 = models.FloatField() # p3 = models.FloatField() # p4 = models.FloatField() # p5 = models.FloatField() # p6 = models.FloatField() # p7 = models.FloatField() # p8 = models.FloatField() # p9 = models.FloatField() # p10 = models.FloatField() # p11 = models.FloatField() # p12 = models.FloatField() # p13 = models.FloatField() # p14 = models.FloatField() # p15 = models.FloatField() # p16 = models.FloatField() # p17 = models.FloatField() # p18 = models.FloatField() # p19 = models.FloatField() # p20 = models.FloatField() # c1 = models.FloatField() # c2 = models.FloatField() # c3 = models.FloatField() # c4 = models.FloatField() # c5 = models.FloatField() # c6 = models.FloatField() # c7 = models.FloatField() # c8 = models.FloatField() # c9 = models.FloatField() # c10 = models.FloatField() # c11 = models.FloatField() # c12 = models.FloatField() # c13 = models.FloatField() # c14 = models.FloatField() # c15 = models.FloatField() # c16 = models.FloatField() # c17 = models.FloatField() # c18 = models.FloatField() # c19 = models.FloatField() # c20 = models.FloatField() def decision_choices(player): if not player.algorithm: choices = [[False, 'Invest Myself'], [True, "Use Brandon's Recommendation"]] else: choices = [[False, 'Invest Myself'], [True, "Use Algorithm's Recommendation"]] return choices def set_values(player): rou = player.round_number participant = player.participant if rou == 1: endow = 500 else: endow = getattr(participant, f'r{rou-1}') player.starting_endow = endow print(player.starting_endow) player.asset_change = round(getattr(participant, f"ac{rou}") * 100, 3) print(player.asset_change) if player.asset_change > 0: player.pos = True else: player.pos = False player.sugg_amount = round(getattr(participant, f"sa{rou}") * 100, 3) print(player.sugg_amount) # PAGES class Introduction(Page): def is_displayed(player: Player): return player.round_number == 1 class AdvisorAlgHistIntro(Page): def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 1 and player.historical == 1 class AdvisorHumHistIntro(Page): def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 0 and player.historical == 1 class AdvisorAlgPresIntro(Page): def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 1 and player.historical == 0 class AdvisorHumPresIntro(Page): def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 0 and player.historical == 0 class AdvisorAlgHist(Page): def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 1 and player.historical == 1 class AdvisorAlgPres(Page): def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 1 and player.historical == 0 class AdvisorHumHist(Page): def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 0 and player.historical == 1 class AdvisorHumPres(Page): def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 0 and player.historical == 0 class BetrayAlgHist(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 1 and player.historical == 1 and player.betray == 1 class BetrayAlgPres(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 1 and player.historical == 0 and player.betray == 1 class BetrayHumHist(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 0 and player.historical == 1 and player.betray == 1 class BetrayHumPres(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 and player.algorithm == 0 and player.historical == 0 and player.betray == 1 class MakingDecisionAlg(Page): def is_displayed(player: Player): return player.algorithm == 1 def vars_for_template(player: Player): player.title_text = "Please Wait" if player.historical == 1: player.body_text = "The algorithm's historical recommendation is being retrieved. The next button will appear when the recommendation is ready." else: player.body_text = "The algorithm's recommendation is being prepared. The next button will appear when the recommendation is ready." class MakingDecisionHum(Page): def is_displayed(player: Player): return player.algorithm == 0 def vars_for_template(player: Player): player.title_text = "Please Wait" if player.historical == 1: player.body_text = "Brandon's historical recommendation is being retrieved. The next button will appear when the recommendation is ready." else: player.body_text = "Brandon's recommendation is being prepared. The next button will appear when the recommendation is ready." class DecisionAlgPres(Page): form_model = 'player' form_fields = ['decision'] def is_displayed(player: Player): return player.algorithm == 1 and player.historical == 0 def vars_for_template(player: Player): set_values(player) class DecisionAlgHist(Page): form_model = 'player' form_fields = ['decision'] def is_displayed(player: Player): return player.algorithm == 1 and player.historical == 1 def vars_for_template(player: Player): set_values(player) class DecisionHumPres(Page): form_model = 'player' form_fields = ['decision'] def is_displayed(player: Player): return player.algorithm == 0 and player.historical == 0 def vars_for_template(player: Player): set_values(player) class DecisionHumHist(Page): form_model = 'player' form_fields = ['decision'] def is_displayed(player: Player): return player.algorithm == 0 and player.historical == 1 def vars_for_template(player: Player): set_values(player) class Invest(Page): form_model = 'player' form_fields = ['invest_amount'] @staticmethod def is_displayed(player: Player): return not player.decision class Results(Page): def vars_for_template(player: Player): rou = player.round_number participant = player.participant if player.decision: player.invest_amount = player.sugg_amount else: pass player.invest_return = round((player.starting_endow * (player.invest_amount / 100)) * (player.asset_change / 100), 2) end_en = round(player.starting_endow + player.invest_return, 2) player.ending_endow = end_en player.c_return = round(player.ending_endow - 500, 2) if rou == 20: participant = player.participant participant.final_endow = player.ending_endow else: pass player.asset_price = getattr(participant, f'ap{rou}') setattr(participant, f"r{rou}", end_en) for i in range(1, Constants.num_rounds + 1): if i > rou: setattr(participant, f"r{i}", 0) else: pass class Final(Page): def is_displayed(player: Player): return player.round_number == 20 page_sequence = [ Introduction, AdvisorAlgHistIntro, AdvisorAlgPresIntro, AdvisorHumHistIntro, AdvisorHumPresIntro, AdvisorAlgHist, AdvisorAlgPres, AdvisorHumHist, AdvisorHumPres, BetrayAlgHist, BetrayAlgPres, BetrayHumHist, BetrayHumPres, MakingDecisionAlg, MakingDecisionHum, DecisionAlgPres, DecisionAlgHist, DecisionHumPres, DecisionHumHist, Invest, Results, Final ]