from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import itertools author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'diss_exp_ex_both' players_per_group = 2 num_rounds = 2 # Total amount of resource to be split endowment = c(100) #instructions template for exchange exchangeinstructions = 'diss_exp/exchangeinstructions_template.html' class Subsession(BaseSubsession): def creating_session(self): #the two treatments treatments = itertools.cycle(['reciprocal', 'negotiated']) if self.round_number == 1: for player in self.get_players(): player.participant.vars['treatment'] = next(treatments) # #assigns players to treatments on a first-come-first-served basis (alternating) # #(on MTurk, how the queue is formed is kinda random I guess) # #ensures that everyone in a group is playing in the same treatment # if self.round_number == 1: # for g in self.get_groups(): # if 'treatment' in self.session.config: # treatment = self.session.config['treatment'] # #if more than 2 players must add code for additional players # # NOTE: participant.vars is an ephemeral thing # # whose value is more permanently stored # # in p[num].treatment = thing # p1 = g.get_player_by_id(1) # p1.participant.vars['treatment'] = treatment # p1.treatment = treatment # p2 = g.get_player_by_id(2) # p2.participant.vars['treatment'] = treatment # p2.treatment = treatment # else: # treatment = next(treatments) # p1 = g.get_player_by_id(1) # p1.participant.vars['treatment'] = treatment # p1.treatment = treatment # p2 = g.get_player_by_id(2) # p2.participant.vars['treatment'] = treatment # p2.treatment = treatment # else: # for g in self.get_groups(): # p1 = g.get_player_by_id(1) # treatment1 = p1.participant.vars['treatment'] # p1.treatment = treatment1 # p2 = g.get_player_by_id(2) # treatment2 = p2.participant.vars['treatment'] # p2.treatment = treatment2 class Group(BaseGroup): treatment = models.StringField(initial="Not Assigned Yet") request = models.CurrencyField( doc=""" Amount requested by this player. """, min=1, max=Constants.endowment, label="Please enter an amount from 0 to 100", ) accept = models.StringField( label=("Do you accept this division?"), choices=["Yes","No"], widget=widgets.RadioSelect, ) def set_treatment(self): print("treatments are being set") p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) #setting group's treatment print("player 1's treatment: ",p1.participant.vars['treatment']) print("player 2's treatment: ",p2.participant.vars['treatment']) self.treatment = p1.participant.vars['treatment'] print(self.treatment) #check that the players in the group are in the same treatment if p1.participant.vars['treatment'] == p2.participant.vars['treatment']: #make the group's treatment the same as the players #this prevents player treatment from changing across rounds #and also works with the request/accept functions being group level print("CORRECT: PLAYERS IN GROUP HAVE SAME TREATMENTS :)") else: print("ERROR: PLAYERS IN SAME GROUP HAVE DIFFERENT TREATMENTS :(") print("this group's treatment: ", self.treatment) def set_payoffs(self): p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) if p1.treatment == "negotiated" and self.accept == "Yes": p1.participant.payoff = p1.participant.payoff + self.request p2.participant.payoff = p2.participant.payoff + Constants.endowment - self.request elif p1.treatment == "negotiated" and self.accept == "No": p1.participant.payoff == p1.participant.payoff + 0 p2.participant.payoff == p2.participant.payoff + 0 else: p1.participant.payoff = p1.participant.payoff + self.request p2.participant.payoff = p2.participant.payoff + Constants.endowment - self.request print("exchange phase payoff has been reached") print("payoff from exchange for p1: ", self.request) print("payoff from exchange for p2:", Constants.endowment - self.request) print("total participant payoff for p1: ", p1.participant.payoff) print("total participant payoff for p2: ", p2.participant.payoff) class Player(BasePlayer): treatment = models.StringField(initial="")