# -*- coding: utf-8 -*- # from __future__ import division from otree.db import models from otree.constants import BaseConstants from otree.models import BaseSubsession, BaseGroup, BasePlayer from otree import widgets from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random # 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. """ source_code = "https://github.com/oTree-org/oTree/tree/master/prisoner" bibliography = () links = { "Wikipedia": { "Prisoner's Dilemma": "https://en.wikipedia.org/wiki/Prisoner%27s_dilemma" } } keywords = ("Prisoner's Dilemma",) class Constants(BaseConstants): name_in_url = 'TreatmentA' players_per_group = 2 num_rounds = 12 # Points made if player defects and the other cooperates""", down_up_amount = c(0) # Points made if both players cooperate up_amount = c(350) up_down_amount = c(150) down_amount = c(300) center_amount = c(325) center_up_amount = c(325) center_down_amount = c(575) down_center_amount = c(25) up_center_amount = c(350) base_points = c(0) training_1_choices = [ 'You get 11 points, the other individual gets 12 points', 'You get 12 points, the other individual gets 11 points', 'You get 15 points, the other individual gets 16 points', 'You get 16 points, the other individual gets 15 points' ] training_1_correct = training_1_choices[0] class Subsession(BaseSubsession): def creating_session(self): if self.round_number >1: players = self.get_players() random.shuffle(players) group_matrix = [] for i in range(0, len(players), Constants.players_per_group): group_matrix.append(players[i:i+Constants.players_per_group]) self.set_groups(group_matrix) if self.round_number == 1: paying_round = random.randint(1, Constants.num_rounds) self.session.vars['paying_round'] = paying_round class Group(BaseGroup): pass class Player(BasePlayer): training_question_1 = models.CharField( choices=Constants.training_1_choices, widget=widgets.RadioSelect(), #timeout_default=Constants.training_1_choices[1] ) def is_training_question_1_correct(self): return self.training_question_1 == Constants.training_1_correct decision = models.CharField( choices=['up', 'center', 'down'], doc="""This player's decision""", widget=widgets.RadioSelect() ) def other_player(self): return self.get_others_in_group()[0] round_result = models.CurrencyField() def set_payoff(self): points_matrix = {'up': {'up': Constants.up_amount, 'center': Constants.up_center_amount, 'down': Constants.up_down_amount}, 'center': {'up': Constants.center_up_amount, 'center': Constants.center_amount, 'down': Constants.center_down_amount}, 'down': {'up': Constants.down_up_amount, 'center': Constants.down_center_amount, 'down': Constants.down_amount}} self.round_result = (points_matrix[self.decision] [self.other_player().decision]) if self.subsession.round_number == 8: self.payoff = self.round_result else: self.payoff = c(0) def set_globals(self): self.session.vars['round_result'] = [p.round_result for p in self.in_all_rounds()]