from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from django.forms.widgets import NumberInput from random import uniform import utils author = 'Tommaso Batistoni - t.batistoni@ucl.ac.uk' doc = """ Mutual Aid Game """ class Constants(BaseConstants): name_in_url = 'taskcpr' players_per_group = None num_rounds = 4 max_seconds_waiting_for_partners = 1000 * 20 * 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): return utils.set_partner_based_on_identity(still_waiting_for_task, task, num_players) class Group(BaseGroup): pass class Player(BasePlayer): cooperate_partner = models.StringField() cooperate_partner_name = models.StringField() cooperate_decision = models.CurrencyField( label=f'Please use a multiple of 0.1 credit:', 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