from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) #from otree_tools.widgets import AdvancedSliderWidget import random from operator import itemgetter, attrgetter author = 'Your name here' doc = """ Experiment for the lecture "Strategische Interaktion" by Prof. Dr. Sandra Ludwig. Split pot. """ class Constants(BaseConstants): name_in_url = 'SI_exp03' players_per_group = None num_rounds = 1 pot = c(10) class Subsession(BaseSubsession): def creating_session(self): # self.group_randomly() # Note: Stupid python list indexing[start=zero, end=exclusive!] if self.round_number == 1: self.session.vars['exp03_paying_players'] = random.sample(range(0,self.session.num_participants,1), 2) # print('set the paying players to', self.session.vars['exp1_paying_players']) def vars_for_admin_report(self): all_choices_ind = sorted([p.choice for p in self.get_players()]) all_choices_tab = [] for choice in all_choices_ind: count = all_choices_ind.count(choice) all_choices_tab.append([choice,count]) all_choices_tab = [list(t) for t in set(map(tuple, all_choices_tab))] all_choices_tab = sorted(all_choices_tab,key=itemgetter(0),reverse=True) all_choices_tab = sorted(all_choices_tab,key=itemgetter(1),reverse=True) amounts = [str(item[0]) for item in all_choices_tab] counts = [item[1] for item in all_choices_tab] print('Sorted choices:') print(all_choices_tab) return {'all_choices_tab': str(all_choices_tab), 'amounts': str(amounts), 'counts': counts, 'paying_players': [ self.get_players()[self.session.vars['exp03_paying_players'][0]].participant.label, self.get_players()[self.session.vars['exp03_paying_players'][0]].payoff, self.get_players()[self.session.vars['exp03_paying_players'][1]].participant.label, self.get_players()[self.session.vars['exp03_paying_players'][1]].payoff ]} def set_payoffs(self): for p in self.get_players(): p.payoff = 0 p1 = self.get_players()[self.session.vars['exp03_paying_players'][0]] p2 = self.get_players()[self.session.vars['exp03_paying_players'][1]] print(p1.choice + p2.choice ) print(Constants.pot) if p1.choice + p2.choice <= Constants.pot: p1.payoff = p1.choice p2.payoff = p2.choice else: p1.payoff = c(0) p2.payoff = c(0) summe = sum(p.choice for p in self.get_players()) # for player in self.get_players(): # player.participant.vars['exp03_my_choice'] = player.choice # player.participant.vars['exp03_others_choice'] = self.get_player_by_id(player.id_in_group % 2 + 1).choice # player.participant.vars['exp03_my_payoff'] = player.payoff # player.participant.vars['exp03_total_requested'] = summe # player.participant.vars['exp03_total_words'] = 'nicht größer' if summe <= Constants.pot else 'größer' # player.participant.vars['exp03_pot'] = Constants.pot # Save payoffs for live payout self.session.vars['exp03_paying_players_payoff'] = [ self.get_players()[self.session.vars['exp03_paying_players'][0]].payoff, self.get_players()[self.session.vars['exp03_paying_players'][1]].payoff ] class Group(BaseGroup): pass class Player(BasePlayer): choice = models.CurrencyField( min = c(0), max = Constants.pot, label = "Wieviel Geld beanspruchen Sie für sich?", initial = None)