from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'treatment_1' players_per_group = None calculator_template = 'treatment_1/calculator.html' num_rounds = 60 var_length=10 endowment=7 class Subsession(BaseSubsession): def creating_session(self): for p in self.get_players(): if self.round_number==1: if 'treatment' in self.session.config: p.participant.vars["treatment_1"] = self.session.config['treatment'] p.treatment_1=p.participant.vars["treatment_1"] #here session will be created and each player start with random decission if self.round_number==1: player_id=list(range(1, len(self.get_players())+1)) type_A_ids=random.sample(player_id, 1) for p in self.get_players(): if p.id_in_group in type_A_ids: p.type_A=True else: p.type_A=False for item in type_A_ids: player_id.remove(item) self.session.vars['group_1']=player_id self.session.vars['group_1'].append(type_A_ids[0]) for p in self.get_players(): p.participant.vars['payment_p1']=[0, 0] p.participant.vars['payment_p2']=[0, 0, 0, 0, 0, 0, 0, 0] p.participant.vars['payment_p3_block2']=[0, 0, 0] p.participant.vars['payment_p4_block1']=[0, 0, 0] p.participant.vars['payment_p4_block2']=[0, 0] p.participant.vars['pay_for_task_p4']=[0, 0] prob=random.uniform(0, 1) if prob<=0.7: p.participant.vars["part"]=1 elif prob>0.7 and prob<=0.9: p.participant.vars["part"]=2 elif prob>0.9 and prob<=0.95: p.participant.vars["part"]=3 elif prob>0.95 and prob<=1: p.participant.vars["part"]=3 p.part_for_payment=p.participant.vars["part"] if p.participant.vars["part"]==1: p.participant.vars["round"]=random.choice([1, 2]) p.round_for_payment=p.participant.vars["round"] elif p.participant.vars["part"]==2: p.participant.vars["round"]=random.choice([1, 2, 3, 4, 5, 6, 7, 8]) p.round_for_payment=p.participant.vars["round"] elif p.participant.vars["part"]==3: p.participant.vars["block"]=random.choice([1, 2]) p.participant.vars["round"]=random.choice([1, 2, 3]) p.block_for_payment=p.participant.vars["block"] p.round_for_payment=p.participant.vars["round"] elif p.participant.vars["part"]==4: p.participant.vars["block"]=random.choice([1, 2]) p.block_for_payment=p.participant.vars["block"] if p.block_for_payment==1: p.participant.vars["round"]=random.choice([1, 2, 3]) p.round_for_payment=p.participant.vars["round"] elif p.block_for_payment==2: p.participant.vars["round"]=random.choice([1, 2]) p.round_for_payment=p.participant.vars["round"] else: for p in self.get_players(): p.type_A=p.in_round(self.round_number - 1).type_A if self.round_number<=2: for p in self.get_players(): if self.round_number==1: decission_ordering=random.sample([1, 2], 2) p.decission_p1_1=decission_ordering[0] p.decission_p1_2=decission_ordering[1] p.participant.vars["random_payment"]=round(random.uniform(0, 15), 2) elif self.round_number==2: p.decission_p1_1=p.in_round(self.round_number - 1).decission_p1_1 p.decission_p1_2=p.in_round(self.round_number - 1).decission_p1_2 p.find_active_decission_p1() elif (self.round_number>=3 and self.round_number<=10): for p in self.get_players(): if self.round_number==3: task_ordering=random.sample([1, 2, 3, 4, 5, 6, 7, 8], 8) p.task_1=task_ordering[0] p.task_2=task_ordering[1] p.task_3=task_ordering[2] p.task_4=task_ordering[3] p.task_5=task_ordering[4] p.task_6=task_ordering[5] p.task_7=task_ordering[6] p.task_8=task_ordering[7] elif (self.round_number>3 and self.round_number<=10): p.task_1=p.in_round(self.round_number - 1).task_1 p.task_2=p.in_round(self.round_number - 1).task_2 p.task_3=p.in_round(self.round_number - 1).task_3 p.task_4=p.in_round(self.round_number - 1).task_4 p.task_5=p.in_round(self.round_number - 1).task_5 p.task_6=p.in_round(self.round_number - 1).task_6 p.task_7=p.in_round(self.round_number - 1).task_7 p.task_8=p.in_round(self.round_number - 1).task_8 p.find_active_task() for i in range(1, Constants.var_length): exec("p.string%s = ''.join(random.choices(['b', 'd', 'q', 'p'], k=25))" % i) exec("p.sd1_%s = %s" % (i, random.sample([x for x in range(10, 100, 1)], 1)[0])) exec("p.sd2_%s = %s" % (i, random.sample([x for x in range(10, 100, 1)], 1)[0])) exec("p.dd1_%s = %s" % (i, random.sample([x for x in range(100, 1000, 1)], 1)[0])) exec("p.dd2_%s = %s" % (i, random.sample([x for x in range(100, 1000, 1)], 1)[0])) exec("p.td1_%s = %s" % (i, random.sample([x for x in range(1000, 10000, 1)], 1)[0])) exec("p.td2_%s = %s" % (i, random.sample([x for x in range(1000, 10000, 1)], 1)[0])) exec("p.qd1_%s = %s" % (i, random.sample([x for x in range(10000, 100000, 1)], 1)[0])) exec("p.qd2_%s = %s" % (i, random.sample([x for x in range(10000, 100000, 1)], 1)[0])) elif (self.round_number>=11 and self.round_number<=28): for p in self.get_players(): if (self.round_number>=11 and self.round_number<=16): p.find_Block_1_2_decission_num() p.rand_prac=random.uniform(0.01, 0.49) elif self.round_number==17: question_ordering=random.sample([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 12) p.question_1=question_ordering[0] p.question_2=question_ordering[1] p.question_3=question_ordering[2] p.question_4=question_ordering[3] p.question_5=question_ordering[4] p.question_6=question_ordering[5] p.question_7=question_ordering[6] p.question_8=question_ordering[7] p.question_9=question_ordering[8] p.question_10=question_ordering[9] p.question_11=question_ordering[10] p.question_12=question_ordering[11] p.find_active_question() elif (self.round_number>17 and self.round_number<=28): p.question_1=p.in_round(self.round_number - 1).question_1 p.question_2=p.in_round(self.round_number - 1).question_2 p.question_3=p.in_round(self.round_number - 1).question_3 p.question_4=p.in_round(self.round_number - 1).question_4 p.question_5=p.in_round(self.round_number - 1).question_5 p.question_6=p.in_round(self.round_number - 1).question_6 p.question_7=p.in_round(self.round_number - 1).question_7 p.question_8=p.in_round(self.round_number - 1).question_8 p.question_9=p.in_round(self.round_number - 1).question_9 p.question_10=p.in_round(self.round_number - 1).question_10 p.question_11=p.in_round(self.round_number - 1).question_11 p.question_12=p.in_round(self.round_number - 1).question_12 p.find_active_question() elif self.round_number==29: for p in self.get_players(): p.computer_draw=0.95*random.uniform(0, 1.25)+0.05*random.uniform(1.25, 4.5) p.question_to_do=random.sample([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 1)[0] for i in range(1, Constants.var_length): exec("p.string%s = ''.join(random.choices(['b', 'd', 'q', 'p'], k=25))" % i) exec("p.sd1_%s = %s" % (i, random.sample([x for x in range(10, 100, 1)], 1)[0])) exec("p.sd2_%s = %s" % (i, random.sample([x for x in range(10, 100, 1)], 1)[0])) exec("p.dd1_%s = %s" % (i, random.sample([x for x in range(100, 1000, 1)], 1)[0])) exec("p.dd2_%s = %s" % (i, random.sample([x for x in range(100, 1000, 1)], 1)[0])) exec("p.td1_%s = %s" % (i, random.sample([x for x in range(1000, 10000, 1)], 1)[0])) exec("p.td2_%s = %s" % (i, random.sample([x for x in range(1000, 10000, 1)], 1)[0])) exec("p.qd1_%s = %s" % (i, random.sample([x for x in range(10000, 100000, 1)], 1)[0])) exec("p.qd2_%s = %s" % (i, random.sample([x for x in range(10000, 100000, 1)], 1)[0])) elif (self.round_number>=30 and self.round_number<=(34+len(self.get_players()))): for p in self.get_players(): p.comp_draw_p4_op=random.choice([0, 1, 2]) p.comp_draw_p4_pay=random.uniform(0, 7) if self.round_number == 30: for p in self.get_players(): p.delete_1=random.choice([1, 2, 3, 4]) p.delete_2=random.choice([5, 6, 7, 8]) p.part_4_order=random.choice([1, 2]) elif (self.round_number>30 and self.round_number<=(34+len(self.get_players()))): for p in self.get_players(): p.delete_1=p.in_round(self.round_number - 1).delete_1 p.delete_2=p.in_round(self.round_number - 1).delete_2 p.part_4_order=p.in_round(self.round_number - 1).part_4_order for i in range(1, Constants.var_length): exec("p.string%s = ''.join(random.choices(['b', 'd', 'q', 'p'], k=25))" % i) exec("p.sd1_%s = %s" % (i, random.sample([x for x in range(10, 100, 1)], 1)[0])) exec("p.sd2_%s = %s" % (i, random.sample([x for x in range(10, 100, 1)], 1)[0])) exec("p.dd1_%s = %s" % (i, random.sample([x for x in range(100, 1000, 1)], 1)[0])) exec("p.dd2_%s = %s" % (i, random.sample([x for x in range(100, 1000, 1)], 1)[0])) exec("p.td1_%s = %s" % (i, random.sample([x for x in range(1000, 10000, 1)], 1)[0])) exec("p.td2_%s = %s" % (i, random.sample([x for x in range(1000, 10000, 1)], 1)[0])) exec("p.qd1_%s = %s" % (i, random.sample([x for x in range(10000, 100000, 1)], 1)[0])) exec("p.qd2_%s = %s" % (i, random.sample([x for x in range(10000, 100000, 1)], 1)[0])) class Group(BaseGroup): pass class Player(BasePlayer): treatment_1 = models.BooleanField() type_A=models.BooleanField() part_for_payment=models.IntegerField() block_for_payment=models.IntegerField() round_for_payment=models.IntegerField() #---------PART 1 variables and functions-------------------- decission_p1_1=models.IntegerField() decission_p1_2=models.IntegerField() active_decission_p1=models.IntegerField() def find_active_decission_p1(self): if (self.decission_p1_1==self.round_number): self.active_decission_p1=1 elif (self.decission_p1_2==self.round_number): self.active_decission_p1=2 #if fixed_amount is equals 1 then subject choice was fixed amount, if 2 then she chose computer fixed_amount=models.IntegerField(widget=widgets.RadioSelect) #----------------------------------------------------------- #---------PART 2 variables and functions-------------------- for i in range(1,9): locals()["task_{}".format(i)] =models.IntegerField() del i active_task=models.IntegerField() def find_active_task(self): if (self.task_1+2==self.round_number): self.active_task=1 elif (self.task_2+2==self.round_number): self.active_task=2 elif (self.task_3+2==self.round_number): self.active_task=3 elif (self.task_4+2==self.round_number): self.active_task=4 elif (self.task_5+2==self.round_number): self.active_task=5 elif (self.task_6+2==self.round_number): self.active_task=6 elif (self.task_7+2==self.round_number): self.active_task=7 elif (self.task_8+2==self.round_number): self.active_task=8 for i in range(1,Constants.var_length): locals()["correct{}".format(i)] =models.BooleanField() del i for i in range(1,Constants.var_length): locals()["count_sum{}".format(i)] =models.FloatField() del i n_correct=models.IntegerField(initial=0) for i in range(1,Constants.var_length): locals()["string{}".format(i)] =models.StringField() del i def check_correct_strings(self, letter): for i in range(1,Constants.var_length): exec("self.correct%s = self.count_sum%s ==self.string%s.count(letter)" % (i, i, i)) del i for i in range(1,Constants.var_length): locals()["sd1_{}".format(i)] =models.FloatField() del i for i in range(1,Constants.var_length): locals()["sd2_{}".format(i)] =models.FloatField() del i def check_correct_sd(self): for i in range(1,Constants.var_length): exec("self.correct%s =(self.count_sum%s-self.sd1_%s-self.sd2_%s)==0.0" % (i, i, i, i)) del i for i in range(1,Constants.var_length): locals()["dd1_{}".format(i)] =models.FloatField() del i for i in range(1,Constants.var_length): locals()["dd2_{}".format(i)] =models.FloatField() del i def check_correct_dd(self): for i in range(1,Constants.var_length): exec("self.correct%s = (self.count_sum%s-self.dd1_%s-self.dd2_%s)==0.0" % (i, i, i, i)) del i for i in range(1,Constants.var_length): locals()["td1_{}".format(i)] =models.FloatField() del i for i in range(1,Constants.var_length): locals()["td2_{}".format(i)] =models.FloatField() del i def check_correct_td(self): for i in range(1,Constants.var_length): exec("self.correct%s = (self.count_sum%s-self.td1_%s-self.td2_%s)==0.0" % (i, i, i, i)) del i for i in range(1,Constants.var_length): locals()["qd1_{}".format(i)] =models.FloatField() del i for i in range(1,Constants.var_length): locals()["qd2_{}".format(i)] =models.FloatField() del i def check_correct_qd(self): for i in range(1,Constants.var_length): exec("self.correct%s = (self.count_sum%s-self.qd1_%s-self.qd2_%s)==0.0" % (i, i, i, i)) del i #----------------------------------------------------------- #---------PART 3 variables and functions-------------------- decission=models.FloatField(min=0.00, max=5.00) decission_num=models.IntegerField() def find_Block_1_2_decission_num(self): if self.round_number==11 or self.round_number==14: self.decission_num=1 elif self.round_number==12 or self.round_number==15: self.decission_num=2 elif self.round_number==13 or self.round_number==16: self.decission_num=3 rand_prac=models.FloatField() answer_1=models.IntegerField(widget=widgets.RadioSelect) answer_2=models.IntegerField(widget=widgets.RadioSelect) answer_3=models.IntegerField(widget=widgets.RadioSelect) answer_4=models.IntegerField(widget=widgets.RadioSelect) correct_ans_1=models.BooleanField() correct_ans_2=models.BooleanField() correct_ans_3=models.BooleanField() correct_ans_4=models.BooleanField() def check_correct_ans_p_3(self): if self.answer_1==1: self.correct_ans_1=True else: self.correct_ans_1=False if self.answer_2==3: self.correct_ans_2=True else: self.correct_ans_2=False if self.answer_3==2: self.correct_ans_3=True else: self.correct_ans_3=False if self.answer_4==2: self.correct_ans_4=True else: self.correct_ans_4=False for i in range(1,13): locals()["question_{}".format(i)] =models.IntegerField() del i active_question=models.IntegerField() def find_active_question(self): if (self.question_1+16)==self.round_number: self.active_question=1 elif (self.question_2+16)==self.round_number: self.active_question=2 elif (self.question_3+16)==self.round_number: self.active_question=3 elif (self.question_4+16)==self.round_number: self.active_question=4 elif (self.question_5+16)==self.round_number: self.active_question=5 elif (self.question_6+16)==self.round_number: self.active_question=6 elif (self.question_7+16)==self.round_number: self.active_question=7 elif (self.question_8+16)==self.round_number: self.active_question=8 elif (self.question_9+16)==self.round_number: self.active_question=9 elif (self.question_10+16)==self.round_number: self.active_question=10 elif (self.question_11+16)==self.round_number: self.active_question=11 elif (self.question_12+16)==self.round_number: self.active_question=12 question_to_do=models.IntegerField() computer_draw=models.FloatField() def find_task_to_do_p3(self): if self.in_round(self.question_to_do + 16).decission>self.computer_draw: self.participant.vars['rate_for_p3_task']=0.5 if self.in_round(self.question_to_do+16).active_question==1: self.active_task=1 elif self.in_round(self.question_to_do+16).active_question==2: self.active_task=1 elif self.in_round(self.question_to_do+16).active_question==3: self.active_task=1 elif self.in_round(self.question_to_do+16).active_question==4: self.active_task=5 elif self.in_round(self.question_to_do+16).active_question==5: self.active_task=5 elif self.in_round(self.question_to_do+16).active_question==6: self.active_task=5 elif self.in_round(self.question_to_do+16).active_question==7: self.active_task=6 elif self.in_round(self.question_to_do+16).active_question==8: self.active_task=6 elif self.in_round(self.question_to_do+16).active_question==9: self.active_task=5 elif self.in_round(self.question_to_do+16).active_question==10: self.active_task=2 elif self.in_round(self.question_to_do+16).active_question==11: self.active_task=2 elif self.in_round(self.question_to_do+16).active_question==12: self.active_task=2 elif self.in_round(self.question_to_do+16).decission<=self.computer_draw: self.participant.vars['rate_for_p3_task']=self.computer_draw if self.in_round(self.question_to_do+16).active_question==1: self.active_task=2 elif self.in_round(self.question_to_do+16).active_question==2: self.active_task=3 elif self.in_round(self.question_to_do+16).active_question==3: self.active_task=4 elif self.in_round(self.question_to_do+16).active_question==4: self.active_task=6 elif self.in_round(self.question_to_do+16).active_question==5: self.active_task=7 elif self.in_round(self.question_to_do+16).active_question==6: self.active_task=8 elif self.in_round(self.question_to_do+16).active_question==7: self.active_task=7 elif self.in_round(self.question_to_do+16).active_question==8: self.active_task=8 elif self.in_round(self.question_to_do+16).active_question==9: self.active_task=5 elif self.in_round(self.question_to_do+16).active_question==10: self.active_task=3 elif self.in_round(self.question_to_do+16).active_question==11: self.active_task=6 elif self.in_round(self.question_to_do+16).active_question==12: self.active_task=7 #----------------------------------------------------------- #---------PART 4 variables and functions-------------------- def check_correct_ans_p_4(self): if self.round_number==30: if self.answer_1==2: self.correct_ans_1=True else: self.correct_ans_1=False if self.answer_2==2: self.correct_ans_2=True else: self.correct_ans_2=False if self.answer_3==1: self.correct_ans_3=True else: self.correct_ans_3=False if self.answer_4==1: self.correct_ans_4=True else: self.correct_ans_4=False elif self.round_number==31: if self.answer_1==1: self.correct_ans_1=True else: self.correct_ans_1=False if self.answer_2==1: self.correct_ans_2=True else: self.correct_ans_2=False if self.answer_3==1: self.correct_ans_3=True else: self.correct_ans_3=False if self.answer_4==1: self.correct_ans_4=True else: self.correct_ans_4=False elif self.round_number==32: if self.answer_1==2: self.correct_ans_1=True else: self.correct_ans_1=False if self.answer_2==2: self.correct_ans_2=True else: self.correct_ans_2=False if self.answer_3==2: self.correct_ans_3=True else: self.correct_ans_3=False if self.answer_4==2: self.correct_ans_4=True else: self.correct_ans_4=False part_4_order=models.IntegerField() decission_p4=models.IntegerField(widget=widgets.RadioSelect) delete_1=models.IntegerField() delete_2=models.IntegerField() comp_draw_p4_op=models.IntegerField() comp_choice=models.IntegerField() comp_draw_p4_pay=models.FloatField() max_payment=models.FloatField(min=0.00, max=Constants.endowment) decission_p4_type_A=models.IntegerField(widget=widgets.RadioSelect) decission_p4_type_B=models.IntegerField(widget=widgets.RadioSelect) q_1_school=models.IntegerField(widget=widgets.RadioSelect) q_2_status=models.IntegerField(widget=widgets.RadioSelect) q_3_GPA=models.FloatField() q_4_terms=models.FloatField(widget=widgets.RadioSelect) for i in range(1,9): locals()["q_5_task_{}".format(i)] =models.IntegerField(min=1, max=9) del i q_6_confusing=models.LongStringField(blank=True) q_7_feedback=models.LongStringField(blank=True)