from otree.api import * import numpy as np import random c = cu doc = 'Tutorial' class Constants(BaseConstants): name_in_url = 'tutorial' players_per_group = None num_rounds=1 endowment_trial = 10 multiplier_trial = 1.5 opponent_endowment = 20 opponent_contri = 0.5 def points_display_rounding(exact_value): ''' Converts numerical value into displayable points value: float without decimal places -> int; float with decimals -> rounded to 2 places ''' if exact_value > 0: if exact_value % 1 == 0: display_value =int(exact_value) else: display_value =round(exact_value,2) else: display_value = 0 return display_value class Subsession(BaseSubsession): pass #In the first subsession, participants in a group get assigned the categorical or flexible treatment #the data field is stored on the participant level as creating_session is executed at every subsession def creating_session(subsession): pass class Group(BaseGroup): pass class Player(BasePlayer): endowment_trial = models.IntegerField(initial=Constants.endowment_trial) input1 = models.IntegerField(label='How much do you contribute') payoff_trial = models.FloatField(initial=0) answer1 = models.BooleanField(label='Q1', Choices=[ [0, 'No'], [1, 'Yes'],]) answer2 = models.BooleanField(label='Q1', Choices=[ [0, 'No'], [1, 'Yes'],]) class Tutorial(Page): #Consent page form_model = 'player' def pg_trial(player): return ((player.input1 + Constants.opponent_endowment*Constants.opponent_contri)*Constants.multiplier_trial) class Trial_1(Page): #Trial game timeout_seconds = 120 form_model = 'player' form_fields=['input1'] @staticmethod def before_next_page(player, timeout_happened): player.payoff_trial = pg_trial(player)/2 + player.endowment_trial - player.input1 class Result_1(Page): #Consent page form_model = 'player' timeout_seconds = 120 @staticmethod def js_vars(player): return dict( contribution1=player.input1, contribution2=Constants.opponent_endowment*Constants.opponent_contri, ret1=player.endowment_trial - player.input1, ret2=Constants.opponent_endowment*(1-Constants.opponent_contri) ) @staticmethod def vars_for_template(player): pg_rounded = points_display_rounding(pg_trial(player)) opp_con = int(Constants.opponent_endowment*Constants.opponent_contri) return dict( contribution2=opp_con, fraction1 = round(player.input1/player.endowment_trial*100,1), total_contrib = player.input1 + opp_con, pg = pg_rounded, pg_share = points_display_rounding(pg_trial(player)/2), ret_endow = points_display_rounding(player.endowment_trial - player.input1), display_payoff = points_display_rounding(player.payoff_trial) ) class Question_1(Page): #Consent page form_model = 'player' form_fields=['answer1'] class Question_2(Page): #Consent page form_model = 'player' form_fields=['answer2'] page_sequence = [Tutorial, Trial_1, Result_1, Question_1, Question_2]