import random import math 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() active = 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() non_rounded_payment = models.CurrencyField() active = models.IntegerField() 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) Q1 = models.PositiveIntegerField(choices=[[1, 'Más probable
'], [2, 'Igual de probable
'], [3, 'Menos probable
'], ], widget=widgets.RadioSelectHorizontal) Q2 = models.PositiveIntegerField(choices=[[1, 'Más probable
'], [2, 'Igual de probable
'], [3, 'Menos probable
'], ], widget=widgets.RadioSelectHorizontal) Q3 = models.PositiveIntegerField(choices=[[1, 'Más probable
'], [2, 'Igual de probable
'], [3, 'Menos probable
'], ], widget=widgets.RadioSelectHorizontal) Q4 = models.PositiveIntegerField(choices=[[1, 'Más probable
'], [2, 'Igual de probable
'], [3, 'Menos probable
'], ], widget=widgets.RadioSelectHorizontal) Q5 = models.PositiveIntegerField(choices=[[1, 'Más probable
'], [2, 'Igual de probable
'], [3, 'Menos probable
'], ], widget=widgets.RadioSelectHorizontal) Q6 = models.PositiveIntegerField(choices=[[1, 'Más probable
'], [2, 'Igual de probable
'], [3, 'Menos probable
'], ], widget=widgets.RadioSelectHorizontal) Q7 = models.TextField(label="Por favor, explica las razones de tus respuestas.") 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 = 5.36 * float(p.contribution1) p.kantian_effect = float(p.kantian) * 5.36 p.others_effect = 5.36 * 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)) def rounding_the_payments(payoff): return math.ceil(payoff * 2) / 2 # PAGES class ConsentForm(Page): form_model = 'player' class Instructions(Page): form_model = 'player' class Instructions2(Page): form_model = 'player' class InstructionesPronostico(Page): form_model = 'player' class InstructionesPronostico2(Page): form_model = 'player' class Donacion(Page): form_model = 'player' form_fields = ['contribution1'] class Pronostico(Page): form_model = 'player' form_fields = ['beliefs1'] class Message(Page): form_model = 'player' class Donacion2a(Page): form_model = 'player' form_fields = ['contribution2'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.session.config['treatment'] == 1 class Donacion2b(Page): form_model = 'player' form_fields = ['contribution2'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.session.config['treatment'] == 2 class Donacion2c(Page): form_model = 'player' form_fields = ['contribution2'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.session.config['treatment'] == 3 class Pronostico2(Page): form_model = 'player' form_fields = ['beliefs2'] class MyWaitPage1(WaitPage): form_model = 'player' template_name = 'Mallorca/WaitingPageContributions.html' after_all_players_arrive = compute_average1 class MyWaitPage2(WaitPage): form_model = 'player' template_name = 'Mallorca/WaitingPageContributions2.html' after_all_players_arrive = compute_average2 class QuestionnaireBins(Page): form_model = 'player' form_fields = ['Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7'] class MyWaitPage3(WaitPage): form_model = 'player' template_name = 'Mallorca/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.non_rounded_payment = p.payoff_transfer + p.payoff_beliefs p.payoff = rounding_the_payments(p.non_rounded_payment) 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.non_rounded_payment = p.payoff_transfer + p.payoff_beliefs p.payoff = rounding_the_payments(p.non_rounded_payment) class Codigo(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, Codigo, Donacion, InstructionesPronostico, Pronostico, MyWaitPage1, Instructions2, Donacion2a, Donacion2b, Donacion2c, InstructionesPronostico2, Pronostico2, MyWaitPage3, Results, QuestionnaireIntro, QuestionnaireBins, QuestionnaireN, QuestionnaireTest, Questionnaire]