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 = 'agua' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 35 NUM_PARTICIPANTS = 2 # Initial amount allocated to the dictator ENDOWMENT = 24 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): subsession.group_randomly() class Group(BaseGroup): kept = models.IntegerField( doc="""Amount dictator decided to keep for himself""", min=0, max=C.ENDOWMENT, label="", ) class Player(BasePlayer): hriego = models.IntegerField(initial=12) demand = models.IntegerField(initial=12) deficit = models.IntegerField(initial=0) # FUNCTIONS def set_payoffs(group: Group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) gain1 = min(group.kept, p1.demand) left2 = C.ENDOWMENT - group.kept p1.payoff = gain1 p1.hriego = group.kept p1.deficit = max(0, p1.demand - group.kept) p2.payoff = min(left2, p2.demand) p2.hriego = left2 p2.deficit = max(0, p2.demand - left2) def update_demand(group: Group): ronda = int(group.round_number) p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) d_pri_min = 10 d_pri_max = 14 d_ver_min = 12 d_ver_max = 20 d_oto_min = 12 d_oto_max = 16 if ronda <= 5: p1.demand = random.randint(12, 18) elif ronda > 5 & ronda <= 15: p1.demand = random.randint(d_pri_min, d_pri_max) elif ronda > 15 & ronda <= 25: p1.demand = random.randint(d_ver_min, d_ver_max) else: p1.demand = random.randint(d_oto_min, d_oto_max) p2.demand = p1.demand # 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 = 'group' form_fields = ['kept'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 @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 Turno2WaitPage(WaitPage): template_name = 'test_ale/Turno2WaitPage.html' after_all_players_arrive = set_payoffs @staticmethod def vars_for_template(player: Player): demandrect = round(400 * player.demand / 24) ronda = int(player.round_number) offsetrect = round(400 - demandrect) + 10 return dict(demandrect=demandrect, ronda=ronda, offsetrect=offsetrect, rondatitle=ronda-5) class WaitPage(WaitPage): title_text = "Le ha tocado el TURNO 2" body_text = "Esperando que el otro jugador tome 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.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, Turno2WaitPage, Results]