import random from otree.api import * class Constants(BaseConstants): name_in_url = 'Mallorca' players_per_group = None num_rounds = 1 endowment = 10 class Subsession(BaseSubsession): pass class Group(BaseGroup): r_var = models.FloatField() average_contribution1 = models.FloatField() average_contribution2 = models.FloatField() total_participants = models.IntegerField() NSize = models.IntegerField() class Player(BasePlayer): average_others1 = models.FloatField() average_others2 = models.FloatField() payoff_transfer = models.CurrencyField() payoff_beliefs = models.CurrencyField() payoff_total = models.CurrencyField() deviation1 = models.FloatField() deviation2 = models.FloatField() kantian = models.FloatField() kantian_effect = models.FloatField() effect = models.FloatField() others_effect = models.FloatField() consent = models.IntegerField(widget=widgets.RadioSelect, label="", choices=[[0, 'Dono el meu consentiment'], [1, 'Abandono aquest estudi']]) contribution1 = models.CurrencyField( min=0, max=Constants.endowment, label="" ) contribution2 = models.CurrencyField( min=0, max=Constants.endowment, label="" ) beliefs1 = models.CurrencyField( min=0, max=Constants.endowment, label="" ) beliefs2 = models.CurrencyField( min=0, max=Constants.endowment, label="" ) Sexo = models.StringField(widget=widgets.RadioSelectHorizontal(), choices=['Hombre', 'Mujer']) Edad = models.IntegerField(choices=range(18, 60, 1)) Estudios = models.TextField(label="¿Qué estudios estás cursando actualmente? (En caso de no estar actualmente estudiando, ¿cuáles fueron tus últimos estudios completados?)") Nacionalidad = models.TextField(label="¿Cuál es tu nacionalidad?") Code = models.TextField(label="Escribe tu código aquí:") MoralA1 = models.PositiveIntegerField(choices=[[1, '

Totalmente en desacuerdo

'], [2, '

En desacuerdo

'], [3, '

Indiferente

'], [4, '

De acuerdo

'], [5, '

Totalmente de acuerdo

'], ], widget=widgets.RadioSelectHorizontal) MoralA2 = models.PositiveIntegerField(choices=[[1, '

Totalmente en desacuerdo

'], [2, '

En desacuerdo

'], [3, '

Indiferente

'], [4, '

De acuerdo

'], [5, '

Totalmente de acuerdo

'], ], widget=widgets.RadioSelectHorizontal) MoralA3 = models.PositiveIntegerField(choices=[[1, '

Totalmente en desacuerdo

'], [2, '

En desacuerdo

'], [3, '

Indiferente

'], [4, '

De acuerdo

'], [5, '

Totalmente de acuerdo

'], ], widget=widgets.RadioSelectHorizontal) MoralA4 = models.PositiveIntegerField(choices=[[1, '

Totalmente en desacuerdo

'], [2, '

En desacuerdo

'], [3, '

Indiferente

'], [4, '

De acuerdo

'], [5, '

Totalmente de acuerdo

'], ], widget=widgets.RadioSelectHorizontal) MoralA5 = models.PositiveIntegerField(choices=[[1, '

Totalmente en desacuerdo

'], [2, '

En desacuerdo

'], [3, '

Indiferente

'], [4, '

De acuerdo

'], [5, '

Totalmente de acuerdo

'], ], widget=widgets.RadioSelectHorizontal) MoralA6 = models.PositiveIntegerField(choices=[[1, '

Totalmente en desacuerdo

'], [2, '

En desacuerdo

'], [3, '

Indiferente

'], [4, '

De acuerdo

'], [5, '

Totalmente de acuerdo

'], ], widget=widgets.RadioSelectHorizontal) N = models.PositiveIntegerField(choices=[[1, '

A

'], [2, '

B

'], [3, '

C

'], [4, '

D

'], [5, '

E

'], [6, '

F

'], [7, '

G

'], ], widget=widgets.RadioSelectHorizontal) # FUNCTIONS def compute_average1(group: Group): players = group.get_players() contributions1 = [p.contribution1 for p in players] group.NSize = len(contributions1) group.average_contribution1 = float(sum(contributions1)/(len(contributions1))) for p in players: p.average_others1 = float(((len(contributions1)*group.average_contribution1) - p.contribution1)/(len(contributions1) - 1)) p.kantian = float(group.NSize * p.contribution1) p.effect = 4.5*float(p.contribution1) p.kantian_effect = float(p.kantian)*4.5 p.others_effect =4.5*float(p.average_others1) def compute_average2(group: Group): players = group.get_players() contributions2 = [p.contribution2 for p in players] group.average_contribution2 = float(sum(contributions2)/len(contributions2)) for p in players: p.average_others2 = float(((len(contributions2)*group.average_contribution2) - p.contribution2)/(len(contributions2) - 1)) # PAGES class ConsentForm(Page): form_model = 'player' class Instructions(Page): form_model = 'player' class Instructions2(Page): form_model = 'player' class InstructionsBeliefs(Page): form_model = 'player' class InstructionsBeliefs2(Page): form_model = 'player' class Contribution1(Page): form_model = 'player' form_fields = ['contribution1'] class Beliefs1(Page): form_model = 'player' form_fields = ['beliefs1'] class Message(Page): form_model = 'player' class Contribution2(Page): form_model = 'player' form_fields = ['contribution2'] class Beliefs2(Page): form_model = 'player' form_fields = ['beliefs2'] class MyWaitPage1(WaitPage): form_model = 'player' template_name = 'MallorcaLabExperiment/WaitingPageContributions.html' after_all_players_arrive = compute_average1 class MyWaitPage2(WaitPage): form_model = 'player' template_name = 'MallorcaLabExperiment/WaitingPageContributions2.html' after_all_players_arrive = compute_average2 class MyWaitPage3(WaitPage): form_model = 'player' template_name = 'MallorcaLabExperiment/MyWaitPage.html' def after_all_players_arrive(group: Group): random_var = random.uniform(0, 1) group.r_var = random_var players = group.get_players() contributions1 = [p.contribution1 for p in players] contributions2 = [p.contribution2 for p in players] group.average_contribution1 = float(sum(contributions1) / (len(contributions1))) group.average_contribution2 = float(sum(contributions2) / len(contributions2)) for p in players: p.average_others1 = float(((len(contributions1) * group.average_contribution1) - p.contribution1) / ( len(contributions1) - 1)) p.average_others2 = float(((len(contributions2) * group.average_contribution2) - p.contribution2) / ( len(contributions2) - 1)) p.deviation1 = float(2 - 0.5*(p.average_others1 - p.beliefs1) * (p.average_others1 - p.beliefs1)) p.deviation2 = float(2 - 0.5*(p.average_others2 - p.beliefs2) * (p.average_others2 - p.beliefs2)) if group.r_var >= 0.5: p.payoff_transfer = Constants.endowment - p.contribution1 if p.deviation1 > 0: p.payoff_beliefs = p.deviation1 else: p.payoff_beliefs = 0 p.payoff_total = p.payoff_transfer + p.payoff_beliefs p.payoff = p.payoff_transfer + p.payoff_beliefs else: p.payoff_transfer = Constants.endowment - p.contribution2 if p.deviation2 > 0: p.payoff_beliefs = p.deviation2 else: p.payoff_beliefs = 0 p.payoff_total = p.payoff_transfer + p.payoff_beliefs p.payoff = p.payoff_transfer + p.payoff_beliefs class Code(Page): form_model = 'player' form_fields = ['Code'] class Results(Page): form_model = 'player' class Questionnaire(Page): form_model = 'player' form_fields = ['Sexo', 'Edad', 'Estudios', 'Nacionalidad'] class QuestionnaireN(Page): form_model = 'player' form_fields = ['N'] class QuestionnaireTest(Page): form_model = 'player' form_fields = ['MoralA1', 'MoralA2', 'MoralA3', 'MoralA4', 'MoralA5', 'MoralA6'] class QuestionnaireIntro(Page): form_model = 'player' page_sequence = [Instructions, Code, Contribution1, InstructionsBeliefs, Beliefs1, MyWaitPage1, Instructions2, Contribution2, InstructionsBeliefs2, Beliefs2, MyWaitPage3, Results, QuestionnaireIntro, QuestionnaireN, QuestionnaireTest, Questionnaire]