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 import utils author = 'Tommaso Batistoni - t.batistoni@ucl.ac.uk' doc = """ Trust Game where each of the two players takes both roles """ class Constants(BaseConstants): name_in_url = 'tasktrs' players_per_group = 2 num_rounds = 1 max_seconds_waiting_for_partners = 1000 * 20 * 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): return utils.set_partner_based_on_identity(still_waiting_for_task, task, num_players) 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() empathy_self = models.IntegerField() 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 'This should not exceed the tripled quantity' 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