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 = 'agua2_SG' PLAYERS_PER_GROUP = None NUM_ROUNDS = 35 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.FloatField(initial=0) pagoJ1 = models.IntegerField(initial=0) pagoJ2 = models.IntegerField(initial=0) deficit = models.IntegerField(initial=0) vecino = models.IntegerField(initial=0) demanda_vecino = models.IntegerField(initial=0) kept_vecino = models.IntegerField(initial=0) # FUNCTIONS def update_demand(group: Group): ronda = int(group.round_number) players = group.get_players() d_pri_min = 10 d_pri_max = 14 d_ver_min = 12 d_ver_max = 24 for p in players: if ronda <= 5: p.demand = random.randint(12, 18) elif ronda > 5 & ronda <= 10: 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] #demands = [p.demand for p in players] for p in players: p.pagoJ1 = min(p.kept, p.demand) p.left = C.ENDOWMENT - p.kept p.pagoJ2 = min(p.left, p.demand) p.turno = round(random.random(), 0) + 1 #1:J1 2:J2 pagosJ2 = [p.pagoJ2 for p in players] demandasJ2 = [p.demand for p in players] keptJ2 = [p.kept for p in players] for p in players: if p.turno == 1: p.payoff = p.pagoJ1 p.deficit = max(0, p.demand - p.kept) else: v = random.randint(0,len(players)-1) p.payoff = pagosJ2[v] p.demanda_vecino = demandasJ2[v] p.kept_vecino = keptJ2[v] p.vecino = v #numero del jugador asignado como vecino p.deficit = max(0, p.demand - p.left) # 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) offsetrect = round(400 - demandrect) return dict(demandrect=demandrect, ronda=ronda, rondatitle=ronda-5) 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) return dict(round=ronda, rondatitle=ronda - 5) class Results1(Page): @staticmethod def vars_for_template(player: Player): group = player.group ronda = int(group.round_number) if player.id_in_group==1: hrvecino = 24-player.hriego # ancho rectangulos hriegorect = round(400 * player.hriego / 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.hriego + player.deficit / 2) / 24) + 5 # linea de decision riego (fin del rectangulo hriegorect) linehriego = hriegorect + 10 else: hrvecino = 24 - player.hriego # ancho rectangulos hriegorect = round(400 * player.hriego / 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 * hrvecino / 24 / 2) + 5 textdeficit = 400 - round(400 * (player.hriego + player.deficit / 2) / 24) + 5 # linea de decision riego (fin del rectangulo hriegorect) linehriego = offshriego return dict(hrvecino=hrvecino, round=ronda, rondatitle=ronda-5, demandrect=demandrect, offsetrect=offsetrect, hriegorect=hriegorect, offshriego=offshriego, textdeficit=textdeficit, texthriego=texthriego, linehriego=linehriego, texthrvecino=texthrvecino) page_sequence = [Introduction, Demand, Offer, Pagos, Results]