A project could only be played inside the editor, behind the login.
To show a game to someone else there was no way short of an account.
A release freezes the saved project under a random token and serves
it on a page that holds nothing but the game view.
- Menu -> Release... opens a dialog with the state of the release,
the link with Copy link and Open, Publish or Update release, and
Take down behind a confirmation.
- Publish saves first and refuses while anything is unsaved, like
snapshots. Updating keeps the link. Take down kills it, and a new
Publish hands out a new token.
- The link is play.html?game=<32 hex>. The page needs no session,
paints Theme.colors.background, zooms with Auto and waits for
"Click to play" so that sound and keyboard focus are allowed.
- The game always starts in the start scene. Persistent variables
are stored per link in the browser. Plugins are not loaded, and the
dialog warns about them and about a project without a scene.
- GET ?r=releases/{token} is the only route besides auth/* that works
without a login. The public answer carries the token as project.id,
so the project id stays hidden. Release files are written with 0640
and nginx does not serve them directly (measured 403).
- Starting, the frame and Load scene moved into runtime/GameRun.ts.
Play in the editor and the play page run through the same code.
- WorldViewport moved to src/viewport/ and no longer needs the
EditorState. Pointer forwarding (gamePointer.ts) and setting up
textures, fonts and sounds (projectFiles.ts) exist only once.
- Vite builds two pages. The play page loads 4 kB of its own code
plus the shared chunk (Pixi, runtime, drawing, 653 kB) and Rapier,
without the editor.
Found on the way, not fixed here: data/projects/<id>.json answers 200
without a login because the files are 0644.
Tests:
- ./check.sh: 194 files, 2808 tests, exit 0.
- tests/php/releases.test.php: 20 checks, also added to CI.
- New e2e module 60 with 10 checks, played in a browser without a
session. Module 46 counts the menu entries and was updated.
- Full e2e run: 1572 checks passed.
- Counter-checks: 3 unit, 2 PHP and 3 browser mutations all fail.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.6 KiB
TypeScript
66 lines
2.6 KiB
TypeScript
// The page of a released game (#266): the game view and nothing else, no editor and no sign-in.
|
|
// Static imports for the same reason as in `main.ts`: Pixi only knows the render pipes that exist
|
|
// when the renderer is created.
|
|
import type { Ticker } from 'pixi.js';
|
|
import { deserializeProject } from './core/store/serialize';
|
|
import { loadReleasedGame } from './core/store/HttpReleaseStore';
|
|
import { releaseToken } from './core/store/ReleaseStore';
|
|
import { saveArea } from './editor/saves';
|
|
import { createPage, showFailure } from './page';
|
|
import { GameScreen } from './player/GameScreen';
|
|
import { installPlayerHook } from './player/playerHook';
|
|
import { Player } from './player/Player';
|
|
import { ui } from './ui';
|
|
import { projectPictureFactory } from './viewport/generatedPictures';
|
|
import { preloadProjectFiles, setUpProjectFiles } from './viewport/projectFiles';
|
|
import { textureLibrary } from './viewport/TextureLibrary';
|
|
import { createEmptyProject } from './core/model/Project';
|
|
|
|
const API_ENDPOINT = '/game-maker/api/index.php';
|
|
|
|
async function main(): Promise<void> {
|
|
const params = new URLSearchParams(location.search);
|
|
const { app, root } = await createPage(params);
|
|
// Every key belongs to the game, Tab included: there is nothing else on the page to move to.
|
|
ui().focus.tabBelongsElsewhere = () => true;
|
|
setUpProjectFiles(app.renderer);
|
|
|
|
const source = { project: createEmptyProject('', ''), on: () => {} };
|
|
const screen = new GameScreen(source);
|
|
root.add(screen, 1);
|
|
root.resize(app.screen.width, app.screen.height);
|
|
let player: Player | null = null;
|
|
installPlayerHook(screen, () => player);
|
|
|
|
const token = releaseToken(location.search);
|
|
if (!token) {
|
|
screen.say('This link does not lead to a game.');
|
|
return;
|
|
}
|
|
screen.say('Loading…');
|
|
let game;
|
|
try {
|
|
game = await loadReleasedGame(API_ENDPOINT, token);
|
|
} catch (error) {
|
|
console.error('[release]', error);
|
|
screen.say('This game is not available.');
|
|
return;
|
|
}
|
|
const project = deserializeProject(game.project, token);
|
|
source.project = project;
|
|
document.title = project.name || 'Game';
|
|
textureLibrary.setPictureFactory(projectPictureFactory(() => project));
|
|
preloadProjectFiles(project);
|
|
|
|
const loaded = new Player(project, screen, saveArea(token, 'release'));
|
|
player = loaded;
|
|
app.ticker.add((ticker: Ticker) => loaded.tick(ticker.deltaMS / 1000));
|
|
// A key starts the game as well as a click does: both count as the gesture sound waits for.
|
|
window.addEventListener('keydown', (e) => {
|
|
if ((e.key === 'Enter' || e.key === ' ') && screen.canStart) loaded.start();
|
|
});
|
|
await loaded.prepare();
|
|
}
|
|
|
|
main().catch(showFailure);
|