diff --git a/client/__tests__/BacklogRoulette.test.tsx b/client/__tests__/BacklogRoulette.test.tsx index 051357208..8c917e8b6 100644 --- a/client/__tests__/BacklogRoulette.test.tsx +++ b/client/__tests__/BacklogRoulette.test.tsx @@ -36,6 +36,22 @@ describe("Backlog roulette", () => { vi.restoreAllMocks(); }); + it("formats fractional completion estimates without changing budget eligibility", async () => { + render( + + ); + fireEvent.click(screen.getByRole("button", { name: "Pick a game" })); + expect(screen.getByText("About 5 hours to finish")).toBeVisible(); + fireEvent.keyDown(screen.getByRole("combobox", { name: "Game length" }), { key: "ArrowDown" }); + fireEvent.click( + await screen.findByRole("option", { name: "A short adventure · up to 5 hours" }) + ); + expect(screen.getByText("No games in this draw")).toBeVisible(); + }); + it("draws only visible owned games and never repeats on consecutive rerolls", () => { render( { expect(crypto.getRandomValues).toHaveBeenCalledTimes(2); expect(screen.getByTestId("roulette-title")).toHaveTextContent("Quest a"); }); + + it("draws within the completion budget and keeps unknown lengths in Any length", async () => { + render( + + ); + fireEvent.click(screen.getByRole("button", { name: "Pick a game" })); + expect(screen.getByText("Chosen from 5 matching games.")).toBeVisible(); + const chooseLength = async (name: string) => { + fireEvent.keyDown(screen.getByRole("combobox", { name: "Game length" }), { + key: "ArrowDown", + }); + fireEvent.click(await screen.findByRole("option", { name, exact: true })); + }; + await chooseLength("A short adventure · up to 5 hours"); + expect(screen.getByTestId("roulette-title")).toHaveTextContent("Quest short"); + expect(screen.getByText("About 5 hours to finish")).toBeVisible(); + expect(screen.getByRole("button", { name: "Roll again" })).toBeDisabled(); + await chooseLength("A weekend quest · up to 15 hours"); + expect(screen.getByTestId("roulette-title")).toHaveTextContent("Quest weekend"); + expect(screen.getByText("Chosen from 2 matching games.")).toBeVisible(); + await choosePool("Currently playing"); + expect(screen.getByText("No games in this draw")).toBeVisible(); + await choosePool("Owned games"); + expect(screen.getByText("Chosen from 2 matching games.")).toBeVisible(); + await chooseLength("Any length"); + expect(screen.getByText("Chosen from 5 matching games.")).toBeVisible(); + }); }); diff --git a/client/src/components/BacklogRoulette.tsx b/client/src/components/BacklogRoulette.tsx index 3ed037ad8..d9999d392 100644 --- a/client/src/components/BacklogRoulette.tsx +++ b/client/src/components/BacklogRoulette.tsx @@ -48,19 +48,27 @@ export default function BacklogRoulette({ }) { const [open, setOpen] = useState(false); const [pool, setPool] = useState(pools[0] ?? "all"); + const [maxHours, setMaxHours] = useState("any"); const [selectedId, setSelectedId] = useState(null); const [, navigate] = useLocation(); const [starting, setStarting] = useState(false); - const candidates = games.filter( - (game) => !game.hidden && (pool === "all" || game.status === pool) - ); + function eligibleGames(nextPool: Pool, nextMaxHours: string) { + return games.filter( + (game) => + !game.hidden && + (nextPool === "all" || game.status === nextPool) && + (nextMaxHours === "any" || + (Number.isFinite(game.timeToBeatNormally) && + (game.timeToBeatNormally ?? 0) > 0 && + (game.timeToBeatNormally ?? Infinity) <= Number(nextMaxHours))) + ); + } + const candidates = eligibleGames(pool, maxHours); const selected = candidates.find((game) => game.id === selectedId); /** Draw uniformly from the chosen pool, skipping the previous game when possible. */ - function roll(nextPool = pool) { - const eligible = games.filter( - (game) => !game.hidden && (nextPool === "all" || game.status === nextPool) - ); + function roll(nextPool = pool, nextMaxHours = maxHours) { + const eligible = eligibleGames(nextPool, nextMaxHours); const choices = eligible.length > 1 ? eligible.filter((game) => game.id !== selectedId) : eligible; if (!choices.length) { @@ -148,6 +156,31 @@ export default function BacklogRoulette({ Your current search and filters apply. Hidden games stay out of the draw.

+
+ + +

+ Estimated total time to finish, not a play session. Length filters exclude games + without a known normal completion time. +

+
{selected ? (
@@ -166,6 +199,16 @@ export default function BacklogRoulette({ {selected.title} + {Number.isFinite(selected.timeToBeatNormally) && + (selected.timeToBeatNormally ?? 0) > 0 && ( +

+ About{" "} + {selected.timeToBeatNormally?.toLocaleString("en-US", { + maximumFractionDigits: 1, + })}{" "} + hours to finish +

+ )}

{selected.genres?.join(" · ")}

@@ -188,7 +231,7 @@ export default function BacklogRoulette({

{candidates.length ? "Roll again to pick from your updated list." - : "Choose another pool or adjust your filters to find a game."} + : "Choose another pool, game length or Library filters to find a game."}

)} diff --git a/docs/BACKLOG_ROULETTE.md b/docs/BACKLOG_ROULETTE.md index c35494215..697e354ac 100644 --- a/docs/BACKLOG_ROULETTE.md +++ b/docs/BACKLOG_ROULETTE.md @@ -28,6 +28,17 @@ Drawing a game never changes your collection; only Mark as Playing changes statu No API key, command line or additional setup is needed. The feature uses the Library's existing authenticated data and status update endpoint. +## Find a weekend quest + +Use **Game length** to choose **A short adventure** (up to 5 hours), **A weekend +quest** (up to 15 hours), or **A longer journey** (up to 40 hours). Roulette uses +the game's normal completion estimate from IGDB. This is estimated total time +to finish, not a single play session or remaining time for a game in progress. +Games without a positive, known estimate stay out of length-filtered draws; +**Any length**, the default, includes them. The selected game's estimate appears +beside its status. Changing the length draws again using the same pool and +Library filters. + ## Pick from Playing On **Playing**, **Pick a game** draws only from your currently playing games diff --git a/docs/pr-evidence/weekend-quest-desktop.png b/docs/pr-evidence/weekend-quest-desktop.png new file mode 100644 index 000000000..c0db1b21a Binary files /dev/null and b/docs/pr-evidence/weekend-quest-desktop.png differ diff --git a/docs/pr-evidence/weekend-quest-light.png b/docs/pr-evidence/weekend-quest-light.png new file mode 100644 index 000000000..af5c1e87a Binary files /dev/null and b/docs/pr-evidence/weekend-quest-light.png differ diff --git a/docs/pr-evidence/weekend-quest-mobile.png b/docs/pr-evidence/weekend-quest-mobile.png new file mode 100644 index 000000000..4e185c3a7 Binary files /dev/null and b/docs/pr-evidence/weekend-quest-mobile.png differ diff --git a/tests/e2e/backlog-roulette.spec.ts b/tests/e2e/backlog-roulette.spec.ts index be66d2049..3892b0a13 100644 --- a/tests/e2e/backlog-roulette.spec.ts +++ b/tests/e2e/backlog-roulette.spec.ts @@ -2,6 +2,45 @@ import { test, expect } from "@playwright/test"; import { seedGame, uniqueId } from "./helpers"; test.describe("Backlog roulette", () => { + test("picks a weekend quest by completion time", async ({ page }, testInfo) => { + const id = uniqueId(); + const prefix = `Weekend quest ${id}`; + await page.goto("/"); + const short = await seedGame(page, { + title: `${prefix} Short adventure`, + igdbId: id, + status: "owned", + timeToBeatNormally: 4.56789, + }); + await seedGame(page, { + title: `${prefix} Epic journey`, + igdbId: id + 1, + status: "owned", + timeToBeatNormally: 60, + }); + await seedGame(page, { title: `${prefix} Mystery`, igdbId: id + 2, status: "owned" }); + await page.reload(); + await page.getByPlaceholder("Search your library...").fill(prefix); + await page.getByRole("button", { name: "Pick a game", exact: true }).click(); + const dialog = page.getByRole("dialog", { name: "Backlog roulette" }); + await dialog.getByRole("combobox", { name: "Game length" }).click(); + await page.getByRole("option", { name: "A weekend quest · up to 15 hours" }).click(); + await expect(dialog.getByTestId("roulette-title")).toHaveText(short.title); + await expect(dialog.getByText("Chosen from 1 matching game.")).toBeVisible(); + await expect(dialog.getByText("About 4.6 hours to finish")).toBeVisible(); + await expect(dialog.getByRole("button", { name: "Roll again" })).toBeDisabled(); + await page.screenshot({ path: testInfo.outputPath("weekend-quest-desktop.png") }); + await page.setViewportSize({ width: 390, height: 844 }); + await page.screenshot({ path: testInfo.outputPath("weekend-quest-mobile.png") }); + await page.evaluate(() => { + document.documentElement.classList.remove("dark"); + document.documentElement.classList.add("light"); + }); + await page.screenshot({ path: testInfo.outputPath("weekend-quest-light.png") }); + await dialog.getByRole("combobox", { name: "Game length" }).click(); + await page.getByRole("option", { name: "Any length", exact: true }).click(); + await expect(dialog.getByText("Chosen from 3 matching games.")).toBeVisible(); + }); test("draws owned games, rerolls, opens details and saves Mark as Playing", async ({ page, }, testInfo) => { diff --git a/tests/e2e/helpers.ts b/tests/e2e/helpers.ts index c78c2e82d..7b484287a 100644 --- a/tests/e2e/helpers.ts +++ b/tests/e2e/helpers.ts @@ -7,7 +7,7 @@ import type { Page } from "@playwright/test"; */ export async function seedGame( page: Page, - game: { title: string; igdbId: number; status?: string } + game: { title: string; igdbId: number; status?: string; timeToBeatNormally?: number } ): Promise<{ id: string; title: string }> { const cookies = await page.context().cookies(); const csrf = cookies.find((c) => c.name === "questarr_csrf")?.value ?? ""; @@ -17,6 +17,7 @@ export async function seedGame( title: game.title, igdbId: game.igdbId, status: game.status ?? "wanted", + timeToBeatNormally: game.timeToBeatNormally, platforms: ["PC (Microsoft Windows)"], genres: ["Adventure"], releaseDate: "2020-01-15",