feat: client shell, sign-in and protocol client (#67)
New `client/` package (Vite + TypeScript + PixiJS v8, plain DOM overlay): the pure reducer seam (no Pixi import), an append-only slice registry build-time glob, the `proto` 2 WS client with envelope/ack/closing mapping, the sign-in door (`POST /join`, one "Key not recognised." for wrong and unknown), the DOM shell/HUD and a minimal Pixi stage created only after a live `hello`. `hello` resets every slice so a reconnect cannot show stale state; `proto-mismatch` shows reload; `revoked` returns to the sign-in page and stops. The browser e2e moves into the client package (`client/e2e/`, owning playwright-core) and runs the real Vite build through the TLS/HTTP2 test nginx: join -> canvas -> hello/roster for GM and player, no data before join, identical wrong/unknown key refusal, proto mismatch, revoked. `devbox run client-test` adds the Vitest seam; `devbox run e2e` builds the client first. The #66 stub dist stays local to `tools/vhost.test.mjs`, so `devbox run test` still needs no npm install. Refs #67
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
# Render modules: append-only
|
||||
|
||||
Every `*.ts` file here that exports `renderModule` is registered by the build-time glob in
|
||||
`../stage.ts`. Add one file per feature; never edit the registry.
|
||||
|
||||
```ts
|
||||
import type { RenderModule } from '../stage.ts'
|
||||
|
||||
export const renderModule: RenderModule = {
|
||||
name: 'tiles', // unique
|
||||
mount({ app, world, layers, store }) {
|
||||
// create your objects in `layers.*`, subscribe to `store` for state changes,
|
||||
// return a teardown only for global resources you install.
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
Layers bottom to top: `tiles`, `grid`, `tokens`, `fog`, `gm`, `dom` (DESIGN 7.1). Update only the
|
||||
entities that changed (DESIGN 7.2); no per-frame full redraw.
|
||||
@@ -0,0 +1,105 @@
|
||||
// The Pixi stage (DESIGN 7.1): one Application, one world container under pan/zoom, fixed layers
|
||||
// bottom to top. Render modules are append-only, exactly like reducer slices: every file in
|
||||
// ./modules/ that exports `renderModule` is picked up by the glob, so a later ticket (tiles/grid
|
||||
// T6, fog T7, tokens T8, walls T10, GM overlays) adds one file and touches no other module.
|
||||
//
|
||||
// Pixi is imported here (and in modules/) only. The reducer, the WS client and the DOM shell stay
|
||||
// Pixi-free so their unit tests run without a GPU or a DOM.
|
||||
|
||||
import { Application, Container } from 'pixi.js'
|
||||
import type { Store } from '../store.ts'
|
||||
import { PROTO } from '../protocol.ts'
|
||||
|
||||
export interface RenderLayers {
|
||||
tiles: Container
|
||||
grid: Container
|
||||
tokens: Container
|
||||
fog: Container
|
||||
gm: Container
|
||||
dom: Container
|
||||
}
|
||||
|
||||
export interface RenderContext {
|
||||
app: Application
|
||||
world: Container
|
||||
layers: RenderLayers
|
||||
store: Store
|
||||
}
|
||||
|
||||
export interface RenderModule {
|
||||
readonly name: string
|
||||
/** Start drawing. Return a teardown for anything global the module installed. */
|
||||
mount(context: RenderContext): void | (() => void)
|
||||
}
|
||||
|
||||
interface ModuleExport {
|
||||
renderModule: RenderModule
|
||||
}
|
||||
|
||||
const modules = import.meta.glob<ModuleExport>('./modules/*.ts', { eager: true })
|
||||
|
||||
const RENDER_MODULES: readonly RenderModule[] = Object.keys(modules)
|
||||
.sort()
|
||||
.map(path => {
|
||||
const module = modules[path]
|
||||
if (!module || !module.renderModule) {
|
||||
throw new Error(`render module ${path} has no \`renderModule\` export`)
|
||||
}
|
||||
return module.renderModule
|
||||
})
|
||||
|
||||
const moduleNames = new Set(RENDER_MODULES.map(module => module.name))
|
||||
if (moduleNames.size !== RENDER_MODULES.length) throw new Error('duplicate render module name')
|
||||
|
||||
export interface Stage {
|
||||
readonly app: Application
|
||||
readonly context: RenderContext
|
||||
destroy(): void
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the stage inside `host`. The caller creates it only when the session is live, so a
|
||||
* visitor without a session never gets a canvas (story 1). `PROTO` is touched here so the build
|
||||
* keeps the constant referenced even before an intent module exists.
|
||||
*/
|
||||
export async function createStage(host: HTMLElement, store: Store): Promise<Stage> {
|
||||
const app = new Application()
|
||||
await app.init({
|
||||
resizeTo: window,
|
||||
background: 0x000000,
|
||||
antialias: true,
|
||||
autoDensity: true,
|
||||
resolution: Math.min(window.devicePixelRatio || 1, 2),
|
||||
preference: 'webgl',
|
||||
})
|
||||
app.canvas.id = 'stage-canvas'
|
||||
app.canvas.dataset['proto'] = String(PROTO)
|
||||
host.replaceChildren(app.canvas)
|
||||
|
||||
const world = new Container()
|
||||
const layers: RenderLayers = {
|
||||
tiles: new Container(),
|
||||
grid: new Container(),
|
||||
tokens: new Container(),
|
||||
fog: new Container(),
|
||||
gm: new Container(),
|
||||
dom: new Container(),
|
||||
}
|
||||
world.addChild(layers.tiles, layers.grid, layers.tokens, layers.fog, layers.gm, layers.dom)
|
||||
app.stage.addChild(world)
|
||||
|
||||
// A v1 scene is large and the camera starts at the origin until T6 adds pan/zoom; scaling the
|
||||
// world down is not a map feature, so the world simply sits under the camera transform.
|
||||
const context: RenderContext = { app, world, layers, store }
|
||||
const teardowns = RENDER_MODULES.map(module => module.mount(context))
|
||||
|
||||
return {
|
||||
app,
|
||||
context,
|
||||
destroy() {
|
||||
for (const teardown of teardowns) teardown?.()
|
||||
app.destroy(true, { children: true })
|
||||
host.replaceChildren()
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user