from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class MatchSelectPage(Page): form_model = 'player' def get_form_fields(self): flist = [] for i in range(1, Constants.players_per_group): flist.append('pfield'+str(i)) flist.append('rfield'+str(i)) flist.append('unmatchPref') return flist def vars_for_template(self): import json other_players = [] for p in self.subsession.get_players(): if p.uid != self.player.uid: other_players.append(p) return dict( other_players=other_players, my_score=self.participant.vars['score'], megaRound=self.session.vars['megaRound'], this_treatment = self.session.vars['treatment'] ) def before_next_page(self): import json other_pl_str = json.loads(self.player.otherPlayers) #this is a dict! other_players = [] for op in other_pl_str: for pp in self.subsession.get_players() : if pp.uid == other_pl_str[op]: other_players.append(pp) this_prefs = {} # { player_visible_id : [uid, pref_rank, role] } for i in range(1, Constants.players_per_group ): global this_pref global this_role exec("global this_pref; this_pref = self.player.pfield"+str(i)) exec("global this_role; this_role = self.player.rfield"+str(i)) this_prefs[i] = [other_players[i-1].uid, this_pref, this_role ] self.player.prefsPlayers = json.dumps(this_prefs) self.participant.vars['prefsPlayers'] = json.dumps(this_prefs) def error_message(self, values): #Ensure that every preference order rank is only chosen once at maximum flag = 0 for i in range(1, Constants.players_per_group): for j in range(1, Constants.players_per_group): if i == j: continue if values['pfield'+str(i)] == values['pfield'+str(j)]: #print(values['pfield'+str(j)], values['pfield'+str(i)]) if values['pfield'+str(j)] != 0: #print('Error') flag = 1 if flag == 1: flag = 0 return 'Error: You can only use every rank number once! Please check again' class MatchWaitPage(WaitPage): def after_all_players_arrive(self): self.subsession.create_matching() class MatchResultPage(Page): def vars_for_template(self): return dict( pref_dat = "", # self.subsession.matches , my_id=self.player.uid, my_score=self.participant.vars['score'], megaRound=self.session.vars['megaRound'] ) def before_next_page(self): pass #self.subsession.back_up() def vars_for_template(self): return dict( my_id=self.player.uid, my_score=self.participant.vars['score'], megaRound=self.session.vars['megaRound'] ) class App2LandingPage(Page): def before_next_page(self): self.subsession.on_app_start() def vars_for_template(self): return dict( my_id=self.player.uid, my_score=self.participant.vars['score'], megaRound=self.session.vars['megaRound'], this_D = self.session.vars['D'], this_treatment = self.session.vars['treatment'], this_r = self.session.vars['r'] * 100, this_s = self.session.vars['s'] * 100, ) class MatchExplainPage(Page): # Only show in first encounter and only during testing def is_displayed(self): return self.session.vars['megaRound'] == 1 and self.session.vars['test_round'] == 1 def vars_for_template(self): return dict( my_id=self.player.uid, my_score=self.participant.vars['score'], megaRound=self.session.vars['megaRound'], ) class MatchUnderstandPage(Page): form_model = 'player' form_fields = ['p_answer_undst_1', 'r_answer_undst_1','p_answer_undst_2', 'r_answer_undst_2','p_answer_undst_3', 'r_answer_undst_3',] # XXX 'p_answer_undst_4', 'r_answer_undst_4'] def is_displayed(self): # Don't show while testing and only once per period return self.session.vars['test_round'] != 1 & self.session.vars['megaRound'] == 1 # XXX def vars_for_template(self): import json other_players = [] for p in self.subsession.get_players(): if p.uid != self.player.uid: other_players.append(p) self_skills = json.loads(self.player.self_underst_params) other_skills = json.loads(self.player.others_underst_params) # adjust to display units fsk1 = str( int( round(self_skills[0] * 100, 0 ) ) ) + "%" fsk2 = str( int( round(self_skills[1] * 100, 0 ) ) ) + "%" other_skills= [ [str( int( round(p[0] * 100, 0 ) ) ) + "%", p[1], p[2]] for p in other_skills ] other_skills= [ [p[0], str( int( round(p[1] * 100, 0 ) ) ) + "%", p[2]] for p in other_skills ] return dict( #other_players=other_players, fake_skill1 = fsk1, fake_skill2 = fsk2, other_players = other_skills, ) def before_next_page(self): pass def error_message(self, values): pass page_sequence = [ App2LandingPage, # MatchExplainPage, MatchSelectPage, MatchWaitPage, MatchUnderstandPage, MatchResultPage, # PostMatchWaitPage XXX ]