from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from django.forms.widgets import NumberInput from itertools import cycle from random import uniform from treatments_solo_tasks import * author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'taskcpr' players_per_group = None num_rounds = 4 max_seconds_waiting_for_partners = 1000 * 65 * 1 endowment = 1 multiplier = 2 empathy_values = [(n, f'global/imgs/emoji{n}.png') for n in range(1, 6)] class Subsession(BaseSubsession): # match players and identify the relevant partner identity treatment def set_partner_based_on_identity(self, still_waiting_for_task, task, num_players): # look for group in condition other_region or other_school for t in ['region', 'school']: s = set(p.participant.vars['data'].get(t) for p in still_waiting_for_task) if len(s) == num_players: i = [s.pop() for _ in range(num_players)] group = [next(p for p in still_waiting_for_task if p.participant.vars['data'].get(t) == j) for j in i] if t == 'region': for p in group: p.participant.vars['partner'] = 'other_region' p.cooperate_partner = 'other_region' else: for p in group: p.participant.vars['partner'] = 'other_school' p.cooperate_partner = 'other_school' return group # look for group in condition same class schools = set(p.participant.vars['data'].get('school') for p in still_waiting_for_task) for s in schools: classes = set(p.participant.vars['data'].get('class') for p in still_waiting_for_task if p.participant.vars['data'].get('school') == s) for c in classes: group = [p for p in still_waiting_for_task if p.participant.vars['data'].get('school') == s and p.participant.vars['data'].get('class') == c] if len(group) >= num_players: group = group[:num_players] for p in group: p.participant.vars['partner'] = 'same_class' p.cooperate_partner = 'same_class' return group[:num_players] # randomize to other two conditions: name, ctrl group = still_waiting_for_task[:num_players] if task == 'redistribute_team': partner_treatment = next(treatments_solo_tasks['partner_redistribute']) else: partner_treatment = next(treatments_solo_tasks['partner']) for p in group: p.participant.vars['partner'] = partner_treatment p.cooperate_partner = partner_treatment return group class Group(BaseGroup): pass class Player(BasePlayer): cooperate_partner = models.StringField() cooperate_partner_name = models.StringField() cooperate_decision = models.CurrencyField( label=f'Veuillez utiliser un multiple de 0.1 crédit :', min=0, max=Constants.endowment, widget=NumberInput(attrs={'step': 0.1})) empathy = models.IntegerField() timeout = models.BooleanField() def set_payoff(self): if self.participant.vars.get('unmatched'): self.participant.vars['other_cooperate'] = round(uniform(0, 1), 1) self.payoff = Constants.endowment - self.cooperate_decision + \ self.participant.vars['other_cooperate'] * Constants.multiplier else: self.payoff = Constants.endowment - self.cooperate_decision + \ self.get_others_in_group()[0].cooperate_decision * Constants.multiplier