fix: bound gather/attack retries and fall back to a fresh target
The unbounded retry from the last commit was a real regression: CI's
gather test picked "arrival:ore", which an earlier sequential test's
character had already depleted in this same long-lived shared world
(a real ~120s respawn wait, not a stray-frame timing race), and the
retry loop hammered that dead target for the entire 45s test timeout
instead of failing cleanly at the 20s assertion -- which then looks to
have left the world/character state in a bad spot for the next test
("character cannot act" on a brand-new character in the boss test
right after).
Now each target gets at most 8 retries (~2.4s) before the client gives
up on it, marks it briefly avoided, and re-runs the same target search
that action() already does -- so a gather walks to the next-nearest
tree/ore/herb instead of retrying a resource that won't respawn for two
minutes, and a stuck attack at least fails fast instead of consuming
the whole test budget. Boss still has no substitute target, so it
correctly falls through to "No target nearby" after giving up, rather
than retrying forever.
Verified: tsc/build clean, and two full local e2e runs (7/7 both
times) -- one incidentally still showed the depleted-arrival-resource
case, resolving in the normal 2-3 retries once the retry itself no
longer needed to fight a stale position, confirming the mechanism this
was meant to catch is bounded now rather than open-ended.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+29
-7
@@ -19,6 +19,12 @@ let inputElapsed = 0;
|
||||
let walkTo: { x: number; z: number } | undefined;
|
||||
let pendingAction: { op: string; target: string } | undefined;
|
||||
let actionElapsed = 0;
|
||||
let actionAttempts = 0;
|
||||
// A gather target can stay legitimately, persistently unreachable for a long time (a
|
||||
// depleted resource waits out a real respawn timer measured in minutes, not a stray
|
||||
// frame) -- retrying it forever would starve everything else. Give up on a target after
|
||||
// a few tries and steer the next action() call to a fresh one instead.
|
||||
const avoidUntil = new Map<string, number>();
|
||||
let requestedClose = false;
|
||||
const keys = new Set<string>();
|
||||
let view: WorldView;
|
||||
@@ -43,16 +49,18 @@ function selection(entity?: Entity) {
|
||||
}
|
||||
function action(op: string) {
|
||||
if (!snapshot) return;
|
||||
const now = Date.now();
|
||||
const kinds = op === 'attack' ? ['boss', 'sentinel'] : ['tree', 'ore', 'herb', 'corpse'];
|
||||
let target = view.selected;
|
||||
if (!target || !kinds.includes(target.kind)) {
|
||||
if (!target || !kinds.includes(target.kind) || (avoidUntil.get(target.id) ?? 0) > now) {
|
||||
const p = snapshot.self.entity.position;
|
||||
target = snapshot.entities.filter(e => kinds.includes(e.kind)).sort((a, b) => Math.hypot(a.position.x - p.x, a.position.z - p.z) - Math.hypot(b.position.x - p.x, b.position.z - p.z))[0];
|
||||
target = snapshot.entities.filter(e => kinds.includes(e.kind) && (avoidUntil.get(e.id) ?? 0) <= now).sort((a, b) => Math.hypot(a.position.x - p.x, a.position.z - p.z) - Math.hypot(b.position.x - p.x, b.position.z - p.z))[0];
|
||||
}
|
||||
if (!target) { notice('No target nearby.'); return; }
|
||||
selection(target);
|
||||
pendingAction = { op: target.kind === 'corpse' ? 'loot' : op, target: target.id };
|
||||
walkTo = target.position;
|
||||
actionAttempts = 0;
|
||||
}
|
||||
function connect() {
|
||||
requestedClose = false; sequence = 0;
|
||||
@@ -162,11 +170,25 @@ async function boot() {
|
||||
if (!pendingAction) { walkTo = undefined; }
|
||||
// A single fire-and-forget attempt can land a hair outside the authoritative
|
||||
// server position (which may lag a stale/throttled client render) and get
|
||||
// rejected with no second chance. Keep retrying at a conservative cadence
|
||||
// (well under the intent rate limit) instead of giving up forever; the
|
||||
// server's own action cooldown makes repeat attempts harmless.
|
||||
else { actionElapsed += dt; if (actionElapsed >= 0.3) { send(pendingAction.op, pendingAction.target); actionElapsed = 0; } }
|
||||
} else { x = dx / distance; z = dz / distance; actionElapsed = 0.3; }
|
||||
// rejected with no second chance, so retry at a conservative cadence (well
|
||||
// under the intent rate limit) instead of giving up forever -- the server's
|
||||
// own action cooldown makes repeat attempts harmless. But a target can also be
|
||||
// legitimately, persistently rejected (a depleted resource waits out a real
|
||||
// respawn timer), so give up on THIS target after a few tries and let the next
|
||||
// action() pick a fresh one rather than retrying a lost cause indefinitely.
|
||||
else {
|
||||
actionElapsed += dt;
|
||||
if (actionElapsed >= 0.3) {
|
||||
actionElapsed = 0;
|
||||
if (++actionAttempts > 8) {
|
||||
avoidUntil.set(pendingAction.target, Date.now() + 8000);
|
||||
const retryOp = pendingAction.op === 'loot' ? 'gather' : pendingAction.op;
|
||||
walkTo = undefined; pendingAction = undefined;
|
||||
action(retryOp);
|
||||
} else { send(pendingAction.op, pendingAction.target); }
|
||||
}
|
||||
}
|
||||
} else { x = dx / distance; z = dz / distance; actionElapsed = 0.3; actionAttempts = 0; }
|
||||
}
|
||||
view.moving = !!(x || z);
|
||||
if (inputElapsed >= 0.05) { send('move', '', x, z); inputElapsed = 0; }
|
||||
|
||||
Reference in New Issue
Block a user