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_pd' players_per_group = None num_rounds = 1 # payoff if 1 player defects and the other cooperates""", betrayer_payoff = c(4) betrayed_payoff = c(0) # payoff if both players cooperate or both defect both_cooperate_payoff = c(2) both_defect_payoff = c(1) #instructions template for PD pdinstructions = 'diss_exp/pdinstructions_template.html' class Subsession(BaseSubsession): def creating_session(self): pass class Group(BaseGroup): # kept = models.CurrencyField( # doc="""Amount dictator decided to keep for himself""", # min=0, # max=Constants.endowment, # label="I will keep", # ) def set_payoffs(self): for p in self.get_players(): p.set_payoff() # def set_payoffs(self): # p1 = self.get_player_by_id(1) # p2 = self.get_player_by_id(2) # p1.payoff = self.kept # p2.payoff = Constants.endowment - self.kept class Player(BasePlayer): #fields for the survey gender = models.StringField( choices=["Man","Woman","Other","Would Rather Not Say"], widget=widgets.RadioSelect ) age = models.IntegerField() race = models.StringField( choices=["White", "Black or African American", "American Indian or Alaskan Native", "Asian", "Native Hawaiian and Other Pacific Islander", "Biracial or Multiracial", "Would Rather Not Say"], widget=widgets.RadioSelect ) #fields for PD treatment = models.StringField() ## the choices an agent can make decision = models.StringField( choices=[['Cooperate', 'Cooperate'], ['Defect', 'Defect']], doc="""This player's decision""", widget=widgets.RadioSelect, ) def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): #players' treatment treatment = self.treatment id_in_group = self.id_in_group #initializing payoffs both_cooperate_payoff = Constants.both_cooperate_payoff betrayed_payoff = Constants.betrayed_payoff betrayer_payoff = Constants.betrayer_payoff both_defect_payoff = Constants.both_defect_payoff #payoffs based on decisions of both payoff_matrix = dict( Cooperate=dict( Cooperate=both_cooperate_payoff, Defect=betrayed_payoff, ), Defect=dict( Cooperate=betrayer_payoff, Defect=both_defect_payoff ), ) #payoff from PD payoff_PD = payoff_matrix[self.decision][self.other_player().decision] self.participant.payoff = self.participant.payoff + payoff_PD print("pd phase payoff has been reached") print("payoff from PD: ", payoff_PD) print("total participant payoff from PD: ", self.participant.payoff) #self.participant.total_earnings = self.participant.total_earnings + payoff_PD