from otree.api import * #Right now, it asks both but it only stores the last value entered. #I need to learn how to tell the computer to store both values separately (base level should be player) #And then use the options selected by Participant to determine the group payoffs. #This should be clearer after running at least one group game. 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 = 'Task2' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 1 #INSTRUCTIONS_TEMPLATE = 'Task2/instructions.html' # Initial amount allocated to the dictator ENDOWMENT = cu(30) NPO = ['HumanRightsWatch', 'CARE'] def creating_session(Subsession): Subsession.group_randomly(fixed_id_in_group=True) session = Subsession.session class Subsession(BaseSubsession): pass def call_subsession(subsession: Subsession): return(subsession) class Group(BaseGroup): Divider_result = models.CurrencyField() Receiver_result = models.CurrencyField() NPO_result = models.CurrencyField() NPO = models.StringField() class Player(BasePlayer): donation_NPO = models.CurrencyField( doc="""Amount dictator decided to keep for himself""", min=0, max=C.ENDOWMENT, label="b) I would give to the Nonprofit: ", ) donation_other = models.CurrencyField( doc="""Amount dictator decided to give to the other player""", min=0, max=C.ENDOWMENT, label="a) I would give to the other participant: ", ) kept = models.CurrencyField() # FUNCTIONS class Introduction(Page): pass class Introduction2(Page): pass class Task2(WaitPage): @staticmethod def after_all_players_arrive(group: Group): session = group.session class Offer(Page): form_model = 'player' form_fields = ['donation_other','donation_NPO'] @staticmethod def before_next_page(player: Player, timeout_happened): player.kept = C.ENDOWMENT - player.donation_NPO - player.donation_other @staticmethod def error_message(player: Player, values): #With error message we can check for certain conditons. total_donation = values['donation_NPO'] + values['donation_other'] if total_donation > C.ENDOWMENT: return "You tried to exceed your initial allocation" class Offer2(Page): form_model = 'player' form_fields = ['donation_other','donation_NPO'] @staticmethod def before_next_page(player: Player, timeout_happened): player.kept = C.ENDOWMENT - player.donation_NPO - player.donation_other @staticmethod def error_message(player: Player, values): #With error message we can check for certain conditons. total_donation = values['donation_NPO'] + values['donation_other'] if total_donation > C.ENDOWMENT: return "You tried to exceed your initial allocation" def set_payoffs(group: Group): import random player_list = group.get_players() p1 = player_list[0] p2 = player_list[1] p1.payoff = p1.kept p2.payoff = p1.donation_other group.Divider_result = p1.kept group.Receiver_result = p1.donation_other group.NPO_result = p1.donation_NPO r = random.randint(1, 2) group.NPO = C.NPO[r-1] class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs #donation = p1.donation, class ResultsTask2(Page): def before_next_page(player: Player, timeout_happened): participant = player.participant group = player.group participant.T2id = player.id_in_group participant.T2receiverresult = group.Receiver_result participant.T2nporesult = group.NPO_result participant.T2npo = group.NPO participant.T2payoff = player.payoff page_sequence = [Introduction, Introduction2, Offer, Offer2, ResultsWaitPage, ResultsTask2] #I can change SurveyandResults to the end at the end #remaining: Preventing the player from donating more than 10 between the NPO and the other player.