from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) from . import widgets as school_choice_widgets import random from django import forms from PIL import Image doc = """ This is a one-period public goods game with 2 players. """ class Constants(BaseConstants): name_in_url = 'Inequality_Study_Free' players_per_group = 8 num_rounds = 16 endowment = c(100) instructions_template = 'Inequality_Study_Free/instructions.html' #https://www.w3schools.com/colors/colors_picker.asp?colorhex=E8B5CE img = Image.open("C:/Users/Dia_Type1.jpg") choicesButtons =[ ('HH','Type 1','#99ccff'), ('HL','Type 2','#99ffcc'), ('LH','Type 3','#ffccff'), ('LL','Type 4','#ffcc99') ] class Subsession(BaseSubsession): def SetType(self): pass # q = 1 # for p in self.get_players(): # play_num = q # q = q + 1 # print(play_num) # if play_num >= 1: # for g in self.get_groups(): # p1 = g.get_player_by_id(1) # p1.endowment = 75 # p1.multiplier = 1.7 # p1.type = "HH" # if play_num >= 2: # for g in self.get_groups(): # p2 = g.get_player_by_id(2) # p2.endowment = 75 # p2.multiplier = 1.7 # p2.type = "HH" # if play_num >= 3: # for g in self.get_groups(): # p3 = g.get_player_by_id(3) # p3.endowment = 75 # p3.multiplier = 1.3 # p3.type = "HL" # if play_num >= 4: # for g in self.get_groups(): # p4 = g.get_player_by_id(4) # p4.endowment = 75 # p4.multiplier = 1.3 # p4.type = "HL" # if play_num >= 5: # for g in self.get_groups(): # p5 = g.get_player_by_id(5) # p5.endowment = 25 # p5.multiplier = 1.7 # p5.type = "LH" # if play_num >= 6: # for g in self.get_groups(): # p6 = g.get_player_by_id(6) # p6.endowment = 25 # p6.multiplier = 1.7 # p6.type = "LH" # if play_num >= 7: # for g in self.get_groups(): # p7 = g.get_player_by_id(7) # p7.endowment = 25 # p7.multiplier = 1.3 # p7.type = "LL" # if play_num >= 8: # for g in self.get_groups(): # p8 = g.get_player_by_id(8) # p8.endowment = 25 # p8.multiplier = 1.3 # p8.type = "LL" # # if play_num >= 9: # for g in self.get_groups(): # p9 = g.get_player_by_id(9) # p9.endowment = 75 # p9.multiplier = 1.7 # p9.type = "HH" # if play_num >= 10: # for g in self.get_groups(): # p10 = g.get_player_by_id(10) # p10.endowment = 75 # p10.multiplier = 1.7 # p10.type = "HH" # if play_num >= 11: # for g in self.get_groups(): # p11 = g.get_player_by_id(11) # p11.endowment = 75 # p11.multiplier = 1.3 # p11.type = "HL" # if play_num >= 12: # for g in self.get_groups(): # p12 = g.get_player_by_id(12) # p12.endowment = 75 # p12.multiplier = 1.3 # p12.type = "HL" # if play_num >= 13: # for g in self.get_groups(): # p13 = g.get_player_by_id(13) # p13.endowment = 25 # p13.multiplier = 1.7 # p13.type = "LH" # if play_num >= 14: # for g in self.get_groups(): # p14 = g.get_player_by_id(14) # p14.endowment = 25 # p14.multiplier = 1.7 # p14.type = "LH" # if play_num >= 15: # for g in self.get_groups(): # p15 = g.get_player_by_id(15) # p15.endowment = 25 # p15.multiplier = 1.3 # p15.type = "LL" # if play_num >= 16: # for g in self.get_groups(): # p16 = g.get_player_by_id(16) # p16.endowment = 25 # p16.multiplier = 1.3 # p16.type = "LL" def belief_preferences_set(self): for p in self.get_players(): if p.beliefs_preference[:2] == "HH": p.beliefs_partnerchoice1 = "HH" else: if p.beliefs_preference[:2] == "HL": p.beliefs_partnerchoice1 = "HL" else: if p.beliefs_preference[:2] == "LH": p.beliefs_partnerchoice1 = "LH" else: p.beliefs_partnerchoice1 = "LL" for p in self.get_players(): if p.beliefs_preference[3:5] == "HH": p.beliefs_partnerchoice2 = "HH" else: if p.beliefs_preference[3:5] == "HL": p.beliefs_partnerchoice2 = "HL" else: if p.beliefs_preference[3:5] == "LH": p.beliefs_partnerchoice2 = "LH" else: p.beliefs_partnerchoice2 = "LL" for p in self.get_players(): if p.beliefs_preference[6:8] == "HH": p.beliefs_partnerchoice3 = "HH" else: if p.beliefs_preference[6:8] == "HL": p.beliefs_partnerchoice3 = "HL" else: if p.beliefs_preference[6:8] == "LH": p.beliefs_partnerchoice3 = "LH" else: p.beliefs_partnerchoice3 = "LL" for p in self.get_players(): if p.beliefs_preference[-2:] == "HH": p.beliefs_partnerchoice4 = "HH" else: if p.beliefs_preference[-2:] == "HL": p.beliefs_partnerchoice4 = "HL" else: if p.beliefs_preference[-2:] == "LH": p.beliefs_partnerchoice4 = "LH" else: p.beliefs_partnerchoice4 = "LL" for p in self.get_players(): p.participant.vars['beliefs_preference'] = p.beliefs_preference p.participant.vars['beliefs_partnerchoice1'] = p.beliefs_partnerchoice1 p.participant.vars['beliefs_partnerchoice2'] = p.beliefs_partnerchoice2 p.participant.vars['beliefs_partnerchoice3'] = p.beliefs_partnerchoice3 p.participant.vars['beliefs_partnerchoice4'] = p.beliefs_partnerchoice4 for p in self.get_players(): if p.id_in_subsession != 9: print(p.beliefs_preference) print("partnerchoice1") print(p.beliefs_partnerchoice1) print("partnerchoice1") print(p.beliefs_partnerchoice2) print("partnerchoice2") print(p.beliefs_partnerchoice3) print("partnerchoice3") print(p.beliefs_partnerchoice4) print("partnerchoice4") print(p.beliefs_partnerchoice4) def random_group_order(self): self.group_randomly() print(self.get_group_matrix()) def do_my_shuffle(self): ##get all players in the session players = self.get_players() ##calculate total numbers of players in the session num_players = self.session.num_participants ##connect all players to in-game participant number/variables for x in range(num_players): players[int(x)] = "p"+str(x+1) print(players) print("p"+str(x+1)+".partnerchoice1") print("p"+str(x+1)+".partnerchoice2") print("p"+str(x+1)+".partnerchoice3") print("p"+str(x+1)+".partnerchoice4") print("p"+str(x+1)+".multiplier") print("p"+str(x+1)+".match") print("p"+str(x+1)+".endowment") print("p"+str(x+1)+".type") for p_self in self.get_players(): if p_self.id_in_subsession != 9: print("preference:") print(p_self.preference) print(len(p_self.preference)) for p in self.get_players(): if p.preference[:2] == "HH": p.partnerchoice1 = "HH" else: if p.preference[:2] == "HL": p.partnerchoice1 = "HL" else: if p.preference[:2] == "LH": p.partnerchoice1 = "LH" else: p.partnerchoice1 = "LL" for p in self.get_players(): print("partnerchoice1") print(p.partnerchoice1) for p in self.get_players(): if p.preference[3:5] == "HH": p.partnerchoice2 = "HH" else: if p.preference[3:5] == "HL": p.partnerchoice2 = "HL" else: if p.preference[3:5] == "LH": p.partnerchoice2 = "LH" else: p.partnerchoice2 = "LL" for p in self.get_players(): print("partnerchoice2") print(p.partnerchoice2) for p in self.get_players(): if p.preference[6:8] == "HH": p.partnerchoice3 = "HH" else: if p.preference[6:8] == "HL": p.partnerchoice3 = "HL" else: if p.preference[6:8] == "LH": p.partnerchoice3 = "LH" else: p.partnerchoice3 = "LL" for p in self.get_players(): print("partnerchoice3") print(p.partnerchoice3) for p in self.get_players(): if p.preference[-2:] == "HH": p.partnerchoice4 = "HH" else: if p.preference[-2:] == "HL": p.partnerchoice4 = "HL" else: if p.preference[-2:] == "LH": p.partnerchoice4 = "LH" else: p.partnerchoice4 = "LL" for p in self.get_players(): print("partnerchoice4") print(p.partnerchoice4) if self.round_number == 1: for p in self.get_players(): if p.preference[:2] == "HH": p.partnerchoice1_HH_1 = 1 else: if p.preference[:2] == "HL": p.partnerchoice1_HL_1 = 1 else: if p.preference[:2] == "LH": p.partnerchoice1_LH_1 = 1 else: if p.preference[:2] == "LL": p.partnerchoice1_LL_1 = 1 if self.round_number > 1: for p in self.get_players(): if p.preference[:2] == "HH": p.partnerchoice1_HH_1 = p.in_round(self.round_number-1).partnerchoice1_HH_1 + 1 p.partnerchoice1_HL_1 = p.participant.vars['partnerchoice1_HL_1'] p.partnerchoice1_LH_1 = p.participant.vars['partnerchoice1_LH_1'] p.partnerchoice1_LL_1 = p.participant.vars['partnerchoice1_LL_1'] else: if p.preference[:2] == "HL": p.partnerchoice1_HL_1 = p.partnerchoice1_HL_1 = p.in_round(self.round_number-1).partnerchoice1_HL_1 + 1 p.partnerchoice1_HH_1 = p.participant.vars['partnerchoice1_HH_1'] p.partnerchoice1_LH_1 = p.participant.vars['partnerchoice1_LH_1'] p.partnerchoice1_LL_1 = p.participant.vars['partnerchoice1_LL_1'] else: if p.preference[:2] == "LH": p.partnerchoice1_LH_1 = p.partnerchoice1_LH_1 = p.in_round(self.round_number-1).partnerchoice1_LH_1 + 1 p.partnerchoice1_HH_1 = p.participant.vars['partnerchoice1_HH_1'] p.partnerchoice1_HL_1 = p.participant.vars['partnerchoice1_HL_1'] p.partnerchoice1_LL_1 = p.participant.vars['partnerchoice1_LL_1'] else: if p.preference[:2] == "LL": p.partnerchoice1_LL_1 = p.partnerchoice1_LL_1 = p.in_round(self.round_number-1).partnerchoice1_LL_1 + 1 p.partnerchoice1_HH_1 = p.participant.vars['partnerchoice1_HH_1'] p.partnerchoice1_HL_1 = p.participant.vars['partnerchoice1_HL_1'] p.partnerchoice1_LH_1 = p.participant.vars['partnerchoice1_LH_1'] if self.round_number == 1: for p in self.get_players(): if p.preference[3:5] == "HH": p.partnerchoice1_HH_2 = 1 else: if p.preference[3:5] == "HL": p.partnerchoice1_HL_2 = 1 else: if p.preference[3:5] == "LH": p.partnerchoice1_LH_2 = 1 else: if p.preference[3:5] == "LL": p.partnerchoice1_LL_2 = 1 if self.round_number > 1: for p in self.get_players(): if p.preference[3:5] == "HH": p.partnerchoice1_HH_2 = p.in_round(self.round_number-1).partnerchoice1_HH_2 + 1 p.partnerchoice1_HL_2 = p.participant.vars['partnerchoice1_HL_2'] p.partnerchoice1_LH_2 = p.participant.vars['partnerchoice1_LH_2'] p.partnerchoice1_LL_2 = p.participant.vars['partnerchoice1_LL_2'] else: if p.preference[3:5] == "HL": p.partnerchoice1_HL_2 = p.partnerchoice1_HL_2 = p.in_round(self.round_number-1).partnerchoice1_HL_2 + 1 p.partnerchoice1_HH_2 = p.participant.vars['partnerchoice1_HH_2'] p.partnerchoice1_LH_2 = p.participant.vars['partnerchoice1_LH_2'] p.partnerchoice1_LL_2 = p.participant.vars['partnerchoice1_LL_2'] else: if p.preference[3:5] == "LH": p.partnerchoice1_LH_2 = p.partnerchoice1_LH_2 = p.in_round(self.round_number-1).partnerchoice1_LH_2 + 1 p.partnerchoice1_HH_2 = p.participant.vars['partnerchoice1_HH_2'] p.partnerchoice1_HL_2 = p.participant.vars['partnerchoice1_HL_2'] p.partnerchoice1_LL_2 = p.participant.vars['partnerchoice1_LL_2'] else: if p.preference[3:5] == "LL": p.partnerchoice1_LL_2 = p.partnerchoice1_LL_2 = p.in_round(self.round_number-1).partnerchoice1_LL_2 + 1 p.partnerchoice1_HH_2 = p.participant.vars['partnerchoice1_HH_2'] p.partnerchoice1_HL_2 = p.participant.vars['partnerchoice1_HL_2'] p.partnerchoice1_LH_2 = p.participant.vars['partnerchoice1_LH_2'] if self.round_number == 1: for p in self.get_players(): if p.preference[6:8] == "HH": p.partnerchoice1_HH_3 = 1 else: if p.preference[6:8] == "HL": p.partnerchoice1_HL_3 = 1 else: if p.preference[6:8] == "LH": p.partnerchoice1_LH_3 = 1 else: if p.preference[6:8] == "LL": p.partnerchoice1_LL_3 = 1 if self.round_number > 1: for p in self.get_players(): if p.id_in_subsession != 9: if p.preference[6:8] == "HH": p.partnerchoice1_HH_3 = p.in_round(self.round_number-1).partnerchoice1_HH_3 + 1 p.partnerchoice1_HL_3 = p.participant.vars['partnerchoice1_HL_3'] p.partnerchoice1_LH_3 = p.participant.vars['partnerchoice1_LH_3'] p.partnerchoice1_LL_3 = p.participant.vars['partnerchoice1_LL_3'] else: if p.preference[6:8] == "HL": p.partnerchoice1_HL_3 = p.partnerchoice1_HL_3 = p.in_round(self.round_number-1).partnerchoice1_HL_3 + 1 p.partnerchoice1_HH_3 = p.participant.vars['partnerchoice1_HH_3'] p.partnerchoice1_LH_3 = p.participant.vars['partnerchoice1_LH_3'] p.partnerchoice1_LL_3 = p.participant.vars['partnerchoice1_LL_3'] else: if p.preference[6:8] == "LH": p.partnerchoice1_LH_3 = p.partnerchoice1_LH_3 = p.in_round(self.round_number-1).partnerchoice1_LH_3 + 1 p.partnerchoice1_HH_3 = p.participant.vars['partnerchoice1_HH_3'] p.partnerchoice1_HL_3 = p.participant.vars['partnerchoice1_HL_3'] p.partnerchoice1_LL_3 = p.participant.vars['partnerchoice1_LL_3'] else: if p.preference[6:8] == "LL": p.partnerchoice1_LL_3 = p.partnerchoice1_LL_3 = p.in_round(self.round_number-1).partnerchoice1_LL_3 + 1 p.partnerchoice1_HH_3 = p.participant.vars['partnerchoice1_HH_3'] p.partnerchoice1_HL_3 = p.participant.vars['partnerchoice1_HL_3'] p.partnerchoice1_LH_3 = p.participant.vars['partnerchoice1_LH_3'] if self.round_number == 1: for p in self.get_players(): if p.preference[-2:] == "HH": p.partnerchoice1_HH_4 = 1 else: if p.preference[-2:] == "HL": p.partnerchoice1_HL_4 = 1 else: if p.preference[-2:] == "LH": p.partnerchoice1_LH_4 = 1 else: if p.preference[-2:] == "LL": p.partnerchoice1_LL_4 = 1 if self.round_number > 1: for p in self.get_players(): if p.preference[-2:] == "HH": p.partnerchoice1_HH_4 = p.in_round(self.round_number-1).partnerchoice1_HH_4 + 1 p.partnerchoice1_HL_4 = p.participant.vars['partnerchoice1_HL_4'] p.partnerchoice1_LH_4 = p.participant.vars['partnerchoice1_LH_4'] p.partnerchoice1_LL_4 = p.participant.vars['partnerchoice1_LL_4'] else: if p.preference[-2:] == "HL": p.partnerchoice1_HL_4 = p.partnerchoice1_HL_4 = p.in_round(self.round_number-1).partnerchoice1_HL_4 + 1 p.partnerchoice1_HH_4 = p.participant.vars['partnerchoice1_HH_4'] p.partnerchoice1_LH_4 = p.participant.vars['partnerchoice1_LH_4'] p.partnerchoice1_LL_4 = p.participant.vars['partnerchoice1_LL_4'] else: if p.preference[-2:] == "LH": p.partnerchoice1_LH_4 = p.partnerchoice1_LH_4 = p.in_round(self.round_number-1).partnerchoice1_LH_4 + 1 p.partnerchoice1_HH_4 = p.participant.vars['partnerchoice1_HH_4'] p.partnerchoice1_HL_4 = p.participant.vars['partnerchoice1_HL_4'] p.partnerchoice1_LL_4 = p.participant.vars['partnerchoice1_LL_4'] else: if p.preference[-2:] == "LL": p.partnerchoice1_LL_4 = p.partnerchoice1_LL_4 = p.in_round(self.round_number-1).partnerchoice1_LL_4 + 1 p.partnerchoice1_HH_4 = p.participant.vars['partnerchoice1_HH_4'] p.partnerchoice1_HL_4 = p.participant.vars['partnerchoice1_HL_4'] p.partnerchoice1_LH_4 = p.participant.vars['partnerchoice1_LH_4'] for p in self.get_players(): p.participant.vars['partnerchoice1_HH_1'] = p.partnerchoice1_HH_1 p.participant.vars['partnerchoice1_HH_2'] = p.partnerchoice1_HH_2 p.participant.vars['partnerchoice1_HH_3'] = p.partnerchoice1_HH_3 p.participant.vars['partnerchoice1_HH_4'] = p.partnerchoice1_HH_4 p.participant.vars['partnerchoice1_HL_1'] = p.partnerchoice1_HL_1 p.participant.vars['partnerchoice1_HL_2'] = p.partnerchoice1_HL_2 p.participant.vars['partnerchoice1_HL_3'] = p.partnerchoice1_HL_3 p.participant.vars['partnerchoice1_HL_4'] = p.partnerchoice1_HL_4 p.participant.vars['partnerchoice1_LH_1'] = p.partnerchoice1_LH_1 p.participant.vars['partnerchoice1_LH_2'] = p.partnerchoice1_LH_2 p.participant.vars['partnerchoice1_LH_3'] = p.partnerchoice1_LH_3 p.participant.vars['partnerchoice1_LH_4'] = p.partnerchoice1_LH_4 p.participant.vars['partnerchoice1_LL_1'] = p.partnerchoice1_LL_1 p.participant.vars['partnerchoice1_LL_2'] = p.partnerchoice1_LL_2 p.participant.vars['partnerchoice1_LL_3'] = p.partnerchoice1_LL_3 p.participant.vars['partnerchoice1_LL_4'] = p.partnerchoice1_LL_4 ##create new variables for in the loop to count loop itterations x = 0 d={} for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): p_self.match = 0 p_other.match = 0 for p_self in self.get_players(): if p_self.id_in_subsession == 9: p_self.match == 1 ##loop decision for matched choices 1st / 1st for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if p_self.partnerchoice1 == p_other.type and p_other.partnerchoice1 == p_self.type and p_self.match == 0 and p_other.match == 0: x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 1/1:") print(p_self.match) ##loop decision for matched choices 1st / 2nd for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if (p_self.partnerchoice1 == p_other.type and p_other.partnerchoice2 == p_self.type and p_self.match == 0 and p_other.match == 0) or (p_self.partnerchoice2 == p_other.type and p_other.partnerchoice1 == p_self.type and p_self.match == 0 and p_other.match == 0): x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 1/2:") print(p_self.match) ##loop decision for matched choices 2nd / 2nd for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if p_self.partnerchoice2 == p_other.type and p_other.partnerchoice2 == p_self.type and p_self.match == 0 and p_other.match == 0: x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 2/2:") print(p_self.match) ##loop decision for matched choices 1st / 3rd for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if (p_self.partnerchoice1 == p_other.type and p_other.partnerchoice3 == p_self.type and p_self.match == 0 and p_other.match == 0) or (p_self.partnerchoice3 == p_other.type and p_other.partnerchoice1 == p_self.type and p_self.match == 0 and p_other.match == 0): x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 1/3:") print(p_self.match) ##loop decision for matched choices 2nd / 3rd for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if (p_self.partnerchoice2 == p_other.type and p_other.partnerchoice3 == p_self.type and p_self.match == 0 and p_other.match == 0) or (p_self.partnerchoice3 == p_other.type and p_other.partnerchoice2 == p_self.type and p_self.match == 0 and p_other.match == 0): x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 2/3:") print(p_self.match) ##loop decision for matched choices 3rd / 3rd for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if p_self.partnerchoice3 == p_other.type and p_other.partnerchoice3 == p_self.type and p_self.match == 0 and p_other.match == 0: x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 3/3:") print(p_self.match) ##loop decision for matched choices 1st / 4th for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if (p_self.partnerchoice1 == p_other.type and p_other.partnerchoice4 == p_self.type and p_self.match == 0 and p_other.match == 0) or (p_self.partnerchoice4 == p_other.type and p_other.partnerchoice1 == p_self.type and p_self.match == 0 and p_other.match == 0): x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 1/4:") print(p_self.match) ##loop decision for matched choices 2nd / 4th for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if (p_self.partnerchoice2 == p_other.type and p_other.partnerchoice4 == p_self.type and p_self.match == 0 and p_other.match == 0) or (p_self.partnerchoice4 == p_other.type and p_other.partnerchoice2 == p_self.type and p_self.match == 0 and p_other.match == 0): x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 2/4:") print(p_self.match) ##loop decision for matched choices 3rd / 4th for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if (p_self.partnerchoice3 == p_other.type and p_other.partnerchoice4 == p_self.type and p_self.match == 0 and p_other.match == 0) or (p_self.partnerchoice4 == p_other.type and p_other.partnerchoice3 == p_self.type and p_self.match == 0 and p_other.match == 0): x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 3/4:") print(p_self.match) ##loop decision for matched choices 4th / 4th for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if p_self.partnerchoice4 == p_other.type and p_other.partnerchoice4 == p_self.type and p_self.match == 0 and p_other.match == 0: x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after 4/4:") print(p_self.match) ##couple remaining players who did not got a match for p_self in self.get_players(): for p_other in p_self.get_others_in_group(): if p_self.match == 0 and p_other.match == 0: x = x + 1 d["group_matrix{0}".format(x)]=[p_self,p_other] p_other.match = 1 p_self.match = 1 for p_self in self.get_players(): print("match self after final:") print(p_self.match) # for p_self in self.get_players(): # if p_self.id_in_subsession == 9: # d["group_matrix{0}".format(5)]=[p_self, 10] import pandas as pd group_matrix_df = pd.DataFrame(d) #groups are now in data frame group_matrix_dfT = group_matrix_df.T #change rows and collumns here group_matrix_list = group_matrix_dfT.values.tolist() #make list so that it can be used for setting groups #check if everything works correctly print("group_matrix_df:") print(group_matrix_df) print("group_matrix_dfT:") print(group_matrix_dfT) print("group_matrix_list") print(group_matrix_list) #create groups and print them self.set_group_matrix(group_matrix_list) print(self.get_group_matrix()) class Group(BaseGroup): def random_group_order(self): self.subsession.random_group_order() def do_my_shuffle(self): self.subsession.do_my_shuffle() def belief_preferences_set(self): self.subsession.belief_preferences_set() def SetType(self): q = 1 for p in self.get_players(): play_num = q q = q + 1 print(play_num) p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) p3 = self.get_player_by_id(3) p4 = self.get_player_by_id(4) p5 = self.get_player_by_id(5) p6 = self.get_player_by_id(6) p7 = self.get_player_by_id(7) p8 = self.get_player_by_id(8) if play_num >= 1: p1.endowment = 75 p1.multiplier = 1.7 p1.type = "HH" print("p1") print(p1.type) if play_num >= 2: p2.endowment = 75 p2.multiplier = 1.7 p2.type = "HH" print("p2") print(p2.type) if play_num >= 3: p3.endowment = 75 p3.multiplier = 1.3 p3.type = "HL" print("p3") print(p3.type) if play_num >= 4: p4.endowment = 75 p4.multiplier = 1.3 p4.type = "HL" print("p4") print(p4.type) if play_num >= 5: p5.endowment = 25 p5.multiplier = 1.7 p5.type = "LH" print("p5") print(p5.type) if play_num >= 6: p6.endowment = 25 p6.multiplier = 1.7 p6.type = "LH" print("p6") print(p6.type) if play_num >= 7: p7.endowment = 25 p7.multiplier = 1.3 p7.type = "LL" if play_num >= 8: p8.endowment = 25 p8.multiplier = 1.3 p8.type = "LL" for p in self.get_players(): p.participant.vars['multiplier'] = p.multiplier p.participant.vars['endowment'] = p.endowment p.participant.vars['type'] = p.type p.participant.vars['condition'] = p.condition total_contribution = models.CurrencyField() individual_share = models.CurrencyField() def set_type(self): for p in self.get_players(): if p.multiplier == 1.7 and p.endowment == 75: p.type = "HH" p.condition = self.session.config['condition'] for p in self.get_players(): if p.multiplier == 1.3 and p.endowment == 75: p.type = "HL" p.condition = self.session.config['condition'] for p in self.get_players(): if p.multiplier == 1.7 and p.endowment == 25: p.type = "LH" p.condition = self.session.config['condition'] for p in self.get_players(): if p.multiplier == 1.3 and p.endowment == 25: p.type = "LL" p.condition = self.session.config['condition'] for p in self.get_players(): if p.multiplier == 0 and p.endowment == 0: p.type = "" p.condition = self.session.config['condition'] x = 1 for p in self.get_players(): p.random_id = x x = x + 1 for p in self.get_players(): print(p.random_id) for p in self.get_players(): p.participant.vars['multiplier'] = p.multiplier p.participant.vars['endowment'] = p.endowment p.participant.vars['type'] = p.type p.participant.vars['random_id'] = p.random_id p.participant.vars['condition'] = p.condition def set_payoffs(self): print(self.round_number) #calculate pay-offs p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) p1.addedtoPGG = p1.contribution * p1.multiplier p2.addedtoPGG = p2.contribution * p2.multiplier self.total_contribution = p1.addedtoPGG + p2.addedtoPGG self.individual_share = self.total_contribution / 2 p1.kept = (p1.endowment - p1.contribution) p2.kept = (p2.endowment - p2.contribution) p1.earnings = self.individual_share + (p1.endowment - p1.contribution) p2.earnings = self.individual_share + (p2.endowment - p2.contribution) #count accumulated_wealth if self.round_number == 1: p1.accumulated_wealth = p1.earnings p2.accumulated_wealth = p2.earnings else: p1.accumulated_wealth = p1.in_round(self.round_number-1).accumulated_wealth + p1.earnings p2.accumulated_wealth = p2.in_round(self.round_number-1).accumulated_wealth + p2.earnings #count accumulated_con if self.round_number == 1: p1.accumulated_con = p1.contribution p2.accumulated_con = p2.contribution else: p1.accumulated_con = p1.in_round(self.round_number-1).accumulated_con + p1.contribution p2.accumulated_con = p2.in_round(self.round_number-1).accumulated_con + p2.contribution for p in self.get_players(): p.participant.vars['earnings'] = p.earnings p.participant.vars['accumulated_wealth'] = p.accumulated_wealth if self.round_number == 1: #in first round there is no info yet if p2.type == "HH": p1.con_other1 = p2.contribution p1.con_other2 = 0 p1.con_other3 = 0 p1.con_other4 = 0 else: if p2.type == "HL": p1.con_other2 = p2.contribution p1.con_other1 = 0 p1.con_other3 = 0 p1.con_other4 = 0 else: if p2.type == "LH": p1.con_other3 = p2.contribution p1.con_other1 = 0 p1.con_other2 = 0 p1.con_other4 = 0 else: p1.con_other4 = p2.contribution p1.con_other1 = 0 p1.con_other2 = 0 p1.con_other3 = 0 if self.round_number > 1: if p2.type == "HH": p1.con_other1 = p1.in_round(self.round_number-1).con_other1 + p2.contribution p1.con_other2 = p1.in_round(self.round_number-1).con_other2 p1.con_other3 = p1.in_round(self.round_number-1).con_other3 p1.con_other4 = p1.in_round(self.round_number-1).con_other4 else: if p2.type == "HL": p1.con_other2 = p1.in_round(self.round_number-1).con_other2 + p2.contribution p1.con_other1 = p1.in_round(self.round_number-1).con_other1 p1.con_other3 = p1.in_round(self.round_number-1).con_other3 p1.con_other4 = p1.in_round(self.round_number-1).con_other4 else: if p2.type == "LH": p1.con_other3 = p1.in_round(self.round_number-1).con_other3 + p2.contribution p1.con_other1 = p1.in_round(self.round_number-1).con_other1 p1.con_other2 = p1.in_round(self.round_number-1).con_other2 p1.con_other4 = p1.in_round(self.round_number-1).con_other4 else: p1.con_other4 = p1.in_round(self.round_number-1).con_other4 + p2.contribution p1.con_other1 = p1.in_round(self.round_number-1).con_other1 p1.con_other2 = p1.in_round(self.round_number-1).con_other2 p1.con_other3 = p1.in_round(self.round_number-1).con_other3 if self.round_number == 1: #in first round there is no info yet if p1.type == "HH": p2.con_other1 = p1.contribution p2.con_other2 = 0 p2.con_other3 = 0 p2.con_other4 = 0 else: if p1.type == "HL": p2.con_other2 = p1.contribution p2.con_other1 = 0 p2.con_other3 = 0 p2.con_other4 = 0 else: if p1.type == "LH": p2.con_other3 = p1.contribution p2.con_other1 = 0 p2.con_other2 = 0 p2.con_other4 = 0 else: p2.con_other4 = p1.contribution p2.con_other1 = 0 p2.con_other2 = 0 p2.con_other3 = 0 if self.round_number > 1: if p1.type == "HH": p2.con_other1 = p2.in_round(self.round_number-1).con_other1 + p1.contribution p2.con_other2 = p2.in_round(self.round_number-1).con_other2 p2.con_other3 = p2.in_round(self.round_number-1).con_other3 p2.con_other4 = p2.in_round(self.round_number-1).con_other4 else: if p1.type == "HL": p2.con_other2 = p2.in_round(self.round_number-1).con_other2 + p1.contribution p2.con_other1 = p2.in_round(self.round_number-1).con_other1 p2.con_other3 = p2.in_round(self.round_number-1).con_other3 p2.con_other4 = p2.in_round(self.round_number-1).con_other4 else: if p1.type == "LH": p2.con_other3 = p2.in_round(self.round_number-1).con_other3 + p1.contribution p2.con_other1 = p2.in_round(self.round_number-1).con_other1 p2.con_other2 = p2.in_round(self.round_number-1).con_other2 p2.con_other4 = p2.in_round(self.round_number-1).con_other4 else: p2.con_other4 = p2.in_round(self.round_number-1).con_other4 + p1.contribution p2.con_other1 = p2.in_round(self.round_number-1).con_other1 p2.con_other2 = p2.in_round(self.round_number-1).con_other2 p2.con_other3 = p2.in_round(self.round_number-1).con_other3 if self.round_number > 1: p1.beliefs_HH = p1.in_round(self.round_number-1).beliefs_HH p1.beliefs_HL = p1.in_round(self.round_number-1).beliefs_HL p1.beliefs_LH = p1.in_round(self.round_number-1).beliefs_LH p1.beliefs_LL = p1.in_round(self.round_number-1).beliefs_LL p2.beliefs_HH = p2.in_round(self.round_number-1).beliefs_HH p2.beliefs_HL = p2.in_round(self.round_number-1).beliefs_HL p2.beliefs_LH = p2.in_round(self.round_number-1).beliefs_LH p2.beliefs_LL = p2.in_round(self.round_number-1).beliefs_LL print("Con Partner") print("p1 other1:") print(p1.con_other1) print("p1 other2:") print(p1.con_other2) print("p1 other3:") print(p1.con_other3) print("p1 other4:") print(p1.con_other4) print("---") print("p2 other1:") print(p2.con_other1) print("p2 other2:") print(p2.con_other2) print("p2 other3:") print(p2.con_other3) print("p2 other4:") print(p2.con_other4) print("believes HH =") print(p1.beliefs_HH) print("believes HL =") print(p1.beliefs_HL) print("believes LH =") print(p1.beliefs_LH) print("believes LL =") print(p1.beliefs_LL) print("believes HH =") print(p2.beliefs_HH) print("believes HL =") print(p2.beliefs_HL) print("believes LH =") print(p2.beliefs_LH) print("believes LL =") print(p2.beliefs_LL) #count previous partner for player 1 if self.round_number == 1: #in first round there is no info yet if p2.type == "HH": p1.coupled_HH = p1.coupled_HH + 1 p1.coupled_HL = p1.coupled_HL p1.coupled_LH = p1.coupled_LH p1.coupled_LL = p1.coupled_LL else: if p2.type == "HL": p1.coupled_HL = p1.coupled_HL + 1 p1.coupled_HH = p1.coupled_HH p1.coupled_LH = p1.coupled_LH p1.coupled_LL = p1.coupled_LL else: if p2.type == "LH": p1.coupled_LH = p1.coupled_LH + 1 p1.coupled_HH = p1.coupled_HH p1.coupled_HL = p1.coupled_HL p1.coupled_LL = p1.coupled_LL else: p1.coupled_LL = p1.coupled_LL + 1 p1.coupled_HH = p1.coupled_HH p1.coupled_HL = p1.coupled_HL p1.coupled_LH = p1.coupled_LH if self.round_number > 1: if p2.type == "HH": p1.coupled_HH = p1.in_round(self.round_number-1).coupled_HH + 1 p1.coupled_HL = p1.in_round(self.round_number-1).coupled_HL p1.coupled_LH = p1.in_round(self.round_number-1).coupled_LH p1.coupled_LL = p1.in_round(self.round_number-1).coupled_LL else: if p2.type == "HL": p1.coupled_HL = p1.in_round(self.round_number-1).coupled_HL + 1 p1.coupled_HH = p1.in_round(self.round_number-1).coupled_HH p1.coupled_LH = p1.in_round(self.round_number-1).coupled_LH p1.coupled_LL = p1.in_round(self.round_number-1).coupled_LL else: if p2.type == "LH": p1.coupled_LH = p1.in_round(self.round_number-1).coupled_LH + 1 p1.coupled_HH = p1.in_round(self.round_number-1).coupled_HH p1.coupled_HL = p1.in_round(self.round_number-1).coupled_HL p1.coupled_LL = p1.in_round(self.round_number-1).coupled_LL else: p1.coupled_LL = p1.in_round(self.round_number-1).coupled_LL + 1 p1.coupled_HH = p1.in_round(self.round_number-1).coupled_HH p1.coupled_HL = p1.in_round(self.round_number-1).coupled_HL p1.coupled_LH = p1.in_round(self.round_number-1).coupled_LH if self.round_number == 1: #in first round there is no info yet if p1.type == "HH": p2.coupled_HH = p2.coupled_HH + 1 p2.coupled_HL = p2.coupled_HL p2.coupled_LH = p2.coupled_LH p2.coupled_LL = p2.coupled_LL else: if p1.type == "HL": p2.coupled_HL = p2.coupled_HL + 1 p2.coupled_HH = p2.coupled_HH p2.coupled_LH = p2.coupled_LH p2.coupled_LL = p2.coupled_LL else: if p1.type == "LH": p2.coupled_LH = p2.coupled_LH + 1 p2.coupled_HH = p2.coupled_HH p2.coupled_HL = p2.coupled_HL p2.coupled_LL = p2.coupled_LL else: p2.coupled_LL = p2.coupled_LL + 1 p2.coupled_HH = p2.coupled_HH p2.coupled_HL = p2.coupled_HL p2.coupled_LH = p2.coupled_LH if self.round_number > 1: if p1.type == "HH": p2.coupled_HH = p2.in_round(self.round_number-1).coupled_HH + 1 p2.coupled_HL = p2.in_round(self.round_number-1).coupled_HL p2.coupled_LH = p2.in_round(self.round_number-1).coupled_LH p2.coupled_LL = p2.in_round(self.round_number-1).coupled_LL else: if p1.type == "HL": p2.coupled_HL = p2.in_round(self.round_number-1).coupled_HL + 1 p2.coupled_HH = p2.in_round(self.round_number-1).coupled_HH p2.coupled_LH = p2.in_round(self.round_number-1).coupled_LH p2.coupled_LL = p2.in_round(self.round_number-1).coupled_LL else: if p1.type == "LH": p2.coupled_LH = p2.in_round(self.round_number-1).coupled_LH + 1 p2.coupled_HH = p2.in_round(self.round_number-1).coupled_HH p2.coupled_HL = p2.in_round(self.round_number-1).coupled_HL p2.coupled_LL = p2.in_round(self.round_number-1).coupled_LL else: p2.coupled_LL = p2.in_round(self.round_number-1).coupled_LL + 1 p2.coupled_HH = p2.in_round(self.round_number-1).coupled_HH p2.coupled_HL = p2.in_round(self.round_number-1).coupled_HL p2.coupled_LH = p2.in_round(self.round_number-1).coupled_LH print("coupled_HH =") print(p1.coupled_HH) print("coupled_HL =") print(p1.coupled_HL) print("coupled_LH =") print(p1.coupled_LH) print("coupled_LL =") print(p1.coupled_LL) print("coupled_HH =") print(p2.coupled_HH) print("coupled_HL =") print(p2.coupled_HL) print("coupled_LH =") print(p2.coupled_LH) print("coupled_LL =") print(p2.coupled_LL) for p in self.get_players(): p.participant.vars['con_other1'] = p.con_other1 p.participant.vars['con_other2'] = p.con_other2 p.participant.vars['con_other3'] = p.con_other3 p.participant.vars['con_other4'] = p.con_other4 p.participant.vars['beliefs_HH'] = p.beliefs_HH p.participant.vars['beliefs_HL'] = p.beliefs_HL p.participant.vars['beliefs_LH'] = p.beliefs_LH p.participant.vars['beliefs_LL'] = p.beliefs_LL p.participant.vars['coupled_HH'] = p.coupled_HH p.participant.vars['coupled_HL'] = p.coupled_HL p.participant.vars['coupled_LH'] = p.coupled_LH p.participant.vars['coupled_LL'] = p.coupled_LL def get_multiplier_previous_round(self): for p in self.get_players(): if p.participant.vars['type'] == "HH": p.typedisplay = "Type 1" else: if p.participant.vars['type'] == "HL": p.typedisplay = "Type 2" else: if p.participant.vars['type'] == "LH": p.typedisplay = "Type 3" else: p.typedisplay = "Type 4" for p in self.get_players(): p.participant.vars['round_count'] = 0 print("round_count") print(p.round_count) #get global variables created in first app for p in self.get_players(): if p.participant.vars['multiplier'] == 1.7: p.multiplier = 1.7 else: p.multiplier = 1.3 for p in self.get_players(): if p.participant.vars['endowment'] == 75: p.endowment = 75 else: p.endowment = 25 for p in self.get_players(): if p.participant.vars['type'] == "HH": p.type = "HH" else: if p.participant.vars['type'] == "HL": p.type = "HL" else: if p.participant.vars['type'] == "LH": p.type = "LH" else: p.type = "LL" for p in self.get_players(): if p.round_number > 1: p.configs = p.in_round(self.round_number-1).configs if self.round_number == 1: for p in self.get_players(): p.is_dropout = False p.no_response = 0 if self.round_number != 1: for p in self.get_players(): p.is_dropout = p.participant.vars['is_dropout'] def my_method(self): #make the multiplier selected a global variable across apps for p in self.get_players(): p.participant.vars['endowment'] = p.endowment p.participant.vars['multiplier'] = p.multiplier p.participant.vars['type'] = p.type p.participant.vars['accumulated_wealth'] = p.accumulated_wealth p.participant.vars['con_other1'] = p.con_other1 p.participant.vars['con_other2'] = p.con_other2 p.participant.vars['con_other3'] = p.con_other3 p.participant.vars['con_other4'] = p.con_other4 p.participant.vars['beliefs_HH'] = p.beliefs_HH p.participant.vars['beliefs_HL'] = p.beliefs_HL p.participant.vars['beliefs_LH'] = p.beliefs_LH p.participant.vars['beliefs_LL'] = p.beliefs_LL p.participant.vars['coupled_HH'] = p.coupled_HH p.participant.vars['coupled_HL'] = p.coupled_HL p.participant.vars['coupled_LH'] = p.coupled_LH p.participant.vars['coupled_LL'] = p.coupled_LL p.participant.vars['accumulated_con'] = p.accumulated_con p.participant.vars['is_dropout'] = p.is_dropout print(p.participant.vars['endowment']) print(p.participant.vars['multiplier']) print(p.participant.vars['type']) print(p.participant.vars['accumulated_wealth']) def count_round(self): for p in self.get_players(): p.participant.vars['round_count'] = p.round_number + 1 print("round_count") print(p.participant.vars['round_count']) class Player(BasePlayer): multiplier = models.FloatField(choices=[[1.7,"1.7"],[1.3,"1.3"]]) accumulated_wealth = models.CurrencyField() contribution = models.CurrencyField( min=0, max=Constants.endowment, doc="""The amount contributed by the player""" ) def contribution_max(self): return self.endowment endowment = models.CurrencyField(choices=[[75,"75"],[25,"25"]]) addedtoPGG = models.CurrencyField() type = models.StringField() typedisplay = models.StringField() match = models.IntegerField(initial=0) earnings = models.CurrencyField() kept = models.CurrencyField() beliefs_HH = models.CurrencyField(min=0, max=75) beliefs_HL = models.CurrencyField(min=0, max=75) beliefs_LH = models.CurrencyField(min=0, max=25) beliefs_LL = models.CurrencyField(min=0, max=25) con_other1 = models.CurrencyField(initial=0) con_other2 = models.CurrencyField(initial=0) con_other3 = models.CurrencyField(initial=0) con_other4 = models.CurrencyField(initial=0) coupled_HH = models.IntegerField(initial=0) coupled_HL = models.IntegerField(initial=0) coupled_LH = models.IntegerField(initial=0) coupled_LL = models.IntegerField(initial=0) configs = models.StringField() round_count = models.IntegerField(initial=1) num = models.IntegerField() accumulated_con = models.CurrencyField() partnerchoice1_HH_1 = models.IntegerField(initial=0) partnerchoice1_HH_2 = models.IntegerField(initial=0) partnerchoice1_HH_3 = models.IntegerField(initial=0) partnerchoice1_HH_4 = models.IntegerField(initial=0) partnerchoice1_HL_1 = models.IntegerField(initial=0) partnerchoice1_HL_2 = models.IntegerField(initial=0) partnerchoice1_HL_3 = models.IntegerField(initial=0) partnerchoice1_HL_4 = models.IntegerField(initial=0) partnerchoice1_LH_1 = models.IntegerField(initial=0) partnerchoice1_LH_2 = models.IntegerField(initial=0) partnerchoice1_LH_3 = models.IntegerField(initial=0) partnerchoice1_LH_4 = models.IntegerField(initial=0) partnerchoice1_LL_1 = models.IntegerField(initial=0) partnerchoice1_LL_2 = models.IntegerField(initial=0) partnerchoice1_LL_3 = models.IntegerField(initial=0) partnerchoice1_LL_4 = models.IntegerField(initial=0) Attention_Check_2 = models.IntegerField() condition = models.StringField() random_id = models.IntegerField() timeout = models.BooleanField(initial=False) is_dropout = models.BooleanField(initial=False) no_response = models.IntegerField(initial=0) timeout_beliefs = models.BooleanField(initial=False) timeout_contribution = models.BooleanField(initial=False) timeout_preferencesbeliefs = models.BooleanField(initial=False) timeout_preferences = models.BooleanField(initial=False) preference = models.TextField(widget=school_choice_widgets.OrderedChoice( choices= { 'itemsToChoose':random.sample(list(Constants.choicesButtons), len(list(Constants.choicesButtons))), 'maxNumOptions':4 })) beliefs_preference = models.TextField(widget=school_choice_widgets.OrderedChoice( choices= { 'itemsToChoose':random.sample(list(Constants.choicesButtons), len(list(Constants.choicesButtons))), 'maxNumOptions':4 })) auto_id = models.IntegerField() partnerchoice1 = models.StringField( choices=["HH", "HL", "LH", "LL"], ) partnerchoice2 = models.StringField( choices=["HH", "HL", "LH", "LL"], ) partnerchoice3 = models.StringField( choices=["HH", "HL", "LH", "LL"], ) partnerchoice4 = models.StringField( choices=["HH", "HL", "LH", "LL"], ) beliefs_partnerchoice1 = models.StringField( choices=["HH", "HL", "LH", "LL"], ) beliefs_partnerchoice2 = models.StringField( choices=["HH", "HL", "LH", "LL"], ) beliefs_partnerchoice3 = models.StringField( choices=["HH", "HL", "LH", "LL"], ) beliefs_partnerchoice4 = models.StringField( choices=["HH", "HL", "LH", "LL"], ) def my_method(self): #make the income and multiplier selected a global variable across apps self.participant.vars['multiplier'] = self.multiplier self.participant.vars['endowment'] = self.endowment self.participant.vars['type'] = self.type self.participant.vars['accumulated_wealth'] = self.accumulated_wealth self.participant.vars['con_other1'] = self.con_other1 self.participant.vars['con_other2'] = self.con_other2 self.participant.vars['con_other3'] = self.con_other3 self.participant.vars['con_other4'] = self.con_other4 self.participant.vars['beliefs_HH'] = self.beliefs_HH self.participant.vars['beliefs_HL'] = self.beliefs_HL self.participant.vars['beliefs_LH'] = self.beliefs_LH self.participant.vars['beliefs_LL'] = self.beliefs_LL self.participant.vars['coupled_HH'] = self.coupled_HH self.participant.vars['coupled_HL'] = self.coupled_HL self.participant.vars['coupled_LH'] = self.coupled_LH self.participant.vars['coupled_LL'] = self.coupled_LL self.participant.vars['accumulated_con'] = self.accumulated_con self.participant.vars['is_dropout'] = self.is_dropout print(self.participant.vars['multiplier']) print(self.participant.vars['endowment']) print(self.participant.vars['type']) print(self.participant.vars['accumulated_wealth']) def other_player(self): #make it possible to show data other player in pages return self.get_others_in_group()[0] def p_other(self): #not sure if this is important self.get_others_in_group()