from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import time doc = """ This is a one-shot "Prisoner's Dilemma". Two players are asked separately whether they want to cooperate or defect. Their choices directly determine the payoffs. """ class Constants(BaseConstants): name_in_url = 'prisoner' players_per_group = 2 conversion_rate = 1/150 # $0.006 for every point scored in Dal Bo and Frechette AER 2011 instructions_template = 'prisoner/instructions.html' time_limit = False time_limit_seconds = 3600 # time limit for session (in seconds) since first round of first match (3600 in Dal Bo and Frechette AER 2011) # payoff if 1 player defects and the other cooperates""", C1 = 0 C2 = 2 C3 = 4 B1 = 1.5 B2 = 2 B3 = 2.5 B4 = 4 num_rounds = 15 class Subsession(BaseSubsession): match_number = models.IntegerField() round_in_match_number = models.IntegerField() def get_sorted_players_by_opponent(self): result = [] players = self.get_players() seen = set() for player in players: if player.participant.id_in_session in seen: continue opponent = player.get_others_in_group()[0] result.append(player) result.append(opponent) seen.add(player.participant.id_in_session) seen.add(opponent.participant.id_in_session) return result def get_all_moves(self): bids = [] #2D array with one array of all bids per round payoffs = [] for player in self.get_sorted_players_by_opponent(): for round in range(self.session.config['rounds']): if len(bids) <= round: bids.append([player.participant.vars['bids'][round]]) payoffs.append([player.participant.vars['payoffs'][round]]) else: bids[round].append(player.participant.vars['bids'][round]) payoffs[round].append(player.participant.vars['payoffs'][round]) return dict(bids=bids, payoffs=payoffs) class Group(BaseGroup): pass class Player(BasePlayer): name = models.StringField() decision1 = models.StringField( choices=['C1', 'C2', 'C3'], doc="""This player's decision""", widget=widgets.RadioSelect ) decision2 = models.StringField( choices=['B1', 'B2', 'B3', 'B4'], doc="""This player's decision""", widget=widgets.RadioSelect ) def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): dec1 = {'C1': 0, 'C2': 2, 'C3': 4} dec2 = {'B1': 1.5, 'B2': 2, 'B3': 2.5, 'B4': 4} print(dec1[self.other_player().decision1]) print(dec2[self.other_player().decision2]) self.payoff = dec1[self.other_player().decision1] * dec2[self.other_player().decision2] print(self.payoff)