The fog layer (DESIGN 7.1 layer 4, spec #63 decision 9) keeps the explored grid in one cell-resolution RGBA canvas: never-seen cells opaque black, explored cells black at 0.6 alpha. A second canvas is the sprite texture: the explored canvas is copied into it and the union of the server's vision polygons is cut out with `destination-out`. A full explored snapshot repaints; a cell diff mutates only the new bytes and uploads the dirty rectangle. Player markers from the marker set/del frames are sprites above the fog; the GM layer hides the fog. The cut-out is baked instead of using a Graphics stencil mask: on the e2e's software renderer the stencil state cost ~350 ms of the phone-class viewport fill budget (412x869 dpr 2.625), while baking keeps the fill at the pre-fog baseline. The cut-out is rasterized at cell resolution, the same grid the server sends. Integration fix in the tile layer: a tile denied with 403 because it was unexplored is asked once more when the fog says its cells became explored; unexplored denials still never retry (the map 403 test is unchanged). Proven at the reducer seam (fog slice) and by screenshot pixel probes in the browser e2e: black/dim/visible, window see-through, one-way blind side, secret door, marker anchor and retraction, a drag that moves the fog with no reload, and a reconnect resync. Fog update cost recorded under the 412x869 dpr 2.625 / 4x CPU emulation.
212 lines
6.8 KiB
TypeScript
212 lines
6.8 KiB
TypeScript
// The tile scheduler (#69, spec #63 decision 12): viewport-order requests, a bounded concurrency of
|
|
// 8, a decoded-texture cache the tile module keeps across reconnect, no duplicate in-flight fetch,
|
|
// and a denied tile (403/404) is remembered so the client never retries it in a loop.
|
|
import { describe, expect, it } from 'vitest'
|
|
import { TILE_CONCURRENCY, TileScheduler, tilesForCells } from '../src/tiles.ts'
|
|
import type { TileLoadResult } from '../src/tiles.ts'
|
|
|
|
interface Harness {
|
|
scheduler: TileScheduler
|
|
calls: string[]
|
|
maxActive: number
|
|
settle(url: string, result: TileLoadResult): void
|
|
settleAll(result: TileLoadResult): void
|
|
idle: number
|
|
}
|
|
|
|
function harness(concurrency = TILE_CONCURRENCY): Harness {
|
|
const calls: string[] = []
|
|
const pending = new Map<string, (result: TileLoadResult) => void>()
|
|
let active = 0
|
|
let maxActive = 0
|
|
const state = { idle: 0 }
|
|
const scheduler = new TileScheduler({
|
|
concurrency,
|
|
load: url => {
|
|
calls.push(url)
|
|
active += 1
|
|
maxActive = Math.max(maxActive, active)
|
|
return new Promise<TileLoadResult>(resolve => {
|
|
pending.set(url, result => {
|
|
active -= 1
|
|
pending.delete(url)
|
|
resolve(result)
|
|
})
|
|
})
|
|
},
|
|
onIdle: () => {
|
|
state.idle += 1
|
|
},
|
|
})
|
|
return {
|
|
scheduler,
|
|
calls,
|
|
get maxActive() {
|
|
return maxActive
|
|
},
|
|
settle(url, result) {
|
|
const resolve = pending.get(url)
|
|
if (!resolve) throw new Error(`no pending load for ${url}`)
|
|
resolve(result)
|
|
},
|
|
settleAll(result) {
|
|
for (const url of [...pending.keys()]) {
|
|
pending.get(url)?.(result)
|
|
pending.delete(url)
|
|
}
|
|
},
|
|
get idle() {
|
|
return state.idle
|
|
},
|
|
}
|
|
}
|
|
|
|
function urls(count: number): string[] {
|
|
return Array.from({ length: count }, (_, index) => `/tiles/1/v1/${index}_0.png`)
|
|
}
|
|
|
|
/** Let every queued promise callback and the next pump round run. */
|
|
async function flush(): Promise<void> {
|
|
for (let i = 0; i < 20; i++) await Promise.resolve()
|
|
}
|
|
|
|
describe('TileScheduler', () => {
|
|
it('starts at most 8 loads at once, in the order asked', async () => {
|
|
expect(TILE_CONCURRENCY).toBe(8)
|
|
const h = harness()
|
|
const list = urls(20)
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls).toEqual(list.slice(0, 8))
|
|
expect(h.maxActive).toBe(8)
|
|
h.settle(list[0] as string, 'ok')
|
|
await flush()
|
|
expect(h.calls[8]).toBe(list[8])
|
|
expect(h.maxActive).toBe(8)
|
|
// Each settle opens a slot for the next queued tile; drain every round.
|
|
for (let round = 0; round < 4 && h.calls.length < 20; round++) {
|
|
h.settleAll('ok')
|
|
await flush()
|
|
}
|
|
expect(h.calls.length).toBe(20)
|
|
expect(h.maxActive).toBe(8)
|
|
})
|
|
|
|
it('never asks twice for the same tile', () => {
|
|
const h = harness()
|
|
const list = urls(4)
|
|
h.scheduler.setDesired(list)
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls).toEqual(list)
|
|
})
|
|
|
|
it('remembers a denied tile and never retries it', async () => {
|
|
const h = harness()
|
|
const list = urls(1)
|
|
h.scheduler.setDesired(list)
|
|
h.settle(list[0] as string, 'denied')
|
|
await flush()
|
|
expect(h.scheduler.status(list[0] as string)).toBe('denied')
|
|
h.scheduler.setDesired(list)
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls).toEqual(list)
|
|
})
|
|
|
|
it('reports idle once when every wanted tile is settled', async () => {
|
|
const h = harness()
|
|
const list = urls(3)
|
|
h.scheduler.setDesired(list)
|
|
h.settleAll('ok')
|
|
await flush()
|
|
expect(h.scheduler.isComplete(list)).toBe(true)
|
|
expect(h.idle).toBe(1)
|
|
})
|
|
|
|
it('drops queued work that left the viewport (no prefetch beyond the margin)', async () => {
|
|
const h = harness()
|
|
const list = urls(20)
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls.length).toBe(8)
|
|
h.scheduler.setDesired([list[0] as string])
|
|
h.settleAll('ok')
|
|
await flush()
|
|
// Only the first 8 were ever started; the 12 queued tiles were cancelled.
|
|
expect(h.calls.length).toBe(8)
|
|
expect(h.scheduler.status(list[19] as string)).toBeUndefined()
|
|
})
|
|
|
|
it('does not retry a failed tile on the same window, only on a new one', async () => {
|
|
const h = harness()
|
|
const list = urls(2)
|
|
h.scheduler.setDesired([list[0] as string])
|
|
h.settle(list[0] as string, 'error')
|
|
await flush()
|
|
expect(h.scheduler.isComplete([list[0] as string])).toBe(false)
|
|
expect(h.calls).toEqual([list[0]])
|
|
// Same window: the rAF sync after the failure must not hammer the server.
|
|
h.scheduler.setDesired([list[0] as string])
|
|
expect(h.calls).toEqual([list[0]])
|
|
// A new window that still wants the tile asks once more.
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls).toEqual([list[0], list[0], list[1]])
|
|
})
|
|
|
|
it('markLoaded satisfies a tile from the texture cache without a request', () => {
|
|
const h = harness()
|
|
const list = urls(1)
|
|
h.scheduler.markLoaded(list[0] as string)
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls).toEqual([])
|
|
expect(h.scheduler.isComplete(list)).toBe(true)
|
|
})
|
|
|
|
it('retryFailed makes a transient failure askable again after a reconnect', async () => {
|
|
const h = harness()
|
|
const list = urls(1)
|
|
h.scheduler.setDesired(list)
|
|
h.settle(list[0] as string, 'error')
|
|
await flush()
|
|
h.scheduler.retryFailed()
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls).toEqual([list[0], list[0]])
|
|
})
|
|
})
|
|
|
|
describe('fog-driven denied retry (#70)', () => {
|
|
it('re-asks a denied tile exactly once after its cells became explored', async () => {
|
|
const h = harness()
|
|
const list = urls(2)
|
|
h.scheduler.setDesired(list)
|
|
h.settle(list[0] as string, 'denied')
|
|
h.settle(list[1] as string, 'ok')
|
|
await flush()
|
|
expect(h.calls).toEqual(list)
|
|
|
|
// The fog says cells under the first tile became explored: only that denied tile is stale.
|
|
h.scheduler.retryDenied([list[0] as string])
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls).toEqual([...list, list[0]])
|
|
h.settleAll('ok')
|
|
await flush()
|
|
|
|
// A plain re-sync (no new cells) must not ask again: the denial is still remembered.
|
|
h.scheduler.setDesired(list)
|
|
h.scheduler.retryDenied([])
|
|
h.scheduler.setDesired(list)
|
|
expect(h.calls).toEqual([...list, list[0]])
|
|
})
|
|
|
|
it('maps explored cells to the tiles they touch', () => {
|
|
const scene = { id: 3, art_version: 7, tile_size: 32 }
|
|
const grid = { cols: 4, cell: 32 }
|
|
// Cells (1,0) -> tile (1,0), (1,1) -> tile (1,1), (3,1) -> tile (3,1).
|
|
expect(tilesForCells(scene, grid, [1, 5, 7])).toEqual([
|
|
'/tiles/3/v7/1_0.png',
|
|
'/tiles/3/v7/1_1.png',
|
|
'/tiles/3/v7/3_1.png',
|
|
])
|
|
// Two cells in one tile are one url; a smaller cell than a tile still maps correctly.
|
|
expect(tilesForCells(scene, { cols: 16, cell: 8 }, [0, 1, 2])).toEqual(['/tiles/3/v7/0_0.png'])
|
|
expect(tilesForCells(scene, { cols: 0, cell: 0 }, [1])).toEqual([])
|
|
})
|
|
})
|