from otree.api import *
doc = """
We allow firms the choice whether or not to join a cartel.
This decision is made prior to firms independently deciding on quantity.
If a cartel is formed, a chat window is formed allowing for free chat during
the decision period.
"""
class C(BaseConstants):
NAME_IN_URL = 'CartelFines'
# HISTORY_TEMPLATE = 'CartelFines/history.html'
NUM_ROUNDS = 1
PLAYERS_PER_GROUP = 3
INFORMATION_TEMPLATE = 'cartel_decision/instructions_comm.html'
CARTEL_INFO = 'cartel_decision/cartel_info.html'
class Subsession(BaseSubsession):
cartel_formed = models.BooleanField(initial=False)
#def creating_session(self):
# Randomly group players (change to balanced groups later)
# self.group_randomly()
class Group(BaseGroup):
yes_votes_cast = models.IntegerField(initial=0)
# Define the votes needed, including field_maybe_none to allow for None values
votes_needed = models.IntegerField(default=3, field_maybe_none=False)
class Player(BasePlayer):
join_cartel = models.BooleanField(widget=widgets.RadioSelect, choices=[
(True, 'Yes, I want to join the cartel'),
(False, 'No, I do not want to join the cartel')
])
in_cartel = models.BooleanField(initial=False)
vote_submitted = models.BooleanField(initial=False)
def is_cartel_formed(self):
return self.group.subsession.cartel_formed
# FUNCTIONS
"""
def creating_session(session):
# generate balanced groups based on the session config
groups = make_balanced_groups(
players=session.get_participants(),
treatment_list=['treatment1', 'treatment2', 'treatment3']
)
# assign treatments to each participant
for group in groups:
for p in group:
p.participant.vars['treatment'] = group.treatment
# set the treatment in the session fields
session.vars['treatment'] = groups[0].treatment/*
"""
# PAGES
class CartelVote(Page):
form_model = 'player'
form_fields = ['join_cartel']
timeout_seconds = 60
def vars_for_template(self):
return {
'votes_needed': self.group.field_maybe_none('votes_needed'),
'player_in_cartel': self.in_cartel,
}
def before_next_page(self, timeout_happened):
if timeout_happened:
self.in_cartel = False
self.vote_submitted = True
# self.save()
return
if self.join_cartel:
self.in_cartel = True
self.group.yes_votes_cast += 1
self.vote_submitted = True
else:
self.vote_submitted = True
# self.group.save()
# self.save()
if self.group.yes_votes_cast == self.group.votes_needed:
self.group.subsession.cartel_formed = True
# self.group.save()
def is_displayed(self):
return not self.field_maybe_none('vote_submitted') and not self.field_maybe_none('in_cartel') and not self.group.subsession.cartel_formed
class ResultsWaitPage(WaitPage):
pass
class Results(Page):
@staticmethod
def vars_for_template(self):
group_id = self.id_in_group
return {
'group_id': group_id,
}
page_sequence = [CartelVote, ResultsWaitPage, Results]