from otree.api import * import random doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'p2' players_per_group = 2 num_rounds = 1 showup = 125 intervals = [ [-75, -50], [-49, -25], [-24, -1], [0, 25], [26, 50], [51, 75], [76, 100], [101, 125], [126, 150], [151, 175], [176, 200], [201, 225], [226, 250] ] class Subsession(BaseSubsession): pass def creating_session(subsession): if subsession.session.vars.get('group_matrix'): group_matrix = subsession.session.vars['group_matrix'] subsession.set_group_matrix(group_matrix) for player in subsession.get_players(): # set treatment from session config, if not set before if player.participant.vars.get('treatment') is None: player.treatment = subsession.session.config['treatment'] player.participant.vars['treatment'] = player.treatment else: player.treatment = player.participant.vars['treatment'] # set types by group ids, if not grouped before if player.participant.vars.get('type') is None: if player.id_in_group == 1: player.type = "A" if player.id_in_group == 2: player.type = "B" player.participant.vars['type'] = player.type else: player.type = player.participant.vars['type'] # set fake decisions from p1 for results for group in subsession.get_groups(): players = group.get_players() print(players, "PPP") print( [p for p in players if p.type == "A"]) for p in players: print(p.participant.vars) print(p.group.id) print(p.id_in_group) player_A = [p for p in players if p.type == "A"][0] player_B = [p for p in players if p.type == "B"][0] player_B.participant.vars['p1'] = {'decision_B': 50} class Group(BaseGroup): pass class Player(BasePlayer): type = models.StringField() treatment = models.StringField() decision1_A = models.IntegerField() decision2_A = models.IntegerField() decision1_B = models.IntegerField() decision2_B = models.IntegerField() # PAGES class Instructions_A(Page): def is_displayed(player: Player): return player.type == "A" class Decision1_A(Page): form_model = 'player' form_fields = ['decision1_A'] def is_displayed(player: Player): return player.type == "A" class Decision2_A_T1(Page): form_model = 'player' form_fields = ['decision2_A'] def is_displayed(player: Player): return player.type == "A" and (player.treatment == "T1" or player.treatment == "T1_B") class Decision2_A_T2(Page): form_model = 'player' form_fields = ['decision2_A'] def is_displayed(player: Player): return player.type == "A" and (player.treatment == "T2" or player.treatment == "T2_B") # B Player class Instructions_B(Page): def is_displayed(player: Player): return player.type == "B" class Decision1_B(Page): form_model = 'player' form_fields = ['decision1_B'] def is_displayed(player: Player): return player.type == "B" class Decision2_B(Page): form_model = 'player' form_fields = ['decision2_B'] def is_displayed(player: Player): return player.type == "B" class WaitForAll(WaitPage): def after_all_players_arrive(group: Group): players = group.get_players() player_A = [p for p in players if p.type == "A"][0] player_B = [p for p in players if p.type == "B"][0] # calculate average of Bs within treatment all_players_b = [p for p in group.subsession.get_players() if p.type == "B"] b_choices = [p.participant.vars['p1']['decision_B'] for p in all_players_b] average_of_all_b = sum(b_choices) / len(b_choices) # player A random_decision = random.randint(1, 2) # # A1 d = (abs(average_of_all_b - player_A.decision1_A)) average_b_reward = max(0, 200 - (0.25 * (d ** 2))) # # A2 n1 = random.randint(0, 100) n2 = random.randint(0, 100) random_b_player = random.choice(all_players_b) random_b_player_decision = random_b_player.participant.vars['p1']['decision_B'] # # for T1, T1_B correct_guess = False if player_A.treatment == "T1" or player_A.treatment == "T1_B": if random_b_player_decision < 0: correct_guess = True # # for T2, T2_B if player_A.treatment == "T2" or player_A.treatment == "T2_B": if random_b_player_decision > 50: correct_guess = True # # payment determination win_guess = False lower = False if correct_guess: if int(player_A.decision2_A) > n1 or int(player_A.decision2_A) > n2: win_guess = True lower = False if int(player_A.decision2_A) <= n1 and int(player_A.decision2_A) <= n2: lower = True if not correct_guess: if int(player_A.decision2_A) < n1 or int(player_A.decision2_A) < n2: win_guess = True lower = True if int(player_A.decision2_A) >= n1 and int(player_A.decision2_A) >= n2: lower = False pay = 0 if random_decision == 1: pay = int(average_b_reward) if random_decision == 2: if win_guess: pay = 200 player_A.participant.vars['p2'] = { 'decision': random_decision, 'n1': n1, 'n2': n2, 'lower': lower, 'win_guess': win_guess, 'average_of_b_number': average_of_all_b, 'average_b_reward': average_b_reward, 'random_b_player_decision': random_b_player_decision, 'correct_guess': correct_guess, 'decision1_A': player_A.decision1_A, 'decision2_A': player_A.decision2_A, 'decision1_B': player_B.decision1_B, 'decision2_B': player_B.decision2_B, 'pay_1': int(average_b_reward) } if player_A.participant.vars.get('payoffs'): player_A.participant.vars['payoffs']['p2'] = pay + Constants.showup else: player_A.participant.vars['payoffs'] = {'p2': pay + Constants.showup} # player B selected_decision = random.randint(1, 2) payoff_b = 0 dd = (abs(player_B.decision1_B - player_A.decision1_A)) guess_a_reward = max(0, 200 - (0.25 * (dd ** 2))) percent_difference = abs((player_B.decision2_B - player_A.decision2_A)) if selected_decision == 1: payoff_b = int(guess_a_reward) if selected_decision == 2: if percent_difference <= 5: payoff_b = 200 player_B.participant.vars['p2'] = { 'decision': selected_decision, 'percent_difference': percent_difference, 'guess_a_reward': guess_a_reward, 'decision1_A': player_A.decision1_A, 'decision2_A': player_A.decision2_A, 'decision1_B': player_B.decision1_B, 'decision2_B': player_B.decision2_B, 'pay_1': int(guess_a_reward) } if player_B.participant.vars.get('payoffs'): player_B.participant.vars['payoffs']['p2'] = payoff_b + Constants.showup else: player_B.participant.vars['payoffs'] = {'p2': payoff_b + Constants.showup} player_A.participant.vars['payoffs']['p2_b'] = player_B.participant.vars['payoffs']['p2'] player_B.participant.vars['payoffs']['p2_a'] = player_A.participant.vars['payoffs']['p2'] player_A.participant.vars['payoffs']['p2_a'] = player_A.participant.vars['payoffs']['p2'] player_B.participant.vars['payoffs']['p2_b'] = player_B.participant.vars['payoffs']['p2'] class Complete(Page): pass page_sequence = [Instructions_A, Decision1_A, Decision2_A_T1, Decision2_A_T2, Instructions_B, Decision1_B, Decision2_B, WaitForAll, Complete]