from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Priyama Majumdar' doc = """ Modified Public Goods Game """ class Constants(BaseConstants): name_in_url = 'exp2' players_per_group = 4 num_rounds = 21 initial_endowment = 200 alpha = 0.4 class Subsession(BaseSubsession): def creating_session(self): num_players = len(self.get_players()) num_groups = int(num_players/4) self.group_randomly() #Grouping changes every round list_types = ['P1', 'P2', 'P3', 'P4'] for i in range(num_groups): current_group = self.get_group_matrix()[i] random.shuffle(list_types) for j in range(Constants.players_per_group): current_group[j].alias = list_types[j] class Group(BaseGroup): def set_payoffs(self): total_public_pot = sum([p.group_contribution for p in self.get_players()]) for p in self.get_players(): guess = (total_public_pot - p.group_contribution)/3 p.private_contri = Constants.initial_endowment - p.group_contribution p.common_invest = int(Constants.alpha*total_public_pot) p.noguess = p.private_contri + p.common_invest if p.card_number >= guess - 5 and p.card_number <= guess+5: p.round_payoff = p.noguess +10 else: p.round_payoff = p.noguess class Player(BasePlayer): def set_final_payoff(self): self.paying_round = random.randint(1,2) all_rounds_payoffs = [p.round_payoff for p in self.in_all_rounds()] self.payoff = all_rounds_payoffs[self.paying_round-1] + all_rounds_payoffs[1] noguess = models.IntegerField() common_invest = models.IntegerField() card_number = models.IntegerField(min=0,max=200, label="How much do you think others will contribute on an average?You will receieve a bonus of Rs.10 if your guess is correct or within five rupees of the average contribution made by the other players.") paying_round = models.IntegerField() alias = models.StringField() group_contribution = models.IntegerField(min=0, max=200, label="Amount you want to put in Group Account") private_contri = models.IntegerField() round_payoff = models.IntegerField()