from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants, Player import random import time class MR(Page): form_model = 'player' form_fields = ['response1', 'response2', 'response3', 'response4', 'response5', 'response6'] def vars_for_template(self): return { 'round_number': self.round_number, 'round_pic_dir': self.participant.vars['qn_order'][self.round_number - 1][1], 'trmt_instructions': Constants.trmt_instructions[self.participant.vars['payout_trmt']] } def before_next_page(self): print("Player", self.player.id_in_group, "question", self.round_number) #standard payoff if self.participant.vars['payout_trmt'] == 'std': resp_list = [self.player.response1, self.player.response2, self.player.response3, self.player.response4, self.player.response5, self.player.response6, self.player.response7, self.player.response8, 'n'] index = [0, 1, 2, 3, 4, 5, 6, 7, 8] CL = set(zip(index, resp_list)) #Choice List; all responses print('chosen options: ', CL) correct_ans = (self.participant.vars['qn_order'][self.round_number - 1][0], True) CL_bar = set() #Chosen List; all chosen responses if (correct_ans in CL): print(10, 'points awarded') self.player.payoff += c(10) else: print('nothing awarded') pass #randomized payoff elif self.participant.vars['payout_trmt'] == 'rand': resp_list = [self.player.response1, self.player.response2, self.player.response3, self.player.response4, self.player.response5, self.player.response6, self.player.response7, self.player.response8, 'n'] index = [0, 1, 2, 3, 4, 5, 6, 7, 8] CL = set(zip(index, resp_list)) #Choice List; all responses print('chosen options: ', CL) correct_ans = (self.participant.vars['qn_order'][self.round_number - 1][0], True) CL_bar = set() #Chosen List; all chosen responses if (correct_ans in CL): for k in range(len(resp_list)-1): if resp_list[k] == True: CL_bar.add((k,True)) print('chosen options: ', CL_bar) chosen = random.sample(CL_bar,1)[0] print('randomly sampled option to implement', chosen) if chosen == correct_ans: print(10, 'points awarded') self.player.payoff += c(10) else: print('sampled option is not correct, nothing awarded') print(" ") pass else: print('correct answer is not among chosen options, nothing awarded') pass #split payoff elif self.participant.vars['payout_trmt'] == 'split': resp_list = [self.player.response1, self.player.response2, self.player.response3, self.player.response4, self.player.response5, self.player.response6, self.player.response7, self.player.response8, 'n'] index = [0, 1, 2, 3, 4, 5, 6, 7, 8] CL = set(zip(index, resp_list)) CL_bar = set() correct_ans = (self.participant.vars['qn_order'][self.round_number - 1][0], True) if correct_ans in CL: for k in range(len(resp_list)-1): if resp_list[k] == True: CL_bar.add((k,resp_list[k])) print(len(CL_bar),"chosen options are ",CL_bar) payout = 10 / len(CL_bar) self.player.payoff += c(payout) print(payout, " awarded") else: print("nothing awarded") print(" ") else: print("unsuccessful payoff assignment") def get_timeout_seconds(self): #if self.participant.vars['tc_trmt'] == True: # return self.participant.vars['expiry'] - time.time() #elif self.participant.vars['tc_trmt'] == False: # pass return self.participant.vars['expiry'] - time.time() def error_message(self, values): #write error messages for std #print('values is', values) if (not values['response1']) and (not values['response2']) and (not values['response3']) and (not values['response4']) and (not values['response5']) and (not values['response6']): return 'Please pick at least one option' if self.participant.vars['payout_trmt'] == 'std': print(values['response1'] + values['response2'] + values['response3'] + values['response4'] + values['response5'] + values['response6']) if values['response1'] + values['response2'] + values['response3'] + values['response4'] + values['response5'] + values['response6'] > 1: return 'Please pick exactly one option' def is_displayed(self): print('this is matrix', self.participant.vars['qn_order'][self.round_number - 1][1]) print('correct answer is', (self.participant.vars['qn_order'][self.round_number - 1][0])) if self.participant.vars['tc_trmt'] == True: return self.participant.vars['expiry'] - time.time() > 3 elif self.participant.vars['tc_trmt'] == False: return True page_sequence = [MR]