from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) author = 'Marlene Bargou' doc = "" class Constants(BaseConstants): name_in_url = 'aejwf' players_per_group = None num_rounds = 1 endowment = 10 gender = [[1, 'weiblich'], [2, 'männlich'], [3, 'divers']] jobs = [[1, 'Angestellter/Beamter'], [2, 'Arbeiter'], [3, 'Rentner'], [4, 'Schüler'], [5, 'Keine der Anwortmöglichkeiten zutreffend']] take_choices = [[0, '0 ECU'], [1, '1 ECU'], [2, '2 ECU'], [3, '3 ECU'], [4, '4 ECU'], [5, '5 ECU'], [6, '6 ECU'], [7, '7 ECU'], [8, '8 ECU'], [9, '9 ECU'], [10, '10 ECU']] labor_code = '2020-XX-' timertext_hard = "Bitte treffen Sie zügig eine Entscheidung. Falls Sie nicht innerhalb einer Minute eine Eingabe machen," \ " werden Sie ausgeschlossen." timertext_soft = "Bitte treffen Sie zügig eine Entscheidung. Falls Sie nicht innerhalb einer Minute eine Eingabe machen," \ " werden Sie weitergeleitet." timeout = 6 * 60 takex = [4, 6, 5, 5, 5, 5, 5, 5, 5, 5, 7, 6, 5, 4, 3, 2, 1, 7, 5, 4, 3, 5, 6, 3, 6] # TODO: len(takex) muss mind. Anzahl an Gruppen ensprechen!!! mean_counter = 5 # TODO: set back to 5 class Subsession(BaseSubsession): ended = models.BooleanField(initial=False) def creating_session(self): self.set_code() # set groups of 3 matrix = [] ppg = self.session.config['ppg'] ps = self.get_players() nr_groups = int(len(ps) / ppg) for g in range(nr_groups): group = [] for _ in range(ppg): group.append(ps.pop(0).id_in_subsession) matrix.append(group) for p in ps: matrix.append([p.id_in_subsession]) p.is_redundant = True self.set_group_matrix(matrix) # pass every group one algo-take if self.session.config['name'] != 'algorithm3': import random self.session.vars['takex'] = random.sample(Constants.takex, len(Constants.takex)) takex = self.session.vars['takex'] for g in self.get_groups(): g.takemax = takex.pop(0) def set_payoff2(self): for g in self.get_groups(): if g.hasDropout is True or g.is_redundant(): continue g.evaluateMean() g.deviation() for p in g.get_players(): payoff2 = 0 if p.deviation is not None: if p.deviation <= 0.1: payoff2 = 5 elif p.deviation <= 0.2: payoff2 = 4 elif p.deviation <= 0.3: payoff2 = 3 elif p.deviation <= 0.4: payoff2 = 2 elif p.deviation <= 0.5: payoff2 = 1 p.ecu2 = payoff2 def end(self): if self.ended is not True: for g in self.get_groups(): g.hasDropout = True self.ended = True def set_code(self): for p in self.get_players(): p.code = Constants.labor_code p.code += p.session.code p.code += "-" p.code += str(p.participant.code) class Group(BaseGroup): takeb = models.IntegerField( label="", choices=Constants.take_choices ) takeb1 = models.IntegerField( choices=Constants.take_choices[0:2], label="" ) takeb2 = models.IntegerField( choices=Constants.take_choices[0:3], label="" ) takeb3 = models.IntegerField( choices=Constants.take_choices[0:4], label="" ) takeb4 = models.IntegerField( choices=Constants.take_choices[0:5], label="" ) takeb5 = models.IntegerField( choices=Constants.take_choices[0:6], label="" ) takeb6 = models.IntegerField( choices=Constants.take_choices[0:7], label="" ) takeb7 = models.IntegerField( choices=Constants.take_choices[0:8], label="" ) takeb8 = models.IntegerField( choices=Constants.take_choices[0:9], label="" ) takeb9 = models.IntegerField( choices=Constants.take_choices[0:10], label="" ) takeb10 = models.IntegerField( choices=Constants.take_choices, label="" ) takemax = models.IntegerField() take = models.IntegerField(label="", choices=Constants.take_choices) takeMean = models.FloatField() hasDropout = models.BooleanField() def set_payoff1(self): treatment = self.session.config['name'] if treatment == 'algorithm1': if self.takemax is not None: self.evaluateTakeB() if treatment != 'algorithm3': # algo3 already has take if self.takeb is not None: self.evaluateTake() take = self.take if take is not None: p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) p1.ecu1 = 10 - take p2.ecu1 = take def evaluateTakeB(self): temp = self.takemax if temp == 0: self.takeb = 0 elif temp == 1: self.takeb = self.takeb1 elif temp == 2: self.takeb = self.takeb2 elif temp == 3: self.takeb = self.takeb3 elif temp == 4: self.takeb = self.takeb4 elif temp == 5: self.takeb = self.takeb5 elif temp == 6: self.takeb = self.takeb6 elif temp == 7: self.takeb = self.takeb7 elif temp == 8: self.takeb = self.takeb8 elif temp == 9: self.takeb = self.takeb9 else: self.takeb = self.takeb10 def evaluateTake(self): if self.takeb < self.takemax: self.take = self.takeb else: self.take = self.takemax def evaluateMean(self): import random treatment = self.session.config['name'] counter = 0 take_sum = 0 groups = self.subsession.get_groups() groups = random.sample(groups, len(groups)) index = 0 # for algorithm1, based on takeb10 if treatment == 'algorithm1': while counter < Constants.mean_counter: if index < len(groups): current = groups[index] if current != self and not current.is_redundant(): take = current.takeb10 if take is not None: take_sum += take counter += 1 index += 1 else: self.subsession.end() break # for algorithm2, based on takeb elif treatment == 'algorithm2': while counter < Constants.mean_counter: if index < len(groups): current = groups[index] if current != self and not current.is_redundant(): take = current.takeb if take is not None: take_sum += take counter += 1 index += 1 else: self.subsession.end() break # for algorithm3, based on take else: while counter < Constants.mean_counter: if index < len(groups): current = groups[index] if current != self and not current.is_redundant(): take = current.take if take is not None: take_sum += take counter += 1 index += 1 else: self.subsession.end() break if counter != 0: self.takeMean = round(take_sum / counter, 1) else: self.subsession.end() def deviation(self): for p in self.get_players(): if (p.guess is not None) and (self.takeMean is not None): p.deviation = round(abs(self.takeMean - p.guess), 1) def is_redundant(self): players = self.get_players() return len(players) != self.session.config['ppg'] class Player(BasePlayer): guess = models.FloatField( min=0.0, max=10.0, label='ECU' ) deviation = models.FloatField() ecu0 = models.IntegerField(initial=2) # participation fee: 2 ECU ecu1 = models.IntegerField(initial=0) # ECU payoff from decision ecu2 = models.IntegerField(initial=0) # ECU payoff from guessing ecu_total = models.IntegerField(initial=0) birthyear = models.IntegerField( min=1940, max=2010 ) gender = models.IntegerField( choices=Constants.gender, widget=widgets.RadioSelectHorizontal ) isStudent = models.BooleanField( choices=[[True, "Ja"], [False, "Nein"]], widget=widgets.RadioSelectHorizontal ) school = models.StringField() enrolled = models.IntegerField( min=2005, max=2021 ) job = models.IntegerField( choices=Constants.jobs, widget=widgets.RadioSelect ) test = models.IntegerField( min=0, max=10 ) comment = models.LongStringField( blank=True ) code = models.StringField() confirmation = models.BooleanField( choices=[[True, "Ja"], [False, "Nein"]], widget=widgets.RadioSelectHorizontal ) isDropout = models.BooleanField(initial=False) is_redundant = models.BooleanField(initial=False)