from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) author = 'Wang Song' doc = """ Dictator Game """ class Constants(BaseConstants): name_in_url = 'dictator' players_per_group = 24 num_rounds = 1 dollar_per_ecu = 0.05 class Subsession(BaseSubsession): def creating_session(self): # use id_in_group to identify and match # stage 1: play as dictator # stage 2: play as dictatee for p in self.get_players(): if p.id_in_group == 24: p.stage1_dictatee_id = 1 else: p.stage1_dictatee_id = p.id_in_group + 1 if p.id_in_group == 1: p.stage2_dictator_id = 24 else: p.stage2_dictator_id = p.id_in_group - 1 class Group(BaseGroup): pass class Player(BasePlayer): stage1_keep = models.CurrencyField( choices=list(range(0, 51)), #widget=widgets.Slider, ) stage2_receive = models.CurrencyField() stage1_dictatee_id = models.IntegerField() stage2_dictator_id = models.IntegerField() def set_payoff(self): p = self.group.get_player_by_id(self.stage2_dictator_id) self.stage2_receive = 50 - p.stage1_keep self.payoff = (self.stage1_keep + self.stage2_receive)*Constants.dollar_per_ecu self.participant.vars['game1_payoff'] = self.payoff self.participant.payoff = self.payoff