How to use the otree.api.models.CurrencyField 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
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 oTree-org / oTree / trust / models.py View on Github external
# 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


class Player(BasePlayer):
    def role(self):
        return {1: 'A', 2: 'B'}[self.id_in_group]
github oTree-org / oTree / public_goods / models.py View on Github external
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 / common_value_auction / models.py View on Github external
def creating_session(self):
        for g in self.get_groups():
            import random

            item_value = random.uniform(
                Constants.min_allowable_bid, Constants.max_allowable_bid
            )
            g.item_value = round(item_value, 1)


class Group(BaseGroup):
    item_value = models.CurrencyField(
        doc="""Common value of the item to be auctioned, random for treatment"""
    )

    highest_bid = models.CurrencyField()

    def set_winner(self):
        import random

        players = self.get_players()
        self.highest_bid = max([p.bid_amount for p in players])

        players_with_highest_bid = [
            p for p in players if p.bid_amount == self.highest_bid
        ]
        winner = random.choice(
            players_with_highest_bid
        )  # if tie, winner is chosen at random
        winner.is_winner = True
        for p in players:
            p.set_payoff()
github oTree-org / oTree / traveler_dilemma / models.py View on Github external
winner.adjustment = Constants.adjustment_abs
            loser.adjustment = -Constants.adjustment_abs
            winner.payoff = self.lower_claim + winner.adjustment
            loser.payoff = self.lower_claim + loser.adjustment


class Player(BasePlayer):
    claim = models.CurrencyField(
        min=Constants.min_amount,
        max=Constants.max_amount,
        doc="""
        Each player's claim
        """,
    )

    adjustment = models.CurrencyField()

    def other_player(self):
        return self.get_others_in_group()[0]
github oTree-org / oTree / public_goods / models.py View on Github external
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 / traveler_dilemma / models.py View on Github external
# 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
                loser = p1
            self.lower_claim = winner.claim
github oTree-org / oTree / bargaining / models.py View on Github external
class Group(BaseGroup):
    total_requests = models.CurrencyField()

    def set_payoffs(self):
        players = self.get_players()
        self.total_requests = sum([p.request for p in players])
        if self.total_requests <= Constants.amount_shared:
            for p in players:
                p.payoff = p.request
        else:
            for p in players:
                p.payoff = c(0)


class Player(BasePlayer):
    request = models.CurrencyField(
        doc="""
        Amount requested by this player.
        """,
        min=0,
        max=Constants.amount_shared,
    )

    def other_player(self):
        return self.get_others_in_group()[0]
github oTree-org / oTree / trust_simple / models.py View on Github external
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


class Player(BasePlayer):
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)