from otree.api import * c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'columbiacardtask' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1728 NUM_BLOCKS = 54 BOXES = 32 LOSSCARDS = (1, 2, 3) GAINAMOUNTS = (10, 10, 10, 20, 20, 20, 30, 30, 30) LOSSAMOUNTS = (250, 250, 250, 250, 250, 250, 250, 250, 250, 500, 500, 500, 500, 500, 500, 500, 500, 500, 750, 750, 750, 750, 750, 750, 750, 750, 750) NFCLONG = 0 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_BLOCKS+1)] random.shuffle(indices1) indices2 = [i for i in range(1,C.NUM_BLOCKS+1)] random.shuffle(indices2) indices3 = [i for i in range(1,C.NUM_BLOCKS+1)] random.shuffle(indices3) indices4 = [i for i in range(1,C.NUM_BLOCKS+1)] random.shuffle(indices4) indices5 = [i for i in range(1,C.NUM_BLOCKS+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): prolificcode = models.StringField() block = models.IntegerField(initial=1) round_in_block = models.IntegerField(initial=1) blockends = models.IntegerField(initial=0) blocklosscards = models.IntegerField() blockgain = models.IntegerField() blockloss = models.IntegerField() firstlosscard = models.IntegerField() losscardlist = models.StringField() accept = models.IntegerField() gaincardsturned = models.IntegerField() blockendround = models.IntegerField() y = models.StringField(blank=True) x = models.StringField(blank=True) reward = models.FloatField() jsdectime_start = models.FloatField(blank=True) jsdectime_end = models.FloatField(blank=True) jsdectime_touches = models.StringField(blank=True) jsdectime_firsttouch = models.FloatField(blank=True) dectime = models.FloatField() lookingtime = models.FloatField() 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() nfcscales = models.StringField() nfcall = models.StringField() browser = models.StringField(blank=True, initial='') screenwidth = models.IntegerField(blank=True, initial=0) screenheight = models.IntegerField(blank=True, initial=0) ismobile_touchstart = models.IntegerField(blank=True, initial=0) computer_randomblock1 = models.IntegerField() computer_randomblock2 = models.IntegerField() randomprofit1 = models.IntegerField() randomprofit2 = models.IntegerField() checkup1 = models.StringField(choices=[['2', 'Profits earned from all blocks'], ['1', 'Profits earned from two randomly selected blocks after the practice blocks'], ['3', 'Profits earned from the highest profit blocks']], label='
My bonus payments for the study consist of ...
', widget=widgets.RadioSelect) checkup2 = models.StringField(choices=[['2', 'True'], ['1', 'False']], label='The order in which candidates are presented is the same in all blocks
', widget=widgets.RadioSelect) checkup3 = models.StringField(choices=[['1', 'Then the same candidate will not be shown again in the same block'], ['2', 'Then the same candidate may be shown again in the same block']], label='If I reject a candidate, ...
', widget=widgets.RadioSelect) polyarea = models.FloatField() profit = models.IntegerField() def checkup1_error_message(player: Player, value): if value != '1': return "Your answer is wrong. Hint: as the instructions state, two blocks will be randomly selected at the end of the study and the profits from these blocks paid to you." def checkup2_error_message(player: Player, value): if value != '1': return "Your answer is wrong. The order in which candidates are presented is randomised in all blocks and therefore is different between blocks." def checkup3_error_message(player: Player, value): if value != '1': return "Your answer is wrong. After rejecting a candidate there will not be another opportunity to evaluate the candidate in the same block." def poly_area(player: Player): x = player.x y = player.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] < 237: if x[jj] >= x[0]-150: xy.append([x[jj],y[jj]]) jjj = jj # stopping index else: if x[jj] < x[0]+150: xy.append([x[jj],y[jj]]) jjj = jj # stopping index # close the curve to determine the polyarea # startleft value (initial position of the grey bid box) is 237, is this across all devices? if not, # then needs to be returned by the app # bid is accepted or rejected when its x-coordinate is 150 px from startleft 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 """ Calculates polygon area. """ # checked 29/12/2022 => OK! 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]) return -0.5*s class Mobilecheck(Page): form_model = 'player' form_fields = ['ismobile_touchstart', 'screenheight', 'screenwidth', 'browser'] @staticmethod def is_displayed(player: Player): session = player.session return player.round_number == 1 and session.config['pilot'] == 0 class Welcome(Page): form_model = 'player' form_fields = ['prolificcode'] @staticmethod def is_displayed(player: Player): session = player.session return player.round_number == 1 and session.config['pilot'] == 0 class Instructions(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( icons = session.config['icons'] ) class BlockStart(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): if player.round_number > 1: prev_player = player.in_round(player.round_number - 1) if prev_player.blockends == 1: display = True else: display = False else: display = True return display @staticmethod def vars_for_template(player: Player): session = player.session participant = player.participant # player.block is set on Decision page if player.round_number == 1: blockk = 1 else: prev_player = player.in_round(player.round_number - 1) blockk = prev_player.block + 1 # THE FOLLOWING VARIABLES ARE SET ONLY AT THE START OF THE BLOCK # AND THEN ON Decision PAGE COPIED FROM PREVIOUS ROUND IN EACH ROUND OF THE BLOCK # determine the amount of loss cards, gainamount, and lossamount trialnumber = participant.indexlist[blockk-1] # vector of how many loss cards in each block [1,2,3,1,2,3,1,2,3,...] losscardsvec = int(C.NUM_BLOCKS / len(C.LOSSCARDS)) * C.LOSSCARDS # how many loss cards in this specific block player.blocklosscards = losscardsvec[trialnumber - 1] # which specific card numbers are loss cards in this block import random numbs = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32] losscardlist = [] num1 = random.choice(numbs) losscardlist.append(num1) numbs.remove(num1) if (player.blocklosscards >= 2): num2 = random.choice(numbs) losscardlist.append(num2) numbs.remove(num2) if (player.blocklosscards == 3): num3 = random.choice(numbs) losscardlist.append(num3) # which one is the first loss card because the block ends in this card player.firstlosscard = min(losscardlist) player.losscardlist = str(losscardlist) # gain amount in this block based on trialnumber (excel file) 10,10,10,20,20,20,30,30,30,10,10,10,... gainsvec = int(C.NUM_BLOCKS / len(C.GAINAMOUNTS)) * C.GAINAMOUNTS player.blockgain = gainsvec[trialnumber - 1] # loss amount in this block based on trialnumber (excel file) 250,250,250,250,250,250,250,250,250,500,500,... lossvec = int(C.NUM_BLOCKS / len(C.LOSSAMOUNTS)) * C.LOSSAMOUNTS player.blockloss = lossvec[trialnumber - 1] return dict(blockk = blockk, pilot = session.config['pilot'], icons = session.config['icons']) class Decision(Page): form_model = 'player' form_fields = ['y', 'x', 'jsdectime_start', 'jsdectime_end', 'jsdectime_touches', 'jsdectime_firsttouch', 'accept'] @staticmethod def vars_for_template(player: Player): session = player.session # assign block number for rounds > 1 if player.round_number > 1: prev_player = player.in_round(player.round_number - 1) if prev_player.blockends == 1: player.block = prev_player.block + 1 else: player.block = prev_player.block # assign player.round_in_block if player.round_number == 1: player.round_in_block = 1 else: prev_player = player.in_round(player.round_number - 1) if prev_player.blockends == 1: player.round_in_block = 1 else: player.round_in_block = prev_player.round_in_block + 1 # assign number of loss cards for this block # and the specific cards that are loss cards out of the 32 cards if player.round_in_block > 1: prev_player = player.in_round(player.round_number - 1) player.blocklosscards = prev_player.blocklosscards player.firstlosscard = prev_player.firstlosscard player.losscardlist = prev_player.losscardlist player.blockgain = prev_player.blockgain player.blockloss = prev_player.blockloss return dict( pilot = session.config['pilot'], icons = session.config['icons'] ) @staticmethod def js_vars(player: Player): session = player.session # create blockround for js_vars if player.round_number == 1: blockround = 1 else: prev_player = player.in_round(player.round_number - 1) if prev_player.blockends == 1: blockround = 1 else: blockround = prev_player.round_in_block + 1 return dict( blockround = blockround, boxes = C.BOXES, icons = session.config['icons'], losscards = player.blocklosscards ) @staticmethod def before_next_page(player: Player, timeout_happened): # the block can end only if the player stops (accepts) or turns over a loss card # this is used in BlockEnd page if player.accept == 1 or player.round_in_block == player.firstlosscard: player.blockends = 1 player.gaincardsturned = player.round_in_block - 1 if player.round_in_block == player.firstlosscard: player.profit = max(0,player.gaincardsturned * player.blockgain - player.blockloss) else: player.profit = player.gaincardsturned * player.blockgain player.dectime = (player.jsdectime_end - player.jsdectime_firsttouch)/1000 player.lookingtime = (player.jsdectime_firsttouch - player.jsdectime_start)/1000 player.polyarea = poly_area(player) class BlockEnd(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.blockends == 1 @staticmethod def vars_for_template(player: Player): session = player.session player.blockendround = player.round_number # table of block results tabledata = "" for j in range(player.round_number): j_player = player.in_round(j+1) if j_player.blockends == 1: if session.config['icons'] == 'smileys': tabledata += "