Initialize the map camera when civilization selection completes

This commit is contained in:
coyotte508 2026-10-02 14:49:48 +02:00
commit ea4ef543bf
2 changed files with 48 additions and 10 deletions

View file

@ -62,6 +62,7 @@ try {
}
const a = await open(0),
b = await open(1),
c = await open(2),
spectator = await open(undefined);
const picker = (page) => page.getByRole('dialog', { name: 'Choose your civilization' });
const choices = (page) =>
@ -108,18 +109,50 @@ try {
.waitFor();
assert.equal(await choices(b).count(), 0);
assert.equal(JSON.parse(state).map.tiles.length, 0);
const last = JSON.parse(engine.webView(engine.stripSecret(state, 2), 2)).civilizations[0];
state = engine.tryMove(state, JSON.stringify(last.action), 2);
await Promise.all(pages.map(({ page, seat }) => emit(page, seat)));
await picker(c)
.getByRole('button', { name: /Lock in/ })
.click();
await picker(a).waitFor({ state: 'detached' });
await picker(b).waitFor({ state: 'detached' });
assert.equal(await a.locator('.player-card').count(), 3);
assert.equal(JSON.parse(state).state, 'Playing');
assert.ok(JSON.parse(state).map.tiles.length > 0);
for (const { page, seat } of pages) {
await picker(page).waitFor({ state: 'detached' });
await page.locator('.city-map-label').first().waitFor();
// These labels are projected through the actual map camera every render.
// Checking only game data or player cards misses a camera poisoned by the empty draft map.
await page.evaluate(
() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))),
);
const map = await page.locator('.map-world').evaluate((el) => {
const bounds = el.getBoundingClientRect();
return [...el.querySelectorAll('.city-map-label')].map((label) => {
const rect = label.getBoundingClientRect();
return {
projected: !!label.style.transform && !/NaN|Infinity/.test(label.style.transform),
onMap:
rect.right > bounds.left &&
rect.left < bounds.right &&
rect.bottom > bounds.top &&
rect.top < bounds.bottom,
};
});
});
assert.ok(
map.length >= 3 && map.every((city) => city.projected),
`Seat ${seat}: map camera stays valid after the draft`,
);
assert.ok(
map.some((city) => city.onMap),
`Seat ${seat}: map is visible without a reload`,
);
}
await b.screenshot({ path: `/tmp/clash-civdraft-map-${width}.png` });
assert.deepEqual(errors, []);
for (const { page } of pages) await page.close();
console.log(
`${width}x${height}: independent private picks, preserved selection, waiting/reconnect, spectator privacy, compact lock button and simultaneous reveal verified.`,
`${width}x${height}: independent private picks, preserved selection, waiting/reconnect, spectator privacy, compact lock button and visible map after simultaneous reveal verified.`,
);
}
} finally {

View file

@ -873,6 +873,7 @@ export class World {
this.lastSignature = signature;
this.settleMotion?.();
const previous = new Map(this.pieces.map((p) => [this.pieceKey(p), p.position.clone()]));
const hadTiles = this.tiles.size > 0;
this.clearBoard();
const models = new PieceModels(
(color) => this.material(color),
@ -881,11 +882,14 @@ export class World {
const cityPositions = new Set(s.game.players.flatMap((p) => p.cities?.map((c) => c.position) ?? []));
const unitPositions = new Set(s.game.players.flatMap((p) => p.units?.map((u) => u.position) ?? []));
const coords = s.game.map.tiles.map(([p]) => positionXY(p));
const minX = Math.min(...coords.map((c) => c[0])),
maxX = Math.max(...coords.map((c) => c[0])),
minZ = Math.min(...coords.map((c) => c[1])),
maxZ = Math.max(...coords.map((c) => c[1]));
this.center.set((minX + maxX) / 2, 0, (minZ + maxZ) / 2);
// The civilization draft has no map yet. Empty bounds would poison the camera with NaN.
if (coords.length) {
const minX = Math.min(...coords.map((c) => c[0])),
maxX = Math.max(...coords.map((c) => c[0])),
minZ = Math.min(...coords.map((c) => c[1])),
maxZ = Math.max(...coords.map((c) => c[1]));
this.center.set((minX + maxX) / 2, 0, (minZ + maxZ) / 2);
}
for (const [position, terrain] of mapTiles) {
const kind = typeof terrain === 'string' ? terrain : 'Barren';
const [x, z] = positionXY(position);
@ -1271,7 +1275,8 @@ export class World {
}
}
if (animate) this.animatePieces(previous);
if (this.selectionSignature === '') this.reset();
// Frame a newly created map, including when the last opponent finishes choosing.
if (!hadTiles && this.tiles.size > 0) this.reset();
}
const settler = s.view?.units?.find((u) => s.selectedUnits.includes(u.id));
const focusedPosition =