from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random class Contribution(Page): form_model = 'player' form_fields = ['contribution'] class ResultsWaitPage(WaitPage): def after_all_players_arrive(self): # Every round - update pot etc self.group.total_contribution = 0 for player in self.group.get_players(): player.participant.payoff -= player.contribution player.session.vars["total_pot"] += player.contribution self.group.total_contribution += player.contribution # Final round if self.round_number == Constants.num_rounds: if self.session.vars["total_pot"] >= Constants.group_target: print("They met the target!") self.session.vars.update(target_met=True) self.session.vars.update(success=True) else: print("They missed the target!") self.session.vars.update(target_met=False) if random.random()<0.5: print("They got lucky!") self.session.vars.update(success=True) else: print("They got unlucky and ruined!") self.session.vars.update(success=False) for participant in self.session.get_participants(): participant.payoff = c(0) class Results(Page): def vars_for_template(self): list_of_contributions = [self.player.contribution] for player in self.player.get_others_in_group(): list_of_contributions.append(player.contribution) return { "Contributions": list_of_contributions } class GroupResult(Page): def is_displayed(self): return self.round_number == Constants.num_rounds class RuinResult(Page): def is_displayed(self): if self.round_number == Constants.num_rounds: return self.session.vars["target_met"] == False # Need to add FailLucky / FailUnlucky in here page_sequence = [Contribution, ResultsWaitPage, Results, GroupResult, RuinResult]