from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class Introduction(Page): def is_displayed(self): return self.round_number == 1 class MyPage(Page): form_model = 'player' form_fields = ['quantity', ] def before_next_page(self): p = self.player #p.expected_cost = round(Constants.overage * p.quantity* norm.cdf(p.quantity, loc=1000, scale=100) - Constants.overage * norm.expect(loc=1000, scale=100, lb=None,ub=p.quantity)\ # + Constants.underage*norm.expect(loc=1000,scale=100, lb=p.quantity, ub=None)-Constants.underage*p.quantity*(1-norm.cdf(p.quantity, loc=1000,scale=100))) p.demand = Constants.round_demand[p.round_number - 1] if p.quantity > p.demand: p.mismatch_cost = Constants.overage * (p.quantity - p.demand) else: p.mismatch_cost = Constants.underage * (p.demand - p.quantity) p.payoff = c(0.135 - p.mismatch_cost * 0.00004) def vars_for_template(self): y1 = Constants.determined_demand[:] y2 = Constants.determined_demand[:] players = self.player.in_previous_rounds() new_round_num_minus = self.player.new_round_number - 1 for p in players: y1.append(p.demand) for p in players: y2.append(p.demand) round_operator = self.round_number while round_operator <= 20: round_operator = round_operator + 1 y1.append(None) players.reverse() y2.reverse() return { 'highchart_series1': y1, 'player_in_previous_rounds': players, 'demand_series': y2, 'new_round_num_minus': new_round_num_minus } def error_message(self, values): print('', values) if values['quantity'] < 0: return 'Please enter order quantity with positive integer.' if values['quantity'] >= 4000: return 'You ordered too many quantities. Please enter order quantity less than 4000.' class Results(Page): def vars_for_template(self): payoff_up = 0 players = self.player.in_all_rounds() for p in players: payoff_up = payoff_up + p.payoff return { 'payoff_up': payoff_up } class EndPage(Page): def is_displayed(self): return self.round_number == Constants.num_rounds def vars_for_template(self): fp = self.participant.payoff_plus_participation_fee() return { 'fp': fp, } page_sequence = [ Introduction, MyPage, Results, EndPage ]