from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Maggie Lehr' doc = """ network trust game w/ proportionality discretion from distributor """ class Constants(BaseConstants): name_in_url = 'multilateral_decision_pni' players_per_group = 4 num_rounds = 30 endowment = c(100) multiplier = 3 correct_guess = c(40) instructions_template = 'multi_trust_simple/Instructions.html' class Subsession(BaseSubsession): dice = models.IntegerField() def creating_session(self): print('creating session') if self.round_number == 1: self.group_randomly(fixed_id_in_group=True) self.session.vars['matrix1'] = self.get_group_matrix() print('first group is', self.session.vars['matrix1']) if 1 < self.round_number: self.group_like_round(1) class Group(BaseGroup): total_sender_allocation = models.CurrencyField() kept_amount = models.CurrencyField(min=0) send_back_amount1 = models.CurrencyField(min=0) send_back_amount2 = models.CurrencyField(min=0) send_back_amount3 = models.CurrencyField(min=0) allocated_amount1 = models.CurrencyField() allocated_amount2 = models.CurrencyField() allocated_amount3 = models.CurrencyField() expected_1 = models.BooleanField() expected_2 = models.BooleanField() expected_3 = models.BooleanField() def set_payoffs(self): sender1 = self.get_player_by_role('sender 1') sender2 = self.get_player_by_role('sender 2') sender3 = self.get_player_by_role('sender 3') distributor = self.get_player_by_role('distributor') all_allocations = [p.sender_allocation for p in self.get_players()] sum_allocations = float(sum(all_allocations[0:3])) total_budget = self.total_sender_allocation * Constants.multiplier allocated_amount1 = float(all_allocations[0]) allocated_amount2 = float(all_allocations[1]) allocated_amount3 = float(all_allocations[2]) expectation_1 = sender1.sender_expectation expectation_2 = sender2.sender_expectation expectation_3 = sender3.sender_expectation for player in [distributor, sender1, sender2, sender3]: if self.subsession.round_number == self.session.vars['paying_round1']: distributor.payoff = Constants.endowment + self.kept_amount remainder1 = Constants.endowment - allocated_amount1 remainder2 = Constants.endowment - allocated_amount2 remainder3 = Constants.endowment - allocated_amount3 divide1 = allocated_amount1 / sum_allocations divide2 = allocated_amount2 / sum_allocations divide3 = allocated_amount3 / sum_allocations sender1.payoff = remainder1 + ((total_budget - self.kept_amount) * divide1) sender2.payoff = remainder2 + ((total_budget - self.kept_amount) * divide2) sender3.payoff = remainder3 + ((total_budget - self.kept_amount) * divide3) if (allocated_amount1 - 10) < expectation_1 < ( allocated_amount1 + 10): sender1.payoff += Constants.correct_guess else: pass if (allocated_amount2 - 10) < expectation_2 < ( allocated_amount2 + 10): sender2.payoff += Constants.correct_guess else: pass if (allocated_amount3 - 10) < expectation_3 < ( allocated_amount3 + 10): sender3.payoff += Constants.correct_guess else: pass ''' class PlayerBot(Bot): def play_round(self): yeild (pages.Introduction) yeild (pages.Send, {'sender_allocation': 20}) yeild (pages.SBSelfWaitPage) yield (pages.SBSelf, {'contribution': 10}) yeild (pages.SBRemainderWaitPage) yeild (pages.SBRemainder, {'send_back_amount1': 160}, {'send_back_amount2': 10}, {'send_back_amount3': 10}) yeild (pages.RoundResultsWaitPage) yeild (pages.RoundResults) yeild (pages.ResultsWaitPage) yield (pages.Results) ''' class Player(BasePlayer): sender_allocation = models.CurrencyField(min=0, max=Constants.endowment) sender_expectation = models.IntegerField(min=0, max=900) def role(self): if self.id_in_group == Constants.players_per_group: return 'distributor' return 'sender {}'.format(self.id_in_group)