from otree.api import *
c = cu
doc = ''
class Constants(BaseConstants):
name_in_url = 'E1_pgg'
players_per_group = 3
num_rounds = 20
multiplier = 2
endowment = 6
thanks = 4.3
admin_report_template = 'E1_pgg/admin_report.html'
instructions_template = 'E1_pgg/instructions.html'
def vars_for_admin_report(subsession):
session = subsession.session
contributions = [p.contribution for p in subsession.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 Subsession(BaseSubsession):
pass
def set_payoffs(group):
players = group.get_players()
#グループ全員の公共の金庫への貢献量
group.total_contribution = sum([p.contribution for p in players])
#個人への分配
group.individual_share = round((group.total_contribution * Constants.multiplier / Constants.players_per_group),2)
for p in players:
#個人の金庫+分配 #ゲーム後の個人の収益
p.pay = round(((Constants.endowment - p.contribution) + group.individual_share),2)
#●回ゲーム後の個人の収益
if p.round_number > 1:
prev_player = p.in_round(p.round_number - 1)
prev_player.total += p.pay
p.total = round((prev_player.total),2)
if p.round_number == 1:
p.total = p.pay
#謝礼金
import math
p.thanks =math.ceil(p.total * Constants.thanks) + 300
class Group(BaseGroup):
total_contribution = models.IntegerField(initial=0)
individual_share = models.FloatField(initial=0)
class Player(BasePlayer):
contribution = models.IntegerField(label='6トークンのうち『グループの口座』に何トークン投資しますか?
(残りのトークンは全て『あなたの口座』に入れられます。)
0~6で入力してください。
 ', max=Constants.endowment, min=0)
pay = models.FloatField()
total = models.FloatField(initial=0)
thanks = models.IntegerField()
def custom_export(players):
import datetime
yield ['session', 'participant_code', 'group', 'id_in_group','participant_current_page',
'participant_index_in_pages', 'participant_max_page_index', 'round_number', 'total_token','thanks',
'contribution', 'group_total_contribution', 'group_individual_share', 'each_token'
]
for p in players:
pp = p.participant
session = p.session
yield [session.code, pp.code, p.group.id_in_subsession, p.id_in_group, pp._current_page_name,
pp._index_in_pages, pp._max_page_index, p.round_number, p.total, p.thanks,
p.contribution, p.group.total_contribution, p.group.individual_share, p.pay,
]
class MyWaitPage(WaitPage):
wait_for_all_groups = True
body_text = 'しばらくお待ちください'
class Contribute(Page):
form_model = 'player'
form_fields = ['contribution']
timeout_seconds = 75
class WaitPage1(WaitPage):
after_all_players_arrive = set_payoffs
title_text = 'しばらくお待ちください'
body_text = '他の参加者の入力が終わるまでお待ちください。'
class Results(Page):
form_model = 'player'
timeout_seconds = 15
@staticmethod
def vars_for_template(player):
group = player.group
return dict(total_earnings=group.total_contribution * Constants.multiplier)
class Last(Page):
form_model = 'player'
@staticmethod
def is_displayed(player):
return player.round_number == Constants.num_rounds
page_sequence = [MyWaitPage, Contribute, WaitPage1, Results, Last]