from random import random, randint, sample from otree import settings from otree.api import * doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'dohme_lotterie' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 ### Todo: A_HIGH_MONEY = 10 A_LOW_MONEY = 8 B_HIGH_MONEY = 19.75 B_LOW_MONEY = 0.5 AMOUNT_WINNER = 2 REAL_WORLD_CURRENCY_PER_POINT = 0.07 class Subsession(BaseSubsession): REAL_WORLD_CURRENCY_PER_POINT = models.FloatField(initial=C.REAL_WORLD_CURRENCY_PER_POINT) pass class Group(BaseGroup): pass def creating_session(subsession): subsession.REAL_WORLD_CURRENCY_PER_POINT = subsession.session.config['real_world_currency_per_point'] def generate_question_scale(id = 1,percentage = 10, a_high = C.A_HIGH_MONEY, a_low = C.A_LOW_MONEY, b_high = C.B_HIGH_MONEY, b_low = C.B_LOW_MONEY): choices = [ [False, f'Lotterie A: mit {percentage}% Wahrscheinlichkeit {round(a_high, 2)}€ und mit {100-percentage}% Wahrscheinlichkeit {round(a_low, 2)}€'], [True, f'Lotterie B: mit {percentage}% Wahrscheinlichkeit {round(b_high, 2)}€ und mit {100-percentage}% Wahrscheinlichkeit {round(b_low, 2)}€.'] ] return models.BooleanField(label=f"{id}.",choices=choices, widget=widgets.RadioSelectHorizontal) class Player(BasePlayer): ten = generate_question_scale(1, 10) twenty = generate_question_scale(2, 20) thirty = generate_question_scale(3, 30) fourty = generate_question_scale(4, 40) fifty = generate_question_scale(5, 50) sixty = generate_question_scale(6, 60) seventy = generate_question_scale(7, 70) eighty = generate_question_scale(8, 80) ninety = generate_question_scale(9, 90) hundred = generate_question_scale(10, 100) winner = models.BooleanField(initial=False) chosen_row = models.IntegerField(initial=-1) payoff_euro = models.FloatField(initial=-1) def calculate_winner(player_list): winner_list = sample(player_list, 1 if len(player_list) < C.AMOUNT_WINNER else C.AMOUNT_WINNER) money = [[C.A_LOW_MONEY, C.A_HIGH_MONEY] , [C.B_LOW_MONEY, C.B_HIGH_MONEY]] for winner in winner_list: winner.winner = True row = randint(1,10) percentage = random() fields = [winner.ten, winner.twenty, winner.thirty, winner.fourty, winner.fifty, winner.sixty, winner.seventy, winner.eighty, winner.ninety, winner.hundred] money_field = fields[row-1] ## wegen dem Zähler ''' money_field = Boolean ^= 0 oder 1 money[0]= A werte money[1]= B Werte percentage < row*0.1 ^= Zeile * 10 / 100 ''' winnings = money[money_field][percentage > row * .1] winner.payoff = winnings / C.REAL_WORLD_CURRENCY_PER_POINT winner.payoff_euro = winnings winner.chosen_row = row class IntroPart1(Page): pass class Part1(Page): form_model = 'player' form_fields = ['ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety','hundred'] class ResultsWaitPage(WaitPage): @staticmethod def after_all_players_arrive(group): calculate_winner(group.get_players()) class Results(Page): pass page_sequence = [IntroPart1, Part1,ResultsWaitPage, Results]