from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random from django import forms from django.forms.widgets import NumberInput from django.db import models as djmodels author = 'Jian S.' doc = """ eg_contest_without punishment """ class Constants(BaseConstants): name_in_url = 'eg_contest_20191106' players_per_group = None players_per_round = 12 num_rounds = 21 id_in_group = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) endowment = c(1000) v = c(1000) class Subsession(BaseSubsession): def creating_session(self): print ('in creating session') if self.round_number == 1: #paying_round = random.randint(2, Constants.num_rounds) #self.session.vars['paying_round'] = paying_round self.session.vars['riskaversionchosen'] = random.randint(1, 10) #print('set the paying round to', paying_round) for p in self.get_players(): p.riskaversionchosen = self.session.vars['riskaversionchosen'] class Group(BaseGroup): def set_payoffs(self): for p in self.get_players(): p.cost = 10*((p.tokens)**2)/2 #p.token_U_total = self.get_player_by_id(1).token_U + self.get_player_by_id(2).token_U+self.get_player_by_id(5).token_U+self.get_player_by_id(6).token_U + self.get_player_by_id(9).token_U + self.get_player_by_id(10).token_U #sum([o.token_U for o in self.get_player_by_id in (1,2,5,6,9,10)]) #p.token_R_total = self.get_player_by_id(3).token_R + self.get_player_by_id(4).token_R+self.get_player_by_id(7).token_R+self.get_player_by_id(8).token_R + self.get_player_by_id(11).token_R + self.get_player_by_id(12).token_R #sum([o.token_R for o in self.get_player_by_id in (3,4,7,8,11,12)]) p.token_W_total = self.get_player_by_id(1).tokens + self.get_player_by_id(3).tokens + self.get_player_by_id(5).tokens+self.get_player_by_id(7).tokens + self.get_player_by_id(9).tokens + self.get_player_by_id(11).tokens #sum([o.token_W for o in self.get_player_by_id in (1,3,5,7,9,11)]) p.token_P_total = self.get_player_by_id(2).tokens + self.get_player_by_id(4).tokens + self.get_player_by_id(6).tokens+self.get_player_by_id(8).tokens + self.get_player_by_id(10).tokens + self.get_player_by_id(12).tokens #sum([o.token_P for o in self.get_player_by_id in (2,4,6,8,10,12)]) p.token_total = p.token_W_total + p.token_P_total if p.token_total == 0: for player in self.get_players(): #player.prob_U = 0 #player.prob_R = 0 player.prob_P = 0 player.prob_W = 0 else: #p.prob_U = round(p.token_U_total / p.token_total,2) #p.prob_R = round(p.token_R_total / p.token_total,2) p.prob_W = round(p.token_W_total / p.token_total,2) p.prob_P = round(p.token_P_total / p.token_total,2) if p.token_total != 0: z = random.random() while (z == 1): z = random.random() for player in self.get_players(): if 0 <= z and z < player.prob_W: # player.U_win = True # player.R_win = False player.W_win = True player.P_win = False elif player.prob_W <= z and z < 1: #player.R_win = True #player.U_win = False player.W_win = False player.P_win = True #elif player.prob_U + player.prob_R <= z and z < player.prob_U + player.prob_R + player.prob_W: # player.W_win = True # player.U_win = False # player.R_win = False # player.P_win = False #elif player.prob_U + player.prob_R + player.prob_W <= z and z < 1: # player.P_win = True # player.U_win = False # player.W_win = False # player.R_win = False else: for p in self.get_players(): #p.U_win = False #p.R_win = False p.W_win = False p.P_win = False for p in self.get_players(): #if p.U_win: # if p.id_in_group in (1,2,5,6,9,10): #for p in self.id_in_group in (1,2,5,6,9,10): # p.round_earning = Constants.v + Constants.endowment - p.cost # elif p.id_in_group in (3,4,7,8,11,12): # p.round_earning = Constants.endowment - p.cost #elif p.R_win: # if p.id_in_group in (3,4,7,8,11,12): # p.round_earning = Constants.v + Constants.endowment - p.cost # elif p.id_in_group in (1,2,5,6,9,10): # p.round_earning = Constants.endowment - p.cost if p.W_win: if p.id_in_group in (1,3,5,7,9,11): p.round_earning = Constants.v + Constants.endowment - p.cost elif p.id_in_group in (2,4,6,8,10,12): p.round_earning = Constants.endowment - p.cost elif p.P_win: if p.id_in_group in (2,4,6,8,10,12): p.round_earning = Constants.v + Constants.endowment - p.cost elif p.id_in_group in (1,3,5,7,9,11): p.round_earning = Constants.endowment - p.cost else: for p in self.get_players(): p.round_earning = Constants.endowment def set_joywinning_payoffs(self): for p in self.get_players(): p.joy_winning_earnings = 1000 - p.joy_winning_bids p.total_bid1 = self.get_player_by_id(1).joy_winning_bids + self.get_player_by_id(2).joy_winning_bids + self.get_player_by_id(3).joy_winning_bids + self.get_player_by_id(4).joy_winning_bids + self.get_player_by_id(5).joy_winning_bids + self.get_player_by_id(6).joy_winning_bids p.total_bid2 = self.get_player_by_id(7).joy_winning_bids + self.get_player_by_id(8).joy_winning_bids + self.get_player_by_id(9).joy_winning_bids + self.get_player_by_id(10).joy_winning_bids + self.get_player_by_id(11).joy_winning_bids + self.get_player_by_id(12).joy_winning_bids if p.total_bid1 == 0: for player in self.get_players(): player.joy_winning_prob1 = 0 else: p.joy_winning_prob1 = round(p.joy_winning_bids / p.total_bid1, 2) if p.total_bid2 == 0: for player in self.get_players(): player.joy_winning_prob2 = 0 else: p.joy_winning_prob2 = round(p.joy_winning_bids / p.total_bid2, 2) if p.total_bid1 != 0: z1 = random.random() if 0 <= z1 and z1 < self.get_player_by_id(1).joy_winning_prob1: self.get_player_by_id(1).joy_winning_win = True elif self.get_player_by_id(1).joy_winning_prob1 <= z1 and z1 < self.get_player_by_id(1).joy_winning_prob1 + self.get_player_by_id(2).joy_winning_prob1: self.get_player_by_id(2).joy_winning_win = True elif self.get_player_by_id(1).joy_winning_prob1+self.get_player_by_id(2).joy_winning_prob1 <= z1 and z1 < self.get_player_by_id(1).joy_winning_prob1+self.get_player_by_id(2).joy_winning_prob1+ self.get_player_by_id(3).joy_winning_prob1: self.get_player_by_id(3).joy_winning_win = True elif self.get_player_by_id(1).joy_winning_prob1 + self.get_player_by_id(2).joy_winning_prob1 + self.get_player_by_id(3).joy_winning_prob1 <= z1 and z1 < self.get_player_by_id(1).joy_winning_prob1 + self.get_player_by_id(2).joy_winning_prob1 + self.get_player_by_id(3).joy_winning_prob1 + self.get_player_by_id(4).joy_winning_prob1: self.get_player_by_id(4).joy_winning_win = True elif self.get_player_by_id(1).joy_winning_prob1 + self.get_player_by_id(2).joy_winning_prob1 + self.get_player_by_id(3).joy_winning_prob1 + self.get_player_by_id(4).joy_winning_prob1 <= z1 and z1 < self.get_player_by_id(1).joy_winning_prob1 + self.get_player_by_id(2).joy_winning_prob1 + self.get_player_by_id(3).joy_winning_prob1 + self.get_player_by_id(4).joy_winning_prob1 + self.get_player_by_id(5).joy_winning_prob1: self.get_player_by_id(5).joy_winning_win = True else: self.get_player_by_id(6).joy_winning_win = True #if p.joy_winning_bids == p.highest_bid1: # p.joy_winning_win = True #else: # p.joy_winning_win = False if p.total_bid2 != 0: z2 = random.random() if 0 <= z2 and z2 < self.get_player_by_id(7).joy_winning_prob2: self.get_player_by_id(7).joy_winning_win = True elif self.get_player_by_id(7).joy_winning_prob2 <= z2 and z2 < self.get_player_by_id(7).joy_winning_prob2 + self.get_player_by_id(8).joy_winning_prob2: self.get_player_by_id(8).joy_winning_win = True elif self.get_player_by_id(7).joy_winning_prob2+self.get_player_by_id(8).joy_winning_prob2 <= z2 and z2 < self.get_player_by_id(7).joy_winning_prob2+self.get_player_by_id(8).joy_winning_prob2+ self.get_player_by_id(9).joy_winning_prob2: self.get_player_by_id(9).joy_winning_win = True elif self.get_player_by_id(7).joy_winning_prob2 + self.get_player_by_id(8).joy_winning_prob2 + self.get_player_by_id(9).joy_winning_prob2 <= z2 and z2 < self.get_player_by_id(7).joy_winning_prob2 + self.get_player_by_id(8).joy_winning_prob2 + self.get_player_by_id(9).joy_winning_prob2 + self.get_player_by_id(10).joy_winning_prob2: self.get_player_by_id(10).joy_winning_win = True elif self.get_player_by_id(7).joy_winning_prob2 + self.get_player_by_id(8).joy_winning_prob2 + self.get_player_by_id(9).joy_winning_prob2 + self.get_player_by_id(10).joy_winning_prob2 <= z2 and z2 < self.get_player_by_id(7).joy_winning_prob2 + self.get_player_by_id(8).joy_winning_prob2 + self.get_player_by_id(9).joy_winning_prob2 + self.get_player_by_id(10).joy_winning_prob2 + self.get_player_by_id(11).joy_winning_prob2: self.get_player_by_id(11).joy_winning_win = True else: self.get_player_by_id(12).joy_winning_win = True class Player(BasePlayer): tokens = models.IntegerField( min=0,max=14, label ='How many tokens would you like to purchase for your group ?') #token_U = models.IntegerField (min=0, max = 15, label = 'How many tokens you would like to purchase to group Red?') #token_R = models.IntegerField (min=0, max = 15, label = 'How many tokens you would like to purchase to group Blue? ') #token_W = models.IntegerField(min=0, max=15, label='How many tokens you would like to purchase to group Rectangle?') #token_P = models.IntegerField(min=0, max=15, label='How many tokens you would like to purchase to group Triangle? ') #U_win = models.BooleanField() #R_win = models.BooleanField() W_win = models.BooleanField() P_win = models.BooleanField() #token_U_total = models.IntegerField() #token_R_total = models.IntegerField() token_W_total = models.IntegerField() token_P_total = models.IntegerField() token_total = models.IntegerField() #prob_U = models.FloatField() #prob_R = models.FloatField() prob_W = models.FloatField() prob_P = models.FloatField() round_earning = models.CurrencyField() #payoff = models.CurrencyField() cost = models.CurrencyField() joy_winning_bids = models.IntegerField(min=0, max=1000, label='How many tokens you would like to pay for the bid (1 token = 1 point)?') joy_winning_win = models.BooleanField(initial=False) joy_winning_earnings = models.CurrencyField() joy_winning_prob1 = models.FloatField() joy_winning_prob2 = models.FloatField() total_bid1 = models.IntegerField() total_bid2 = models.IntegerField() age = models.IntegerField( label='What is your age?', min=18, max=125) gender = models.StringField( choices=['Male', 'Female', 'Other'], label='What is your gender?', widget=widgets.RadioSelect) citizenship = models.StringField( label='Your country of citizenship') major = models.StringField( label='What is your major?') year = models.StringField( choices=['Freshman', 'Sophomore', 'Junior', 'Senior', 'Graduate', 'Other'], label='Which year are you in?', widget=widgets.RadioSelect) risk = models.StringField( choices=['Strongly Disagree', 'Disagree', 'Slightly Disagree', 'Slightly Agree', 'Agree', 'Strongly Agree'], label='You are the person who is fully prepared to take risks.', widget=widgets.RadioSelect) decision = models.TextField(initial=None, verbose_name="How you made your decisions in Part 1?") comments = models.TextField(blank=True, initial=None, verbose_name="Do you have any comment, questions, or complains about today's experiment?") attachment = models.StringField( choices=['1. I am feeling like I do not belong to my group at all', '2. I am feeling like I stay at the boundary of my group', '3. I am feeling like I only partially belong to my group, and I treat myself more than my group.', '4. I am feeling like I partially belong to my group, and I treat myself and my group equally', '5. I am feeling like I partially belong to my group, and I treat my group more than myself', '6. I am feeling like I totally belong to my group'], label='Please make your decision:', widget=widgets.RadioSelect) HLchoice1 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 1: The lottery has 10% chance to get $3, 90% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice2 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 2: The lottery has 20% chance to get $3, 80% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice3 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 3: The lottery has 30% chance to get $3, 70% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice4 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 4: The lottery has 40% chance to get $3, 60% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice5 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 5: The lottery has 50% chance to get $3, 50% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice6 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 6: The lottery has 60% chance to get $3, 40% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice7 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 7: The lottery has 70% chance to get $3, 30% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice8 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 8: The lottery has 80% chance to get $3, 20% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice9 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 9: The lottery has 90% chance to get $3, 10% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) HLchoice10 = models.StringField( choices=['$1.5 Sure amount', 'Lottery'], label='Game 10: The lottery has 100% chance to get $3, 0% chance to get $0.1.', widget=widgets.RadioSelectHorizontal) riskaversionchosen = models.IntegerField()