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 = 'dots_multiplication' players_per_group = None dot_conditions = [250] numbers_probs = [20, 30] num_rounds = len(numbers_probs) class Subsession(BaseSubsession): def before_session_starts(self): if self.round_number == 1: for p in self.get_players(): c = random.sample(Constants.dot_conditions, 1) p.participant.vars['conditions'] = c p.participant.vars['dots_paying_round'] = random.randint(1, Constants.num_rounds) list_of_tasks = [(x, c[0]) for x in Constants.numbers_probs] p.participant.vars['dot_sequence'] = random.sample(list_of_tasks, len(list_of_tasks)) for p in self.get_players(): p.set_task() if p.round_number == p.participant.vars['dots_paying_round']: p.on_paying_round = True class Group(BaseGroup): pass class Player(BasePlayer): guess = models.FloatField() task_identifier_percent = models.IntegerField() task_identifier_cloud = models.IntegerField() on_paying_round = models.BooleanField(initial=False) def set_task(self): task_tuple = self.participant.vars['dot_sequence'][self.round_number - 1] setattr(self, 'task_identifier_percent', task_tuple[0]) setattr(self, 'task_identifier_cloud', task_tuple[1]) def set_payoffs(self): payoff = 0 correct_guess = self.task_identifier_cloud * self.task_identifier_percent/100 if abs(self.guess - correct_guess) <= 5: payoff = self.session.config['incentive_dots'] if 'possible_payoffs' in self.participant.vars: self.participant.vars['possible_payoffs'].append(payoff) else: self.participant.vars['possible_payoffs'] = [payoff]