from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import csv import ast import itertools author = 'Robizon' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'control' players_per_group = None part_0_length=2 #consent part_1_length=4 #instructions part_2_length=2 #quiz part_3_length=6 #counting task part_4_length=4 #belifs part_5_length=1 #risk task part_6_length=2 #Demographics num_rounds = part_0_length+part_1_length+part_2_length+part_3_length+part_4_length+part_5_length+part_6_length+2 box_length=100 chunks=10 low_end=2 high_end=3 with open('control/server_data.csv') as server_data: server_data= list(csv.DictReader(server_data)) class Subsession(BaseSubsession): def creating_session(self): if self.round_number==1: treatment = itertools.cycle(['C', 'T1', 'T2']) for p in self.get_players(): # with predifined treatments if self.session.config['treatment']!='M': p.participant.vars['treatment']=self.session.config['treatment'] else: p.participant.vars['treatment']=next(treatment) # variation in treatments in a session #if p.id_in_group<=2: # p.participant.vars['treatment']='C' #elif p.id_in_group in [3, 4, 5, 6]: # p.participant.vars['treatment']='T1' #else: # p.participant.vars['treatment']='T2' p.treatment=p.participant.vars['treatment'] p.participant.vars['rounds']=[1, 100, 100, 100, 100, 100, 100, 100] p.participant.vars['server_data']=Constants.server_data.copy() p.participant.vars['server_data']=p.participant.vars['server_data'] p.participant.vars['server_pay']=[p.participant.vars['server_data'][i] \ for i in range(0, len(p.participant.vars['server_data'])) \ if p.participant.vars['server_data'][i]['already_paid']=='0'] #if p.participant.vars['treatment']=='C': #p.participant.vars['server_pay']=p.participant.vars['server_data'][:27] #for i in []: # del p.participant.vars['server_pay'][i] #elif p.participant.vars['treatment']=='T1': #p.participant.vars['server_pay']=p.participant.vars['server_data'][27:54] #for i in []: # del p.participant.vars['server_pay'][i] #else: #p.participant.vars['server_pay']=p.participant.vars['server_data'][54:] #for i in []: # del p.participant.vars['server_pay'][i] # with predifined treatments if p.id_in_group<=len(p.participant.vars['server_pay']): p.participant.vars['server_pay']=p.participant.vars['server_pay'][p.id_in_group-1] else: p.participant.vars['server_pay']=p.participant.vars['server_pay'][p.id_in_group-1-len(p.participant.vars['server_pay'])] # treatment variation in session #if p.participant.vars['treatment']=='C': # p.participant.vars['server_pay']=p.participant.vars['server_pay'][0] #elif p.participant.vars['treatment']=='T1': # if p.id_in_group in [3, 4,]: # p.participant.vars['server_pay']=p.participant.vars['server_pay'][0] # else: # p.participant.vars['server_pay']=p.participant.vars['server_pay'][1] #else: # if p.id_in_group in [7, 8,]: # p.participant.vars['server_pay']=p.participant.vars['server_pay'][0] # elif p.id_in_group in [9, 10,]: # p.participant.vars['server_pay']=p.participant.vars['server_pay'][1] # else: # p.participant.vars['server_pay']=p.participant.vars['server_pay'][2] # id for web \ufeffid # id for local id p.pay_match_id=ast.literal_eval(p.participant.vars['server_pay']['\ufeffid']) p.participant.vars['server_data'].remove(p.participant.vars['server_pay']) p.participant.vars['server_6']=random.sample(p.participant.vars['server_data'], Constants.part_3_length-1) if p.participant.vars['treatment']!="C": while sum([ast.literal_eval(p.participant.vars['server_6'][i]['result_treat_ok']) for i in range(0, len(p.participant.vars['server_6']))])=p.participant.vars['random_for_pay'][i] for i in range(Constants.part_3_length)] p.participant.vars['round_case']=random.choice([[Constants.low_end]*2+[Constants.high_end]*4, [Constants.low_end]*2+[Constants.high_end]*4, [Constants.low_end]*3+[Constants.high_end]*3]) random.shuffle(p.participant.vars['round_case']) p.participant.vars['c']=0 for i in range(0, len(p.participant.vars['round_case'])): if p.participant.vars['round_case'][i]==3: p.participant.vars['c']+=1 if p.participant.vars['c']==2: p.participant.vars['win'][i]=False p.participant.vars['image_id']=[['image_{}'.format(i) for i in range(Constants.chunks*i, Constants.chunks*(i+1))] for i in range(Constants.chunks)] p.participant.vars['val_id']=[[i for i in range(Constants.chunks*i, Constants.chunks*(i+1))] for i in range(Constants.chunks)] p.participant.vars['quiz_n_correct']=0 p.participant.vars['strike']=0 p.participant.vars['quiz_your_answers']=[0]*4 p.participant.vars['correct_answers']=[1, 2, 0, 3] p.participant.vars['correctly_answered']=[0]*4 p.participant.vars['risk_list']=list(range(10, 100, 10)) random.shuffle(p.participant.vars['risk_list']) class Group(BaseGroup): pass class Player(BasePlayer): treatment=models.StringField() pay_match_id=models.IntegerField() share=models.FloatField() share_update=models.FloatField() match_id=models.IntegerField() n_correct=models.IntegerField() n_incorrect=models.IntegerField() win=models.IntegerField() random_num=models.FloatField() text_from_B=models.StringField() round_case=models.IntegerField() lottery=models.IntegerField() quiz_1=models.IntegerField(widget=widgets.RadioSelect) quiz_2=models.IntegerField(widget=widgets.RadioSelect) def check_correct(self, current_round): for i in self.participant.vars['answered_list_p1'][current_round]: if self.participant.vars['list_of_smiles'][current_round][i]==1: self.participant.vars['correct'][current_round][i]=1 self.n_correct+=1 else: self.participant.vars['correct'][current_round][i]=-1 self.n_incorrect+=1 belief_1=models.IntegerField() belief_2=models.IntegerField() belief_3=models.IntegerField() belief_4=models.IntegerField() belief_5=models.IntegerField() belief_6=models.IntegerField() belief_1_float=models.FloatField(min=0, max=2.5) belief_2_float=models.FloatField(min=0, max=2.5) belief_1_string=models.LongStringField() belief_2_string=models.LongStringField() minutes_b=models.IntegerField() seconds_b=models.IntegerField() minutes_you=models.IntegerField() seconds_you=models.IntegerField() donate_to=models.IntegerField(choices=[[0, "Animal Aid Unlimited (Animal support organization)"], [1, "Save the Children (Child support organization)"], [2, "Environmental Defense Fund (Environment support organization)"]]) donate=models.FloatField(min=0, max=1) q_age=models.IntegerField(min=1, max=120) q_gender=models.IntegerField(widget=widgets.RadioSelect) q_6_confusing=models.FloatField(blank=True) q_7_feedback=models.LongStringField(blank=True)