from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import datetime class Instruction1(Page): def is_displayed(self): return self.round_number == 1 class Instruction2(Page): def is_displayed(self): return self.round_number == 1 class Simpletest1(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' form_fields = ['q1', 'q2', 'q3'] class Simpletest2(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' form_fields = ['q41', 'q42', 'q43', 'q44'] class Experiment(Page): form_model = 'player' form_fields = ['allocation1', 'allocation2'] K = Constants.K def vars_for_template(self): y1 = Constants.historical_demand_retailer1[:] x1 = Constants.historical_demand_retailer1[:] y2 = Constants.historical_demand_retailer2[:] x2 = Constants.historical_demand_retailer2[:] players = self.player.in_previous_rounds() new_round_num_minus = self.player.new_round_number - 1 if self.player.round_number == 1: last_demand1 = Constants.historical_demand_retailer1[29] last_demand2 = Constants.historical_demand_retailer2[29] else: last_demand1 = Constants.realized_demand_retailer1[self.player.round_number - 2] last_demand2 = Constants.realized_demand_retailer2[self.player.round_number - 2] for p in players: y1.append(p.demand1) y2.append(p.demand2) round_operator = self.round_number while round_operator <= 15: round_operator = round_operator + 1 y1.append(None) y2.append(None) x1.reverse() players.reverse() new_round_number = self.player.round_number + 29 next_period = new_round_number + 1 return { 'highchart_series1': y1, 'highchart_series2': y2, 'player_in_previous_rounds': players, 'demand_series1': x1, 'demand_series2': x2, 'period': new_round_number, 'new_round_num_minus': new_round_num_minus, 'next_period': next_period, 'last_demand1': last_demand1, 'last_demand2': last_demand2, } def before_next_page(self): self.player.demand1 = Constants.realized_demand_retailer1[self.player.round_number - 1] self.player.demand2 = Constants.realized_demand_retailer2[self.player.round_number - 1] if self.player.allocation1 >= self.player.demand1: self.player.payoff1 = Constants.unit_profit_retailer1 * self.player.demand1 else: self.player.payoff1 = Constants.unit_profit_retailer1 * self.player.allocation1 if self.player.allocation2 >= self.player.demand2: self.player.payoff2 = Constants.unit_profit_retailer2 * self.player.demand2 else: self.player.payoff2 = Constants.unit_profit_retailer2 * self.player.allocation2 self.player.profit = self.player.payoff1 + self.player.payoff2 self.player.total_allocation = self.player.allocation1 + self.player.allocation2 def error_message(self, values): print('', values) if values["allocation1"] + values["allocation2"] != Constants.K: return 'The sum of allocation to the retail stores should be equal to your total inventory' + "=" + str( Constants.K) + "." elif values["allocation1"] > Constants.K: return 'The sum of the allocation exceeds the Total inventory level. You can allocate only up to your limited inventory' + "=" + str( Constants.K) + "." elif values["allocation2"] > Constants.K: return 'The sum of the allocation exceeds the Total inventory level. You can allocate only up to your limited inventory' + "=" + str( Constants.K) + "." class Results(Page): def vars_for_template(self): allocation1 = self.player.allocation1 allocation2 = self.player.allocation2 total_allocation = self.player.total_allocation demand1 = self.player.demand1 demand2 = self.player.demand2 total_demand = self.player.demand1 + self.player.demand2 payoff1 = self.player.payoff1 payoff2 = self.player.payoff2 total_payoff = self.player.payoff1 + self.player.payoff2 K = Constants.K payoff_up = 0 players = self.player.in_all_rounds() for p in players: payoff_up = payoff_up + p.profit y1 = Constants.historical_demand_retailer1[:] x1 = Constants.historical_demand_retailer1[:] y2 = Constants.historical_demand_retailer2[:] x2 = Constants.historical_demand_retailer2[:] players = self.player.in_previous_rounds() new_round_num_minus = self.player.new_round_number - 1 if self.player.round_number == 1: last_demand1 = Constants.historical_demand_retailer1[29] last_demand2 = Constants.historical_demand_retailer2[29] else: last_demand1 = Constants.realized_demand_retailer1[self.player.round_number - 2] last_demand2 = Constants.realized_demand_retailer2[self.player.round_number - 2] for p in players: y1.append(p.demand1) y2.append(p.demand2) round_operator = self.round_number while round_operator <= 15: round_operator = round_operator + 1 y1.append(None) y2.append(None) x1.reverse() players.reverse() new_round_number = self.player.round_number + 29 next_period = new_round_number + 1 return { 'payoff_up': payoff_up, 'allocation1': allocation1, 'allocation2': allocation2, 'period': new_round_number, 'demand1': demand1, 'demand2': demand2, 'next_period': next_period, 'new_round_num_minus': new_round_num_minus, 'total_allocation': total_allocation, 'demand_series1': x1, 'demand_series2': x2, 'total_demand': total_demand, 'payoff1': payoff1, 'payoff2': payoff2, 'total_payoff': total_payoff, 'K': K, } class EndPage(Page): def is_displayed(self): return self.round_number == Constants.num_rounds def vars_for_template(self): participation_payoff = 0.5 payoff_up = 0 players = self.player.in_all_rounds() for p in players: payoff_up = payoff_up + p.profit dollar = payoff_up / 10000 total_dollar = dollar + participation_payoff self.participant.payoff = c(payoff_up) return {'payoff_up': payoff_up, 'participation_payoff': participation_payoff, 'dollar': dollar, 'total_dollar': total_dollar} page_sequence = [ Instruction1, Instruction2, Simpletest1, Simpletest2, Experiment, Results, EndPage, ]