from otree.api import * class C(BaseConstants): NAME_IN_URL = 'CRSD_Hom5' PLAYERS_PER_GROUP = 6 NUM_ROUNDS = 10 ENDOWMENT = cu(120) ASSET = cu(120) COMMON_POOL = 360 THRESHOLD = 600 LOSS_RATE = 0.8 INSTRUCTION_TEMPLATE = 'CRSD_Hom5/instructions.html' INSTRUCTION_SUMMARY_TEMPLATE = 'CRSD_Hom5/instructions_summary.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): total_round_cont = models.CurrencyField() cum_group_cont = models.CurrencyField() cum_com_pool = models.CurrencyField() class Player(BasePlayer): contribution = models.CurrencyField(widget=widgets.RadioSelectHorizontal, label="Bu turdaki kararınız nedir?", choices=[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]) endowment = models.CurrencyField() tot_cont = models.CurrencyField() odeme = models.FloatField() # FUNCTIONS def set_round_cont(group): players = group.get_players() contributions = [p.contribution for p in players] group.total_round_cont = sum(contributions) group.cum_group_cont = sum([g.total_round_cont for g in group.in_all_rounds()]) group.cum_com_pool = C.COMMON_POOL + sum([g.total_round_cont for g in group.in_all_rounds()]) return group.total_round_cont def set_p_tot_cont(player): p_all_round = player.in_all_rounds() p_contributions = [p.contribution for p in p_all_round] single_p_all_cont = sum(p_contributions) player.tot_cont = single_p_all_cont return player.tot_cont def set_first_p_cont(player): group = player.group first_p = group.get_player_by_id(1).in_all_rounds() first_p_cont = [f.contribution for f in first_p] first_p_sum_cont = sum(first_p_cont) return first_p_sum_cont def set_second_p_cont(player): group = player.group second_p = group.get_player_by_id(2).in_all_rounds() second_p_cont = [s.contribution for s in second_p] second_p_sum_cont = sum(second_p_cont) return second_p_sum_cont def set_third_p_cont(player): group = player.group third_p = group.get_player_by_id(3).in_all_rounds() third_p_cont = [t.contribution for t in third_p] third_p_sum_cont = sum(third_p_cont) return third_p_sum_cont def set_fourth_p_cont(player): group = player.group fourth_p = group.get_player_by_id(4).in_all_rounds() fourth_p_cont = [x.contribution for x in fourth_p] fourth_p_sum_cont = sum(fourth_p_cont) return fourth_p_sum_cont def set_fifth_p_cont(player): group = player.group fifth_p = group.get_player_by_id(5).in_all_rounds() fifth_p_cont = [z.contribution for z in fifth_p] fifth_p_sum_cont = sum(fifth_p_cont) return fifth_p_sum_cont def set_sixth_p_cont(player): group = player.group sixth_p = group.get_player_by_id(6).in_all_rounds() sixth_p_cont = [t.contribution for t in sixth_p] sixth_p_sum_cont = sum(sixth_p_cont) return sixth_p_sum_cont def current_endowment(player): p_prev_cont = [p.contribution for p in player.in_previous_rounds()] if player.round_number == 1: player.endowment = C.ENDOWMENT else: player.endowment = C.ENDOWMENT - sum(p_prev_cont) return player.endowment def average_round_cont(group): g_all_round_conts = [g.total_round_cont for g in group.in_all_rounds()] avg_cont = [i / C.PLAYERS_PER_GROUP for i in g_all_round_conts] return avg_cont def set_payoffs(group): # group = player.group players = group.get_players() g_total_contribution = set_round_cont(group) + sum([g.total_round_cont for g in group.in_previous_rounds()]) cum_common_pool = C.COMMON_POOL + g_total_contribution if cum_common_pool < C.THRESHOLD: for p in players: p.payoff = (1 - C.LOSS_RATE) * C.ASSET + (C.ENDOWMENT - p.tot_cont) else: for p in players: p.payoff = C.ASSET + (C.ENDOWMENT - p.tot_cont) def setodeme(group): players = group.get_players() for p in players: odeme = p.participant.payoff_plus_participation_fee() p.odeme = float(odeme) print("participant payoff", p.participant.payoff_plus_participation_fee()) print("odeme", odeme) # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 form_model = 'player' class Contribute(Page): form_model = 'player' form_fields = ['contribution'] @staticmethod def vars_for_template(player: Player): curr_endowment = current_endowment(player) return dict(current_endowment=curr_endowment) class ResultsWaitPage(WaitPage): after_all_players_arrive = set_round_cont @staticmethod def before_next_page(player): player.set_p_tot_cont() class RoundResults(Page): timeout_seconds = 120 @staticmethod def vars_for_template(player: Player): group = player.group cumulative_cont = set_round_cont(group) + sum([g.total_round_cont for g in group.in_previous_rounds()]) cum_common_pool = C.COMMON_POOL + cumulative_cont remaining_thres = C.THRESHOLD - cum_common_pool player_in_all_rounds = player.in_all_rounds() player_total_cont = set_p_tot_cont(player) group_in_all_rounds = group.in_all_rounds() average_round_contribution = average_round_cont(group) first_player = group.get_player_by_id(1).in_all_rounds() second_player = group.get_player_by_id(2).in_all_rounds() third_player = group.get_player_by_id(3).in_all_rounds() fourth_player = group.get_player_by_id(4).in_all_rounds() fifth_player = group.get_player_by_id(5).in_all_rounds() sixth_player = group.get_player_by_id(6).in_all_rounds() first_player_total_cont = set_first_p_cont(player) second_player_total_cont = set_second_p_cont(player) third_player_total_cont = set_third_p_cont(player) fourth_player_total_cont = set_fourth_p_cont(player) fifth_player_total_cont = set_fifth_p_cont(player) sixth_player_total_cont = set_sixth_p_cont(player) first_p_current_endowment = C.ENDOWMENT - set_first_p_cont(player) second_p_current_endowment = C.ENDOWMENT - set_second_p_cont(player) third_p_current_endowment = C.ENDOWMENT - set_third_p_cont(player) fourth_p_current_endowment = C.ENDOWMENT - set_fourth_p_cont(player) fifth_p_current_endowment = C.ENDOWMENT - set_fifth_p_cont(player) sixth_p_current_endowment = C.ENDOWMENT - set_sixth_p_cont(player) return dict(cumulative_cont=cumulative_cont, remaining_threshold=remaining_thres, player_in_all_rounds=player_in_all_rounds, group_in_all_rounds=group_in_all_rounds, cum_common_pool=cum_common_pool, player_total_cont=player_total_cont, first_player=first_player, second_player=second_player, third_player=third_player, fourth_player=fourth_player, fifth_player=fifth_player, sixth_player=sixth_player, average_round_contribution=average_round_contribution, p1totcont=first_player_total_cont, p2totcont=second_player_total_cont, p3totcont=third_player_total_cont, p4totcont=fourth_player_total_cont, p5totcont=fifth_player_total_cont, p6totcont=sixth_player_total_cont, p1_curr_endow=first_p_current_endowment, p2_curr_endow=second_p_current_endowment, p3_curr_endow=third_p_current_endowment, p4_curr_endow=fourth_p_current_endowment, p5_curr_endow=fifth_p_current_endowment, p6_curr_endow=sixth_p_current_endowment ) class FinalResultsWaitPage(WaitPage): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS after_all_players_arrive = set_payoffs class Results(Page): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS @staticmethod def vars_for_template(player: Player): group = player.group # payoffs = set_payoffs(player) # print("payoffs:", payoffs) p_all_round = player.in_all_rounds() p_contributions = [player.contribution for player in p_all_round] # print("Player Contributions:", p_contributions) single_p_all_cont = sum(p_contributions) g_total_contribution = set_round_cont(group) + sum([g.total_round_cont for g in group.in_previous_rounds()]) # print("Group Total Contribution: ", g_total_contribution) final_endowment = C.ENDOWMENT - set_p_tot_cont(player) cum_common_pool_last = C.COMMON_POOL + set_round_cont(group) + sum( [g.total_round_cont for g in group.in_previous_rounds()]) return dict(single_p_all_cont=single_p_all_cont, g_total_contribution=g_total_contribution, # payoffs=payoffs, final_endowment=final_endowment, cum_common_pool=cum_common_pool_last ) class EarningsWaitPage(WaitPage): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS after_all_players_arrive = setodeme page_sequence = [Introduction, Contribute, ResultsWaitPage, RoundResults, FinalResultsWaitPage, Results, EarningsWaitPage]