""" Payment randomization per written protocol (run when participant reaches Results). 1. Assign sequential completion_id among finishers; bonus_eligible if id % 20 == 0. 2. If eligible: random bonus_part in {1=Risk, 2=Time, 3=Ambiguity}. 3. Resolve bonus from one random choice in the selected part. """ import random from otree.api import Currency as c BONUS_ELIGIBLE_MODULO = 20 def sync_participation_fee(participant, session): """Store session participation fee (set in admin to match Prolific study payment).""" cfg = session.config pv = participant.vars if 'force_participation_fee' in cfg: pv['participation_fee'] = cfg['force_participation_fee'] else: pv['participation_fee'] = cfg.get('participation_fee', 0) def participation_fee_c(participant, session): sync_participation_fee(participant, session) return c(participant.vars['participation_fee']) def _next_completion_id(session): session.vars['completion_counter'] = session.vars.get('completion_counter', 0) + 1 return session.vars['completion_counter'] def assign_payment_protocol(participant, session): """Run once before Results. Honors force_* keys in session.config for testing.""" sync_participation_fee(participant, session) pv = participant.vars if pv.get('payment_protocol_done'): return cfg = session.config if cfg.get('seed_results_test_vars'): _apply_test_seed(pv, cfg) pv['payment_protocol_done'] = True return completion_id = _next_completion_id(session) pv['completion_id'] = completion_id pv['bonus_eligible'] = (completion_id % BONUS_ELIGIBLE_MODULO == 0) if isinstance(cfg.get('force_bonus_eligible'), bool): pv['bonus_eligible'] = cfg['force_bonus_eligible'] if not pv['bonus_eligible']: pv['bonus_part'] = None pv['bonus_display_eligible'] = False pv['payoff_risk'] = c(0) pv['payoff_time'] = c(0) pv['payoff_ambig'] = c(0) pv['payment_protocol_done'] = True return if isinstance(cfg.get('force_bonus_part'), int) and cfg['force_bonus_part'] in (1, 2, 3): bonus_part = cfg['force_bonus_part'] else: bonus_part = random.choice([1, 2, 3]) pv['bonus_part'] = bonus_part if bonus_part == 1: _resolve_risk_bonus(participant, cfg) elif bonus_part == 2: _resolve_time_bonus(participant, cfg) elif bonus_part == 3: _resolve_ambiguity_bonus(participant, cfg) pv['bonus_display_eligible'] = bool(pv.get('bonus_eligible')) and not pv.get( 'time_payment_lookup_failed', False ) pv['payment_protocol_done'] = True def _apply_test_seed(pv, cfg): """Test sessions: optional completion_id / eligibility / payoffs from config.""" if 'force_completion_id' in cfg: pv['completion_id'] = cfg['force_completion_id'] if 'force_bonus_eligible' not in cfg: pv['bonus_eligible'] = (pv['completion_id'] % BONUS_ELIGIBLE_MODULO == 0) if 'force_bonus_eligible' in cfg: pv['bonus_eligible'] = cfg['force_bonus_eligible'] if 'force_bonus_part' in cfg: pv['bonus_part'] = cfg['force_bonus_part'] if not pv.get('bonus_eligible'): pv['bonus_part'] = None for key, cfg_key in ( ('payoff_risk', 'force_payoff_risk'), ('payoff_time', 'force_payoff_time'), ('payoff_ambig', 'force_payoff_ambig'), ('time_selected_task', 'force_time_selected_task'), ('time_selected_choice', 'force_time_selected_choice'), ): if cfg_key in cfg: pv[key] = cfg[cfg_key] if 'force_participation_fee' in cfg: pv['participation_fee'] = cfg['force_participation_fee'] def _resolve_risk_bonus(participant, cfg): from Risk.models import Player as RiskPlayer, Constants as RiskConstants pv = participant.vars if isinstance(cfg.get('force_risk_round_to_pay'), int): round_to_pay = cfg['force_risk_round_to_pay'] else: round_to_pay = random.randint(1, RiskConstants.num_rounds) if isinstance(cfg.get('force_risk_row_to_pay'), int): row = cfg['force_risk_row_to_pay'] else: row = random.randint(1, RiskConstants.num_choices) risk_player = RiskPlayer.objects_filter(participant=participant, round_number=round_to_pay).first() if not risk_player: pv['payoff_risk'] = c(0) return choice_field = f'choice_{row}' choice = getattr(risk_player, choice_field, None) mpl_raw = pv['rand_sequence'][round_to_pay - 1] lot_hi = mpl_raw[0][0] lot_lo = mpl_raw[1][0] prob_hi = mpl_raw[2][0] cert = pv['mpl_sequence'][round_to_pay - 1][4][row - 1] if isinstance(cfg.get('force_risk_random_draw'), (int, float)): draw = float(cfg['force_risk_random_draw']) else: draw = random.random() if choice == 'A': payoff = lot_hi if draw <= prob_hi else lot_lo realization = 'high' if draw <= prob_hi else 'low' elif choice == 'B': payoff = cert realization = 'not_applicable' else: payoff = c(0) realization = 'not_applicable' pv['risk_round_to_pay'] = round_to_pay pv['risk_row_to_pay'] = row pv['risk_choice_field'] = choice_field pv['risk_random_draw'] = draw pv['risk_selected_choice'] = choice pv['risk_lottery_realization'] = realization pv['payoff_risk'] = c(payoff) pv['risk_final_bonus'] = c(payoff) def _time_app_and_round(seq, path, task, round_in_task): """Map (task, round 1..18) to oTree app label and global round_number.""" order48 = seq in (1, 2) if path == 'Lot1→CE': lot_app = 'Time_Adaptive_Lot_1' if order48 else 'Time_Adaptive_Lot_1_84' ce_app = 'Time_Adaptive_CE' if order48 else 'Time_Adaptive_CE_84' else: lot_app = 'Time_Adaptive_Lot_2' if order48 else 'Time_Adaptive_Lot_2_84' ce_app = 'Time_Adaptive_CE' if order48 else 'Time_Adaptive_CE_84' if task in ('lot_4w', 'ce_4w'): app = lot_app if task.startswith('lot') else ce_app otree_round = round_in_task if order48 else (18 + round_in_task) else: app = lot_app if task.startswith('lot') else ce_app otree_round = (18 + round_in_task) if order48 else round_in_task return app, otree_round def _get_time_player(participant, app_label, round_number): if app_label == 'Time_Adaptive_Lot_1': from Time_Adaptive_Lot_1.models import Player elif app_label == 'Time_Adaptive_Lot_1_84': from Time_Adaptive_Lot_1_84.models import Player elif app_label == 'Time_Adaptive_Lot_2': from Time_Adaptive_Lot_2.models import Player elif app_label == 'Time_Adaptive_Lot_2_84': from Time_Adaptive_Lot_2_84.models import Player elif app_label == 'Time_Adaptive_CE': from Time_Adaptive_CE.models import Player elif app_label == 'Time_Adaptive_CE_84': from Time_Adaptive_CE_84.models import Player else: return None return Player.objects_filter(participant=participant, round_number=round_number).first() def _resolve_time_bonus(participant, cfg): pv = participant.vars pv['time_payment_lookup_failed'] = False seq = pv.get('discount_seq', 1) path = pv.get('path', 'Lot1→CE') tasks = ['lot_4w', 'lot_8w', 'ce_4w', 'ce_8w'] if cfg.get('force_time_task_to_pay') in tasks: task = cfg['force_time_task_to_pay'] else: task = random.choice(tasks) if isinstance(cfg.get('force_time_round_to_pay'), int): round_in_task = cfg['force_time_round_to_pay'] else: round_in_task = random.randint(1, 18) app_label, otree_round = _time_app_and_round(seq, path, task, round_in_task) time_player = _get_time_player(participant, app_label, otree_round) pv['time_task_to_pay'] = task pv['time_round_to_pay'] = round_in_task pv['time_selected_task'] = task pv['time_payment_app'] = app_label pv['time_payment_round'] = otree_round if not time_player: pv['time_payment_lookup_failed'] = True pv['time_payment_lookup_app'] = app_label pv['time_payment_lookup_round'] = otree_round pv['time_payment_lookup_task'] = task pv['time_payment_lookup_round_in_task'] = round_in_task pv['time_payment_lookup_seq'] = seq pv['time_payment_lookup_path'] = path pv['payoff_time'] = c(0) return choice = time_player.choice pv['time_selected_choice'] = choice pv['time_selected_type'] = 'ce' if task.startswith('ce') else 'lottery' if task.startswith('ce'): if choice == 'early': payment = time_player.early_option else: payment = time_player.late_option pv['payoff_time_date'] = 0 if choice == 'early' else 1 pv['payoff_time'] = c(payment) pv['time_lottery_realization'] = 'not_applicable' pv['time_final_bonus'] = c(payment) return if cfg.get('force_time_random_draw') in (0, 1): draw = cfg['force_time_random_draw'] else: draw = random.randint(0, 1) pv['time_random_draw'] = draw pv['time_random_draw_used'] = draw if choice == 'early': payment = time_player.lot_high_early if draw == 0 else time_player.lot_low_early pv['payoff_time_date'] = 0 pv['time_lottery_realization'] = 'high' if draw == 0 else 'low' else: payment = time_player.lot_high_late if draw == 0 else time_player.lot_low_late pv['payoff_time_date'] = 1 pv['time_lottery_realization'] = 'high' if draw == 0 else 'low' pv['payoff_time'] = c(payment) pv['time_final_bonus'] = c(payment) def _amb_win_probability(task_key, box, known_p, purple_share_u=None): """Return win probability for draw in [0,1].""" from Ambiguity_New.models import Constants as AmbConstants task_meta = next(t for t in AmbConstants.TASKS if t['key'] == task_key) if box == 'U': p = purple_share_u if purple_share_u is not None else 0.5 else: p = known_p if task_meta['prize'] == 'purple': return p return 1.0 - p def _resolve_ambiguity_bonus(participant, cfg): from Ambiguity_New.models import Constants as AmbConstants pv = participant.vars tasks = ['task1', 'task2', 'task3'] if cfg.get('force_amb_task_to_pay') in tasks: task = cfg['force_amb_task_to_pay'] else: task = random.choice(tasks) records = pv.get('amb_choice_records', {}).get(task, []) if not records: records = [{'choice': pv.get('amb_last_choice', {}).get(task), 'p_at_choice': pv.get('amb_state', {}).get(task, {}).get('p', 0.5)}] if records: picked = random.choice(records) else: picked = {'choice': 'K', 'p_at_choice': 0.5} choice = picked.get('choice') p_at_choice = picked.get('p_at_choice') or pv.get('amb_state', {}).get(task, {}).get('indiff', 0.5) if choice == 'I': if cfg.get('force_amb_box_if_indiff') in ('U', 'K'): box = cfg['force_amb_box_if_indiff'] else: box = random.choice(['U', 'K']) known_p = pv.get('amb_state', {}).get(task, {}).get('indiff', p_at_choice) else: box = choice known_p = p_at_choice if box == 'U': if isinstance(cfg.get('force_amb_u_purple_count'), int): purple_count = cfg['force_amb_u_purple_count'] else: purple_count = random.randint(1, 100) purple_share = purple_count / 100.0 win_prob = _amb_win_probability(task, 'U', known_p, purple_share_u=purple_share) pv['amb_u_purple_count'] = purple_count else: purple_share = None win_prob = _amb_win_probability(task, 'K', known_p) pv['amb_u_purple_count'] = None if isinstance(cfg.get('force_amb_random_draw'), (int, float)): draw = float(cfg['force_amb_random_draw']) else: draw = random.random() win = draw <= win_prob payoff = c(AmbConstants.WIN_PAYOFF) if win else c(0) pv['amb_task_to_pay'] = task pv['amb_selected_task'] = task pv['amb_selected_box'] = box pv['amb_selected_prob'] = win_prob pv['amb_selected_draw'] = draw pv['amb_result'] = 'win' if win else 'lose' pv['payoff_ambig'] = payoff pv['amb_final_bonus'] = payoff pv['amb_payoff_set'] = True pv['amb_picked_round'] = picked.get('round_in_task')