from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ Игра Общественное благо. """ class Constants(BaseConstants): name_in_url = 'public_goods_not_anon' players_per_group = 3 num_rounds = 5 instructions_template = 'public_goods_not_anon/Instructions.html' # """Amount allocated to each player""" endowment = 50 multiplier = 1.5 class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: paying_round = random.randint(1, Constants.num_rounds) self.session.vars['paying_round'] = paying_round def vars_for_admin_report(self): contributions = [p.contribution for p in self.get_players() if p.contribution != None] if contributions: return { 'avg_contribution': sum(contributions)/len(contributions), 'min_contribution': min(contributions), 'max_contribution': max(contributions), } else: return { 'avg_contribution': '(no data)', 'min_contribution': '(no data)', 'max_contribution': '(no data)', } class Group(BaseGroup): total_contribution = models.IntegerField() individual_share = models.IntegerField() 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(): p.payoff = (Constants.endowment - p.contribution) + self.individual_share def execute_punishment(self): first = self.get_player_by_id(1) second = self.get_player_by_id(2) third = self.get_player_by_id(3) first.punished_by_P2 = second.punish_P1 first.punished_by_P3 = third.punish_P1 second.punished_by_P1 = first.punish_P2 second.punished_by_P3 = third.punish_P2 third.punished_by_P1 = first.punish_P3 third.punished_by_P2 = second.punish_P3 for p in self.get_players(): p.payoff = p.payoff - (p.punished_by_P1 + p.punished_by_P2 + p.punished_by_P3 + (p.punish_P1 + p.punish_P2 + p.punish_P3)/2 ) class Player(BasePlayer): contribution = models.IntegerField( min=0, max=Constants.endowment, doc="""Взнос игрока""", ) punished_by_P1 = models.IntegerField(initial=0) punished_by_P2 = models.IntegerField(initial=0) punished_by_P3 = models.IntegerField(initial=0) punish_P1 = models.IntegerField(initial=0, verbose_name='Наказать игрока 1') punish_P2 = models.IntegerField(initial=0, verbose_name='Наказать игрока 2') punish_P3 = models.IntegerField(initial=0, verbose_name='Наказать игрока 3')