from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random class Qual_Player1(Page): timeout_seconds = 120 form_model = 'player' form_fields = ['vh', 'vl_guess_1', 'ph_guess_1', 'pl_guess_1'] def before_next_page(self): player = self.player player.vl = random.choice([3.5, 4, 4.5, 5, 5.5]) player.ph_guess_2 = random.choice([598, 913, 1037, 1155, 2589]) player.pl_guess_2 = random.choice([54, 80, 101, 128, 129]) if self.timeout_happened: self.player.vh = random.choice([19, 23.5, 24.5, 25, 37]) self.player.vl_guess_1 = random.choice([3.5, 4, 4.5, 5, 5.5]) self.player.ph_guess_1 = random.choice([598, 913, 1037, 1155, 2589]) self.player.pl_guess_1 = random.choice([54, 80, 101, 128, 129]) # class WaitForP1(WaitPage): # pass # # class Qual_Player2(Page): # # #timeout_seconds = 360 # # form_model = 'group' # form_fields = ['vl', 'ph_guess_2', 'pl_guess_2'] # # # def is_displayed(self): # return self.player.id_in_group == 2 # # # class WaitforP2(WaitPage): # pass class Price(Page): timeout_seconds = 120 form_model = 'player' form_fields = ['ph', 'pl_guess_3'] def before_next_page(self): if self.timeout_happened: self.player.ph = random.choice([598, 913, 1037, 1155, 2589]) self.player.pl_guess_3 = random.choice([54, 80, 101, 128, 129]) class ResultsWaitPage(WaitPage): pass # def after_all_players_arrive(self): # pass class Results(Page): def vars_for_template(self): play = self.player # These are the demand formulas Dh = (100 - (play.ph-play.pl_guess_2)/(Constants.kh*play.vh - Constants.kl*play.vl))/(100 - 0) Dl = ((play.ph-play.pl_guess_2)/(Constants.kh*play.vh - Constants.kl*play.vl) - (play.pl_guess_2)/(Constants.kl*play.vl))/(100-0) if Dh > 1: # Overwrite Dh Dh=1 elif Dh<0: # Overwrite Dh Dh=0 else: Dh=Dh if Dl>1: # Overwrite Dh Dl=1 elif Dl<0: # Overwrite Dh Dl=0 else: Dl=Dl print("#### Calculation display ####") print(Dh) print(Dl) print(play.ph) print((play.vh ** Constants.alpha) / Constants.alpha) if play.ph * Dh - (play.vh ** Constants.alpha) / Constants.alpha > 0.0: payoff_h = (play.ph * Dh - (play.vh ** Constants.alpha) / Constants.alpha) else: payoff_h = (play.ph * Dh - (play.vh ** Constants.alpha) / Constants.alpha) if play.pl_guess_2 * Dl - (play.vl ** Constants.alpha) / Constants.alpha > 0: payoff_l = Constants.r * (play.pl_guess_2 * Dl - (play.vl ** Constants.alpha) / Constants.alpha) else: payoff_l = Constants.r * (play.pl_guess_2 * Dl - (play.vl ** Constants.alpha) / Constants.alpha) print(payoff_h) print(payoff_l) return dict( payoff_high = round(payoff_h,2), payoff_low = round(payoff_l,2), ) page_sequence = [ Qual_Player1, Price, # ResultsWaitPage, Results, ]