testsAndMisc/bucket_catch/packages/frontend/src/hooks/usePuzzleGameLoop.ts
Krzysztof kuhy Rudnicki e165ae8987 bucket_catch: 45 eslint errors -> 5, no suppressions
These were unmasked, not caused, last session: `lint` is
`tsc --noEmit && eslint src`, and a broken tsc had been short-circuiting
the && so eslint had never run on this package.

The binding constraint is the 100% coverage gate, not the linter. Most
idiomatic fixes for no-non-null-assertion add a branch that no test can
reach, which trades a lint error for a coverage failure - and the
no-suppressions rule forbids escaping either way. So every fix here is
one that adds no branch:

- no-non-null-assertion 18 -> 2: a waitFor already proves result.current
  is non-null, so capture it into a local instead of asserting on each
  read. App.tsx's files[0]! was pure noise (the same position also
  reported no-unnecessary-type-assertion). usePuzzleGameLoop's shift()!
  became index-then-shift, since the while condition already indexes [0]
  unasserted.
- non-nullable-type-assertion-style 9 -> 0: the rule-swap trap - eslint
  asks for `!`, which the other rule forbids. Narrowed with instanceof.
- unbound-method 10 -> 1: keep the MockInstance vi.spyOn already returns
  rather than detaching the method. This surfaced a latent bug: one test
  calls restoreAllMocks() mid-test, so the beforeEach handle was stale.
- no-unnecessary-type-assertion 5 -> 1, require-await 1 -> 0.

Coverage held at 100%: 584/217/98/535 -> 585/217/98/536. The +1 statement
and +1 line are the index-then-shift split; branches unchanged at 217 is
the number that proves no fix added an untested path. 145 tests, tsc 0.

The 5 remaining are reported, not silenced. App.tsx's fileResult! needs
the phase state to carry the result ({ kind: "done"; result }) - three
smaller structures all failed the same way. The rest are documented in
the evidence file.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 11:48:45 +02:00

197 lines
6.4 KiB
TypeScript

