from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from django.forms.widgets import NumberInput from random import choice, uniform from treatments_solo_tasks import * author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'tasktrs' players_per_group = 2 num_rounds = 1 max_seconds_waiting_for_partners = 1000 * 65 * 1 endowment = 5 multiplier = 3 step = 0.5 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.trust_partner = 'other_region' else: for p in group: p.participant.vars['partner'] = 'other_school' p.trust_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.trust_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.trust_partner = partner_treatment return group class Group(BaseGroup): partner = models.StringField() class Player(BasePlayer): trust_partner = models.StringField() trust_partner_name = models.StringField() trust_send = models.CurrencyField( label='', min=0, max=Constants.endowment, widget=NumberInput(attrs={'step': Constants.step}) ) timeout_send = models.BooleanField() empathy = models.IntegerField() trust_return = models.CurrencyField(label='', min=0) timeout_return = models.BooleanField() trust_decision_to_pay = models.IntegerField() partner_name = models.StringField() def other_player(self): return self.get_others_in_group()[0] def trust_return_max(self): if self.participant.vars.get('unmatched'): other_sent = self.participant.vars.get('other_sent') else: other_sent = self.other_player().trust_send return other_sent * Constants.multiplier def trust_return_error_message(self, value): if value > self.trust_return_max(): return 'Ceci ne doit pas excéder la quantité triplée' def set_payoff(self): self.trust_decision_to_pay = choice([1, 2]) if self.trust_decision_to_pay == 1: if self.participant.vars.get('unmatched'): other_return = self.participant.vars['other_return'] else: other_return = self.other_player().trust_return self.payoff = Constants.endowment - self.trust_send + other_return else: if self.participant.vars.get('unmatched'): other_sent = self.participant.vars.get('other_sent') else: other_sent = self.other_player().trust_send self.payoff = other_sent * Constants.multiplier - self.trust_return