from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random class Qual(Page): timeout_seconds = 80 form_model = 'player' form_fields = ['vh', 'vl', 'ph_guess_1', 'pl_guess_1'] def before_next_page(self): if self.timeout_happened: self.player.vh = random.uniform(Constants.min_quality_high,Constants.max_quality_high) self.player.vl = random.uniform(Constants.min_quality_low, Constants.max_quality_low) class WaitForPlayer(WaitPage): pass class Price(Page): timeout_seconds = 80 form_model = 'player' form_fields = ['ph', 'pl'] def before_next_page(self): if self.timeout_happened: self.player.ph = round(random.uniform(Constants.min_price_high,Constants.max_price_high),2) self.player.pl = round(random.uniform(Constants.min_price_low,Constants.max_price_low),2) class ResultsWaitPage(WaitPage): def after_all_players_arrive(self): self.group.set_payoffs() class Results(Page): def vars_for_template(self): play_high = self.group.get_player_by_role("Leader") play_low = self.group.get_player_by_role("Follower") # These are the demand formulas Dh = (100 - (play_high.ph-play_low.pl)/(Constants.kh*play_high.vh - Constants.kl*play_low.vl))/(100 - 0) Dl = ((play_high.ph-play_low.pl)/(Constants.kh*play_high.vh - Constants.kl*play_low.group.vl) - (play_low.pl)/(Constants.kl*play_low.group.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) payoff_h = (play_high.ph * Dh - (self.group.vh ** Constants.alpha) / Constants.alpha) payoff_l = Constants.r * (play_low.pl * Dl - (self.group.vl ** Constants.alpha) / Constants.alpha) # if play_high.ph * Dh - (play_high.vh ** Constants.alpha) / Constants.alpha > 0.0: # payoff_h = (play_high.ph * Dh - (play_high.vh ** Constants.alpha) / Constants.alpha) # else: # payoff_h = 0 # if play_low.pl * Dl - (play_low.vl ** Constants.alpha) / Constants.alpha > 0: # payoff_l = Constants.r * (play_low.pl * Dl - (play_low.vl ** Constants.alpha) / Constants.alpha) # else: # payoff_l = 0 print(payoff_h) print(payoff_l) return dict( payoff_high = round(payoff_h,2), payoff_low = round(payoff_l,2), qual_player1 = play_high.vh, qual_player2 = play_low.vl, price_player1 = play_high.ph, price_player2=play_low.pl, ) page_sequence = [ Qual, WaitForPlayer, Price, ResultsWaitPage, Results, ]