from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class IntroPage(Page): def is_displayed(self): return self.round_number == 1 def before_next_page(self): if self.round_number == 1: self.participant.vars['contribution_payout'] = 0 class DecisionPage(Page): form_model = 'player' def get_form_fields(self): if self.player.contr_period > 0: return ['contr_period', 'sub_period', 'treatment'] else: return ['contribute', 'contr_period', 'sub_period', 'treatment'] def is_displayed(self): if self.player.sub_period > 0 and self.player.treatment == 1: return False else: return True def before_next_page(self): self.player.sub_period += 1 def vars_for_template(self): if self.round_number != 1: return { 'player_period': self.player.sub_period + 1, 'treatment_num': self.subsession.treatment, 'player_in_previous_rounds': reversed(self.player.in_rounds(self.subsession.treatment_round , (self.round_number-1))), 'others_in_group': self.player.get_others_in_group(), 'player_in_previous_round': self.player.in_round(self.round_number-1) } else: return { 'player_period': self.player.sub_period + 1, 'treatment_num': self.subsession.treatment, 'player_in_previous_rounds': self.player.in_round(self.round_number), 'others_in_group': self.player.get_others_in_group(), 'player_in_previous_round': self.player.in_round(self.round_number) } class DecisionWait(WaitPage): def is_displayed(self): return self.subsession.treatment == 2 def after_all_players_arrive(self): contribution = 0 players = self.group.get_players() for p in players: if p.contr_period != 0: p.contribute = 1 contribution += p.contribute if p.contribute == 1 and p.contr_period == 0: p.contr_period = p.sub_period elif p.contribute == 0: p.contribute = None self.group.num_contr = contribution class ResultsWaitPage(WaitPage): def after_all_players_arrive(self): contribution = 0 players = self.group.get_players() for p in players: if p.contribute == 1: p.voting_decision = 'Group Project' else: p.voting_decision = 'Private Project' player_actions = { } for p in players: player_actions[p.id_in_group] = p.voting_decision for p in players: other_actions = [] for a in player_actions: if a != p.id_in_group: other_actions.append(player_actions[a]) else: pass p.other1_act = other_actions[0] p.other2_act = other_actions[1] for p in players: if p.contribute is None: p.contribute = 0 contribution += p.contribute if p.contribute == 1 and p.contr_period == 0: p.contr_period = p.sub_period self.group.num_contr = contribution self.group.get_pub_good_value() for p in players: if self.group.num_contr >= 2 and p.contribute == 1: if self.round_number == self.session.vars['paying_round']: p.payoff = self.group.pub_good_value p.project = 'Group Project' p.round_payoff = self.group.pub_good_value else: if self.round_number == self.session.vars['paying_round']: p.payoff = p.priv_good_value p.project = 'Private Project' p.round_payoff = p.priv_good_value player_contr = { } for p in players: player_contr[p.id_in_group] = p.contr_period for p in players: other_actions = [] for a in player_actions: if a != p.id_in_group: other_actions.append(player_contr[a]) else: pass p.other1_cont = other_actions[0] p.other2_cont = other_actions[1] self.group.get_round_contribution() class Results(Page): form_model = 'player' form_fields = ['treatment'] def before_next_page(self): if self.round_number == self.session.vars['paying_round']: self.participant.vars['contribution_payout'] += self.player.payoff def vars_for_template(self): if self.round_number == 1: return { 'treatment_num': self.subsession.treatment, 'player_in_previous_rounds': self.player.in_round(self.round_number), 'others_in_group': self.player.get_others_in_group(), 'player_in_previous_round': self.player.in_round(self.round_number) } else: return { 'treatment_num': self.subsession.treatment, 'player_in_previous_rounds': reversed(self.player.in_rounds(self.subsession.treatment_round , (self.round_number-1))), 'others_in_group': self.player.get_others_in_group(), 'player_in_previous_round': self.player.in_round(self.round_number-1) } class NewBlock(Page): def is_displayed(self): if self.round_number == 1: return False elif self.player.treatment != self.player.in_round(self.round_number-1).treatment: return True else: return False class SummaryPage(Page): def is_displayed(self): return False def vars_for_template(self): return { 'total_dollars': self.participant.payoff_plus_participation_fee(), } page_sequence = [ IntroPage, NewBlock, DecisionPage, DecisionWait, DecisionPage, DecisionWait, DecisionPage, DecisionWait, ResultsWaitPage, Results, SummaryPage ]