import random from otree.api import * class Constants(BaseConstants): name_in_url = 'Mallorca' players_per_group = None num_rounds = 1 endowment = cu(10) show_up_fee = cu(4) class Subsession(BaseSubsession): pass class Group(BaseGroup): r_var = models.FloatField() average_contribution1 = models.CurrencyField() average_contribution2 = models.CurrencyField() total_participants = models.IntegerField() class Player(BasePlayer): average_others1 = models.CurrencyField() average_others2 = models.CurrencyField() payoff_transfer = models.CurrencyField() payoff_beliefs = models.CurrencyField() payoff_total = models.CurrencyField() deviation1 = models.CurrencyField() deviation2 = models.CurrencyField() 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="Quina quantitat voldries donar a CHARITY NAME? (Pots donar qualsevol quantitat entre 0€ i 10€)" ) contribution2 = models.CurrencyField( min=0, max=Constants.endowment, label="Quina quantitat voldries donar a CHARITY NAME? (Pots donar qualsevol quantitat entre 0€ i 10€)" ) beliefs1 = models.CurrencyField( min=0, max=Constants.endowment, label="Quan creus que els altres participants de la sessió han donat de mitjana a CHARITY NAME? (Pots donar pronosticar qualsevol quantitat entre 0€ i 10€)" ) beliefs2 = models.CurrencyField( min=0, max=Constants.endowment, label="Quan creus que els altres participants de la sessió han donat de mitjana a CHARITY NAME? (Pots donar pronosticar qualsevol quantitat entre 0€ i 10€)" ) Sexe = models.StringField(widget=widgets.RadioSelectHorizontal(), choices=['Home', 'Dona']) Edat = models.IntegerField(choices=range(18, 60, 1)) Study = models.TextField(label="Quins estudis estan cursant actualment") # FUNCTIONS def compute_average1(group: Group): players = group.get_players() contributions1 = [p.contribution1 for p in players] group.average_contribution1 = sum(contributions1)/(len(contributions1)) for p in players: p.average_others1 = ((len(contributions1)*group.average_contribution1) - p.contribution1)/(len(contributions1) - 1) def compute_average2(group: Group): players = group.get_players() contributions2 = [p.contribution2 for p in players] group.average_contribution2 = sum(contributions2)/len(contributions2) for p in players: p.average_others2 = ((len(contributions2)*group.average_contribution2) - p.contribution2)/(len(contributions2) - 1) # PAGES class ConsentForm(Page): form_model = 'player' form_fields = ['consent'] class Instructions(Page): @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class Instructions2(Page): @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class InstructionsBeliefs(Page): @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class InstructionsBeliefs2(Page): @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class Contribution1(Page): form_model = 'player' form_fields = ['contribution1'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class Beliefs1(Page): form_model = 'player' form_fields = ['beliefs1'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class Message(Page): @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class Contribution2(Page): form_model = 'player' form_fields = ['contribution2'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class Beliefs2(Page): form_model = 'player' form_fields = ['beliefs2'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class MyWaitPage1(WaitPage): template_name = 'MallorcaLabExperiment/WaitingPageContributions.html' after_all_players_arrive = compute_average1 @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class MyWaitPage2(WaitPage): template_name = 'MallorcaLabExperiment/WaitingPageContributions2.html' after_all_players_arrive = compute_average2 @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class MyWaitPage3(WaitPage): @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 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 = sum(contributions1) / (len(contributions1)) group.average_contribution2 = sum(contributions2) / len(contributions2) for p in players: p.average_others1 = ((len(contributions1) * group.average_contribution1) - p.contribution1) / ( len(contributions1) - 1) p.average_others2 = ((len(contributions2) * group.average_contribution2) - p.contribution2) / ( len(contributions2) - 1) p.deviation1 = (1 / 10) * (p.average_others1 - p.beliefs1) * (p.average_others1 - p.beliefs1) p.deviation2 = (1 / 10) * (p.average_others1 - p.beliefs2) * (p.average_others1 - p.beliefs2) if group.r_var >= 0.5: p.payoff_transfer = Constants.endowment - p.contribution1 if p.deviation1 < cu(1): p.payoff_beliefs = cu(1) - p.deviation1 else: p.payoff_beliefs = 0 p.payoff_total = Constants.show_up_fee + p.payoff_transfer + p.payoff_beliefs else: p.payoff_transfer = Constants.endowment - p.contribution2 if p.deviation2 < cu(1): p.payoff_beliefs = cu(1) - p.deviation2 else: p.payoff_beliefs = 0 p.payoff_total = Constants.show_up_fee + p.payoff_transfer + p.payoff_beliefs class Results(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 class Questionnaire(Page): form_model = 'player' form_fields = ['Sexe', 'Edat', 'Study'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.consent == 0 page_sequence = [ConsentForm, Instructions, Contribution1, InstructionsBeliefs, Beliefs1, Instructions2, Contribution2, InstructionsBeliefs2, Beliefs2, Questionnaire, MyWaitPage3, Results]