from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ This is a four-period vaccination game with 6 players. """ class Constants(BaseConstants): name_in_url = 'vac_game_B' players_per_group = 5 num_rounds = 6 a_events_rate = 0.3 cost_of_a_events = c(30) fixed_cost_of_vaccine = c(5) cost_of_illness = c(45) instructions_template = 'vaccination_B_low_cost/instructions.html' # """Amount allocated to each player""" endowment = c(200) class Subsession(BaseSubsession): pass class Group(BaseGroup): no_vaccinated = models.IntegerField() prob_catching_illness = models.FloatField() def set_random_vars(self): players = self.get_players() for p in players: if p.id_in_group == 1: p.rvar = random.random() if p.id_in_group == 2: p.rvar = random.random() if p.id_in_group == 3: p.rvar = random.random() if p.id_in_group == 4: p.rvar = random.random() if p.id_in_group == 5: p.rvar = random.random() if p.id_in_group == 6: p.rvar = random.random() if p.id_in_group == 7: p.rvar = random.random() if p.id_in_group == 8: p.rvar = random.random() def work_out_no_vaccinated(self): players_who_vaccinated = [p for p in self.get_players() if p.decision == 'Get Vaccinated'] self.no_vaccinated = len(players_who_vaccinated) def work_out_prob_catching_illness(self): if self.no_vaccinated >= (2/3)*Constants.players_per_group: self.prob_catching_illness = 0 if self.no_vaccinated == 3: self.prob_catching_illness = 0.25 if self.no_vaccinated == 2: self.prob_catching_illness = 0.4 if self.no_vaccinated == 1: self.prob_catching_illness = 0.5 if self.no_vaccinated == 0: self.prob_catching_illness = 0.55 def set_payoffs(self): players = self.get_players() for p in players: if p.outcome == 'suffered adverse events': p.payoff = Constants.endowment - Constants.fixed_cost_of_vaccine - Constants.cost_of_a_events elif p.outcome == 'did not suffer adverse events': p.payoff = Constants.endowment - Constants.fixed_cost_of_vaccine elif p.outcome == 'caught the flu': p.payoff = Constants.endowment - Constants.cost_of_illness elif p.outcome == 'did not catch the flu': p.payoff = Constants.endowment def set_outcomes(self): players = self.get_players() for p in players: if p.decision == 'Get Vaccinated' and p.rvar <= Constants.a_events_rate: p.outcome = 'suffered adverse events' elif p.decision == 'Get Vaccinated' and p.rvar > Constants.a_events_rate: p.outcome = 'did not suffer adverse events' elif p.decision == 'Do Not Get Vaccinated' and p.rvar <= self.prob_catching_illness: p.outcome = 'caught the flu' elif p.decision == 'Do Not Get Vaccinated' and p.rvar > self.prob_catching_illness: p.outcome = 'did not catch the flu' def set_cumulative_payoffs(self): players = self.get_players() for p in players: p.cumulative_payoff = sum([p.payoff for p in self.player.in_all_rounds()]) class Player(BasePlayer): decision = models.StringField( choices=['Get Vaccinated', 'Do Not Get Vaccinated'] ) outcome = models.StringField() rvar = models.FloatField() cumulative_payoff = models.IntegerField()