from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class Config(Page): form_model = 'player' def get_form_fields(self): return [ 'resources', 'rounds', 'addShuffling', 'reshufflingLikelihood' ] + ['successProbabilityG{}'.format(i) for i in range(1, Constants.resource_distribution_groups + 1)] def is_displayed(self): return self.round_number == 1 class Instructions(Page): def vars_for_template(self): return { 'resources': self.session.config['resources'], } def is_displayed(self): return self.round_number == 1 class ManagementPage(Page): form_model = 'player' def get_form_fields(self): return ['middle_manager_{}'.format(i) for i in range(1, Constants.resource_distribution_groups + 1)] def vars_for_template(self): previous_round = self.player.in_round(self.round_number) if self.round_number <= 1\ else self.player.in_round(self.round_number - 1) return { 'round_num': self.round_number, 'col': self.round_number * 2 - 2, 'resources': self.session.config['resources'], 'Group_for_all_rounds': self.player.in_rounds(1, Constants.num_rounds), 'group_for_last_round': previous_round, 'info_for_rounds': self.round_number, 'resource_distribution_groups': Constants.resource_distribution_groups } def before_next_page(self): self.player.calculate_unit_stats() if self.round_number == 1: # start game completely randomly self.player.distribute_resources() else: if self.session.config['epsilon_greedy_middle_managers']: # apply e-greedy self.player.distribute_resources_epsilon_greedily() else: # or continue with random resource assignment self.player.distribute_resources() self.player.calculate_outcome() def error_message(self, values): payouts = self.get_payout_values(values) if sum(payouts) > self.session.config['resources']: return 'You have tried to distribute too many resources (you can only distribute ' + str(self.session.config['resources']) + ')' elif sum(payouts) < self.session.config['resources']: return 'You have tried to distribute too few resources (you MUST distribute ' + str(self.session.config['resources']) + ')' def get_payout_values(self, values): payouts = [] for i in range(1, Constants.resource_distribution_groups + 1): payouts.append(values['middle_manager_{}'.format(i)]) return payouts class ResultsWaitPage(WaitPage): pass class Results(Page): def vars_for_template(self): return { 'round_num': self.round_number, 'Group_for_all_rounds': self.player.in_rounds(1, Constants.num_rounds), 'info_for_rounds': self.round_number + 1 } def is_displayed(self): return self.round_number == Constants.num_rounds page_sequence = [ #Config, Instructions, ManagementPage, #ResultsWaitPage, Results ]