from otree.api import * import random import numpy as np c = Currency doc = """ Your app description """ # p = np.array([[22, 22, 22, 22, 22, 22], # [40, 43, 58, 4, 37, 34], # [16, 15, 10, 28, 17, 18], # [75, 75, 50, 45, 60, 50]], dtype="float") p = np.array([[22, 22, 22, 22, 22, 22], [40, 61, 40, 40, 52, 44], [16, 9, 4, 0, 2, 0], [75, 75, 50, 45, 60, 50]]) # dtype="float") rng = np.random.default_rng() rng.shuffle(p, axis=1) # shuffle only the columns, default is 0 class Constants(BaseConstants): name_in_url = 'lottery' players_per_group = None num_rounds = 6 expfee = 3 riskFreepayoff = p[0, :] risckypayoffHigh = p[1, :] risckypayoffLow = p[2, :] pLow = p[3, :] # prob of detection with low payoff pHigh = 100 - p[3, :] # prob of NOT detection with low payoff ums_rate = '$0.03' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): lottery = models.BooleanField( label="Which one do you prefer?") payoff1 = models.FloatField() payoff_selected = models.FloatField() def lottery_choices(player): choices = [ [True, "Receiving {} tokens for sure".format(Constants.riskFreepayoff[player.round_number - 1])], [False, "Participate in a lottery in which you have {}% chance of winning {} tokens and {}% chance of winning {} tokens".format( Constants.pLow[player.round_number - 1], Constants.risckypayoffLow[player.round_number - 1], Constants.pHigh[player.round_number - 1], Constants.risckypayoffHigh[player.round_number - 1])] ] random.shuffle(choices) return choices # ---------------------------------------------------------- # FUNCTION # ---------------------------------------------------------- def creating_session(subsession): session = subsession.session if subsession.round_number == 1: for p in subsession.get_players(): p.participant.vars['exited'] = False # if self.round_number == 1: # for p in self.session.get_participants(): # imgs = Constants.imgs.copy() # random.shuffle(imgs) # p.vars['images'] = imgs # ---------------------------------------------------------- # PAGES # ---------------------------------------------------------- class introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class lotterySelection(Page): form_model = "player" form_fields = ["lottery"] @staticmethod def before_next_page(player: Player, timeout_happened): r = random.random() if player.lottery: player.payoff1 = int(Constants.riskFreepayoff[player.round_number - 1]) else: if r <= Constants.pLow[player.round_number - 1] / 100: player.payoff1 = int(Constants.risckypayoffLow[player.round_number - 1]) else: player.payoff1 = int(Constants.risckypayoffHigh[player.round_number - 1]) player.payoff1 = round(player.payoff1, 0) class finalResult(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds @staticmethod def vars_for_template(player: Player): rnd_round = random.randint(1, Constants.num_rounds) finalpayoff1 = player.in_round(rnd_round).payoff1 player.payoff_selected = finalpayoff1 # finalpayoff1= player.in_round(2).payoff1 return {"finalpayoff1": int(finalpayoff1), "selectedRound": rnd_round} class end(Page): def is_displayed(player: Player): return player.round_number == Constants.num_rounds @ staticmethod def before_next_page(player: Player): participant = player.participant participant.finished = True page_sequence = [introduction, lotterySelection, finalResult, end]