from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from random import randint, choice, sample from treatments import * from intro.models import Constants as IntroConstants author = 'Tommaso Batistoni - t.batistoni@ucl.ac.uk' doc = """ Single player tasks/games, including:
""" class Constants(BaseConstants): name_in_url = 'taskgmsl' players_per_group = None num_rounds = 1 # Time time_endowment = 5 time_today_bonuses = [n for n in range(time_endowment + 1)] time_month_bonuses = [n * 2 for n in time_today_bonuses] time_range_input_steps = len(time_today_bonuses) - 1 # Bret box_value = 1 num_rows = 2 num_cols = 5 box_height = '50px' box_width = '50px' random_payoff = True instructions = True feedback = True results = True dynamic = False time_interval = 1.00 random = True devils_game = True undoable = True # Dictator dictator_endowment = 10 dictator_range_input_steps = dictator_endowment # Moral moral_endowment = 10 moral_num_choices = 5 moral_choice_for_unicef = 'donation of 10 credits to UNICEF; no credits for me' moral_choices_for_self = [f'I recieve {n} credits; no donation to UNICEF' for n in range(2, 12, 2)] # Lying lying_outcomes = [c(n) for n in range(1, 7)] class Subsession(BaseSubsession): def creating_session(self): if 'teachers_session' not in self.session.config or self.session.config.get('teachers_session') is False: names = IntroConstants.stud_first_names print(len(names),names[:10]) print(len(self.get_players())) names_sampled = cycle(sample(names, len(self.get_players()))) for p in self.get_players(): # Assign partner treatment for dictator game p.dictator_partner = next(treatments['partner']) if p.dictator_partner == 'name': p.dictator_partner_name = next(names_sampled) class Group(BaseGroup): pass class Player(BasePlayer): # Time time_decision = models.IntegerField() time_today_payoff = models.CurrencyField() time_delayed_payoff = models.CurrencyField() time_payoff = models.CurrencyField() def set_time_payoff(self): self.time_today_payoff = Constants.time_endowment - self.time_decision self.time_delayed_payoff = self.time_decision * 2 self.time_payoff = self.time_today_payoff + self.time_delayed_payoff # Bret bomb = models.IntegerField() bomb_row = models.PositiveIntegerField() bomb_col = models.PositiveIntegerField() boxes_collected = models.IntegerField() pay_this_round = models.BooleanField() round_result = models.CurrencyField() bret_payoff = models.CurrencyField() def set_bret_payoff(self): if self.bomb: self.round_result = c(0) else: self.round_result = self.boxes_collected * Constants.box_value if self.subsession.round_number == 1: self.participant.vars['round_to_pay'] = randint(1, Constants.num_rounds) if Constants.random_payoff: if self.subsession.round_number == self.participant.vars['round_to_pay']: self.pay_this_round = True self.bret_payoff = self.round_result else: self.pay_this_round = False self.bret_payoff = c(0) else: self.bret_payoff = self.round_result # Dictator dictator_partner = models.StringField() dictator_partner_name = models.StringField() dictator_decision = models.IntegerField(label='Please choose one among the following final distributions :') dictator_payoff = models.CurrencyField() def set_dictator_payoff(self): self.dictator_payoff = Constants.dictator_endowment - self.dictator_decision # Moral for n in range(1, Constants.moral_num_choices + 1): locals()['moral_decision_' + str(n)] = models.IntegerField( choices=[(0, Constants.moral_choices_for_self[n - 1]), (1, Constants.moral_choice_for_unicef)], widget=widgets.RadioSelect) del n moral_decision_to_pay = models.IntegerField() moral_payoff = models.IntegerField() def set_moral_payoff(self): self.moral_decision_to_pay = choice([n for n in range(1, Constants.moral_num_choices + 1)]) choice_to_pay = getattr(self, 'moral_decision_{}'.format(self.moral_decision_to_pay)) self.participant.vars['moral_choice_to_pay'] = choice_to_pay if choice_to_pay == 0: self.moral_payoff = 2 * self.moral_decision_to_pay elif choice_to_pay == 1: self.moral_payoff = 0 # Lying lying_decision = models.IntegerField(label="What number do you see?", min=1, max=6) lying_payoff = models.CurrencyField()