from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random class BeginWaitPage(WaitPage): def after_all_players_arrive(self): self.subsession.dice = random.randint(1, 3) print('this round the dice is', self.subsession.dice) class Send(Page): form_model = 'group' form_fields = ['sent_amount'] def is_displayed(self): return self.player.id_in_group == 1 class SendBack(Page): form_model = 'group' form_fields = ['sent_back_amount'] def is_displayed(self): return self.player.id_in_group == 2 def vars_for_template(self): return { 'tripled_amount': self.group.sent_amount * Constants.multiplication_factor } def sent_back_amount_choices(self): return currency_range(c(0), self.group.sent_amount * Constants.multiplication_factor, c(1)) class WaitForP1(WaitPage): pass class Send2(Page): form_model = 'group' form_fields = ['sent_amount2'] def is_displayed(self): return self.player.id_in_group == 2 class SendBack2(Page): form_model = 'group' form_fields = ['sent_back_amount2'] def is_displayed(self): return self.player.id_in_group == 1 def vars_for_template(self): return { 'tripled_amount': self.group.sent_amount2 * Constants.multiplication_factor } def sent_back_amount2_choices(self): return currency_range(c(0), self.group.sent_amount2 * Constants.multiplication_factor, c(1)) def before_next_page(self): self.participant.vars['foo'] = 1 class WaitForP2(WaitPage): pass class DiceWaitPage(WaitPage): pass class Dice(Page): def vars_for_template(self): return {'dice': self.subsession.dice} class ResultsWaitPage(WaitPage): def is_displayed(self): if self.subsession.dice == 3: return True else: return False def after_all_players_arrive(self): self.group.final_round = self.subsession.round_number group = self.group p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p1.payoff = Constants.endowment - group.sent_amount + group.sent_back_amount p2.payoff = group.sent_amount * Constants.multiplication_factor - group.sent_back_amount class Results(Page): def is_displayed(self): if self.subsession.dice == 3: return True else: return False page_sequence = [ BeginWaitPage, Send, WaitForP1, SendBack, Send2, WaitForP2, SendBack2, DiceWaitPage, Dice, ResultsWaitPage, Results, ]