"""Simulate a full playthrough and write a msgpack data file in the SAME format the live app produces, so we can validate export_csv.py without a browser.""" import os from datetime import datetime, timedelta import msgpack import numpy as np import jax import jax.tree_util as jtu import experiment_structure as E import nicewebrl os.makedirs("data", exist_ok=True) SEED = 999999 PID = "0007" # demo participant (group 0, number 7) PATH = f"data/user_data_{SEED}.msgpack" BASE = datetime(2026, 1, 1, 12, 0, 0) def write_record(f, rec): packed = msgpack.packb(rec) f.write(len(packed).to_bytes(4, "big")) f.write(packed) def env_record(name, timestep, action_idx, t): d = nicewebrl.make_serializable(E.custom_data_fn(timestep)) d.update( pid=PID, gid=int(PID[0]), # app.storage isn't available off-thread; inject here image_seen_time=(BASE + timedelta(seconds=t)).isoformat(), action_taken_time=(BASE + timedelta(seconds=t, milliseconds=450)).isoformat(), computer_interaction=E.action_keys[action_idx], action_name=E.action_to_name[action_idx], action_idx=action_idx, ) return dict(name=name, body="", stage_idx=0, session_id="demo", user_data=dict(user_id=SEED, age=27, sex="Female"), metadata=dict(type="EnvStage", task=name.lower()), data=d) def drive(f, params, name, t0): p = params ts = E.jax_web_env.reset(jax.random.PRNGKey(7), p) t = t0 def step(ts, a, sd): nxt = E.jax_web_env.next_steps(jax.random.PRNGKey(sd), ts, p) return jtu.tree_map(lambda x: x[a], nxt) def nearest(s): ar, ac = int(s.agent_pos[0]), int(s.agent_pos[1]) hp, act, co = np.asarray(s.house_pos), np.asarray(s.house_active), np.asarray(s.consumed) best, bd = None, 1e9 for i in range(E.N_PLACES): if act[i] and not co[i]: dd = abs(hp[i][0] - ar) + abs(hp[i][1] - ac) if dd < bd: best, bd = (int(hp[i][0]), int(hp[i][1])), dd return best rating_target = 0 for stp in range(40000): if bool(ts.last()): break s = ts.state m = int(s.mode) if m == E.M_OVERWORLD: tgt = nearest(s) if tgt is None: break ar, ac = int(s.agent_pos[0]), int(s.agent_pos[1]) tr, tc = tgt a = (E.A_UP if tr < ar else E.A_DOWN) if ar != tr else (E.A_LEFT if tc < ac else E.A_RIGHT) elif m == E.M_CHOICE: a = E.A_LEFT if (stp % 2 == 0) else E.A_RIGHT # vary choices elif m == E.M_OUTCOME: a = E.A_SPACE else: # rating: nudge cursor then confirm cur = int(s.rating_cursor) a = E.A_RIGHT if cur < (1 + (int(s.current_place) % 5)) else E.A_SPACE write_record(f, env_record(name, ts, a, t)) ts = step(ts, a, stp + 1) t += 2 return t with open(PATH, "wb") as f: # quiz: one FeedbackStage record holding all responses (matches the live app) responses = [] for i, (st, co, fb) in enumerate(E.QUIZ): ans = co if i != 1 else (not co) # get one wrong on purpose responses.append(dict(quizIndex=i, position=i, statement=st, answer=("TRUE" if ans else "FALSE"), correct_answer=("TRUE" if co else "FALSE"), is_correct=bool(ans == co), attempt=1)) write_record(f, dict(name="Quiz", body="", stage_idx=0, session_id="demo", user_data=dict(user_id=SEED, age=27, sex="Female"), metadata=dict(type="FeedbackStage"), data=dict(responses=responses))) t = drive(f, E.practice_params_default, "Practice", 100) drive(f, E.main_params_for("0007"), "Explore", t + 10) print(f"wrote {PATH}")