from otree.api import * import random doc = """ Siyasal Kamu Malı Deneyi — Hazine Versiyonu ============================================ Threshold Public Goods Game with Treasury, Political Competition, and Strategic Information Distortion. """ class C(BaseConstants): NAME_IN_URL = 'pgg' PLAYERS_PER_GROUP = 7 NUM_ROUNDS = 12 N_VOTERS = 5 # Economy ENDOWMENT = 20 THRESHOLD = 50 MPCR_MIN = 0.2 MPCR_MAX = 1 REFUND_RATE_FAIL = 0.80 # Belief elicitation BELIEF_BONUS = 2 BELIEF_TOLERANCE_REVENUE = 5 # Treasury INITIAL_TREASURY = 50 TREASURY_FLOOR = -50 # Salaries LEADER_SALARY = 20 OPP_SALARY = 20 # Elections ELECTION_ROUNDS = [4, 8] TERM_START_ROUNDS = [1, 5, 9] # Roles ROLE_LEADER = 'leader' ROLE_OPPOSITION = 'opposition' ROLE_VOTER = 'voter' LEADER_ID = 1 OPPOSITION_ID = 2 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): if subsession.round_number == 1: for p in subsession.get_players(): if p.id_in_group == C.LEADER_ID: p.participant.vars['role'] = C.ROLE_LEADER elif p.id_in_group == C.OPPOSITION_ID: p.participant.vars['role'] = C.ROLE_OPPOSITION else: p.participant.vars['role'] = C.ROLE_VOTER for p in subsession.get_players(): p.role_in_game = p.participant.vars.get('role', C.ROLE_VOTER) def current_role(player: 'Player'): return player.participant.vars.get('role', player.role_in_game or C.ROLE_VOTER) class Group(BaseGroup): # State mpcr = models.FloatField() # Announcements announced_distribution = models.FloatField(initial=0) opp_announced_distribution = models.FloatField(initial=0) # Contributions total_contribution = models.FloatField(initial=0) threshold_met = models.BooleanField(initial=False) excess_contribution = models.FloatField(initial=0) # Revenue allocation actual_project_revenue = models.FloatField(initial=0) revenue_gap = models.FloatField(initial=0) gap_to_treasury = models.FloatField(initial=0) leader_private_gap = models.FloatField(initial=0) excess_to_treasury = models.FloatField(initial=0) leader_private_excess = models.FloatField(initial=0) # Treasury treasury_start = models.FloatField(initial=C.INITIAL_TREASURY) treasury_pre_tax = models.FloatField(initial=0) treasury_end = models.FloatField(initial=0) tax_per_voter = models.FloatField(initial=0) deficit_used_this_term = models.BooleanField(initial=False) # Election votes_incumbent = models.IntegerField(initial=0) votes_opposition = models.IntegerField(initial=0) election_held = models.BooleanField(initial=False) election_winner = models.StringField(initial='') # Helpers def get_leader(self): return next((p for p in self.get_players() if current_role(p) == C.ROLE_LEADER), None) def get_opposition(self): return next((p for p in self.get_players() if current_role(p) == C.ROLE_OPPOSITION), None) def get_voters(self): return [p for p in self.get_players() if current_role(p) == C.ROLE_VOTER] # Core mechanics def draw_mpcr(self): self.mpcr = round(random.uniform(C.MPCR_MIN, C.MPCR_MAX), 2) def initialize_round_state(self): if self.round_number == 1: self.treasury_start = C.INITIAL_TREASURY self.deficit_used_this_term = False else: prev = self.in_round(self.round_number - 1) self.treasury_start = prev.treasury_end if self.round_number in C.TERM_START_ROUNDS: self.deficit_used_this_term = False else: self.deficit_used_this_term = prev.deficit_used_this_term def max_allowed_announcement(self): base = self.treasury_start + C.THRESHOLD * self.mpcr * C.N_VOTERS if self.deficit_used_this_term: return max(0.0, round(base, 2)) return max(0.0, round(base + abs(C.TREASURY_FLOOR), 2)) def check_threshold(self): voters = self.get_voters() self.total_contribution = round(sum(v.contribution or 0 for v in voters), 2) self.threshold_met = self.total_contribution >= C.THRESHOLD self.excess_contribution = round(max(0.0, self.total_contribution - C.THRESHOLD), 2) def collect_opposition_decision(self): if self.session.config.get('treatment', 'T2') != 'T2': self.opp_announced_distribution = 0 return opp = self.get_opposition() if opp is None: self.opp_announced_distribution = 0 return val = opp.field_maybe_none('opp_announced_distribution_input') self.opp_announced_distribution = round(val, 2) if val is not None else 0 def collect_leader_decisions(self): leader = self.get_leader() if leader is None: self.revenue_gap = 0 self.gap_to_treasury = 0 self.leader_private_gap = 0 self.excess_to_treasury = 0 self.leader_private_excess = 0 return actual_revenue = round(C.THRESHOLD * self.mpcr * C.N_VOTERS, 2) self.revenue_gap = round(actual_revenue - self.announced_distribution, 2) positive_gap = max(0.0, self.revenue_gap) gap_input = leader.field_maybe_none('gap_to_treasury_input') if gap_input is None: gap_input = 0 self.gap_to_treasury = round(min(max(gap_input, 0), positive_gap), 2) self.leader_private_gap = round(positive_gap - self.gap_to_treasury, 2) excess_input = leader.field_maybe_none('excess_to_treasury_input') if excess_input is None: excess_input = 0 self.excess_to_treasury = round(min(max(excess_input, 0), self.excess_contribution), 2) self.leader_private_excess = round(self.excess_contribution - self.excess_to_treasury, 2) def calculate_payoffs(self): voters = self.get_voters() leader = self.get_leader() opposition = self.get_opposition() if self.threshold_met: self.actual_project_revenue = round(C.THRESHOLD * self.mpcr * C.N_VOTERS, 2) else: self.actual_project_revenue = 0 if not self.threshold_met: self.revenue_gap = 0 self.gap_to_treasury = 0 self.leader_private_gap = 0 self.excess_to_treasury = 0 self.leader_private_excess = 0 if self.threshold_met: shortfall = max(0, round(self.announced_distribution - self.actual_project_revenue, 2)) self.treasury_pre_tax = round( self.treasury_start - shortfall + self.gap_to_treasury + self.excess_to_treasury, 2 ) else: self.treasury_pre_tax = round(self.treasury_start, 2) if self.treasury_pre_tax < 0: self.tax_per_voter = round((-self.treasury_pre_tax) / C.N_VOTERS, 2) self.treasury_end = 0 self.deficit_used_this_term = True else: self.tax_per_voter = 0 self.treasury_end = round(self.treasury_pre_tax, 2) for p in voters: if self.threshold_met: pre_tax = round( C.ENDOWMENT - p.contribution + self.announced_distribution / C.N_VOTERS, 2 ) else: pre_tax = round( C.ENDOWMENT - p.contribution + C.REFUND_RATE_FAIL * p.contribution, 2 ) belief_val = p.field_maybe_none('belief') if belief_val is not None and abs(belief_val - self.actual_project_revenue) <= C.BELIEF_TOLERANCE_REVENUE: pre_tax += C.BELIEF_BONUS p.belief_correct = True else: p.belief_correct = False p.payoff_pre_tax = round(pre_tax, 2) p.tax_paid = round(self.tax_per_voter, 2) p.payoff_this_round = round(pre_tax - self.tax_per_voter, 2) p.payoff = cu(p.payoff_this_round) if leader: if self.threshold_met: leader_income = C.LEADER_SALARY + self.leader_private_gap + self.leader_private_excess else: leader_income = C.LEADER_SALARY leader.payoff_this_round = round(leader_income, 2) leader.payoff = cu(leader.payoff_this_round) if opposition: opposition.payoff_this_round = round(C.OPP_SALARY, 2) opposition.payoff = cu(opposition.payoff_this_round) def conduct_election(self): self.election_held = True voters = self.get_voters() self.votes_incumbent = sum(1 for v in voters if v.vote == 'incumbent') self.votes_opposition = sum(1 for v in voters if v.vote == 'opposition') if self.votes_opposition > self.votes_incumbent: self.election_winner = 'opposition' leader_participant = None opposition_participant = None for p in self.get_players(): if current_role(p) == C.ROLE_LEADER: leader_participant = p.participant elif current_role(p) == C.ROLE_OPPOSITION: opposition_participant = p.participant if leader_participant and opposition_participant: leader_participant.vars['role'] = C.ROLE_OPPOSITION opposition_participant.vars['role'] = C.ROLE_LEADER for future_p in leader_participant.get_players(): if future_p.round_number >= self.round_number: future_p.role_in_game = C.ROLE_OPPOSITION for future_p in opposition_participant.get_players(): if future_p.round_number >= self.round_number: future_p.role_in_game = C.ROLE_LEADER else: self.election_winner = 'incumbent' class Player(BasePlayer): role_in_game = models.StringField() announced_distribution_input = models.FloatField( min=0, label='Proje başarılı olursa 5 seçmene toplam kaç ECU dağıtılacak?', blank=True, ) opp_announced_distribution_input = models.FloatField( min=0, label='Proje başarılı olursa 5 seçmene toplam kaç ECU dağıtılacak?', blank=True, ) gap_to_treasury_input = models.FloatField( min=0, label='Duyurulan ödeme sonrası kalan kamu geliri fazlasının kaç ECU’sunu hazineye aktarıyorsunuz?', blank=True, ) excess_to_treasury_input = models.FloatField( min=0, label='Aşım katkının kaç ECU’sunu hazineye aktarıyorsunuz?', blank=True, ) contribution = models.FloatField( min=0, max=C.ENDOWMENT, initial=0, label='Kamu projesine katkı miktarınızı giriniz (0 – 20 ECU):', ) belief = models.FloatField( min=0, label='Bu turdaki gerçek proje geliri sizce kaç ECU?', blank=True, ) belief_correct = models.BooleanField(initial=False) vote = models.StringField( choices=[['incumbent', 'Mevcut Lider'], ['opposition', 'Muhalefet Lideri']], widget=widgets.RadioSelect, label='Bir sonraki dönem için kimi destekliyorsunuz?', blank=True, ) payoff_pre_tax = models.FloatField(initial=0) tax_paid = models.FloatField(initial=0) payoff_this_round = models.FloatField(initial=0) class Instructions(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 @staticmethod def vars_for_template(player: Player): election_rounds_text = ' ve '.join(str(r) for r in C.ELECTION_ROUNDS) refund_loss_percent = int(round((1 - C.REFUND_RATE_FAIL) * 100)) return dict( role=current_role(player), treatment=player.session.config.get('treatment', 'T2'), endowment=C.ENDOWMENT, threshold=C.THRESHOLD, mpcr_min=C.MPCR_MIN, mpcr_max=C.MPCR_MAX, refund_rate=int(C.REFUND_RATE_FAIL * 100), refund_loss_percent=refund_loss_percent, belief_bonus=C.BELIEF_BONUS, belief_tolerance_revenue=C.BELIEF_TOLERANCE_REVENUE, leader_salary=C.LEADER_SALARY, opp_salary=C.OPP_SALARY, initial_treasury=C.INITIAL_TREASURY, treasury_floor=C.TREASURY_FLOOR, election_rounds=C.ELECTION_ROUNDS, election_rounds_text=election_rounds_text, num_rounds=C.NUM_ROUNDS, ) class StartRoundWaitPage(WaitPage): title_text = 'Tur başlıyor…' body_text = 'Lütfen diğer oyuncuların hazır olmasını bekleyin.' @staticmethod def after_all_players_arrive(group: Group): group.draw_mpcr() group.initialize_round_state() class LeaderInfoPage(Page): @staticmethod def is_displayed(player: Player): return current_role(player) in [C.ROLE_LEADER, C.ROLE_OPPOSITION] @staticmethod def vars_for_template(player: Player): g = player.group return dict( role=current_role(player), mpcr=round(g.mpcr, 2), round_number=player.round_number, treasury_start=round(g.treasury_start, 2), deficit_used=g.deficit_used_this_term, actual_revenue=round(C.THRESHOLD * g.mpcr * C.N_VOTERS, 2), max_announcement=g.max_allowed_announcement(), threshold=C.THRESHOLD, treasury_floor=C.TREASURY_FLOOR, leader_salary=C.LEADER_SALARY, ) class LeaderAnnouncementPage(Page): form_model = 'player' form_fields = ['announced_distribution_input'] @staticmethod def is_displayed(player: Player): return current_role(player) == C.ROLE_LEADER @staticmethod def error_message(player: Player, values): a = values.get('announced_distribution_input') or 0 max_a = player.group.max_allowed_announcement() if a > max_a: return f'Bu tur için maksimum duyuru miktarı {max_a} ECU’dur.' @staticmethod def vars_for_template(player: Player): g = player.group return dict( mpcr=round(g.mpcr, 2), round_number=player.round_number, treasury_start=round(g.treasury_start, 2), actual_revenue=round(C.THRESHOLD * g.mpcr * C.N_VOTERS, 2), max_announcement=g.max_allowed_announcement(), deficit_used=g.deficit_used_this_term, leader_salary=C.LEADER_SALARY, threshold=C.THRESHOLD, ) @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened or player.field_maybe_none('announced_distribution_input') is None: player.announced_distribution_input = round( C.THRESHOLD * player.group.mpcr * C.N_VOTERS, 2 ) class CollectAnnouncementWaitPage(WaitPage): title_text = 'Liderin duyurusu bekleniyor…' body_text = 'Lütfen bekleyin.' @staticmethod def after_all_players_arrive(group: Group): leader = group.get_leader() if leader: a = leader.field_maybe_none('announced_distribution_input') group.announced_distribution = round(a, 2) if a is not None else 0.0 class OppositionAnnouncementPage(Page): form_model = 'player' form_fields = ['opp_announced_distribution_input'] # timeout_seconds = 120 @staticmethod def is_displayed(player: Player): return current_role(player) == C.ROLE_OPPOSITION and player.session.config.get('treatment', 'T2') == 'T2' @staticmethod def vars_for_template(player: Player): g = player.group actual_revenue = round(C.THRESHOLD * g.mpcr * C.N_VOTERS, 2) leader_announcement = round(g.announced_distribution, 2) return dict( mpcr=round(g.mpcr, 2), actual_revenue=actual_revenue, round_number=player.round_number, treasury_start=round(g.treasury_start, 2), threshold=C.THRESHOLD, leader_announcement=leader_announcement, leader_announcement_per_voter=round(leader_announcement / C.N_VOTERS, 2), leader_gap=round(actual_revenue - leader_announcement, 2), ) @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened or player.field_maybe_none('opp_announced_distribution_input') is None: player.opp_announced_distribution_input = round( C.THRESHOLD * player.group.mpcr * C.N_VOTERS, 2 ) class CollectOppositionAnnouncementWaitPage(WaitPage): title_text = 'Muhalefetin duyurusu bekleniyor…' body_text = 'Lütfen bekleyin.' @staticmethod def after_all_players_arrive(group: Group): group.collect_opposition_decision() class BeliefElicitationPage(Page): form_model = 'player' form_fields = ['belief'] @staticmethod def is_displayed(player: Player): return current_role(player) == C.ROLE_VOTER @staticmethod def vars_for_template(player: Player): g = player.group treatment = player.session.config.get('treatment', 'T2') show_opp = (treatment == 'T2' and g.opp_announced_distribution != 0) return dict( announced_distribution=round(g.announced_distribution, 2), announced_per_voter=round(g.announced_distribution / C.N_VOTERS, 2), opp_announced_distribution=round(g.opp_announced_distribution, 2), opp_per_voter=round(g.opp_announced_distribution / C.N_VOTERS, 2), show_opposition=show_opp, treasury_start=round(g.treasury_start, 2), threshold=C.THRESHOLD, belief_bonus=C.BELIEF_BONUS, belief_tolerance_revenue=C.BELIEF_TOLERANCE_REVENUE, round_number=player.round_number, num_rounds=C.NUM_ROUNDS, ) class ContributionPage(Page): form_model = 'player' form_fields = ['contribution'] @staticmethod def is_displayed(player: Player): return current_role(player) == C.ROLE_VOTER @staticmethod def vars_for_template(player: Player): g = player.group treatment = player.session.config.get('treatment', 'T2') show_opp = (treatment == 'T2' and g.opp_announced_distribution != 0) return dict( endowment=C.ENDOWMENT, threshold=C.THRESHOLD, announced_distribution=round(g.announced_distribution, 2), announced_per_voter=round(g.announced_distribution / C.N_VOTERS, 2), opp_announced_distribution=round(g.opp_announced_distribution, 2), opp_per_voter=round(g.opp_announced_distribution / C.N_VOTERS, 2), show_opposition=show_opp, refund_rate=int(C.REFUND_RATE_FAIL * 100), round_number=player.round_number, num_rounds=C.NUM_ROUNDS, ) class CheckContributionWaitPage(WaitPage): title_text = 'Katkılar toplanıyor…' body_text = 'Lütfen bekleyin.' @staticmethod def after_all_players_arrive(group: Group): group.check_threshold() class LeaderAllocationPage(Page): form_model = 'player' form_fields = ['gap_to_treasury_input', 'excess_to_treasury_input'] # timeout_seconds = 120 @staticmethod def is_displayed(player: Player): if current_role(player) != C.ROLE_LEADER: return False if not player.group.threshold_met: return False actual_revenue = round(C.THRESHOLD * player.group.mpcr * C.N_VOTERS, 2) positive_gap = max(0, round(actual_revenue - player.group.announced_distribution, 2)) excess = player.group.excess_contribution return positive_gap > 0 or excess > 0 @staticmethod def error_message(player: Player, values): actual_revenue = round(C.THRESHOLD * player.group.mpcr * C.N_VOTERS, 2) positive_gap = max(0, round(actual_revenue - player.group.announced_distribution, 2)) excess = player.group.excess_contribution gap_val = values.get('gap_to_treasury_input') or 0 excess_val = values.get('excess_to_treasury_input') or 0 if gap_val < 0 or gap_val > positive_gap: return f'Gelir farkından hazineye aktarılacak miktar 0 ile {positive_gap} arasında olmalıdır.' if excess_val < 0 or excess_val > excess: return f'Aşım katkıdan hazineye aktarılacak miktar 0 ile {excess} arasında olmalıdır.' @staticmethod def vars_for_template(player: Player): g = player.group actual_revenue = round(C.THRESHOLD * g.mpcr * C.N_VOTERS, 2) positive_gap = max(0, round(actual_revenue - g.announced_distribution, 2)) return dict( threshold=C.THRESHOLD, n_voters=C.N_VOTERS, mpcr=round(g.mpcr, 2), actual_revenue=actual_revenue, announced_distribution=round(g.announced_distribution, 2), announced_per_voter=round(g.announced_distribution / C.N_VOTERS, 2), positive_gap=positive_gap, excess_contribution=round(g.excess_contribution, 2), treasury_start=round(g.treasury_start, 2), ) @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened or player.field_maybe_none('gap_to_treasury_input') is None: player.gap_to_treasury_input = 0 if timeout_happened or player.field_maybe_none('excess_to_treasury_input') is None: player.excess_to_treasury_input = 0 class CalculatePayoffsWaitPage(WaitPage): title_text = 'Sonuçlar hesaplanıyor…' body_text = 'Diğer oyuncular bekleniyor.' @staticmethod def after_all_players_arrive(group: Group): group.collect_leader_decisions() group.calculate_payoffs() class RoundResultsPage(Page): @staticmethod def vars_for_template(player: Player): g = player.group treatment = player.session.config.get('treatment', 'T2') show_opp = (treatment == 'T2' and g.opp_announced_distribution != 0) total_so_far = round(float(sum(p.payoff for p in player.in_all_rounds())), 2) actual_rev = round(g.actual_project_revenue, 2) announced = round(g.announced_distribution, 2) announce_diff = round(announced - actual_rev, 2) positive_gap = round(max(0, actual_rev - announced), 2) opp_lie = round(g.opp_announced_distribution - actual_rev, 2) if treatment == 'T2' else 0 voter_contribution_raw = float(player.field_maybe_none('contribution') or 0) voter_contribution_2dp = f"{voter_contribution_raw:.2f}" refund_loss_amount = round((1 - C.REFUND_RATE_FAIL) * voter_contribution_raw, 2) refund_loss_amount_2dp = f"{refund_loss_amount:.2f}" leader_obj = g.get_leader() debug_gap_input = leader_obj.field_maybe_none('gap_to_treasury_input') if leader_obj else None return dict( role=current_role(player), mpcr=round(g.mpcr, 2), actual_project_revenue=actual_rev, announced_distribution=announced, announced_per_voter=round(announced / C.N_VOTERS, 2), opp_announced_distribution=round(g.opp_announced_distribution, 2), opp_per_voter=round(g.opp_announced_distribution / C.N_VOTERS, 2), opp_lie=opp_lie, show_opposition=show_opp, announce_diff=announce_diff, positive_gap=positive_gap, leader_overannounced=(announce_diff > 0), leader_underannounced=(announce_diff < 0), leader_truthful=(announce_diff == 0), total_contribution=round(g.total_contribution, 2), threshold=C.THRESHOLD, threshold_met=g.threshold_met, excess_contribution=round(g.excess_contribution, 2), gap_to_treasury=round(g.gap_to_treasury, 2), leader_private_gap=round(g.leader_private_gap, 2), excess_to_treasury=round(g.excess_to_treasury, 2), leader_private_excess=round(g.leader_private_excess, 2), leader_total_private_take=round(g.leader_private_gap + g.leader_private_excess, 2), treasury_start=round(g.treasury_start, 2), treasury_pre_tax=round(g.treasury_pre_tax, 2), tax_per_voter=round(g.tax_per_voter, 2), treasury_end=round(g.treasury_end, 2), deficit_used=g.deficit_used_this_term, payoff_this_round=round(float(player.payoff_this_round), 2), payoff_pre_tax=round(float(player.payoff_pre_tax), 2) if current_role(player) == C.ROLE_VOTER else None, tax_paid=round(float(player.tax_paid), 2) if current_role(player) == C.ROLE_VOTER else None, voter_contribution=voter_contribution_raw, voter_contribution_2dp=voter_contribution_2dp, refund_loss_amount=refund_loss_amount, refund_loss_amount_2dp=refund_loss_amount_2dp, belief=round(float(player.field_maybe_none('belief') or 0), 2), belief_correct=player.belief_correct if current_role(player) == C.ROLE_VOTER else False, belief_bonus=C.BELIEF_BONUS, total_so_far=total_so_far, endowment=C.ENDOWMENT, leader_salary=C.LEADER_SALARY, opp_salary=C.OPP_SALARY, is_election_round=(player.round_number in C.ELECTION_ROUNDS), round_number=player.round_number, num_rounds=C.NUM_ROUNDS, debug_gap_input=debug_gap_input, debug_revenue_gap=round(g.revenue_gap, 2), debug_gap_to_treasury=round(g.gap_to_treasury, 2), debug_leader_private_gap=round(g.leader_private_gap, 2), ) class ElectionPage(Page): form_model = 'player' form_fields = ['vote'] #timeout_seconds = 120 @staticmethod def is_displayed(player: Player): return ( current_role(player) == C.ROLE_VOTER and player.round_number in C.ELECTION_ROUNDS ) @staticmethod def vars_for_template(player: Player): history = [] treatment = player.session.config.get('treatment', 'T2') for r in range(max(1, player.round_number - 3), player.round_number + 1): g = player.group.in_round(r) diff = round(g.announced_distribution - g.actual_project_revenue, 2) opp_diff = round(g.opp_announced_distribution - g.actual_project_revenue, 2) history.append(dict( round=r, mpcr=round(g.mpcr, 2), actual_revenue=round(g.actual_project_revenue, 2), announced=round(g.announced_distribution, 2), opp_announced=round(g.opp_announced_distribution, 2), diff=diff, overannounced=(diff > 0), underannounced=(diff < 0), truthful=(diff == 0), opp_diff=opp_diff, opp_overannounced=(opp_diff > 0), opp_underannounced=(opp_diff < 0), opp_truthful=(opp_diff == 0), threshold_met=g.threshold_met, treasury_end=round(g.treasury_end, 2), tax_per_voter=round(g.tax_per_voter, 2), )) return dict( round_number=player.round_number, history=history, treatment=treatment, ) @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened or player.field_maybe_none('vote') is None: player.vote = 'incumbent' class ElectionWaitPage(WaitPage): title_text = 'Seçim sonuçları bekleniyor…' body_text = 'Lütfen bekleyin.' @staticmethod def after_all_players_arrive(group: Group): if group.round_number in C.ELECTION_ROUNDS: group.conduct_election() class ElectionResultsPage(Page): @staticmethod def is_displayed(player: Player): return player.round_number in C.ELECTION_ROUNDS @staticmethod def vars_for_template(player: Player): g = player.group total_votes = g.votes_incumbent + g.votes_opposition # role before election = role in previous round if player.round_number > 1: old_role = player.in_round(player.round_number - 1).role_in_game else: old_role = player.role_in_game # role after election = updated participant role new_role = player.participant.vars.get('role', player.role_in_game) return dict( role=old_role, old_role=old_role, new_role=new_role, votes_incumbent=g.votes_incumbent, votes_opposition=g.votes_opposition, election_winner=g.election_winner, role_changed=(old_role != new_role), round_number=player.round_number, pct_incumbent=round(g.votes_incumbent / max(total_votes, 1) * 100), pct_opposition=round(g.votes_opposition / max(total_votes, 1) * 100), treasury_end=round(g.treasury_end, 2), ) class FinalResultsPage(Page): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS @staticmethod def vars_for_template(player: Player): total_ecus = round(float(sum(p.payoff for p in player.in_all_rounds())), 2) return dict( total_ecus=total_ecus, real_money=round(total_ecus * player.session.config['real_world_currency_per_point'], 2), role=current_role(player), final_treasury=round(player.group.treasury_end, 2), ) page_sequence = [ Instructions, StartRoundWaitPage, LeaderInfoPage, LeaderAnnouncementPage, CollectAnnouncementWaitPage, OppositionAnnouncementPage, CollectOppositionAnnouncementWaitPage, BeliefElicitationPage, ContributionPage, CheckContributionWaitPage, LeaderAllocationPage, CalculatePayoffsWaitPage, RoundResultsPage, ElectionPage, ElectionWaitPage, ElectionResultsPage, FinalResultsPage, ]