from otree.api import * c = cu doc = '' class C(BaseConstants): # built-in constants NAME_IN_URL = 'sequentialsearch_live' PLAYERS_PER_GROUP = None NUM_ROUNDS = 76 # user-defined constants NFCLONG = 0 HIVALUE = 30 BASELINEROUNDS = 6 MIDROUND = 80 PRACTICEROUNDS = 5 DISCOUNTFACTOR = 0 DISPLAYALGO = True class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session # https://otree.readthedocs.io/en/latest/treatments.html#creating-session if subsession.round_number == 1: import itertools import random # create 5 different random orderings indices1 = [i for i in range(1,C.NUM_ROUNDS+1)] random.shuffle(indices1) indices2 = [i for i in range(1,C.NUM_ROUNDS+1)] random.shuffle(indices2) indices3 = [i for i in range(1,C.NUM_ROUNDS+1)] random.shuffle(indices3) indices4 = [i for i in range(1,C.NUM_ROUNDS+1)] random.shuffle(indices4) indices5 = [i for i in range(1,C.NUM_ROUNDS+1)] random.shuffle(indices5) # cycle the players between the random orderings index_cycle = itertools.cycle([indices1,indices2,indices3,indices4,indices5]) for player in subsession.get_players(): participant = player.participant participant.indexlist = next(index_cycle) class Group(BaseGroup): pass class Player(BasePlayer): boxvalues = models.StringField() chosencard = models.IntegerField(initial=0) stopped = models.IntegerField(initial=0) chosenboxvalue = models.FloatField(initial=0) recommendation = models.IntegerField() reachedend = models.IntegerField(initial=0) prolificcode = models.StringField(blank=True) reward = models.FloatField(initial=0) bonus = models.FloatField() dectimes = models.StringField(initial='') polyareas = models.StringField(initial='') startleft = models.StringField(initial='') touchtimes = models.StringField(initial='') xs = models.StringField(initial='') ys = models.StringField(initial='') dohmen = models.IntegerField(choices=[[1, '1 = not at all willing to take risks'], [2, '2'], [3, '3'], [4, '4'], [5, '5'], [6, '6'], [7, '7'], [8, '8'], [9, '9'], [10, '10 = very willing to take risks']], widget=widgets.RadioSelect) nfcscore = models.IntegerField() nfcall = models.StringField() affectbased = models.IntegerField(choices=[[1, '1 = Strongly disagree'], [2, '2 = Moderately disagree'], [3, '3 = Slightly disagree'], [4, '4 = Slightly agree'], [5, '5 = Moderately agree'], [6, '6 = Strongly agree']], label='
I solved the task on a gut level
', widget=widgets.RadioSelect) deliberative = models.IntegerField(choices=[[1, '1 = Strongly disagree'], [2, '2 = Moderately disagree'], [3, '3 = Slightly disagree'], [4, '4 = Slightly agree'], [5, '5 = Moderately agree'], [6, '6 = Strongly agree']], label='I tried to solve the task mathematically
', widget=widgets.RadioSelect) handedness = models.StringField(choices=[['Right hand', 'Right hand'], ['Left hand', 'Left hand']], label='Which hand do you typically use to browse your mobile device?
', widget=widgets.RadioSelect) computer_randomround1 = models.IntegerField() computer_randomround2 = models.IntegerField() checkup1 = models.IntegerField(choices=[[2, 'Points earned from all rounds'], [1, 'Points earned from two randomly chosen rounds'], [3, 'Points of random box values']], label='My bonus payments from the study consists of ...
', widget=widgets.RadioSelect) checkup2 = models.IntegerField(choices=[[1, 'Then the last box will determine my points'], [2, 'Then my points will be zero'], [3, 'Then my points will be randomly determined']], label='If I swipe left through all boxes, ...
', widget=widgets.RadioSelect) checkup3 = models.IntegerField(choices=[[1, 'The box values change randomly from round to another'], [2, 'The box values are always the same between rounds, only their order changes']], label='Which of the below statements is true?
', widget=widgets.RadioSelect) def live_open(player: Player, data): group = player.group time = data['time'] # this is a single timestamp indicating when the message was sent to server touchtimes = data['touchtimes'] # these timestamps indicate when the card was touched starttime = data['starttime'] # this is a timestamp recorded on page, indicating when box moving started opened = data['opened'] # this is zero if swiped right player.chosencard += opened player.dectimes += str(int(time) - int(starttime)) + ';' player.xs += data['x'] + ';' player.ys += data['y'] + ';' # this should correspond to decision times player.touchtimes += touchtimes + ';' player.startleft += data['startleft'] + ';' # this is recorded just in case the polyarea calculations are wrong ### POLYAREA x = data['x'] y = data['y'] xx = x.split(',') xx = xx[:-1] xxx = map(float,xx) x = list(map(int,xxx)) yy = y.split(',') yy = yy[:-1] y = list(map(int,yy)) # flip the y coordinate y = list(reversed(y)) xy = [] j = 0 # append all the points in the curve except the tail points for jj in range(j,len(x)): if x[-1] < int(data['startleft']): # rejected when final x-coordinate is 150 px lower than startleft if x[jj] >= x[0]-150: xy.append([x[jj],y[jj]]) jjj = jj # stopping index else: # accepted when final x-coordinate is 150 px higher than startleft if x[jj] < x[0]+150: xy.append([x[jj],y[jj]]) jjj = jj # stopping index # close the curve to determine the polyarea xy.append([x[jjj],y[jjj]]) # append the end point xy.append([x[jjj],y[0]]) # append the point that is vertically upwards from the end point xy.append([x[0],y[0]]) # append the start point l = len(xy) s = 0.0 for i in range(l): j = (i+1)%l # keep index in [0,l) s += (xy[j][0] - xy[i][0])*(xy[j][1] + xy[i][1]) polyarea = -0.5*s player.polyareas += str(polyarea) + ';' response = {'openedcards': player.chosencard} # how many cards have been opened so far return {player.id_in_group: response} # this has been changed from 0: response -- reason for blinking?? def checkup1_error_message(player: Player, value): if value != 1: return "Your answer is wrong. Hint: as the instructions state, two rounds will be randomly selected at the end of the study to determine the bonus." def checkup2_error_message(player: Player, value): if value != 1: return "Your answer is wrong. Hint: as the instructions state, if you do not choose any box then the last box will determine your points." def checkup3_error_message(player: Player, value): if value != 1: return "Your answer is wrong. Hint: as the instructions state, the box values are randomly generated on each new round." class MobileCheck(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == 1 @staticmethod def vars_for_template(player: Player): session = player.session return dict( pilot = session.config['pilot'] ) class Welcome(Page): form_model = 'player' form_fields = ['prolificcode'] @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Middle(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == C.MIDROUND class Instructions(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number <= C.NUM_ROUNDS - C.BASELINEROUNDS @staticmethod def vars_for_template(player: Player): session = player.session return dict( boxes = session.config['boxes'], fullinfo = session.config['fullinfo'], recommendation = player.round_number >= C.MIDROUND and C.DISPLAYALGO, practiceround = player.round_number <= C.PRACTICEROUNDS or (player.round_number >= C.MIDROUND and player.round_number <= C.MIDROUND + C.PRACTICEROUNDS - 1), rounds = C.NUM_ROUNDS - C.BASELINEROUNDS, realround = player.round_number == C.PRACTICEROUNDS + 1, discount = C.DISCOUNTFACTOR > 0, discountpercent = C.DISCOUNTFACTOR*100, ) class Decision(Page): form_model = 'player' form_fields = ['reachedend'] @staticmethod def is_displayed(player: Player): return player.round_number <= C.NUM_ROUNDS - C.BASELINEROUNDS @staticmethod def vars_for_template(player: Player): session = player.session # create the number of boxes opened for js_vars (same as blockround in the old app) import random if player.field_maybe_none('boxvalues') == None: values = [round(random.uniform(0,C.HIVALUE),1) for i in range(session.config['boxes'])] player.boxvalues = str(values).split('[')[1].split(']')[0] import ast values = player.boxvalues #ast.literal_eval(player.boxvalues) valuess = ast.literal_eval(player.boxvalues) N = session.config['boxes'] V = [0] * (N+1) V[N] = 0 for i in range(N-1, -1, -1): cumprob = V[i+1] / C.HIVALUE conditionaldensity = (C.HIVALUE*C.HIVALUE - (V[i+1])*(V[i+1])) / (2*C.HIVALUE) V[i] = conditionaldensity + V[i+1] * cumprob for j in range(len(valuess)): if valuess[j] > V[j]: break recommendation = j + 1 player.recommendation = recommendation return dict( values = values, #str(values).split('[')[1].split(']')[0], pilot = session.config['pilot'], fullinfo = session.config['fullinfo'], recommendation = recommendation ) @staticmethod def js_vars(player: Player): session = player.session boxes = session.config['boxes'] return dict( fullinfo = session.config['fullinfo'], openedcards = player.chosencard, # this is only used to display the initial row (NOT NEEDED IN js_vars?) roundi = player.round_number, boxes = boxes, showrecommendation = True# C.DISCOUNTFACTOR and player.round_number >= C.MIDROUND ) @staticmethod def before_next_page(player: Player, timeout_happened): session = player.session # add 1 to chosencard because unlike in CCT, here the player chooses the box that she swipes right player.chosencard += 1 if player.reachedend == 1: # if ended round by opening the last box player.chosencard = session.config['boxes'] import ast boxvalues = ast.literal_eval(player.boxvalues) player.chosenboxvalue = boxvalues[player.chosencard-1] live_method = 'live_open' class RoundEnd(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number <= C.NUM_ROUNDS - C.BASELINEROUNDS @staticmethod def vars_for_template(player: Player): session = player.session reached = "" if player.reachedend == 1: reached = "You reached the end without stopping. Therefore your chosen box was the last box." if player.round_number <= C.PRACTICEROUNDS: player.reward = 0 elif player.round_number == C.PRACTICEROUNDS + 1: # first round to pay reward player.reward = player.chosenboxvalue elif player.round_number >= C.MIDROUND and player.round_number <= C.MIDROUND + C.PRACTICEROUNDS - 1: prev_player = player.in_round(player.round_number - 1) player.reward = round(prev_player.reward + 0, 1) # do not accumulate reward from middle practice rounds else: prev_player = player.in_round(player.round_number - 1) player.reward = round(prev_player.reward + player.chosenboxvalue, 1) # accumulate reward # create the table of block results tabledata = "" if player.round_number <= C.PRACTICEROUNDS or (player.round_number >= C.MIDROUND and player.round_number <= C.MIDROUND + C.PRACTICEROUNDS - 1): practiceround = 1 else: practiceround = 0 for j in range(player.round_number): if (j+1 > C.PRACTICEROUNDS and j+1 < C.MIDROUND) or (j+1 > C.MIDROUND + C.PRACTICEROUNDS - 1): j_player = player.in_round(j+1) points = round((1/pow(1+C.DISCOUNTFACTOR,j_player.chosencard))*(j_player.chosenboxvalue),1) tabledata += "