from otree.api import * import random import time doc = """ CM experiment. Choice matching task """ class C(BaseConstants): NAME_IN_URL = 'CM_CM_1' PLAYERS_PER_GROUP = None DC_rounds = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] NUM_ROUNDS = len(DC_rounds) DC_scenarios = [(2, 5.5, 5, 4.5), (1, 6, 4, 4), (5, 4.5, 3, 5.5), (4, 5, 5, 4.5), (3, 4, 2, 6), (6, 4.5, 1, 5), (1, 4, 6, 5.5), (2, 5, 3, 5), (6, 5.5, 2, 4)] aux_scenarios =[(2, 5.5, 5, 4.5), (1, 6, 4, 4), (5, 4.5, 3, 5.5), (4, 5, 5, 4.5), (3, 4, 2, 6), (6, 4.5, 1, 5), (1, 4, 6, 5.5), (2, 5, 3, 5), (6, 5.5, 2, 4)] # Price levels (check how to put them in list and draw from it) level1 = 4 level2 = 4.5 level3 = 5 level4 = 5.5 level5 = 6 # Probabilities prob_l = 60 prob_m = 75 prob_h = 90 # Quadratic scoring rules quadratic_score_A = 2 quadratic_score_B = 2 prob_replace_l = 60 prob_replace_m = 75 prob_replace_h = 90 prob_not_replace_l = 100 - prob_replace_l prob_not_replace_m = 100 - prob_replace_m prob_not_replace_h = 100 - prob_replace_h # Number of task tasks = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] num_CE = len(tasks) # payoff frequency method payoff_fr = 4 exchange_pounds = 0.87 frequency_pounds = payoff_fr * exchange_pounds # Experimental budget budget = 6 budget_aux = 8 matching_timeout_mins = 5 smaller_group = 8 timeout = 3 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): if subsession.round_number == 1: # CHECK IF YOU NEED PLAYERS_PER_GROUP OR SUBSESSION.SESSION.NUM_PARTICIPANTS (as in the original) subsession.session.other_part = subsession.session.num_participants - 1 subsession.session.random_prediction_A = random.randint(0, subsession.session.num_participants - 1) # Are these used anywhere? if subsession.session.random_prediction_A < (subsession.session.num_participants - 1): subsession.session.random_prediction_B = \ random.randint(0, subsession.session.num_participants - 1 - subsession.session.random_prediction_A) else: subsession.session.random_prediction_B = 0 subsession.session.random_prediction_C = \ subsession.session.num_participants - 1 - subsession.session.random_prediction_A - \ subsession.session.random_prediction_B subsession.session.random_prediction_A_2 = \ random.randint(int(round((subsession.session.num_participants - 1) * 0.2, 0)), int(round((subsession.session.num_participants - 1) * 0.4, 0))) subsession.session.random_prediction_B_2 = \ random.randint(int(round((subsession.session.num_participants - 1) * 0.2, 0)), (int(round((subsession.session.num_participants - 1) * 0.4, 0)))) subsession.session.random_prediction_C_2 = \ subsession.session.num_participants - 1 - subsession.session.random_prediction_A_2 - \ subsession.session.random_prediction_B_2 if subsession.session.random_prediction_C_2 < 0: subsession.session.random_prediction_C_2 = 0 list_choices_A_temp = " ".join(['A'] * subsession.session.random_prediction_A_2) list_choices_B_temp = " ".join(['B'] * subsession.session.random_prediction_B_2) list_choices_C_temp = " ".join(['C'] * subsession.session.random_prediction_C_2) choices_str = list_choices_A_temp + ' ' + list_choices_B_temp + ' ' + list_choices_C_temp list_choices = choices_str.split() # Random draw from list of randomly drawn choices subsession.session.chosen_letter_2 = random.choice(list_choices) listA_2 = [] # List of matches rnd = random.sample(list(range(1, subsession.session.num_participants)), subsession.session.num_participants - 1) example_prediction_selected_A = [] example_prediction_selected_B = [] example_prediction_selected_C = [] for i in range(0, subsession.session.num_participants - 1): n_t = int(rnd[i]) if i == subsession.session.random_prediction_A_2 - 1: n = str(n_t) + "." else: n = str(n_t) + "," listA_2.append(n) example_prediction_selected_A_temp = random.randint( int(round((subsession.session.num_participants - 1) * 0.2, 0)), int(round((subsession.session.num_participants - 1) * 0.7, 0))) example_prediction_selected_B_temp = random.randint( int(round((subsession.session.num_participants - 1) * 0.2, 0)), int(round((subsession.session.num_participants - 1) * 0.7, 0))) if example_prediction_selected_A_temp + \ example_prediction_selected_B_temp >= subsession.session.num_participants - 1: example_prediction_selected_B_temp = \ subsession.session.num_participants - 1 - example_prediction_selected_A_temp example_prediction_selected_C_temp = 0 else: example_prediction_selected_C_temp = \ subsession.session.num_participants - 1 - example_prediction_selected_A_temp - \ example_prediction_selected_B_temp example_prediction_selected_A.append(example_prediction_selected_A_temp) example_prediction_selected_B.append(example_prediction_selected_B_temp) example_prediction_selected_C.append(example_prediction_selected_C_temp) # indent changed from here subsession.session.example_prediction_selected_A = example_prediction_selected_A subsession.session.example_prediction_selected_B = example_prediction_selected_B subsession.session.example_prediction_selected_C = example_prediction_selected_C subsession.session.listA_2 = listA_2 example_prediction_selected_A_temp = random.randint( int(round((subsession.session.num_participants - 1) * 0.2, 0)), int(round((subsession.session.num_participants - 1) * 0.7, 0))) example_prediction_selected_B_temp = random.randint( int(round((subsession.session.num_participants - 1) * 0.2, 0)), (int(round((subsession.session.num_participants - 1) * 0.7, 0)))) if example_prediction_selected_B_temp + example_prediction_selected_A_temp >= \ subsession.session.num_participants - 1: example_prediction_selected_B_temp = subsession.session.num_participants - 1 - \ example_prediction_selected_A_temp example_prediction_selected_C_temp = 0 else: example_prediction_selected_C_temp = \ subsession.session.num_participants - 1 - example_prediction_selected_A_temp - \ example_prediction_selected_B_temp example_prediction_selected_A.append(example_prediction_selected_A_temp) example_prediction_selected_B.append(example_prediction_selected_B_temp) example_prediction_selected_C.append(example_prediction_selected_C_temp) subsession.session.example_prediction_A = random.randint( int(round((subsession.session.num_participants - 1) * 0.2, 0)), int(round((subsession.session.num_participants - 1) * 0.7, 0))) subsession.session.example_prediction_B = random.randint( int(round((subsession.session.num_participants - 1) * 0.2, 0)), (int(round((subsession.session.num_participants - 1) * 0.7, 0)))) if subsession.session.example_prediction_B + subsession.session.example_prediction_A >= \ subsession.session.num_participants - 1: subsession.session.example_prediction_B = subsession.session.num_participants - 1 - \ subsession.session.example_prediction_A subsession.session.example_prediction_C = 0 else: subsession.session.example_prediction_C = \ subsession.session.num_participants - 1 - subsession.session.example_prediction_A - \ subsession.session.example_prediction_B Pred_A_temp = subsession.session.example_prediction_A / (subsession.session.num_participants - 1) Pred_B_temp = subsession.session.example_prediction_B / (subsession.session.num_participants - 1) Pred_C_temp = subsession.session.example_prediction_C / (subsession.session.num_participants - 1) factor1_temp = Pred_A_temp * Pred_A_temp + Pred_B_temp * Pred_B_temp + Pred_C_temp * Pred_C_temp subsession.session.example_prediction_payoff_A = round( C.quadratic_score_A + C.quadratic_score_B * (2 * Pred_A_temp - factor1_temp), 1) subsession.session.example_prediction_payoff_B = round( C.quadratic_score_A + C.quadratic_score_B * (2 * Pred_B_temp - factor1_temp), 1) subsession.session.example_prediction_payoff_C = round( C.quadratic_score_A + C.quadratic_score_B * (2 * Pred_C_temp - factor1_temp), 1) subsession.session.player_temp_AB_ = [1] * (C.num_CE + 1) subsession.session.player_temp_AB_[0] = random.randint(1, 2) for i in range(0, (C.num_CE + 1)): subsession.session.player_temp_AB_[i] = random.randint(1, 2) for p in subsession.get_players(): round_numbers = list(range(1, C.NUM_ROUNDS + 1)) random.shuffle(round_numbers) p.participant.AUX_rounds = dict(zip(C.DC_rounds, round_numbers)) p.participant.num_rounds = C.NUM_ROUNDS p.participant.CM_predictions = [] p.participant.contador = 1 p.participant.player_temp_AB = subsession.session.player_temp_AB_ # CHANGED FROM ORIGINAL p.participant.num_players = C.PLAYERS_PER_GROUP p.participant.group_pre_A = 0 p.participant.group_pre_B = 0 p.participant.group_pre_C = 0 p.participant.group_elec = 0 p.participant.random_choice = 0 p.participant.Test_choices_A = \ random.randint(0, subsession.session.other_part) p.participant.Test_choices_B = \ random.randint(0, subsession.session.other_part - p.participant.Test_choices_A) p.participant.Test_choices_C = \ subsession.session.other_part - p.participant.Test_choices_B - p.participant.Test_choices_A p.participant.Test_choices_A_pred = \ round(random.random() * subsession.session.other_part, 1) p.participant.Test_choices_B_pred = \ round(random.random() * (subsession.session.other_part - p.participant.Test_choices_A_pred), 1) p.participant.Test_choices_C_pred = \ subsession.session.other_part - p.participant.Test_choices_A_pred - p.participant.Test_choices_B_pred Pred_A = p.participant.Test_choices_A_pred / subsession.session.other_part Pred_B = p.participant.Test_choices_B_pred / subsession.session.other_part Pred_C = p.participant.Test_choices_C_pred / subsession.session.other_part factor1 = Pred_A * Pred_A + Pred_B * Pred_B + Pred_C * Pred_C p.participant.Test_payoff_a_pred = round(C.quadratic_score_A + C.quadratic_score_B * (2 * Pred_A - factor1), 1) p.participant.Test_payoff_b_pred = round(C.quadratic_score_A + C.quadratic_score_B * (2 * Pred_B - factor1), 1) p.participant.Test_payoff_c_pred = round(C.quadratic_score_A + C.quadratic_score_B * (2 * Pred_C - factor1), 1) p.participant.num_prob_replace = 1 p.participant.num_prob_not_replace = 1 p.participant.num_Test_choices_A_prob = 1 p.participant.num_Test_choices_B_prob = 1 p.participant.num_Test_choices_C_prob = 1 p.participant.num_payoff_A_guess = 1 p.participant.num_payoff_B_guess = 1 p.participant.num_payoff_C_guess = 1 p.participant.num_Test_choices_A_pred = 1 p.participant.num_Test_choices_B_pred = 1 p.participant.num_Test_choices_C_pred = 1 p.participant.num_Test_payoff_a_pred = 1 p.participant.num_Test_payoff_b_pred = 1 p.participant.num_Test_payoff_c_pred = 1 random_num = random.random() if p.participant.treatment_p == 3 or p.participant.treatment_p == 6: if random_num <= (C.prob_replace_l * 0.01): replace_pred = 1 else: replace_pred = 0 else: if p.participant.treatment_p == 4 or p.participant.treatment_p == 7 or p.participant.treatment_p == 9: if random_num <= (C.prob_replace_m * 0.01): replace_pred = 1 else: replace_pred = 0 else: if random_num <= (C.prob_replace_h * 0.01): replace_pred = 1 else: replace_pred = 0 p.participant.replace_predictions = replace_pred for w in subsession.get_groups(): w.binding_sit = random.randint(1, subsession.session.num_participants) class Group(BaseGroup): binding_sit = models.IntegerField(initial=0) sum_binding_sit_1 = models.IntegerField(initial=0) sum_binding_sit_2 = models.IntegerField(initial=0) sum_binding_sit_3 = models.IntegerField(initial=0) id_player_groupA = models.StringField(initial='') id_player_groupB = models.StringField(initial='') id_player_groupC = models.StringField(initial='') list_pred_A_A = models.StringField(initial='') list_pred_A_B = models.StringField(initial='') list_pred_A_C = models.StringField(initial='') list_pred_B_A = models.StringField(initial='') list_pred_B_B = models.StringField(initial='') list_pred_B_C = models.StringField(initial='') list_pred_C_A = models.StringField(initial='') list_pred_C_B = models.StringField(initial='') list_pred_C_C = models.StringField(initial='') list_aux_A_A = models.StringField(initial='') list_aux_A_B = models.StringField(initial='') list_aux_A_C = models.StringField(initial='') list_aux_B_A = models.StringField(initial='') list_aux_B_B = models.StringField(initial='') list_aux_B_C = models.StringField(initial='') list_aux_C_A = models.StringField(initial='') list_aux_C_B = models.StringField(initial='') list_aux_C_C = models.StringField(initial='') group_participants = models.StringField(initial='') def choice_binding(player, group): if group.binding_sit == 1: return player.participant.CE_choice_1 if group.binding_sit == 2: return player.participant.CE_choice_2 if group.binding_sit == 3: return player.participant.CE_choice_3 if group.binding_sit == 4: return player.participant.CE_choice_4 if group.binding_sit == 5: return player.participant.CE_choice_5 if group.binding_sit == 6: return player.participant.CE_choice_6 if group.binding_sit == 7: return player.participant.CE_choice_7 if group.binding_sit == 8: return player.participant.CE_choice_8 if group.binding_sit == 9: return player.participant.CE_choice_9 def group_aux(player): if player.group.binding_sit == 1: return player.participant.CE_choice_1_aux if player.group.binding_sit == 2: return player.participant.CE_choice_2_aux if player.group.binding_sit == 3: return player.participant.CE_choice_3_aux if player.group.binding_sit == 4: return player.participant.CE_choice_4_aux if player.group.binding_sit == 5: return player.participant.CE_choice_5_aux if player.group.binding_sit == 6: return player.participant.CE_choice_6_aux if player.group.binding_sit == 7: return player.participant.CE_choice_7_aux if player.group.binding_sit == 8: return player.participant.CE_choice_8_aux if player.group.binding_sit == 9: return player.participant.CE_choice_9_aux def group_pre_A_(player): if player.group.binding_sit == 1: return player.participant.CE1_pred_A if player.group.binding_sit == 2: return player.participant.CE2_pred_A if player.group.binding_sit == 3: return player.participant.CE3_pred_A if player.group.binding_sit == 4: return player.participant.CE4_pred_A if player.group.binding_sit == 5: return player.participant.CE5_pred_A if player.group.binding_sit == 6: return player.participant.CE6_pred_A if player.group.binding_sit == 7: return player.participant.CE7_pred_A if player.group.binding_sit == 8: return player.participant.CE8_pred_A if player.group.binding_sit == 9: return player.participant.CE9_pred_A def group_pre_B_(player): if player.group.binding_sit == 1: return player.participant.CE1_pred_B if player.group.binding_sit == 2: return player.participant.CE2_pred_B if player.group.binding_sit == 3: return player.participant.CE3_pred_B if player.group.binding_sit == 4: return player.participant.CE4_pred_B if player.group.binding_sit == 5: return player.participant.CE5_pred_B if player.group.binding_sit == 6: return player.participant.CE6_pred_B if player.group.binding_sit == 7: return player.participant.CE7_pred_B if player.group.binding_sit == 8: return player.participant.CE8_pred_B if player.group.binding_sit == 9: return player.participant.CE9_pred_B def group_pre_C_(player): if player.group.binding_sit == 1: return player.participant.CE1_pred_C if player.group.binding_sit == 2: return player.participant.CE2_pred_C if player.group.binding_sit == 3: return player.participant.CE3_pred_C if player.group.binding_sit == 4: return player.participant.CE4_pred_C if player.group.binding_sit == 5: return player.participant.CE5_pred_C if player.group.binding_sit == 6: return player.participant.CE6_pred_C if player.group.binding_sit == 7: return player.participant.CE7_pred_C if player.group.binding_sit == 8: return player.participant.CE8_pred_C if player.group.binding_sit == 9: return player.participant.CE9_pred_C def make_field_prediction(label): return models.IntegerField( label=label, min=0, default=0, ) def make_field(label): return models.IntegerField( choices=[[1, "A"], [2, "B"], [3, "Nessun"]], label=label, widget=widgets.RadioSelect, blank=False, ) class Player(BasePlayer): # Choices in each choice situation CE_choice = models.IntegerField( choices=[[1, "A"], [2, "B"], [3, "Nessun"]], label="DCE Choice situation", widget=widgets.RadioSelect, blank=False, ) # Bottle choice CE_choice_aux = models.IntegerField( choices=[[1, "A"], [2, "B"], [3, "Nessun"]], label="Auxilliary Choice situation", widget=widgets.RadioSelect, blank=False, ) ##Prediction of each Choice situtation #CE1 CE1_pred_A = make_field_prediction('CE1_A') CE1_pred_B = make_field_prediction("CE1_B") CE1_pred_C = make_field_prediction("CE1_C") #CE2 CE2_pred_A = make_field_prediction("CE2_A") CE2_pred_B = make_field_prediction("CE2_B") CE2_pred_C = make_field_prediction("CE2_C") #CE3 CE3_pred_A = make_field_prediction("CE3_A") CE3_pred_B = make_field_prediction("CE3_B") CE3_pred_C = make_field_prediction("CE3_C") # CE3 CE4_pred_A = make_field_prediction("CE4_A") CE4_pred_B = make_field_prediction("CE4_B") CE4_pred_C = make_field_prediction("CE4_C") # CE3 CE5_pred_A = make_field_prediction("CE5_A") CE5_pred_B = make_field_prediction("CE5_B") CE5_pred_C = make_field_prediction("CE5_C") # CE3 CE6_pred_A = make_field_prediction("CE6_A") CE6_pred_B = make_field_prediction("CE6_B") CE6_pred_C = make_field_prediction("CE6_C") # CE6 CE7_pred_A = make_field_prediction("CE7_A") CE7_pred_B = make_field_prediction("CE7_B") CE7_pred_C = make_field_prediction("CE7_C") # CE8 CE8_pred_A = make_field_prediction("CE8_A") CE8_pred_B = make_field_prediction("CE8_B") CE8_pred_C = make_field_prediction("CE8_C") # CE9 CE9_pred_A = make_field_prediction("CE9_A") CE9_pred_B = make_field_prediction("CE9_B") CE9_pred_C = make_field_prediction("CE9_C") # Payoffs payoff1_a = models.CurrencyField() payoff1_b = models.CurrencyField() payoff1_c = models.CurrencyField() payoff2_a = models.CurrencyField() payoff2_b = models.CurrencyField() payoff2_c = models.CurrencyField() payoff3_a = models.CurrencyField() payoff3_b = models.CurrencyField() payoff3_c = models.CurrencyField() payoff4_a = models.CurrencyField() payoff4_b = models.CurrencyField() payoff4_c = models.CurrencyField() payoff5_a = models.CurrencyField() payoff5_b = models.CurrencyField() payoff5_c = models.CurrencyField() payoff6_a = models.CurrencyField() payoff6_b = models.CurrencyField() payoff6_c = models.CurrencyField() payoff7_a = models.CurrencyField() payoff7_b = models.CurrencyField() payoff7_c = models.CurrencyField() payoff8_a = models.CurrencyField() payoff8_b = models.CurrencyField() payoff8_c = models.CurrencyField() payoff9_a = models.CurrencyField() payoff9_b = models.CurrencyField() payoff9_c = models.CurrencyField() # Choices list list_choices_A = models.StringField(initial='') list_choices_B = models.StringField(initial='') list_choices_C = models.StringField(initial='') #Auxilliary questions CE_choice_1_aux = make_field('Scelta') CE_choice_2_aux = make_field('Scelta') CE_choice_3_aux = make_field('Scelta') CE_choice_4_aux = make_field('Scelta') CE_choice_5_aux = make_field('Scelta') CE_choice_6_aux = make_field('Scelta') CE_choice_7_aux = make_field('Scelta') CE_choice_8_aux = make_field('Scelta') CE_choice_9_aux = make_field('Scelta') group_elec = models.IntegerField() active_list = models.StringField(initial='') random_draw = models.IntegerField() group_size_2 = models.IntegerField() dropout_mark = models.IntegerField() def group_by_arrival_time_method(subsession: Subsession, waiting_players): d = {} for p in waiting_players: group_id = p.participant.past_group_id if group_id not in d: # since 'd' is initially empty, we need to initialize an empty list (basket) # each time we see a new group ID. d[group_id] = [] players_in_my_group = d[group_id] players_in_my_group.append(p) if len(players_in_my_group) == p.participant.group_size: return players_in_my_group ##################################### PAGES #################################################### class GBAT(WaitPage): title_text = ("Si prega di rimanere su questa pagina - La fase 2 inizierà a breve") body_text = "" group_by_arrival_time = True template_name = 'CM_CM_1/Grouping.html' @staticmethod def is_displayed(player: Player): return player.round_number == 1 @staticmethod def js_vars(player): return dict(arrival_time=player.participant.wait_page_arrival, current_time=time.time(), timeout_mins=C.matching_timeout_mins) @staticmethod def after_all_players_arrive(group: Group): dropouts = [] player_group_id = [] for p in group.get_players(): if p.participant.is_dropout is True: dropouts.append(p) p.dropout_mark = 1 else: p.dropout_mark = 0 group_size = len(group.get_players()) - len(dropouts) for p in group.get_players(): p.participant.group_size_2 = group_size class Pre_CM(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant if timeout_happened: participant.is_dropout = True class CM_1(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['1'] form_model = 'player' form_fields = ['CE1_pred_A', 'CE1_pred_B', 'CE1_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff1_a = payoff_a player.participant.payoff1_b = payoff_b player.participant.payoff1_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE1_pred_A'] + values['CE1_pred_B'] + values[ 'CE1_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[0] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE1_pred_A = player.CE1_pred_A participant.CE1_pred_B = player.CE1_pred_B participant.CE1_pred_C = player.CE1_pred_C if timeout_happened: participant.is_dropout = True participant.CE1_pred_A = 4 participant.CE1_pred_B = 0 participant.CE1_pred_C = 0 class CM_1_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['1'] form_model = 'player' form_fields = ['CE_choice_1_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[0] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_1_aux = player.CE_choice_1_aux participant.page_no_1aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_1_aux = 1 class CM_2(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['2'] form_model = 'player' form_fields = ['CE2_pred_A', 'CE2_pred_B', 'CE2_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff2_a = payoff_a player.participant.payoff2_b = payoff_b player.participant.payoff2_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE2_pred_A'] + values['CE2_pred_B'] + values[ 'CE2_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[1] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE2_pred_A = player.CE2_pred_A participant.CE2_pred_B = player.CE2_pred_B participant.CE2_pred_C = player.CE2_pred_C if timeout_happened: participant.is_dropout = True participant.CE2_pred_A = 4 participant.CE2_pred_B = 0 participant.CE2_pred_C = 0 class CM_2_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['2'] form_model = 'player' form_fields = ['CE_choice_2_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[1] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_2_aux = player.CE_choice_2_aux participant.page_no_2aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_2_aux = 1 class CM_3(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['3'] form_model = 'player' form_fields = ['CE3_pred_A', 'CE3_pred_B', 'CE3_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff3_a = payoff_a player.participant.payoff3_b = payoff_b player.participant.payoff3_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE3_pred_A'] + values['CE3_pred_B'] + values[ 'CE3_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[2] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE3_pred_A = player.CE3_pred_A participant.CE3_pred_B = player.CE3_pred_B participant.CE3_pred_C = player.CE3_pred_C if timeout_happened: participant.is_dropout = True participant.CE3_pred_A = 4 participant.CE3_pred_B = 0 participant.CE3_pred_C = 0 class CM_3_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['3'] form_model = 'player' form_fields = ['CE_choice_3_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[2] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_3_aux = player.CE_choice_3_aux participant.page_no_3aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_3_aux = 1 class CM_4(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['4'] form_model = 'player' form_fields = ['CE4_pred_A', 'CE4_pred_B', 'CE4_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff4_a = payoff_a player.participant.payoff4_b = payoff_b player.participant.payoff4_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE4_pred_A'] + values['CE4_pred_B'] + values[ 'CE4_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[3] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE4_pred_A = player.CE4_pred_A participant.CE4_pred_B = player.CE4_pred_B participant.CE4_pred_C = player.CE4_pred_C if timeout_happened: participant.is_dropout = True participant.CE4_pred_A = 4 participant.CE4_pred_B = 0 participant.CE4_pred_C = 0 class CM_4_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['4'] form_model = 'player' form_fields = ['CE_choice_4_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[3] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_4_aux = player.CE_choice_4_aux participant.page_no_4aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_4_aux = 1 class CM_5(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['5'] form_model = 'player' form_fields = ['CE5_pred_A', 'CE5_pred_B', 'CE5_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff5_a = payoff_a player.participant.payoff5_b = payoff_b player.participant.payoff5_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE5_pred_A'] + values['CE5_pred_B'] + values[ 'CE5_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[4] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE5_pred_A = player.CE5_pred_A participant.CE5_pred_B = player.CE5_pred_B participant.CE5_pred_C = player.CE5_pred_C if timeout_happened: participant.is_dropout = True participant.CE5_pred_A = 4 participant.CE5_pred_B = 0 participant.CE5_pred_C = 0 class CM_5_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['5'] form_model = 'player' form_fields = ['CE_choice_5_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[4] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_5_aux = player.CE_choice_5_aux participant.page_no_5aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_5_aux = 1 class CM_6(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['6'] form_model = 'player' form_fields = ['CE6_pred_A', 'CE6_pred_B', 'CE6_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff6_a = payoff_a player.participant.payoff6_b = payoff_b player.participant.payoff6_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE6_pred_A'] + values['CE6_pred_B'] + values[ 'CE6_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[5] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE6_pred_A = player.CE6_pred_A participant.CE6_pred_B = player.CE6_pred_B participant.CE6_pred_C = player.CE6_pred_C if timeout_happened: participant.is_dropout = True participant.CE6_pred_A = 4 participant.CE6_pred_B = 0 participant.CE6_pred_C = 0 class CM_6_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['6'] form_model = 'player' form_fields = ['CE_choice_6_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[5] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_6_aux = player.CE_choice_6_aux participant.page_no_6aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_6_aux = 1 class CM_7(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['7'] form_model = 'player' form_fields = ['CE7_pred_A', 'CE7_pred_B', 'CE7_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff7_a = payoff_a player.participant.payoff7_b = payoff_b player.participant.payoff7_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE7_pred_A'] + values['CE7_pred_B'] + values[ 'CE7_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[6] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE7_pred_A = player.CE7_pred_A participant.CE7_pred_B = player.CE7_pred_B participant.CE7_pred_C = player.CE7_pred_C if timeout_happened: participant.is_dropout = True participant.CE7_pred_A = 4 participant.CE7_pred_B = 0 participant.CE7_pred_C = 0 class CM_7_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['7'] form_model = 'player' form_fields = ['CE_choice_7_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[6] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_7_aux = player.CE_choice_7_aux participant.page_no_7aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_7_aux = 1 class CM_8(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['8'] form_model = 'player' form_fields = ['CE8_pred_A', 'CE8_pred_B', 'CE8_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff8_a = payoff_a player.participant.payoff8_b = payoff_b player.participant.payoff8_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE8_pred_A'] + values['CE8_pred_B'] + values[ 'CE8_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[7] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE8_pred_A = player.CE8_pred_A participant.CE8_pred_B = player.CE8_pred_B participant.CE8_pred_C = player.CE8_pred_C if timeout_happened: participant.is_dropout = True participant.CE8_pred_A = 4 participant.CE8_pred_B = 0 participant.CE8_pred_C = 0 class CM_8_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['8'] form_model = 'player' form_fields = ['CE_choice_8_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[7] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_8_aux = player.CE_choice_8_aux participant.page_no_8aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_8_aux = 1 class CM_9(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p < 9 and player.round_number == player.participant.DC_rounds['9'] form_model = 'player' form_fields = ['CE9_pred_A', 'CE9_pred_B', 'CE9_pred_C'] @staticmethod def live_method(player: Player, data): if data['clicked_button'] == 1: a = float(data['fieldA']) / (player.participant.group_size_2 - 1) b = float(data['fieldB']) / (player.participant.group_size_2 - 1) c = float(data['fieldC']) / (player.participant.group_size_2 - 1) factor1 = a * a + b * b + c * c payoff_a = round(C.quadratic_score_A + C.quadratic_score_B * (2 * a - factor1), 1) payoff_b = round(C.quadratic_score_A + C.quadratic_score_B * (2 * b - factor1), 1) payoff_c = round(C.quadratic_score_A + C.quadratic_score_B * (2 * c - factor1), 1) response = dict(payoff_a=payoff_a, payoff_b=payoff_b, payoff_c=payoff_c) player.participant.payoff9_a = payoff_a player.participant.payoff9_b = payoff_b player.participant.payoff9_c = payoff_c return {player.id_in_group: response} # Passing data from Python to JavaScript (js_vars) @staticmethod def js_vars(player): return dict(number_players=player.participant.group_size_2, tempA=0, tempB=0, tempC=0) @staticmethod def error_message(player, values): if values['CE9_pred_A'] + values['CE9_pred_B'] + values[ 'CE9_pred_C'] != ( player.participant.group_size_2 - 1): return 'La somma delle predizioni deve essere uguale a ' + str(player.participant.group_size_2 - 1) def vars_for_template(player: Player): tuple_list = C.DC_scenarios scenario = tuple_list[8] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE9_pred_A = player.CE9_pred_A participant.CE9_pred_B = player.CE9_pred_B participant.CE9_pred_C = player.CE9_pred_C if timeout_happened: participant.is_dropout = True participant.CE9_pred_A = 4 participant.CE9_pred_B = 0 participant.CE9_pred_C = 0 class CM_9_aux(Page): @staticmethod def is_displayed(player: Player): return player.participant.treatment_p == 9 and player.round_number == player.participant.AUX_rounds['9'] form_model = 'player' form_fields = ['CE_choice_9_aux'] def vars_for_template(player: Player): tuple_list = C.aux_scenarios scenario = tuple_list[8] product_1 = scenario[0] level1 = scenario[1] product_2 = scenario[2] level2 = scenario[3] group_size = player.participant.group_size_2 - 1 return dict(level1=level1, level2=level2, product_1=product_1, product_2=product_2, group_size=group_size) # For dropouts @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 0.1 # instant timeout, 1 second else: return C.timeout*60 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.CE_choice_9_aux = player.CE_choice_9_aux participant.page_no_9aux = player.round_number if timeout_happened: participant.is_dropout = True participant.CE_choice_9_aux = 1 # Waiting page class WaitPageCM(WaitPage): title_text = ("Si prega di rimanere su questa pagina - La fase 2 finisce a breve") body_text = "" template_name = 'CM_CM_1/Grouping.html' @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS and player.participant.treatment_p < 9 def after_all_players_arrive(group: Group): # Random choice on the spot # randomlist = list(range(1, C.num_CE + 1)) # random.shuffle(randomlist) # group.binding_sit = randomlist[0] # Pre-determined random choice group.binding_sit = 4 playerx = [] for p in group.get_players(): if not p.participant.is_dropout: playerx.append(p) p.participant.random_draw = group.binding_sit print(playerx) # for p in group.get_players(): # previous_player = p.in_round(1) # p.dropout_mark = previous_player.dropout_mark # if p.dropout_mark == 0: # playerx.append(p) # p.participant.random_draw = group.binding_sit # print('players are', playerx) # choices = [1] * playerx[1].num_players # sw = 0 # cont = 1 # while sw == 0: # cont2 = 0 # for p in group.in_round(1).get_players(): # choices[cont2] = choice_binding(p, group) # cont2 = cont2 + 1 # if (choices.count(1) > 1 and choices.count(2) > 1 and choices.count(3) > 1) or \ # (choices.count(1) > 1 and choices.count(2) > 1 and choices.count(3) == 0): # sw = 1 # else: # group.binding_sit = randomlist[cont] # cont = cont + 1 # if cont == 9: # sw = 1 # for p in group.in_round(1).get_players(): playerz = [] for p in group.get_players(): if p.participant.dropout_mark == 0: playerz.append(p) # for p in playerx: print(playerz) for p in playerz: p.participant.group_elec = choice_binding(p, group) p.participant.predA = group_pre_A_(p) p.participant.predB = group_pre_B_(p) p.participant.predC = group_pre_C_(p) group.sum_binding_sit_1 = p.participant.group_elec * (p.participant.group_elec == 1) + group.sum_binding_sit_1 group.sum_binding_sit_2 = int( p.participant.group_elec * (p.participant.group_elec == 2) / 2) + group.sum_binding_sit_2 group.sum_binding_sit_3 = int( p.participant.group_elec * (p.participant.group_elec == 3) / 3) + group.sum_binding_sit_3 for p in playerx: group.id_player_groupA = str(p.id_in_group) * ( p.participant.group_elec == 1) + ',' + group.id_player_groupA group.id_player_groupB = str(p.id_in_group) * ( p.participant.group_elec == 2) + ',' + group.id_player_groupB group.id_player_groupC = str(p.id_in_group) * ( p.participant.group_elec == 3) + ',' + group.id_player_groupC group.list_pred_C_A = str(group_pre_A_(p)) * (p.participant.group_elec == 3) + ',' + group.list_pred_C_A group.list_pred_C_B = str(group_pre_B_(p)) * (p.participant.group_elec == 3) + ',' + group.list_pred_C_B group.list_pred_C_C = str(group_pre_C_(p)) * (p.participant.group_elec == 3) + ',' + group.list_pred_C_C group.list_pred_A_A = str(group_pre_A_(p)) * (p.participant.group_elec == 1) + ',' + group.list_pred_A_A group.list_pred_A_B = str(group_pre_B_(p)) * (p.participant.group_elec == 1) + ',' + group.list_pred_A_B group.list_pred_A_C = str(group_pre_C_(p)) * (p.participant.group_elec == 1) + ',' + group.list_pred_A_C group.list_pred_B_A = str(group_pre_A_(p)) * (p.participant.group_elec == 2) + ',' + group.list_pred_B_A group.list_pred_B_B = str(group_pre_B_(p)) * (p.participant.group_elec == 2) + ',' + group.list_pred_B_B group.list_pred_B_C = str(group_pre_C_(p)) * (p.participant.group_elec == 2) + ',' + group.list_pred_B_C # for p in group.in_round(1).get_players(): for p in playerx: if p.participant.group_elec == 1: p.participant.list_choices_A = " ".join(['A'] * (group.sum_binding_sit_1 - 1)) p.participant.list_choices_B = " ".join(['B'] * (group.sum_binding_sit_2)) p.participant.list_choices_C = " ".join(['C'] * (group.sum_binding_sit_3)) list_pred_A_A_temp = group.list_pred_A_A list_pred_A_B_temp = group.list_pred_A_B list_pred_A_C_temp = group.list_pred_A_C list_pred_A_A_temp2 = list_pred_A_A_temp.split(",") list_pred_A_B_temp2 = list_pred_A_B_temp.split(",") list_pred_A_C_temp2 = list_pred_A_C_temp.split(",") list_pred_A_A_temp3 = [i for i in list_pred_A_A_temp2 if i] list_pred_A_B_temp3 = [i for i in list_pred_A_B_temp2 if i] list_pred_A_C_temp3 = [i for i in list_pred_A_C_temp2 if i] temp_id_resto_temp_ = group.id_player_groupA.split(",") temp_id_resto_temp_2 = [i for i in temp_id_resto_temp_ if i] index_remove = temp_id_resto_temp_2.index(str(p.id_in_group)) del list_pred_A_A_temp3[index_remove] del list_pred_A_B_temp3[index_remove] del list_pred_A_C_temp3[index_remove] p.participant.group_list_pred_A = list_pred_A_A_temp3 p.participant.group_list_pred_B = list_pred_A_B_temp3 p.participant.group_list_pred_C = list_pred_A_C_temp3 temp_id_resto = group.id_player_groupA.replace(str(p.id_in_group), "") temp_id_resto2 = temp_id_resto.split(",") p.participant.id_resto = [i for i in temp_id_resto2 if i] else: if p.participant.group_elec == 2: p.participant.list_choices_A = " ".join(['A'] * (group.sum_binding_sit_1)) p.participant.list_choices_B = " ".join(['B'] * (group.sum_binding_sit_2 - 1)) p.participant.list_choices_C = " ".join(['C'] * (group.sum_binding_sit_3)) list_pred_B_A_temp = group.list_pred_B_A list_pred_B_B_temp = group.list_pred_B_B list_pred_B_C_temp = group.list_pred_B_C list_pred_B_A_temp2 = list_pred_B_A_temp.split(",") list_pred_B_B_temp2 = list_pred_B_B_temp.split(",") list_pred_B_C_temp2 = list_pred_B_C_temp.split(",") list_pred_B_A_temp3 = [i for i in list_pred_B_A_temp2 if i] list_pred_B_B_temp3 = [i for i in list_pred_B_B_temp2 if i] list_pred_B_C_temp3 = [i for i in list_pred_B_C_temp2 if i] temp_id_resto_temp_ = group.id_player_groupB.split(",") temp_id_resto_temp_2 = [i for i in temp_id_resto_temp_ if i] index_remove = temp_id_resto_temp_2.index(str(p.id_in_group)) del list_pred_B_A_temp3[index_remove] del list_pred_B_B_temp3[index_remove] del list_pred_B_C_temp3[index_remove] p.participant.group_list_pred_A = list_pred_B_A_temp3 p.participant.group_list_pred_B = list_pred_B_B_temp3 p.participant.group_list_pred_C = list_pred_B_C_temp3 temp_id_resto = group.id_player_groupB.replace(str(p.id_in_group), "") temp_id_resto2 = temp_id_resto.split(",") p.participant.id_resto = [i for i in temp_id_resto2 if i] else: p.participant.list_choices_A = " ".join(['A'] * (group.sum_binding_sit_1)) p.participant.list_choices_B = " ".join(['B'] * (group.sum_binding_sit_2)) p.participant.list_choices_C = " ".join(['C'] * (group.sum_binding_sit_3 - 1)) list_pred_C_A_temp = group.list_pred_C_A list_pred_C_B_temp = group.list_pred_C_B list_pred_C_C_temp = group.list_pred_C_C list_pred_C_A_temp2 = list_pred_C_A_temp.split(",") list_pred_C_B_temp2 = list_pred_C_B_temp.split(",") list_pred_C_C_temp2 = list_pred_C_C_temp.split(",") list_pred_C_A_temp3 = [i for i in list_pred_C_A_temp2 if i] list_pred_C_B_temp3 = [i for i in list_pred_C_B_temp2 if i] list_pred_C_C_temp3 = [i for i in list_pred_C_C_temp2 if i] temp_id_resto_temp_ = group.id_player_groupC.split(",") temp_id_resto_temp_2 = [i for i in temp_id_resto_temp_ if i] index_remove = temp_id_resto_temp_2.index(str(p.id_in_group)) del list_pred_C_A_temp3[index_remove] del list_pred_C_B_temp3[index_remove] del list_pred_C_C_temp3[index_remove] p.participant.group_list_pred_A = list_pred_C_A_temp3 p.participant.group_list_pred_B = list_pred_C_B_temp3 p.participant.group_list_pred_C = list_pred_C_C_temp3 temp_id_resto = group.id_player_groupC.replace(str(p.id_in_group), "") temp_id_resto2 = temp_id_resto.split(",") p.participant.id_resto = [i for i in temp_id_resto2 if i] def app_after_this_page(player: Player, upcoming_apps): if player.participant.is_dropout == True: return 'CM_final_no_group' class WaitPageCMaux(WaitPage): title_text = ("Si prega di rimanere su questa pagina - La fase 2 finisce a breve") body_text = "" template_name = 'CM_CM_1/Grouping.html' @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS and player.participant.treatment_p == 9 def after_all_players_arrive(group: Group): group.binding_sit = 4 playerx = [] for p in group.get_players(): if not p.participant.is_dropout: playerx.append(p) for p in playerx: p.participant.random_draw = group.binding_sit p.participant.dc_choice = choice_binding(p, group) p.participant.cm_choice = group_aux(p) for p in playerx: other_dc = [] other_cm = [] others = p.get_others_in_group() for other in others: if other in playerx: if other.participant.dc_choice == p.participant.dc_choice: other_dc.append(other.participant.dc_choice) other_cm.append(other.participant.cm_choice) no_choices = len(other_dc) if no_choices > 0: rand_substitute = random.randint(1, no_choices) p.participant.substitute = other_cm[rand_substitute-1] else: p.participant.substitute = p.participant.cm_choice print("other dc", other_dc) print("other cm", other_cm) print("substitute is", p.participant.substitute) def app_after_this_page(player: Player, upcoming_apps): if player.participant.is_dropout == True: return 'CM_final_no_group' page_sequence = [ GBAT, Pre_CM, CM_1, CM_2, CM_3, CM_4, CM_5, CM_6, CM_7, CM_8, CM_9, CM_1_aux, CM_2_aux, CM_3_aux, CM_4_aux, CM_5_aux, CM_6_aux, CM_7_aux, CM_8_aux, CM_9_aux, WaitPageCM, WaitPageCMaux ]