from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ This is two-player Public Goods Game made for Daisuke Nakama in March 2020. In Hetero condition, one participant in a group is given 40yen and the other is given 20yen. They usually keep playing with each other until the last round; but in One-shot condition, they change who to play with every round (the 40-yen-participant is pared up with another 20-yen-participant). """ class Constants(BaseConstants): name_in_url = 'Nakama2020_public_goods' players_per_group = 2 num_rounds = 3 # """Amount allocated to each player""" endowment_1 = c(40) endowment_2 = c(20) multiplier = 1.5 class Subsession(BaseSubsession): '''One-shotでのみ使用 def creating_session(self): self.group_randomly(fixed_id_in_group=False) ''' contribution1_average = models.CurrencyField() contribution2_average = models.CurrencyField() contribution1_average_history = models.CurrencyField() contribution2_average_history = models.CurrencyField() # 参加者がいなくなったとき、その参加者が居続けているように見せたい。ここではその参加者が過去の全体平均の金額を出し続けるよう設定する。 def contributions_average(self): groups = self.get_groups() all_rounds = self.round_number self.contribution1_average = sum([g.contribution1 for g in groups]) / len(groups) self.contribution2_average = sum([g.contribution2 for g in groups]) / len(groups) self.contribution1_average_history = sum([r.contribution1_average for r in self.in_all_rounds()]) / all_rounds self.contribution2_average_history = sum([r.contribution2_average for r in self.in_all_rounds()]) / all_rounds class Group(BaseGroup): treatment = models.StringField() contribution1 = models.CurrencyField( choices=currency_range(c(0), c(40), c(10)), doc="The amount contributed by P1", ) contribution2 = models.CurrencyField( choices=currency_range(c(0), c(20), c(10)), doc="The amount contributed by P2", ) total_contribution = models.CurrencyField() total_earnings = models.CurrencyField() individual_share = models.CurrencyField() def set_payoffs(self): self.total_contribution = self.contribution1 + self.contribution2 self.total_earnings = self.total_contribution * Constants.multiplier self.individual_share = self.total_earnings / Constants.players_per_group p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) p1.payoff = (Constants.endowment_1 - self.contribution1) + self.individual_share p2.payoff = (Constants.endowment_2 - self.contribution2) + self.individual_share class Player(BasePlayer): totalpayoff = models.CurrencyField() skip = models.BooleanField( choices=[ [False, 'Continued'], [True, 'Skipped'], ] ) quiz1 = models.StringField( choices=['0コイン', '200コイン', '500コイン', '800コイン', '1000コイン'], label='下から正解を選んでください.', widget=widgets.RadioSelect)