from __future__ import division import random from otree.api import * from .models import C, Player, GAME_PARAMS WIN_PROB = 0.40 def get_language(player): return player.session.config.get('language', 'en') == 'en' class a_Instructions(Page): def is_displayed(player): return player.round_number == 1 def vars_for_template(player): return dict( en=get_language(player), ) class b1_Pre_Decision(Page): form_model = 'player' form_fields = ['play_round'] def is_displayed(self): return ( self.player.participant.vars[C.COIN_BALANCE_MAIN] > 0 and self.player.participant.vars.get( 'play_next_round_real', True ) ) def before_next_page(self): if not self.player.play_round: self.player.participant.vars[ 'play_next_round_real' ] = False def vars_for_template(self): index = (self.player.round_number - 1) % len(GAME_PARAMS) params = GAME_PARAMS[index] return dict( multiplier=params["multiplier"], balance=self.player.participant.vars[ C.COIN_BALANCE_MAIN ], win_probability=int( self.player.win_probability * 100 ), round_payoff=self.player.field_maybe_none( 'round_payoff' ), en=get_language(self.player), ) class b2_Sure_Pre_Decision(Page): form_model = 'player' form_fields = ['play_round_sure'] def is_displayed(self): return not self.player.participant.vars.get( 'play_next_round_real', True ) def before_next_page(self): if not self.player.play_round_sure: self.player.participant.vars['play_next_round_real'] = True def vars_for_template(self): return dict( balance=self.player.participant.vars[C.COIN_BALANCE_MAIN], multiplier=self.player.multiplier, en=get_language(self.player), ) class b3_Decision(Page): form_model = 'player' form_fields = ['bet_amount', 'choice'] def is_displayed(self): return ( self.player.participant.vars[C.COIN_BALANCE_MAIN] > 0 and self.player.participant.vars.get('play_next_round_real', True) ) def vars_for_template(self): return dict( balance=self.player.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'), en=get_language(self), ) def field_required_message(self, field_name): en = get_language(self) if field_name == 'choice': return ( 'Please choose Heads or Tails.' if en else 'Pilihlah Kepala atau Ekor koin.' ) if field_name == 'bet_amount': return ( 'Please select the amount you want to bet.' if en else 'Pilihlah nominal pertaruhan yang Anda inginkan.' ) def error_message(self, values): en = get_language(self) balance = self.player.participant.vars.get( C.COIN_BALANCE_MAIN, 0 ) if not values.get('bet_amount'): return ( 'Please select the amount you want to bet.' if en else 'Pilihlah nominal pertaruhan yang Anda inginkan.' ) if values['bet_amount'] <= 0: return ( 'The bet amount must be greater than zero.' if en else 'Jumlah taruhan harus lebih besar dari nol.' ) if values['bet_amount'] > balance: return ( 'You cannot bet more than your current balance.' if en else 'Anda tidak dapat bertaruh lebih dari saldo yang Anda miliki.' ) if not values.get('choice'): return ( 'Please choose Heads or Tails.' if en else 'Pilihlah Kepala atau Ekor koin.' ) class c_Results(Page): def is_displayed(self): return ( self.player.participant.vars[C.COIN_BALANCE_MAIN] > 0 and self.player.participant.vars.get( 'play_next_round_real', True ) ) def vars_for_template(self): if self.player.field_maybe_none('coin_result') is None: balance = self.player.participant.vars.get( C.COIN_BALANCE_MAIN, C.INITIAL_BALANCE ) multiplier = self.player.multiplier win_prob = self.player.win_probability # More favorable odds in first 2 rounds if self.player.round_number <= 2: win_prob = 0.55 if random.random() < win_prob: self.player.round_win = True self.player.coin_result = self.player.choice self.player.round_payoff = cu( round( float(self.player.bet_amount) * multiplier ) ) else: self.player.round_win = False self.player.coin_result = ( 'T' if self.player.choice == 'H' else 'H' ) self.player.round_payoff = -self.player.bet_amount balance += self.player.round_payoff self.player.participant.vars[ C.COIN_BALANCE_MAIN ] = balance return dict( coin=self.player.coin_result, win=self.player.round_win, balance=self.player.participant.vars[ C.COIN_BALANCE_MAIN ], multiplier=self.player.multiplier, round_payoff=self.player.round_payoff, en=get_language(self.player), ) class d_Exit(Page): def is_displayed(self): return ( not self.player.participant.vars.get( 'play_next_round_real', True ) ) or ( self.player.participant.vars[ C.COIN_BALANCE_MAIN ] <= 0 ) def vars_for_template(self): final_balance = self.player.participant.vars[ C.COIN_BALANCE_MAIN ] self.player.final_balance = final_balance return dict( final_balance=final_balance, en=get_language(self.player), ) 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.player.participant.vars[ C.COIN_BALANCE_MAIN ] <= 0 ) def vars_for_template(self): return dict( en=get_language(self), ) 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, ]