from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer) import random import numpy #from django.db import models # Create your models here. author = 'Nishtha' doc = """ Dynamic Contest - Treatment : Part 2 """ class Constants(BaseConstants): name_in_url = 'contest_treatment1' players_per_group = 2 num_rounds = 2 endowment = 100 old_multiplier = 3 new_multiplier = 1 Prize = 100 class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: self.group_randomly() list = range(1, Constants.num_rounds+1) paying_round = random.sample(list, 1) self.session.vars['paying_round_1'] = paying_round #self.session.vars['paying_round_2'] = paying_round[1] else: self.group_randomly(fixed_id_in_group=True) class Group(BaseGroup): total_investment = models.FloatField() effective_investment_A = models.FloatField() effective_investment_B = models.FloatField() total_effective_investment = models.FloatField() pwin_A = models.FloatField() pwin_B = models.FloatField() chance_A = models.FloatField() chance_B = models.FloatField() Winner_A = models.BooleanField() effective_investment_A_2 = models.FloatField() effective_investment_B_2 = models.FloatField() total_effective_investment_2 = models.FloatField() pwin_A_2 = models.FloatField() pwin_B_2 = models.FloatField() chance_A_2 = models.FloatField() chance_B_2 = models.FloatField() Winner_A_2 = models.BooleanField() def set_payoffs_1(self): A = self.get_player_by_id(1) B = self.get_player_by_id(2) self.effective_investment_A = Constants.old_multiplier * A.investment self.effective_investment_B = B.investment self.total_investment = A.investment + B.investment self.total_effective_investment = self.effective_investment_A + self.effective_investment_B self.pwin_A = (self.effective_investment_A / self.total_effective_investment) self.pwin_B = (self.effective_investment_B / self.total_effective_investment) self.Winner_A = numpy.random.binomial(1, self.pwin_A, size=None) self.chance_A = round(100 * self.pwin_A, 1) self.chance_B = round(100 * self.pwin_B, 1) if self.Winner_A: A.is_winner = True B.is_winner = False else: A.is_winner = False B.is_winner = True for player in [A, B]: if player.is_winner: player.payoff1 = 200 - player.investment else: player.payoff1 = 150 - player.investment def set_payoffs_2(self): A = self.get_player_by_id(1) B = self.get_player_by_id(2) if self.Winner_A: self.effective_investment_A_2 = Constants.old_multiplier * A.investment_2 else: self.effective_investment_A_2 = A.investment_2 self.effective_investment_B_2 = B.investment_2 self.total_effective_investment_2 = self.effective_investment_A_2 + self.effective_investment_B_2 self.pwin_A_2 = (self.effective_investment_A_2 / self.total_effective_investment_2) self.pwin_B_2 = (self.effective_investment_B_2 / self.total_effective_investment_2) self.Winner_A_2 = numpy.random.binomial(1, self.pwin_A, size=None) self.chance_A_2 = round(100 * self.pwin_A_2, 1) self.chance_B_2 = round(100 * self.pwin_B_2, 1) if self.Winner_A_2: A.is_winner_2 = True B.is_winner_2 = False else: A.is_winner_2 = False B.is_winner_2 = True for player in [A, B]: if player.is_winner_2: player.payoff2 = 200 - player.investment_2 else: player.payoff2 = 150 - player.investment_2 def set_payoffs_total(self): A = self.get_player_by_id(1) B = self.get_player_by_id(2) for player in [A, B]: player.payoff_total = player.payoff1 + player.payoff2 def set_payoffs(self): A = self.get_player_by_id(1) B = self.get_player_by_id(2) if self.subsession.round_number == self.session.vars['paying_round_1']: A.payoff = A.payoff_total B.payoff = B.payoff_total else: A.payoff = 0 B.payoff = 0 class Player(BasePlayer): investment = models.FloatField( min=0, max=Constants.endowment, doc="""Bid in contest""" ) Perceived_investment = models.FloatField( min=0, max=Constants.endowment, doc="""Perceived bid of other player in contest""" ) payoff1 = models.CurrencyField() payoff2 = models.CurrencyField() payoff_total = models.CurrencyField() investment_2 = models.FloatField( min=0, max=Constants.endowment, doc="""Bid in Pd2""" ) Perceived_investment_2 = models.FloatField( min=0, max=Constants.endowment, doc="""Perceived bid of other player in Pd2""" ) level = models.IntegerField( choices=[[1, "Type A"], [2, "Type B"]], widget=widgets.RadioSelectHorizontal ) level2 = models.IntegerField( choices=[[1, "Type A"], [2, "Type B"]], widget=widgets.RadioSelectHorizontal ) rounds = models.IntegerField() periods = models.IntegerField() typechange = models.IntegerField( choices=[[1, "Yes"], [2, "No"]], widget=widgets.RadioSelectHorizontal ) codepena = models.IntegerField( choices=[1, 3], widget=widgets.RadioSelectHorizontal ) codepenb = models.IntegerField( choices=[1, 3], widget=widgets.RadioSelectHorizontal ) codepenc = models.IntegerField( choices=[1, 3], widget=widgets.RadioSelectHorizontal ) codepend = models.IntegerField( choices=[1, 3], widget=widgets.RadioSelectHorizontal ) codepene = models.IntegerField( choices=[1, 3], widget=widgets.RadioSelectHorizontal ) codepenf = models.IntegerField( choices=[1, 3], widget=widgets.RadioSelectHorizontal ) taska = models.IntegerField() taskb = models.IntegerField() guessa = models.IntegerField() guessb = models.IntegerField() slidera = models.IntegerField() sliderb = models.IntegerField() demo1 = models.IntegerField() demo2 = models.IntegerField() is_winner = models.BooleanField() is_winner_2 = models.BooleanField() is_guess = models.BooleanField() def role(self): if self.id_in_group == 1: return 'A' if self.id_in_group == 2: return 'B' def other_player(self): return self.get_others_in_group()[0]