from ._builtin import Page, WaitPage from otree.api import Currency as c, currency_range from .models import Constants import random class Introduction(Page): """Description of the game: How to play and returns expected""" pass class Contribute(Page): """Player: Choose how much to contribute""" form_model = 'player' form_fields = ['contribution'] class ContributeWaitPage(WaitPage): def after_all_players_arrive(self): group = self.group group.roll_dice= random.randint(1, 6) if group.roll_dice < 3: group.factor=0 else: group.factor=3 body_text = "Waiting for other participants to contribute." class Dice(Page): """Player 2 rolls dice""" def is_displayed(self): return self.player.id_in_group == 2 class BeliefWP(WaitPage): pass class Beliefs(Page): form_model = 'group' form_fields = ['belief_roll'] def is_displayed(self): return self.player.id_in_group == 1 class DiceWP (WaitPage): template_name = 'public_goods/DiceWP.html' class DiceResults(Page): def vars_for_template(self): return { 'image_path': 'public_goods/{}.png'.format(self.group.roll_dice) } """Players see what the dice got""" class ResultsWaitPage (WaitPage): def after_all_players_arrive(self): group = self.group if group.belief_roll == 'Good Luck': group.belief = 3 else: group.belief = 0 players = group.get_players() if group.belief != group.factor: group.extra_payment = 0 elif group.belief == 3: group.extra_payment = 1 else: group.extra_payment = 2 contributions = [p.contribution for p in players] group.min_contribution = min(contributions) group.max_contribution = max(contributions) if group.max_contribution == group.min_contribution: group.total_contribution = sum(contributions) else: group.total_contribution = group.min_contribution*2 group.individual_share = ((group.total_contribution * Constants.multiplier)* self.group.factor)/ Constants.players_per_group for p in players: p.payoff = Constants.endowment - group.min_contribution + group.individual_share for p in players: p.sent_back= p.contribution - group.min_contribution class Results(Page): """Players payoff: How much each has earned""" def vars_for_template(self): return { 'total_earnings': self.group.total_contribution * Constants.multiplier* self.group.factor, } page_sequence = [ Introduction, Contribute, ContributeWaitPage, Beliefs, BeliefWP, Dice, DiceWP, DiceResults, ResultsWaitPage, Results ]