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 = 'timeT' players_per_group = 6 num_rounds = 20 time_A = 14 b = 7 beta = 2 f = 2 initial_points = 35 instructions_template = 'timeT/instructions.html' navi_template = 'timeT/navi.html' auszahlung_template = 'timeT/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) for g in self.get_groups(): g.set_routing() 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() if p.choice == p.routing: p.compliance = 1 else: p.compliance = 0 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['t2'] = round(p.total_points, 2) #sum of points from this treatment in 't2' def set_routing(self): for j in range(int(Constants.num_rounds/2)): numbers = [1, 2, 3, 4, 5, 6] route_B = [0, 0, 0] for i in range(3): route_B[i] = random.choice(numbers) numbers.remove(route_B[i]) self.get_player_by_id(route_B[i]).in_round(j + 1).routing = 1 self.get_player_by_id(route_B[i]).in_round(Constants.num_rounds - j).routing = 0 for i in numbers: self.get_player_by_id(i).in_round(j + 1).routing = 0 self.get_player_by_id(i).in_round(Constants.num_rounds - j).routing = 1 class Player(BasePlayer): price = models.FloatField() time = models.IntegerField() points = models.FloatField() total_points = models.FloatField() routing = models.IntegerField() #r=0 Route A zugewiesen, r=1 Route B zugewiesen compliance = models.IntegerField() #compliance = 1 heißt Spieler folgt Navi 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', 'routing', 'compliance', '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.routing, p.compliance, p.price]