from __future__ import division import random from otree.api import * from .models import C, Player, GAME_PARAMS WIN_PROB = 0.40 # 40% chance to win class a_Instructions(Page): def is_displayed(self): return self.subsession.round_number == 1 class b1_Pre_Decision(Page): form_model = 'player' form_fields = ['play_round'] def is_displayed(self): player = self.player return self.participant.vars[C.COIN_BALANCE_MAIN] > 0 and player.participant.vars.get('play_next_round_real', True) def before_next_page(self): player = self.player if not player.play_round: player.participant.vars['play_next_round_real'] = False def vars_for_template(self): index = (self.round_number - 1) % len(GAME_PARAMS) params = GAME_PARAMS[index] return dict( multiplier=params["multiplier"], balance=self.participant.vars[C.COIN_BALANCE_MAIN], win_probability=int(self.player.win_probability * 100), round_payoff=self.player.field_maybe_none('round_payoff') ) class b2_Sure_Pre_Decision(Page): form_model = 'player' form_fields = ['play_round_sure'] def vars_for_template(self): return dict( balance=self.participant.vars[C.COIN_BALANCE_MAIN], multiplier=self.player.multiplier, ) def is_displayed(self): player = self.player return not player.participant.vars.get('play_next_round_real', True) def before_next_page(self): player = self.player if not player.play_round_sure: player.participant.vars['play_next_round_real'] = True class b3_Decision(Page): form_model = 'player' form_fields = ['bet_amount', 'choice'] def is_displayed(self): player = self.player return self.participant.vars[C.COIN_BALANCE_MAIN] > 0 and player.participant.vars.get('play_next_round_real', True) def vars_for_template(self): return dict( balance=self.participant.vars[C.COIN_BALANCE_MAIN], multiplier=self.player.multiplier, win_probability=int(self.player.win_probability * 100), round_payoff=self.player.field_maybe_none('round_payoff') ) def field_required_message(self, field_name): if field_name == 'choice': return 'Please choose Heads or Tails.' if field_name == 'bet_amount': return 'Please select the amount you want to bet.' def error_message(self, values): balance = self.participant.vars.get(C.COIN_BALANCE_MAIN, 0) if not values.get('bet_amount'): return 'Please select the amount you want to bet.' if values['bet_amount'] <= 0: return 'The bet amount cannot be negative.' if values['bet_amount'] > balance: return 'You cannot bet more than your available balance.' if not values.get('choice'): return 'Please choose Heads or Tails.' class c_Results(Page): def is_displayed(self): player = self.player return self.participant.vars[C.COIN_BALANCE_MAIN] > 0 and player.participant.vars.get('play_next_round_real', True) def vars_for_template(self): p = self.player if p.field_maybe_none('coin_result') is None: balance = p.participant.vars.get(C.COIN_BALANCE_MAIN, C.INITIAL_BALANCE) multiplier = p.multiplier win_prob = p.win_probability if p.round_number <= 2: win_prob = 0.55 if random.random() < win_prob: p.round_win = True p.coin_result = p.choice p.round_payoff = p.bet_amount * multiplier else: p.round_win = False p.coin_result = 'T' if p.choice == 'H' else 'H' p.round_payoff = -p.bet_amount balance += p.round_payoff p.participant.vars[C.COIN_BALANCE_MAIN] = balance return dict( coin=p.coin_result, win=p.round_win, balance=p.participant.vars[C.COIN_BALANCE_MAIN], multiplier=p.multiplier, round_payoff=p.round_payoff ) class d_Exit(Page): def is_displayed(self): return (not self.player.participant.vars.get('play_next_round_real', True)) or self.participant.vars[C.COIN_BALANCE_MAIN] <= 0 def vars_for_template(self): final_balance = self.participant.vars[C.COIN_BALANCE_MAIN] self.player.final_balance = final_balance return dict( final_balance=final_balance ) class f_belief_elicit(Page): form_model = 'player' form_fields = ['belief_elicit_1', 'belief_elicit_2'] def is_displayed(self): return (not self.player.participant.vars.get('play_next_round_real', True)) or self.participant.vars[C.COIN_BALANCE_MAIN] <= 0 def app_after_this_page(self, upcoming_apps): return upcoming_apps[0] page_sequence = [ a_Instructions, b1_Pre_Decision, b2_Sure_Pre_Decision, b3_Decision, c_Results, d_Exit, f_belief_elicit, ]