from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random import itertools from otree.widgets import Slider class Constants(BaseConstants): name_in_url = 'number_mapping_dayyear' players_per_group = None numbers_probs = [5, 9, 18, 90, 99, 143, 200, 267, 300, 390] conditions = [ # 'bounded', 'unbounded' ] num_rounds = len(numbers_probs) class Subsession(BaseSubsession): def before_session_starts(self): task_iter = itertools.cycle(Constants.conditions) if self.round_number == 1: for p in self.get_players(): p.participant.vars['condition'] = next(task_iter) p.participant.vars['dayyear_paying_round'] = random.randint(1, Constants.num_rounds) p.participant.vars['page_sequence_map'] = random.sample(Constants.numbers_probs, len(Constants.numbers_probs)) for p in self.get_players(): p.set_current_task_number() p.condition = p.participant.vars['condition'] if p.round_number == p.participant.vars['dayyear_paying_round']: p.on_paying_round = True class Group(BaseGroup): pass class Player(BasePlayer): guess = models.FloatField() task_identifier = models.IntegerField() condition = models.CharField() on_paying_round = models.BooleanField(initial=False) def set_current_task_number(self): setattr(self, 'task_identifier', self.participant.vars['page_sequence_map'][int(self.round_number-1)]) def set_payoffs(self): payoff = 0 if abs(self.guess - float(self.task_identifier)) <= 5: payoff = self.session.config['incentive_day_year'] if 'possible_payoffs' in self.participant.vars: self.participant.vars['possible_payoffs'].append(payoff) else: self.participant.vars['possible_payoffs'] = [payoff]