From 2cc8c02101e9ea2cc7193065acf08e09062003d8 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Tue, 21 Jul 2026 16:32:39 -0400 Subject: [PATCH] feat(v2): record EmulatorJS play sessions in the v2 shell, not v1 Player Wire usePlaySession into the v2 EmulatorJS view so a session is recorded on any exit path (in-emulator Exit/Save & Quit, back nav, or route change), starting when the game goes live and flushing on unmount. flush() is idempotent so overlapping exit paths can't double-submit. Remove the play-session ingest from the shared v1 Player component so there is a single source of truth and no double-recording. immediateExit now only navigates back to the game details view. Note: Player.vue is also mounted by the v1 EmulatorJS view, which no longer records sessions as a result; acceptable while v1 is frozen and slated for removal. Co-Authored-By: Claude Opus 4.8 (1M context) --- frontend/src/v2/views/Player/EmulatorJS.vue | 8 +++- .../src/views/Player/EmulatorJS/Player.vue | 41 +++---------------- 2 files changed, 13 insertions(+), 36 deletions(-) diff --git a/frontend/src/v2/views/Player/EmulatorJS.vue b/frontend/src/v2/views/Player/EmulatorJS.vue index 6b3cf160e..e2b2c9395 100644 --- a/frontend/src/v2/views/Player/EmulatorJS.vue +++ b/frontend/src/v2/views/Player/EmulatorJS.vue @@ -48,6 +48,7 @@ import { useBackgroundArt } from "@/v2/composables/useBackgroundArt"; import { useCoverArt } from "@/v2/composables/useCoverArt"; import { useFullscreenPref } from "@/v2/composables/useFullscreenPref"; import { useInputModality } from "@/v2/composables/useInputModality"; +import { usePlaySession } from "@/v2/composables/usePlaySession"; import type { SliderBtnGroupItem } from "@/v2/lib/primitives/RSliderBtnGroup/types"; import storeGalleryRoms from "@/v2/stores/galleryRoms"; import { installIOSFullscreenShim } from "@/views/Player/EmulatorJS/utils"; @@ -68,6 +69,7 @@ const configStore = storeConfig(); const { playing, fullScreen } = storeToRefs(playingStore); const { fullscreenOnPlay } = useFullscreenPref(); const { modality } = useInputModality(); +const playSession = usePlaySession(); // Ref the Play CTA so we can imperatively focus it on enter (and again // when the user comes back from a running session). RBtn forwards to @@ -437,10 +439,12 @@ onMounted(async () => { // to Play on exit so a Start-Play loop stays on the pad. watch(gameRunning, (running, prev) => { if (running && !prev) { + if (rom.value) playSession.start(rom.value); emitActivityStart(); startActivityHeartbeat(); } if (prev && !running) { + playSession.flush(); stopActivityHeartbeat(); emitActivityStop(); nextTick(focusPlayButton); @@ -457,7 +461,9 @@ function onGamepadButton(e: CustomEvent<{ name?: string }>) { onBeforeUnmount(() => { // Leaving the player (back nav / route change) ends the session even if - // the user never exited the game to the config screen first. + // the user never exited the game to the config screen first. flush() is + // idempotent, so an exit that already flushed via the watch is a no-op. + playSession.flush(); stopActivityHeartbeat(); emitActivityStop(); // Hand the keyboard and gamepad back to the UI; the flag otherwise diff --git a/frontend/src/views/Player/EmulatorJS/Player.vue b/frontend/src/views/Player/EmulatorJS/Player.vue index a4f9eb6d4..0ac44ff47 100644 --- a/frontend/src/views/Player/EmulatorJS/Player.vue +++ b/frontend/src/views/Player/EmulatorJS/Player.vue @@ -11,7 +11,6 @@ import type { NetplayICEServer, } from "@/__generated__"; import { ROUTES } from "@/plugins/router"; -import playSessionApi from "@/services/api/play-session"; import { saveApi as api } from "@/services/api/save"; import storeAuth from "@/stores/auth"; import storeConfig from "@/stores/config"; @@ -55,7 +54,6 @@ const props = defineProps<{ }>(); const romRef = ref(props.rom); const saveRef = ref(props.save); -const sessionStartTime = ref(null); const deviceIDRef = ref(authStore.user?.current_device_id ?? undefined); const theme = useTheme(); const emitter = inject>("emitter"); @@ -385,8 +383,6 @@ window.EJS_onSaveState = async function ({ }; window.EJS_onGameStart = async () => { - sessionStartTime.value = new Date(); - // Install netplay overrides synchronously, before any await below, so they // are in place before room polling or a Create/Join action can start. const netplay = window.EJS_emulator?.netplay; @@ -501,37 +497,12 @@ window.EJS_onGameStart = async () => { }; function immediateExit() { - if (!sessionStartTime.value) { - return router - .push({ name: ROUTES.ROM, params: { rom: romRef.value.id } }) - .catch((error) => { - console.error("Error navigating to console rom", error); - }); - } - - const endTime = new Date(); - const durationMs = endTime.getTime() - sessionStartTime.value.getTime(); - - playSessionApi - .ingestPlaySessions({ - deviceId: deviceIDRef.value, - sessions: [ - { - rom_id: romRef.value.id, - start_time: sessionStartTime.value.toISOString(), - end_time: endTime.toISOString(), - duration_ms: durationMs, - }, - ], - }) - .catch((err) => console.error("Failed to submit play session:", err)) - .finally(() => { - sessionStartTime.value = null; - router - .push({ name: ROUTES.ROM, params: { rom: romRef.value.id } }) - .catch((error) => { - console.error("Error navigating to console rom", error); - }); + // Play-session recording is owned by the v2 player shell (usePlaySession); + // this only returns to the game details view. + router + .push({ name: ROUTES.ROM, params: { rom: romRef.value.id } }) + .catch((error) => { + console.error("Error navigating to console rom", error); }); }