from otree.api import * import math import pandas as pd import itertools import numpy as np import random class C(BaseConstants): NAME_IN_URL = 'task1' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 SVO_TEMPLATE = 'svo/templates/svo_.html' RULES_TEMPLATE = 'svo/templates/rules.html' A = 23 B = 0.25 SVO_VALUES = pd.read_csv("svo/svo_values.csv") class Subsession(BaseSubsession): pass def shuffle_list(co_players): randomized_list = co_players[:] while True: random.shuffle(randomized_list) for a, b in zip(co_players, randomized_list): if a == b: break else: return randomized_list def creating_session(subsession): roles_svo = itertools.cycle(["B", "A"]) players = subsession.get_players() for player in players: player.role_svo = next(roles_svo) player.internal_id = player.participant_id player.payoff_task1 = 0 player.participant.payoff_task1 = 0 player.participant.role_svo = player.role_svo player.participant.self_option_task_1 = 0 player.participant.other_option_task_1 = 0 player.participant.self_payoff_task_1 = 0 player.participant.other_payoff_task_1 = 0 class Group(BaseGroup): pass class Player(BasePlayer): you1 = models.IntegerField() you2 = models.IntegerField() you3 = models.IntegerField() you4 = models.IntegerField() you5 = models.IntegerField() you6 = models.IntegerField() other1 = models.IntegerField() other2 = models.IntegerField() other3 = models.IntegerField() other4 = models.IntegerField() other5 = models.IntegerField() other6 = models.IntegerField() svo_idx = models.IntegerField(choices=[0, 1, 2, 3, 4, 5, 6, 7, 8], widget=widgets.RadioSelect) role_svo = models.StringField() co_player_svo = models.IntegerField() internal_id = models.IntegerField() payoff_task1 = models.FloatField(initial = 0) def pickOptions(self): random_pick = np.random.randint(0, 6) self_picks = [self.you1, self.you2, self.you3, self.you4, self.you5, self.you6] other_picks = [self.other1, self.other2, self.other3, self.other4, self.other5, self.other6] self.participant.self_option_task_1 = random_pick + 1 self.participant.other_option_task_1 = random_pick + 1 self.participant.self_payoff_task_1 = self_picks[random_pick] self.participant.other_payoff_task_1 = other_picks[random_pick] # def setPayoffs(self): # random_pick = np.random.randint(0, 6) # if self.role_svo == "A": # self_picks = [self.you1, self.you2, self.you3, self.you4, self.you5, self.you6] # self.payoff_task1 += self_picks[random_pick] # self.participant.payoff_task1 = np.round(self.payoff_task1, 2) # self.participant.self_option_task_1 = random_pick + 1 # self.participant.self_payoff_task_1 = self_picks[random_pick] # else: # other_players = self.get_others_in_subsession() # for p in other_players: # if p.role_svo == "A" and p.field_maybe_none("you1") is not None: # other_picks = [p.other1, p.other2, p.other3, p.other4, p.other5, p.other6] # self.payoff_task1 += other_picks[random_pick] # p.participant.payoff_task1 = np.round(p.payoff_task1, 2) # p.participant.self_option_task_1 = random_pick + 1 # if self.payoff_task1 == 0: # self_picks = [self.you1, self.you2, self.you3, self.you4, self.you5, self.you6] # self.payoff_task1 += self_picks[random_pick] # self.participant.payoff_task1 = np.round(self.payoff_task1, 2) # self.participant.self_option_task_1 = random_pick + 1 # self.participant.self_payoff_task_1 = self_picks[random_pick] # def setPayoffs(self): # co_player = self.get_others_in_subsession()[0] # random_pick = np.random.randint(0, 6) # self_picks = [self.you1, self.you2, self.you3, self.you4, self.you5, self.you6] # other_picks = [self.other1, self.other2, self.other3, self.other4, self.other5, self.other6] # if self.role_svo == "active": # self.payoff_task1 += self_picks[random_pick] # co_player.payoff_task1 += other_picks[random_pick] # self.participant.payoff_task1 = np.round(self.payoff_task1, 2) # co_player.participant.payoff_task1 = np.round(co_player.payoff_task1, 2) # # self.participant.self_option_task_1 = random_pick + 1 # co_player.participant.received_option_task_1 = random_pick + 1 # self.participant.self_payoff_task_1 = self_picks[random_pick] # co_player.participant.received_payoff_task_1 = other_picks[random_pick] def make_question_html(values): html = "" for v in values: html += f"{v}" return html class GroupsWaitPage(WaitPage): title_text = "Task 1" body_text = "Please wait to be matched with another participant" group_by_arrival_time = True class task1(Page): values = [] def live_method(player, data): svo_values_df = C.SVO_VALUES question_no = data["question_no"] chosen_value = data["chosen_value"] response = dict() if question_no <= 6: you_data = svo_values_df.loc[svo_values_df.question_idx == question_no, "self"] other_data = svo_values_df.loc[svo_values_df.question_idx == question_no, "other"] you_values = make_question_html(you_data) other_values = make_question_html(other_data) response = dict(you_html = you_values, other_html = other_values, you_data = you_data.tolist(), other_data = other_data.tolist()) if chosen_value > -1: you_data_1 = svo_values_df.loc[svo_values_df.question_idx == question_no - 1, "self"].values other_data_1 = svo_values_df.loc[svo_values_df.question_idx == question_no - 1, "other"].values if question_no - 1 == 1: player.you1 = int(you_data_1[chosen_value]) player.other1 = int(other_data_1[chosen_value]) elif question_no - 1 == 2: player.you2 = int(you_data_1[chosen_value]) player.other2 = int(other_data_1[chosen_value]) elif question_no - 1 == 3: player.you3 = int(you_data_1[chosen_value]) player.other3 = int(other_data_1[chosen_value]) elif question_no - 1 == 4: player.you4 = int(you_data_1[chosen_value]) player.other4 = int(other_data_1[chosen_value]) elif question_no - 1 == 5: player.you5 = int(you_data_1[chosen_value]) player.other5 = int(other_data_1[chosen_value]) elif question_no - 1 == 6: player.you6 = int(you_data_1[chosen_value]) player.other6 = int(other_data_1[chosen_value]) player.pickOptions() return {player.id_in_group: response} page_sequence = [task1]