from otree.api import * author = 'Your name here' doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'individual_commitment' PLAYERS_PER_GROUP = 5 OTHER_PLAYERS_PER_GROUP = PLAYERS_PER_GROUP-1 NUM_ROUNDS = 3 INVESTMENT_FACTOR = 2 BUDGET = 10 number_intervals_vars_for_admin = 10 class Subsession(BaseSubsession): pass class Group(BaseGroup): total_contribution = models.FloatField() total_profit = models.FloatField() class Player(BasePlayer): contribution = models.FloatField( min=0, max=C.BUDGET, label="How much do you want to invest?" ) profit = models.FloatField() # FUNCTIONS def vars_for_admin_report(subsession): interval_list = [] for elem in range(C.number_intervals_vars_for_admin): if elem == 0: interval_list.append(str(0) + "-" + str(round(10 / C.number_intervals_vars_for_admin))) else: interval_list.append(str((round(10 / C.number_intervals_vars_for_admin) * elem)) + "-" + str( (round(10 / C.number_intervals_vars_for_admin) * (elem + 1)))) player_contributions = ([p.contribution for p in subsession.get_players()]) absolute_frequencies = [0] * C.number_intervals_vars_for_admin threshold_values = [0] for elem in range(C.number_intervals_vars_for_admin + 1): if elem != 0: threshold_values.append(round(10 / C.number_intervals_vars_for_admin) * elem) for i in range(len(player_contributions)): for elem in range(C.number_intervals_vars_for_admin): if player_contributions[i] > threshold_values[elem] and player_contributions[i] <= threshold_values[elem + 1]: absolute_frequencies[elem] += 1 return { "interval_list": interval_list, "threshold_values": threshold_values, "absolute_frequencies": absolute_frequencies, } # PAGES class Contribution(Page): form_model = "player" form_fields = ["contribution"] class ResultsWaitPage(WaitPage): @staticmethod def after_all_players_arrive(group: Group): total_contribution = 0 for player in group.get_players(): total_contribution += player.contribution group.total_contribution = total_contribution group.total_profit = total_contribution * C.INVESTMENT_FACTOR class Results(Page): @staticmethod def vars_for_template(player: Player): player.profit = ( C.BUDGET - player.contribution ) + player.group.total_profit / C.PLAYERS_PER_GROUP list_of_contributions = [] for p in player.get_others_in_group(): list_of_contributions.append(p.contribution) return { "list_of_contributions": list_of_contributions, "total_contribution_times_two": player.group.total_contribution*2, "evenly_distributed_share": player.group.total_contribution*2/C.PLAYERS_PER_GROUP, } page_sequence = [Contribution, ResultsWaitPage, Results]