from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'controlT' players_per_group = 6 num_rounds = 20 time_A = 14 b = 7 beta = 2 f = 2 initial_points = 35 instructions_template = 'controlT/instructions.html' navi_template = 'controlT/navi.html' auszahlung_template = 'controlT/travelTimes.html' class Subsession(BaseSubsession): def creating_session(self): for p in self.get_players(): p.price = round(random.uniform(0.5, 1.5), 2) class Group(BaseGroup): n_A = models.IntegerField() n_B = models.IntegerField() time_B = models.IntegerField() def set_points(self): players = self.get_players() for p in players: p.binary_choice = p.get_binary_choice() choices = [p.binary_choice for p in players] self.n_B = sum(choices) self.n_A = Constants.players_per_group - self.n_B self.time_B = Constants.b + pow(self.n_B - Constants.f, Constants.beta) for p in players: p.time = Constants.time_A - p.binary_choice * (Constants.time_A - self.time_B) p.points = round(Constants.initial_points - p.price * p.time, 2) if self.round_number == 1: p.total_points = p.points else: p.total_points = p.in_round(self.round_number-1).total_points + p.points if self.round_number == Constants.num_rounds: p.participant.vars['t1'] = round(p.total_points, 2) #sum of points from this treatment in 't1' class Player(BasePlayer): price = models.FloatField() time = models.IntegerField() points = models.FloatField() total_points = models.FloatField() choice = models.BooleanField(label="Welche Route wählen Sie?", widget=widgets.RadioSelectHorizontal, choices=[[True, 'Route A'], [False, 'Route B']]) binary_choice = models.IntegerField() #binary_choice=0 Route A gewählt def get_binary_choice(self): if self.choice == True: self.binary_choice = 0 else: self.binary_choice = 1 return self.binary_choice def custom_export(players): # header row yield ['session', 'participant_code', 'round_number', 'id_in_group', 'points', 'time', 'binary_choice', 'price'] for p in players: yield [p.session.code, p.participant.code, p.round_number, p.id_in_group, p.points, p.time, p.binary_choice, p.price]