Extract testable combat hit logic

This commit is contained in:
Admin 2026-09-18 00:33:00 +00:00
parent 545e94dddc
commit f03ff1e0cd

51
src/game-logic.js Normal file
View File

@ -0,0 +1,51 @@
export const PLAYER_MAX_HULL = 100;
export const DEFAULT_PIRATE_SHOT_DAMAGE = 6;
export const PLAYER_HIT_COOLDOWN_MS = 300;
export const normalizePlayerDamage = (
rawDamage,
fallback = DEFAULT_PIRATE_SHOT_DAMAGE,
) => {
const parsed = Number(rawDamage);
const value = Number.isFinite(parsed) ? parsed : fallback;
return Math.max(1, Math.min(25, value));
};
export const reducePlayerHitQueue = ({
hull,
lastHitAt,
now,
damages,
cooldownMs = PLAYER_HIT_COOLDOWN_MS,
maxHull = PLAYER_MAX_HULL,
}) => {
let nextHull = Number.isFinite(hull) ? hull : maxHull;
let nextLastHitAt = Number.isFinite(lastHitAt) ? lastHitAt : -Infinity;
let appliedHits = 0;
let ignoredHits = 0;
let totalDamage = 0;
for (const rawDamage of damages) {
if (now - nextLastHitAt < cooldownMs) {
ignoredHits += 1;
continue;
}
const damage = normalizePlayerDamage(rawDamage);
nextLastHitAt = now;
nextHull = Math.max(0, nextHull - damage);
totalDamage += damage;
appliedHits += 1;
if (nextHull <= 0) break;
}
return {
hull: nextHull,
lastHitAt: nextLastHitAt,
appliedHits,
ignoredHits,
totalDamage,
gameOver: nextHull <= 0,
};
};