import random from otree.api import * doc = """ One player decides how to divide a certain amount between himself and the other player. See: Kahneman, Daniel, Jack L. Knetsch, and Richard H. Thaler. "Fairness and the assumptions of economics." Journal of business (1986): S285-S300. """ class C(BaseConstants): NAME_IN_URL = 'agua1' PLAYERS_PER_GROUP = None NUM_ROUNDS = 13 NUM_PARTICIPANTS = 2 # Initial amount allocated to the dictator ENDOWMENT = 24 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): demand = models.IntegerField(initial=12) kept = models.IntegerField( doc="""Amount dictator decided to keep for himself""", min=0, max=C.ENDOWMENT, label="", ) left = models.IntegerField(initial=0) turno = models.IntegerField(initial=0) deficit = models.IntegerField(initial=0) vecino = models.IntegerField(initial=0) kept_vecino = models.IntegerField(initial=0) finalpayment = models.CurrencyField() # FUNCTIONS def update_demand(group: Group): ronda = group.round_number players = group.get_players() d_pri_min = 10 d_pri_max = 14 d_ver_min = 12 d_ver_max = 22 for p in players: if ronda <= 3: p.demand = random.randint(12, 18) elif ronda <= 8: p.demand = random.randint(d_pri_min, d_pri_max) else: p.demand = random.randint(d_ver_min, d_ver_max) def set_payoffs(group: Group): players = group.get_players() kepts = [p.kept for p in players] for p in players: p.turno = int(round(random.random(), 0) + 1) # 1:J1 2:J2 if p.turno == 1: p.payoff = min(p.kept, p.demand)*500 # pago como J1 p.deficit = int(max(0, p.demand - p.kept)) else: v = random.randint(0,len(players)-1) p.vecino = v # numero del jugador asignado como vecino p.kept_vecino = kepts[v] left_vecino = C.ENDOWMENT - p.kept_vecino p.payoff = min(left_vecino, p.demand)*500 # pago como J2 p.deficit = int(max(0, p.demand - left_vecino)) # PAGES class Introduction(Page): @staticmethod def is_displayed(player): return player.round_number == 1 class Demand(WaitPage): after_all_players_arrive = update_demand title_text = "Esperando al otro jugador" body_text = "" @staticmethod def is_displayed(player): return True class Offer(Page): form_model = 'player' form_fields = ['kept'] @staticmethod def vars_for_template(player: Player): demandrect = round(400 * player.demand / 24) ronda = int(player.round_number) return dict(demandrect=demandrect, ronda=ronda, rondatitle=ronda-3) class Pagos(WaitPage): title_text = "Calculando Pagos" body_text = "Esperando que los otros jugadores tomen su decisión" after_all_players_arrive = set_payoffs class Results(Page): @staticmethod def vars_for_template(player: Player): group = player.group ronda = int(group.round_number) if player.turno == 1: left = 24 - player.kept # ancho rectangulos hriegorect = round(400 * player.kept / 24) demandrect = round(400 * player.demand / 24) offshriego = 0 # no se usa en player 1 offsetrect = 0 # no se usa en player 1 # posiciones textos en eje x texthriego = round(hriegorect / 2) + 5 texthrvecino = round((400 - hriegorect) / 2) + 5 + hriegorect textdeficit = round(400 * (player.kept + round(player.deficit/2)) / 24) + 5 # linea de decision riego (fin del rectangulo hriegorect) linehriego = hriegorect + 10 else: left=C.ENDOWMENT-player.kept_vecino # ancho rectangulos hriegorect = round(400 * left / 24) offshriego = round(400 - hriegorect) + 10 demandrect = round(400 * player.demand / 24) offsetrect = round(400 - demandrect) + 10 # posiciones textos en eje x texthriego = round(400 - hriegorect / 2) + 5 texthrvecino = round(400 * player.kept_vecino / 24 / 2) + 5 textdeficit = 400 - round(400 * (left + player.deficit / 2) / 24) + 5 # linea de decision riego (fin del rectangulo hriegorect) linehriego = offshriego return dict(left=left, ronda=ronda, rondatitle=ronda - 3, demandrect=demandrect, offsetrect=offsetrect, hriegorect=hriegorect, offshriego=offshriego, textdeficit=textdeficit, texthriego=texthriego, linehriego=linehriego, texthrvecino=texthrvecino) class PagoFinal(Page): #after_all_players_arrive = update_pagos #title_text = "Esperando al otro jugador" #body_text = "" @staticmethod def vars_for_template(player: Player): Nrondapago = random.randint(4, C.NUM_ROUNDS-1) player_in_selected_round = player.in_round(Nrondapago) player.finalpayment = player_in_selected_round.payoff return dict(pagofinal=player.finalpayment, Nrondapago=Nrondapago-3) @staticmethod def is_displayed(player): return player.round_number == C.NUM_ROUNDS page_sequence = [Introduction, Demand, Offer, Pagos, Results, PagoFinal]