from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) doc = """ This is a five-period public goods game with 4 players. """ class Constants(BaseConstants): name_in_url = 'public_goods_jp' players_per_group = 4 num_rounds = 5 instructions_template = 'public_goods_jp/instructions.html' # """Amount allocated to each player""" endowment = c(5) multiplier = 2 class Subsession(BaseSubsession): def vars_for_admin_report(self): contributions = [ p.contribution for p in self.get_players() if p.contribution != None ] if contributions: return dict( avg_contribution=sum(contributions) / len(contributions), min_contribution=min(contributions), max_contribution=max(contributions), ) else: return dict( avg_contribution='(no data)', min_contribution='(no data)', max_contribution='(no data)', ) class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() def set_payoffs(self): self.total_contribution = sum([p.contribution for p in self.get_players()]) self.individual_share = ( self.total_contribution * Constants.multiplier / Constants.players_per_group ) for p in self.get_players(): if p.round_number != 1: p.before_payoff = p.in_round(p.round_number-1).payoff if p.round_number == 1: p.payoff = (Constants.endowment - p.contribution) + self.individual_share else: p.payoff = (p.before_payoff- p.contribution) + self.individual_share if p.id_in_group == 1 or p.id_in_group == 2: p.last_payoff = p.payoff*0.3 else: p.last_payoff = p.payoff class Player(BasePlayer): contribution = models.CurrencyField( min=0,doc="""The amount contributed by the player""", label="プロジェクトにどれだけ支出しますか?" ) last_payoff = models.CurrencyField() before_total_payoff = models.CurrencyField() def contribution_max(self): if self.round_number ==1: return (5) else: return (self.in_round(self.round_number-1).payoff) total_payoff = models.CurrencyField() last_payoff = models.CurrencyField() before_payoff = models.CurrencyField() giveA = models.CurrencyField(label="プレイヤーAへいくら与えるか記入してください。") giveB = models.CurrencyField(label="プレイヤーBへいくら与えるか記入してください。") giveC = models.CurrencyField(label="プレイヤーCへいくら与えるか記入してください。")