from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'inventorygame' players_per_group = None num_rounds = 1 cost = 2 price = 10 class Subsession(BaseSubsession): def vars_for_admin_report(self): sorted_players = sorted(self.get_players(), key=lambda player: player.payoff, reverse=True) return {'sorted_players': sorted_players} class Group(BaseGroup): demand_1 = models.IntegerField() demand_2 = models.IntegerField() demand_3 = models.IntegerField() max_profit = models.CurrencyField() num_winners = models.IntegerField() def calc_profit(self): self.demand_1 = len([p.poster for p in self.get_players() if p.poster == 1]) self.demand_2 = len([p.poster for p in self.get_players() if p.poster == 2]) self.demand_3 = len([p.poster for p in self.get_players() if p.poster == 3]) self.max_profit = -10000 for p in self.get_players(): p.profit_1 = Constants.price * min(self.demand_1, p.qty_1) - Constants.cost * p.qty_1 p.profit_2 = Constants.price * min(self.demand_2, p.qty_2) - Constants.cost * p.qty_2 p.profit_3 = Constants.price * min(self.demand_3, p.qty_3) - Constants.cost * p.qty_3 p.payoff = p.profit_1 + p.profit_2 + p.profit_3 if p.payoff > self.max_profit: self.max_profit = p.payoff winners = [p for p in self.get_players() if p.payoff == self.max_profit] self.num_winners = len(winners) for p in winners: p.is_winner = True class Player(BasePlayer): poster = models.IntegerField( choices=[[1, 'Poster 1'], [2, 'Poster 2'], [3, 'Poster 3']], label='Which is your favorite poster among the three posters shown above?', widget=widgets.RadioSelect) qty_1 = models.IntegerField(min=0, label='How many copies of poster 1 would you like to purchase and keep in stock?') qty_2 = models.IntegerField(min=0, label='How many copies of poster 2 would you like to purchase and keep in stock?') qty_3 = models.IntegerField(min=0, label='How many copies of poster 3 would you like to purchase and keep in stock?') profit_1 = models.CurrencyField() profit_2 = models.CurrencyField() profit_3 = models.CurrencyField() payoff = models.CurrencyField() is_winner = models.BooleanField()