from . import models from ._builtin import Page, WaitPage from otree.api import Currency as c, currency_range from .models import Constants import random class Introduction(Page): timeout_seconds = 100 def is_displayed(self): return (self.round_number == 1) class Decision(Page): form_model = 'player' form_fields = ['decision'] def is_displayed(self): return not self.participant.vars.get('finished_1') class ResultsWaitPage(WaitPage): def is_displayed(self): return not self.participant.vars.get('finished_1') def after_all_players_arrive(self): group = self.group group.is_last_round = (random.random() < Constants.stopping_probability) if group.is_last_round and self.round_number > 1: for player in group.get_players(): player.participant.vars['finished_1'] = True player.participant.vars['last_round_1'] = self.round_number for p in self.group.get_players(): p.set_payoff() class Results(Page): def is_displayed(self): return not self.participant.vars.get('finished_1') def vars_for_template(self): player_in_all_rounds = self.player.in_all_rounds() return { 'my_decision': self.player.decision.lower(), 'other_player_decision': self.player.other_player().decision.lower(), 'same_choice': self.player.decision == self.player.other_player().decision, } class End(Page): def is_displayed(self): return (self.round_number == Constants.num_rounds) def vars_for_template(self): player_in_all_rounds = self.player.in_rounds(1, self.participant.vars['last_round_1']) decisions = [p.decision for p in player_in_all_rounds] other_decisions = [p.other_player().decision for p in player_in_all_rounds] my_payoffs = [p.payoff for p in player_in_all_rounds] previous_player = self.player.in_all_rounds() total = sum([p.payoff for p in player_in_all_rounds]) self.participant.vars['total_1'] = total return { 'my_decisions': decisions, 'other_player': other_decisions, 'my_payoffs': my_payoffs, 'total_1': total, 'previous_player': player_in_all_rounds, } page_sequence = [ Introduction, Decision, ResultsWaitPage, Results, End, ]