from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import itertools import random author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'the_main_app' players_per_group = None num_rounds = 5 endowment = c(100) class Subsession(BaseSubsession): def creating_session(self): players = self.get_players() if self.round_number == 1: ################## Assigns Color randomly ################################ colors = itertools.cycle(['blue', 'red']) for p in players: p.participant.vars['color'] = next(colors) ########################################################################## paying_round = random.randint(1, Constants.num_rounds) self.session.vars['paying_round'] = paying_round print('set the paying round to', paying_round) seq_1 = [[1, 1, 1, 2, 2, 2, 3, 3, 3], [2, 2, 2, 1, 1, 1, 3, 3, 3], [3, 3, 3, 1, 1, 1, 2, 2, 2], [1, 1, 1, 3, 3, 3, 2, 2, 2], [2, 2, 2, 3, 3, 3, 1, 1, 1], [3, 3, 3, 2, 2, 2, 1, 1, 1]] random.shuffle(seq_1) seq_2 = list.copy(seq_1) seq_r = itertools.cycle(seq_1) seq_b = itertools.cycle(seq_2) x = [[1, 2, 3], [2, 3, 1], [3, 1, 2], [1, 3, 2], [2, 1, 3], [3, 2, 1]] y = [[2, 3, 1], [3, 1, 2], [1, 2, 3], [2, 1, 3], [3, 2, 1], [1, 3, 2]] z = [[3, 1, 2], [1, 3, 2], [2, 1, 3], [3, 2, 1], [1, 2, 3], [2, 3, 1]] random.shuffle(x), random.shuffle(y), random.shuffle(z) subseq1 = itertools.cycle(x) subseq2 = itertools.cycle(y) subseq3 = itertools.cycle(z) red_players = [p for p in players if p.participant.vars['color'] == 'red'] blue_players = [p for p in players if p.participant.vars['color'] == 'blue'] for p in red_players: if red_players.index(p) < len(red_players)*(2/3): p.participant.vars['dictator'] = True p.participant.vars['lottery'] = int(random.random() > 0.5) p.participant.vars['seq'] = next(seq_r) p.participant.vars['subseq'] = next(subseq1) + next(subseq2) + next(subseq3) else: p.participant.vars['dictator'] = False for p in blue_players: if blue_players.index(p) < len(blue_players)*(2/3): p.participant.vars['dictator'] = True p.participant.vars['lottery'] = int(random.random() > 0.5) p.participant.vars['seq'] = next(seq_b) p.participant.vars['subseq'] = next(subseq1) + next(subseq2) + next(subseq3) else: p.participant.vars['dictator'] = False for p in players: if p.participant.vars['dictator']: p.task = p.participant.vars['seq'][self.round_number - 1] p.variant = p.participant.vars['subseq'][self.round_number - 1] p.variant2 = p.participant.vars['subseq'][self.session.vars['paying_round'] - 1] p.lottery = p.participant.vars['lottery'] else: p.task = 0 p.variant = 0 p.color = p.participant.vars['color'] p.r_for_pay = self.session.vars['paying_round'] class Group(BaseGroup): pass class Player(BasePlayer): r_for_pay = models.IntegerField() order = models.StringField() color = models.StringField() task = models.IntegerField() variant = models.IntegerField() variant2 = models.IntegerField() time_spent = models.StringField() lottery = models.IntegerField() pay_match = models.IntegerField() def role(self): if self.participant.vars['dictator']: return 'dictator' else: return 'receiver' offer_1 = models.CurrencyField() offer_2 = models.CurrencyField() offer_3 = models.CurrencyField() def set_payoffs(self): if self.role() == 'dictator': if self.in_round(self.r_for_pay).task == 1: self.payoff = c(0) elif self.in_round(self.r_for_pay).task == 2: self.payoff = self.in_round(self.r_for_pay).offer_1 else: self.payoff = self.in_round(self.r_for_pay).offer_1 + \ self.lottery * self.in_round(self.r_for_pay).offer_2 + \ (1 - self.lottery) * self.in_round(self.r_for_pay).offer_3 else: dicts = [p for p in self.get_others_in_subsession() if p.role() == 'dictator'] match = 0 while match == 0: check = random.choice(dicts) if check.color == self.color and check.variant2 != 3: match = check elif check.color != self.color and check.variant2 != 1: match = check else: match = 0 # self.pay_match = int(match.id_in_subsession()) if match.variant2 != 2: self.payoff = random.choice([ match.in_round(self.r_for_pay).offer_2, match.in_round(self.r_for_pay).offer_3]) else: if match.color == self.color: self.payoff = match.in_round(self.r_for_pay).offer_2 else: self.payoff = match.in_round(self.r_for_pay).offer_3