from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import channels import statistics import json def vars_for_all_templates(self): price_list= [] for r in self.subsession.in_previous_rounds(): for g in r.get_groups(): if g.round_number < self.session.vars['init_round']: # only include prices from previous markets if g.someone_accepted: price_list.append(g.amount_offered) if price_list: average_price= statistics.mean(price_list) else: average_price = -10 if int(self.session.vars['time_series'])==1: display_time_series = True else: display_time_series = False return { 'player_in_market': reversed(self.player.in_rounds(self.session.vars['init_round'], self.group.round_number)), 'other_players': self.group.get_players(), 'average_price':round(average_price), 'price_list': price_list, 'market_active': self.group.market_ends() == False, 'display_time_series': display_time_series } #### Round begins, class RoundBeginsWP(WaitPage): template_name = 'walras/WaitPages.html' body_text="" def after_all_players_arrive(self): for p in self.subsession.get_players(): if self.subsession.period() > 1: p.traded=p.participant.vars['traded'] p.trading_price = p.participant.vars['trading_price'] # p.set_payoffs() # match players and select proposer self.group.make_match() #### Screen for proposer class Proposal(Page): # timeout_seconds = 10 form_model = 'group' #form_fields = ['amount_offered','proposer_pass'] form_fields = ['amount_offered'] def is_displayed(self): return self.player.proposing() and self.group.market_ends()==False #### Error messages if you propose a price that would lose you money def amount_offered_error_message(self, value): if self.player.role()=='buyer' and value>self.player.value_cost(): return 'You cannot offer a price higher than your value!' if self.player.role()=='seller' and valueself.player.value_cost(): return "You cannot accept a price higher than your value! You must click 'Reject.'" if self.player.role()=='seller' and price0 and current_first_trade==0: for p in g.get_players(): p.participant.vars['first_trade_period'] = g.subsession.period() if self.subsession.both_groups_done(): ### form groups for next round self.session.vars['current_market'] = self.session.vars['current_market'] + 1 # increment market (used in forming groups and making matches) self.subsession.form_groups(self.round_number + 1,self.round_number+1) # add last market's earnings to list and randomly choose markets to pay (for admin report page) self.subsession.randomize_payments() # reset for new market for p in self.subsession.get_players(): p.participant.vars['market_earn'] = 0 p.participant.vars['traded'] = False p.participant.vars['trading_price'] = -1 p.participant.vars['saw_results'] = False p.participant.vars['first_trade_period'] = 0 self.session.vars['init_round'] = self.round_number + 1 else: # copy grouping to next round for subsession in self.subsession.in_rounds(self.round_number+1,self.round_number+1): subsession.group_like_round(self.round_number) ### set up new market class Results(Page): def is_displayed(self): return self.group.market_ends() == True and self.participant.vars['saw_results'] == False def before_next_page(self): self.participant.vars['saw_results'] = True page_sequence = [ RoundBeginsWP, Proposal, # NoTrade, WaitForOthersWP, Response, InactiveWP, Results, UpdateWP ]