How to use the otree.api.BaseGroup function in otree

To help you get started, we’ve selected a few otree examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oTree-org / oTree / public_goods_simple / models.py View on Github external
class Constants(BaseConstants):
    name_in_url = 'public_goods_simple'
    players_per_group = 3
    num_rounds = 1

    endowment = c(100)
    multiplier = 1.8


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    total_contribution = models.CurrencyField()
    individual_share = models.CurrencyField()

    def set_payoffs(self):
        players = self.get_players()
        contributions = [p.contribution for p in players]
        self.total_contribution = sum(contributions)
        self.individual_share = (
            self.total_contribution * Constants.multiplier / Constants.players_per_group
        )
        for p in players:
            p.payoff = Constants.endowment - p.contribution + self.individual_share


class Player(BasePlayer):
    contribution = models.CurrencyField(min=0, max=Constants.endowment)
github WZBSocialScienceCenter / otreeutils / otreeutils_example1 / models.py View on Github external
doc = """
Example 1 for usage of the otreeutils package.
"""


class Constants(BaseConstants):
    name_in_url = 'otreeutils_example1'
    players_per_group = None
    num_rounds = 1


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    pass


class Player(BasePlayer):
    understanding_questions_wrong_attempts = models.PositiveIntegerField()   # number of wrong attempts on understanding quesions page
github oTree-org / oTree / cournot / models.py View on Github external
name_in_url = 'cournot'
    players_per_group = 2
    num_rounds = 1

    instructions_template = 'cournot/instructions.html'

    # Total production capacity of all players
    total_capacity = 60
    max_units_per_player = int(total_capacity / players_per_group)


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):

    unit_price = models.CurrencyField()

    total_units = models.IntegerField(doc="""Total units produced by all players""")

    def set_payoffs(self):
        players = self.get_players()
        self.total_units = sum([p.units for p in players])
        self.unit_price = Constants.total_capacity - self.total_units
        for p in players:
            p.payoff = self.unit_price * p.units


class Player(BasePlayer):

    units = models.IntegerField(
github oTree-org / oTree / prisoner / models.py View on Github external
instructions_template = 'prisoner/instructions.html'

    # payoff if 1 player defects and the other cooperates""",
    betray_payoff = c(300)
    betrayed_payoff = c(0)

    # payoff if both players cooperate or both defect
    both_cooperate_payoff = c(200)
    both_defect_payoff = c(100)


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    def set_payoffs(self):
        for p in self.get_players():
            p.set_payoff()


class Player(BasePlayer):
    decision = models.StringField(
        choices=[['Cooperate', 'Cooperate'], ['Defect', 'Defect']],
        doc="""This player's decision""",
        widget=widgets.RadioSelect,
    )

    def other_player(self):
        return self.get_others_in_group()[0]

    def set_payoff(self):
github oTree-org / oTree / dictator / models.py View on Github external
class Constants(BaseConstants):
    name_in_url = 'dictator'
    players_per_group = 2
    num_rounds = 1

    instructions_template = 'dictator/instructions.html'

    # Initial amount allocated to the dictator
    endowment = c(100)


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    kept = models.CurrencyField(
        doc="""Amount dictator decided to keep for himself""",
        min=0,
        max=Constants.endowment,
    )

    def set_payoffs(self):
        p1 = self.get_player_by_id(1)
        p2 = self.get_player_by_id(2)
        p1.payoff = self.kept
        p2.payoff = Constants.endowment - self.kept


class Player(BasePlayer):
    pass
github oTree-org / oTree / traveler_dilemma / models.py View on Github external
adjustment_abs = c(2)

    # Player's deduction for the higher claim

    # The maximum claim to be requested
    max_amount = c(100)

    # The minimum claim to be requested
    min_amount = c(2)


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):

    lower_claim = models.CurrencyField()

    def set_payoffs(self):
        p1, p2 = self.get_players()
        if p1.claim == p2.claim:
            self.lower_claim = p1.claim
            for p in [p1, p2]:
                p.payoff = self.lower_claim
                p.adjustment = c(0)
        else:
            if p1.claim < p2.claim:
                winner = p1
                loser = p2
            else:
                winner = p2
github oTree-org / oTree / public_goods / models.py View on Github external
]
        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 Group(BaseGroup):
    total_contribution = models.CurrencyField()

    individual_share = models.CurrencyField()

    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


class Player(BasePlayer):
    contribution = models.CurrencyField(
        min=0, max=Constants.endowment, doc="""The amount contributed by the player"""
github oTree-org / oTree / volunteer_dilemma / models.py View on Github external
instructions_template = 'volunteer_dilemma/instructions.html'

    num_other_players = players_per_group - 1

    # """Payoff for each player if at least one volunteers"""
    general_benefit = c(100)

    # """Cost incurred by volunteering player"""
    volunteer_cost = c(40)


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    num_volunteers = models.IntegerField()

    def set_payoffs(self):
        players = self.get_players()
        self.num_volunteers = sum([p.volunteer for p in players])
        if self.num_volunteers > 0:
            baseline_amount = Constants.general_benefit
        else:
            baseline_amount = c(0)
        for p in players:
            p.payoff = baseline_amount
            if p.volunteer:
                p.payoff -= Constants.volunteer_cost


class Player(BasePlayer):
github oTree-org / oTree / trust_simple / models.py View on Github external
class Constants(BaseConstants):
    name_in_url = 'trust_simple'
    players_per_group = 2
    num_rounds = 1

    endowment = c(10)
    multiplier = 3

    instructions_template = 'trust_simple/instructions.html'


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    sent_amount = models.CurrencyField(
        min=c(0), max=Constants.endowment, doc="""Amount sent by P1"""
    )

    sent_back_amount = models.CurrencyField(doc="""Amount sent back by P2""")

    def sent_back_amount_choices(self):
        return currency_range(c(0), self.sent_amount * Constants.multiplier, c(1))

    def set_payoffs(self):
        p1 = self.get_player_by_id(1)
        p2 = self.get_player_by_id(2)
        p1.payoff = Constants.endowment - self.sent_amount + self.sent_back_amount
        p2.payoff = self.sent_amount * Constants.multiplier - self.sent_back_amount
github oTree-org / oTree / trust / models.py View on Github external
name_in_url = 'trust'
    players_per_group = 2
    num_rounds = 1

    instructions_template = 'trust/instructions.html'

    # Initial amount allocated to each player
    endowment = c(100)
    multiplier = 3


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    sent_amount = models.CurrencyField(
        min=0, max=Constants.endowment, doc="""Amount sent by P1"""
    )

    sent_back_amount = models.CurrencyField(doc="""Amount sent back by P2""", min=c(0))

    def sent_back_amount_max(self):
        return self.sent_amount * Constants.multiplier

    def set_payoffs(self):
        p1 = self.get_player_by_id(1)
        p2 = self.get_player_by_id(2)
        p1.payoff = Constants.endowment - self.sent_amount + self.sent_back_amount
        p2.payoff = self.sent_amount * Constants.multiplier - self.sent_back_amount