outpost/packages/viewer/src/viewer.ts

89 lines
2.4 KiB
TypeScript

import { mountLocalization, localizeTutorial } from "./localization";
import { installPlayerCards, createBoardThumbnail } from "./host-presentation";
import { mount, tick, unmount } from "svelte";
import { registerViewer } from "@boardgamers/protocol/viewer";
import type { GameState, ViewerMove } from "outpost-engine";
import { createActionSounds } from "./lib/sounds";
import App from "./App.svelte";
import { ViewerStore } from "./lib/store.svelte";
import { mountTutorial } from "./tutorial/mount";
import "./lib/theme.css";
registerViewer<GameState, ViewerMove>(
"outpost",
(commands) => {
const store = new ViewerStore(commands);
const sounds = createActionSounds();
const app = mount(App, {
target: commands.target,
props: { store, onPlayerClick: commands.openPlayer },
});
const removeCards = installPlayerCards(commands.target, commands);
const thumbnail = createBoardThumbnail(commands.target);
const localization = mountLocalization(commands.target);
return {
chat: store.chat,
async onThumbnail(size) {
await tick();
return thumbnail.render(commands.target.querySelector(".main"), size, "#0a1421", ".side");
},
async onState(state) {
await localization.ready;
sounds.onState(state);
store.setState(state);
await tick();
},
onMoveResult({ move, ok }) {
store.onMoveResult(move, ok);
},
onPlayer({ index }) {
store.setPlayer(index);
},
onAvatars(avatars) {
store.avatars = avatars;
},
async onPreferences(preferences) {
if (!(await localization.setLocale(preferences.locale))) {
return;
}
store.preferences = preferences;
sounds.onPreferences(preferences);
},
onSettings(settings) {
store.settings = settings;
},
onUndoAvailable(available) {
store.undoAvailable = available;
},
onLog(log) {
store.onGamelog(log);
},
onReplayStart() {
sounds.onReplayStart();
store.startReplay();
},
onReplayTo(to) {
store.replayTo(to);
},
onReplayEnd() {
sounds.onReplayEnd();
store.endReplay();
},
destroy() {
localization.destroy();
removeCards();
thumbnail.destroy();
store.destroy();
sounds.destroy();
void unmount(app);
},
};
},
{ tutorial: localizeTutorial(mountTutorial) }
);
declare global {
interface Window {
outpost: ReturnType<typeof registerViewer<GameState, ViewerMove>>;
}
}