import { useCallback, useEffect, useRef, useState, type RefObject } from "react";
import type { FallingPuzzleItem, PuzzlePiece, PuzzleGameResult } from "../types";
import { sliceImage } from "../lib/sliceImage";
import {
BASKET_Y_OFFSET,
MIN_SPEED,
PIECE_HALF_H,
SPAWN_GAP,
SPEED_RANGE,
aabbCollision,
assignXPositions,
type ScheduledPiece,
} from "./puzzleGeometry";
import { drawBackdrop, drawBasket, drawHUD, drawPiece } from "./puzzleRender";
/**
* Puzzle game loop.
*
* All pieces are scheduled up-front. Their x positions are assigned via
* interval-graph clustering so that pieces arriving simultaneously share a
* spatial cluster the basket can cover in one position — guaranteeing 100%
* is always achievable. Multiple pieces fall simultaneously, creating an
* exciting hectic visual while remaining fair.
*
* The clustering itself lives in `puzzleGeometry.ts` and the canvas drawing in
* `puzzleRender.ts`; what remains here is the per-frame tick.
*/
export function usePuzzleGameLoop(
canvasRef: RefObject<HTMLCanvasElement | null>,
basketXRef: RefObject<number>,
imageFile: File,
gridSize: number,
active: boolean,
): PuzzleGameResult | null {
const [result, setResult] = useState<PuzzleGameResult | null>(null);
const scheduleRef = useRef<ScheduledPiece[]>([]);
const activeItemsRef = useRef<FallingPuzzleItem[]>([]);
const resolvedRef = useRef<FallingPuzzleItem[]>([]);
const resolvedFrameMapRef = useRef<Map<string, number>>(new Map());
const frameRef = useRef<number>(0);
const rafRef = useRef<number>(0);
const imgsRef = useRef<Map<string, HTMLImageElement>>(new Map());
const totalRef = useRef<number>(0);
const startLoop = useCallback(
(pieces: PuzzlePiece[], canvas: HTMLCanvasElement) => {
const ctx = canvas.getContext("2d");
/* istanbul ignore next */
if (!ctx) return;
const basketY = canvas.height - BASKET_Y_OFFSET;
// Build schedule: staggered spawn times, random speeds.
const scheduled: ScheduledPiece[] = pieces.map((piece, i) => ({
piece,
spawnFrame: i * SPAWN_GAP,
speed: MIN_SPEED + Math.random() * SPEED_RANGE,
x: 0, // will be filled by assignXPositions
}));
// Assign x positions with the spatial clustering guarantee.
assignXPositions(scheduled, basketY, canvas.width);
// Sort by spawn time (speeds vary, so original order isn't strictly sorted).
scheduled.sort((a, b) => a.spawnFrame - b.spawnFrame);
scheduleRef.current = scheduled;
activeItemsRef.current = [];
resolvedRef.current = [];
resolvedFrameMapRef.current = new Map();
frameRef.current = 0;
totalRef.current = pieces.length;
const tick = (): void => {
const frame = frameRef.current;
frameRef.current = frame + 1;
drawBackdrop(ctx, canvas);
const basketX = basketXRef.current;
drawBasket(ctx, basketX, basketY);
// Spawn pieces whose time has come.
while (
scheduleRef.current.length > 0 &&
frame >= scheduleRef.current[0].spawnFrame
) {
// The while condition already indexes [0] unasserted, so this is
// the same non-null value; shift() only advances the queue.
const s = scheduleRef.current[0];
scheduleRef.current.shift();
activeItemsRef.current.push({
kind: "puzzle",
id: `puzzle-${s.piece.row}-${s.piece.col}`,
piece: s.piece,
x: s.x,
y: -PIECE_HALF_H,
speed: s.speed,
startFrame: frame,
status: "falling",
});
}
// Update all falling pieces.
const stillActive: FallingPuzzleItem[] = [];
for (const item of activeItemsRef.current) {
const img = imgsRef.current.get(`${item.piece.row}-${item.piece.col}`);
item.y += item.speed;
if (aabbCollision(item.x, item.y, basketX, basketY)) {
item.status = "caught";
resolvedRef.current.push(item);
resolvedFrameMapRef.current.set(item.id, frame);
} else if (item.y > canvas.height + PIECE_HALF_H) {
item.status = "missed";
resolvedRef.current.push(item);
resolvedFrameMapRef.current.set(item.id, frame);
} else {
drawPiece(ctx, item, img, false);
stillActive.push(item);
}
}
activeItemsRef.current = stillActive;
// Flash caught pieces briefly at their catch position.
for (const item of resolvedRef.current) {
if (item.status === "caught") {
// id is always set in the map when piece is resolved (lines above)
const rf = resolvedFrameMapRef.current.get(item.id)!;
if (frame - rf < 30) {
const img = imgsRef.current.get(
`${item.piece.row}-${item.piece.col}`,
);
drawPiece(ctx, item, img, true);
}
}
}
const caughtCount = resolvedRef.current.filter(
(p) => p.status === "caught",
).length;
drawHUD(
ctx,
canvas,
caughtCount,
totalRef.current,
resolvedRef.current.length,
);
if (
activeItemsRef.current.length === 0 &&
scheduleRef.current.length === 0
) {
const caught = resolvedRef.current.filter(
(p) => p.status === "caught",
);
const missed = resolvedRef.current.filter(
(p) => p.status === "missed",
);
setResult({ caughtPieces: caught, missedPieces: missed, gridSize });
return;
}
rafRef.current = requestAnimationFrame(tick);
};
rafRef.current = requestAnimationFrame(tick);
},
[basketXRef, gridSize],
);
useEffect(() => {
if (!active) return;
const canvas = canvasRef.current;
if (!canvas) return;
let cancelled = false;
void sliceImage(imageFile, gridSize).then((pieces) => {
if (cancelled) return;
pieces.forEach((piece) => {
const img = new Image();
img.src = piece.imageUrl;
imgsRef.current.set(`${piece.row}-${piece.col}`, img);
});
startLoop(pieces, canvas);
});
return () => {
cancelled = true;
cancelAnimationFrame(rafRef.current);
};
}, [active, imageFile, gridSize, canvasRef, startLoop]);
return result;
}