Support saved analysis positions and translated chat
This commit is contained in:
parent
2e529beae8
commit
bd5b892cdb
13 changed files with 175 additions and 20 deletions
|
|
@ -10,7 +10,7 @@
|
|||
"fmt": "oxfmt",
|
||||
"fmt:check": "oxfmt --check",
|
||||
"simulate": "pnpm --filter fuji-engine build && node scripts/simulate.mjs",
|
||||
"test:browser": "node tests/browser-smoke.mjs && node tests/bgs-bundle-smoke.mjs && node tests/presentation-smoke.mjs && node tests/chat-smoke.mjs && node tests/mobile-readiness-smoke.mjs && node tests/chat-tabs-smoke.mjs && node tests/chat-mentions-smoke.mjs && node tests/protocol-lifecycle-smoke.mjs && node tests/tutorial-browser.mjs"
|
||||
"test:browser": "node tests/browser-smoke.mjs && node tests/bgs-bundle-smoke.mjs && node tests/presentation-smoke.mjs && node tests/chat-smoke.mjs && node tests/mobile-readiness-smoke.mjs && node tests/chat-tabs-smoke.mjs && node tests/chat-mentions-smoke.mjs && node tests/protocol-lifecycle-smoke.mjs && node tests/tutorial-browser.mjs && node tests/analysis-chat-browser.mjs"
|
||||
},
|
||||
"devDependencies": {
|
||||
"oxfmt": "0.61.0",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "fuji-engine",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"private": true,
|
||||
"license": "AGPL-3.0-only",
|
||||
"files": [
|
||||
|
|
|
|||
|
|
@ -1602,3 +1602,27 @@ test("Map records the transferred face privately for lender and recipient", () =
|
|||
assert.deepEqual(stripSecret(s, 0).log.at(-1)!.dice, [die]);
|
||||
assert.equal(stripSecret(s, 0).players[1]!.dice.find((d) => d.id === die.id)!.face, 0);
|
||||
});
|
||||
|
||||
test("analysis uses public log boundaries and keeps player-specific views playable", async () => {
|
||||
const wrapper = await import("../wrapper.js");
|
||||
let state = initGame(3, {}, "analysis-log");
|
||||
const snapshots: State[] = [];
|
||||
for (let i = 0; i < 35 && !state.outcome; i++) {
|
||||
const active = wrapper.currentPlayer(state);
|
||||
const seat = Array.isArray(active) ? active[0]! : active!;
|
||||
const before = state.log.length;
|
||||
state = await wrapper.moveAI(state, seat);
|
||||
if (state.log.length > before) snapshots.push(structuredClone(state));
|
||||
}
|
||||
const original = structuredClone(state);
|
||||
for (const expected of snapshots.slice(1, 5)) {
|
||||
const copy = wrapper.createAnalysis(state, { to: expected.log.length, sourceEnded: true });
|
||||
assert.deepEqual(copy.log, expected.log);
|
||||
assert.equal(copy.counter, expected.counter);
|
||||
const actor = wrapper.currentPlayer(copy);
|
||||
const seat = Array.isArray(actor) ? actor[0]! : actor!;
|
||||
assert.ok(wrapper.stripSecret(copy, seat).players[seat]);
|
||||
assert.ok((await wrapper.moveAI(copy, seat)).history.length > copy.history.length);
|
||||
}
|
||||
assert.deepEqual(state, original);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1200,11 +1200,12 @@ export function setPlayerName(s: State, seat: number, name: string): State {
|
|||
next.history.push({ player: seat, move: { action: "$name", name } });
|
||||
return next;
|
||||
}
|
||||
export function replay(s: State, to?: number): State {
|
||||
export function replay(s: State, to?: number, logTo?: number): State {
|
||||
// Saves predating skill assignment used the fixed roster.
|
||||
let state = initGame(s.players.length, { skillAssignment: "fixed", ...s.initOptions }, s.seed);
|
||||
state.initOptions = structuredClone(s.initOptions);
|
||||
for (const e of s.history.slice(0, to ?? s.history.length)) {
|
||||
if (logTo !== undefined && state.log.length >= logTo) break;
|
||||
if (e.move.action === "$planning") {
|
||||
restorePlanning(state, e.move.snapshot as unknown as PlanningSnapshot);
|
||||
state.history.push(structuredClone(e));
|
||||
|
|
@ -1213,6 +1214,6 @@ export function replay(s: State, to?: number): State {
|
|||
else if (e.move.action === "$name") state = setPlayerName(state, e.player, e.move.name as string);
|
||||
else state = applyMove(state, e.move, e.player);
|
||||
}
|
||||
if (to === undefined && s.planningSnapshot) restorePlanning(state, s.planningSnapshot);
|
||||
if (to === undefined && logTo === undefined && s.planningSnapshot) restorePlanning(state, s.planningSnapshot);
|
||||
return state;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,3 +53,13 @@ export function messages(data: State): { messages: string[]; data: State } {
|
|||
delete next.chatMessages;
|
||||
return { messages, data: next };
|
||||
}
|
||||
|
||||
export function createAnalysis(s: State, { to }: { to: number; sourceEnded: boolean }): State {
|
||||
if (!Number.isInteger(to) || to < 0 || to > s.log.length) throw Error("Invalid history position.");
|
||||
const copy = to === s.log.length ? replayGame(s) : replayGame(s, undefined, to);
|
||||
copy.players.forEach((player, seat) => {
|
||||
player.name = s.players[seat]!.name;
|
||||
});
|
||||
delete copy.chatMessages;
|
||||
return copy;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "fuji-viewer",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"private": true,
|
||||
"license": "AGPL-3.0-only",
|
||||
"type": "module",
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
"test:tutorial": "node --test src/tutorial/*.spec.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@boardgamers/protocol": "0.3.3",
|
||||
"@boardgamers/protocol": "0.4.0",
|
||||
"fuji-engine": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
|
|
@ -501,7 +501,7 @@
|
|||
</span>
|
||||
</div>
|
||||
<nav class="game-utilities" aria-label="Game options">
|
||||
{#if store.chatState.enabled}<button
|
||||
{#if store.chatAvailable}<button
|
||||
class="utility-button"
|
||||
onclick={openChat}
|
||||
title="Chat"
|
||||
|
|
@ -520,7 +520,7 @@
|
|||
requestAnimationFrame(() =>
|
||||
document
|
||||
.querySelector(
|
||||
window.innerWidth <= 850 && store.chatState.enabled
|
||||
window.innerWidth <= 850 && store.chatAvailable
|
||||
? ".mobile-journal .inline-journal"
|
||||
: ".inline-journal"
|
||||
)
|
||||
|
|
@ -632,7 +632,7 @@
|
|||
(s.phase === "equipment" && me.ready && !canReopenChoice(s, seat!)) ||
|
||||
(s.phase === "eruption" && seat !== 0)}>{mobileLabel} →</button
|
||||
>
|
||||
{#if store.chatState.enabled && store.chatState.unreadIds.length}<button class="dock-chat" onclick={openChat}
|
||||
{#if store.chatAvailable && store.chatState.unreadIds.length}<button class="dock-chat" onclick={openChat}
|
||||
>Chat{store.chatState.unreadIds.length ? ` · ${store.chatState.unreadIds.length}` : ""}</button
|
||||
>{/if}
|
||||
</div>{/if}
|
||||
|
|
@ -881,11 +881,11 @@
|
|||
</div>{/if}
|
||||
{/if}
|
||||
</section>
|
||||
<div class:desktop-journal={store.chatState.enabled}>{@render journalPanel("desktop")}</div>
|
||||
<div class:desktop-journal={store.chatAvailable}>{@render journalPanel("desktop")}</div>
|
||||
</div>
|
||||
<aside class="journey">
|
||||
<div class="social-panel" data-tab={socialTab}>
|
||||
{#if store.chatState.enabled}<nav class="social-tabs" aria-label="Chat and journal">
|
||||
{#if store.chatAvailable}<nav class="social-tabs" aria-label="Chat and journal">
|
||||
<button
|
||||
class:active={socialTab === "chat"}
|
||||
onclick={() => {
|
||||
|
|
@ -902,7 +902,7 @@
|
|||
>
|
||||
</nav>{/if}
|
||||
<ChatPanel {store} />
|
||||
{#if store.chatState.enabled}<div class="mobile-journal">{@render journalPanel("mobile")}</div>{/if}
|
||||
{#if store.chatAvailable}<div class="mobile-journal">{@render journalPanel("mobile")}</div>{/if}
|
||||
</div>
|
||||
<div class="journey-actions">
|
||||
<h2 class="sr-only">{nextAction}</h2>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,19 @@
|
|||
<script lang="ts">
|
||||
import { chatDateSeparators } from "@boardgamers/protocol/chat";
|
||||
import { chatDateSeparators, chatTranslationTitle } from "@boardgamers/protocol/chat";
|
||||
import type { Store } from "./store.svelte";
|
||||
import { bindChatComposer, bindChatViewport, type ChatSuggestions } from "@boardgamers/protocol/chat/dom";
|
||||
let { store }: { store: Store } = $props();
|
||||
const chat = $derived(store.chatState);
|
||||
const labels = $derived(
|
||||
chat.translationLabels ?? {
|
||||
translate: "Translate",
|
||||
translating: "Translating…",
|
||||
translated: "Translated",
|
||||
original: "Show original",
|
||||
retry: "Retry",
|
||||
error: "Translation unavailable",
|
||||
}
|
||||
);
|
||||
const dates = $derived(chatDateSeparators(chat.messages));
|
||||
let composer: HTMLInputElement | undefined = $state();
|
||||
let list: HTMLDivElement | undefined = $state();
|
||||
|
|
@ -47,7 +57,7 @@
|
|||
});
|
||||
</script>
|
||||
|
||||
{#if chat.enabled}
|
||||
{#if store.chatAvailable}
|
||||
<section class="expedition-chat" aria-label="Expedition chat">
|
||||
<header>
|
||||
<h2>Expedition chat</h2>
|
||||
|
|
@ -65,6 +75,10 @@
|
|||
<div bind:this={contents}>
|
||||
{#if !chat.messages.length}<p class="empty">Plan your escape together.</p>{/if}
|
||||
{#each chat.messages as message, i (message._id ?? i)}
|
||||
{@const translation = message._id ? chat.translations[message._id] : undefined}
|
||||
{@const displayed = translation?.shown
|
||||
? { text: translation.text ?? message.text, segments: translation.segments }
|
||||
: message}
|
||||
{@const day = dates[i]}
|
||||
{#if day}<div class="chat-day"><time datetime={day.dateTime}>{day.label}</time></div>{/if}
|
||||
<article data-message-id={message._id} class:system={message.type === "system"}>
|
||||
|
|
@ -93,7 +107,7 @@
|
|||
</div>
|
||||
{/if}
|
||||
<p>
|
||||
{#each message.segments ?? [{ kind: "text" as const, text: message.text }] as segment}
|
||||
{#each displayed.segments ?? [{ kind: "text" as const, text: displayed.text }] as segment}
|
||||
{#if segment.kind === "link" && safeLink(segment.url)}<a
|
||||
href={segment.url}
|
||||
target="_blank"
|
||||
|
|
@ -113,6 +127,23 @@
|
|||
{:else}{segment.text}{/if}
|
||||
{/each}
|
||||
</p>
|
||||
|
||||
{#if message._id && message.type === "text" && chat.translationTarget}
|
||||
<button
|
||||
type="button"
|
||||
class="chat-translate"
|
||||
disabled={!!translation?.pending}
|
||||
title={chatTranslationTitle(translation?.language ?? message.language, chat.translationTarget)}
|
||||
onclick={() => store.chat.toggleTranslation(message._id!)}
|
||||
>{translation?.pending
|
||||
? labels.translating
|
||||
: translation?.shown
|
||||
? `${labels.translated} · ${labels.original}`
|
||||
: translation?.error
|
||||
? `${labels.error} · ${labels.retry}`
|
||||
: labels.translate}</button
|
||||
>
|
||||
{/if}
|
||||
</article>
|
||||
{/each}
|
||||
</div>
|
||||
|
|
@ -142,6 +173,22 @@
|
|||
{/if}
|
||||
|
||||
<style>
|
||||
.chat-translate {
|
||||
min-height: 24px;
|
||||
padding: 2px 4px;
|
||||
margin-left: 4px;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: inherit;
|
||||
font: inherit;
|
||||
font-size: 11px;
|
||||
text-decoration: underline;
|
||||
cursor: pointer;
|
||||
}
|
||||
.chat-translate:disabled {
|
||||
opacity: 0.65;
|
||||
cursor: wait;
|
||||
}
|
||||
.chat-day {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@ import { ChatController } from "@boardgamers/protocol/chat";
|
|||
import { SoundDesign, type SoundCue } from "./sound";
|
||||
import type { View, Move } from "fuji-engine";
|
||||
export class Store {
|
||||
analysis = $state(false);
|
||||
chat = new ChatController();
|
||||
chatState = $state.raw(this.chat.snapshot);
|
||||
chatAvailable = $derived(this.chatState.enabled && !this.analysis);
|
||||
private unsubscribeChat = this.chat.subscribe((snapshot) => {
|
||||
this.chatState = snapshot;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ registerViewer<View, Move>(
|
|||
store.seat = index;
|
||||
},
|
||||
onPreferences(preferences) {
|
||||
store.analysis = preferences.analysis === true;
|
||||
store.colorblind = preferences.colorblind === true;
|
||||
store.setSound(preferences.sound !== false);
|
||||
},
|
||||
|
|
|
|||
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
|
|
@ -34,8 +34,8 @@ importers:
|
|||
packages/viewer:
|
||||
dependencies:
|
||||
'@boardgamers/protocol':
|
||||
specifier: 0.3.3
|
||||
version: 0.3.3
|
||||
specifier: 0.4.0
|
||||
version: 0.4.0
|
||||
fuji-engine:
|
||||
specifier: workspace:*
|
||||
version: link:../engine
|
||||
|
|
@ -61,8 +61,8 @@ importers:
|
|||
|
||||
packages:
|
||||
|
||||
'@boardgamers/protocol@0.3.3':
|
||||
resolution: {integrity: sha512-AS+x84WHXRudW6xt/s/5I3xYmNTQFbZEBp3V+41w0I2RirSr2fWjYnYf0p11nwTbZ6G5upivKz/sjYLtk8HOYA==}
|
||||
'@boardgamers/protocol@0.4.0':
|
||||
resolution: {integrity: sha512-dqrU94QGPZAh7vWhHmu2AHY6bFo7NbNVytb3dyUidou8R6P0FfT3xm0qLR8LbrE4EHEYmm99LajdfTfKmgwEzQ==}
|
||||
|
||||
'@jridgewell/gen-mapping@0.3.13':
|
||||
resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==}
|
||||
|
|
@ -631,7 +631,7 @@ packages:
|
|||
|
||||
snapshots:
|
||||
|
||||
'@boardgamers/protocol@0.3.3':
|
||||
'@boardgamers/protocol@0.4.0':
|
||||
dependencies:
|
||||
zod: 4.6.2
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
packages:
|
||||
- packages/*
|
||||
minimumReleaseAgeExclude:
|
||||
- "@boardgamers/protocol@0.4.0"
|
||||
- "@boardgamers/protocol@0.2.2 || 0.3.0 || 0.3.1 || 0.3.3"
|
||||
|
|
|
|||
69
tests/analysis-chat-browser.mjs
Normal file
69
tests/analysis-chat-browser.mjs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { chromium } from "playwright";
|
||||
import { fileURLToPath } from "node:url";
|
||||
const browser = await chromium.launch({ executablePath: process.env.CHROMIUM_EXECUTABLE });
|
||||
const root = fileURLToPath(new URL("../", import.meta.url));
|
||||
try {
|
||||
for (const game of ["fuji"]) {
|
||||
const engine = await import(
|
||||
game === "fuji" ? root + "/packages/engine/dist/wrapper.js" : root + "/engine/index.js"
|
||||
);
|
||||
const state = engine.stripSecret(await engine.init(3, [], {}, "analysis-chat-browser"), 0);
|
||||
for (const width of [390, 1400]) {
|
||||
const page = await browser.newPage({ viewport: { width, height: 950 } });
|
||||
const errors = [];
|
||||
page.on("pageerror", (e) => errors.push(e.message));
|
||||
await page.setContent(
|
||||
'<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1"></head><body><div id="app"></div></body></html>'
|
||||
);
|
||||
const prefix = game === "fuji" ? root + "/packages/viewer/dist/fuji-viewer" : root + "/dist/viewer";
|
||||
await page.addStyleTag({ path: prefix + ".css" });
|
||||
await page.addScriptTag({ path: prefix + (game === "fuji" ? ".iife.js" : ".js") });
|
||||
await page.evaluate(
|
||||
({ state, game }) => {
|
||||
window.e = (game === "fuji" ? fuji : primordialSoup).launch("#app");
|
||||
window.requests = [];
|
||||
e.on("chat:translate", (request) => {
|
||||
requests.push(request);
|
||||
e.emit("chat:translation", { ...request, ok: true, text: "Bonjour <img src=x>", language: "en" });
|
||||
});
|
||||
e.emit("preferences", { sound: false });
|
||||
e.emit("player", { index: 0 });
|
||||
e.emit("state", state);
|
||||
e.emit("chat:state", { canSend: true, translationTarget: "fr" });
|
||||
e.emit("chat:messages", [
|
||||
{
|
||||
_id: "000000000000000000000070",
|
||||
type: "text",
|
||||
text: "Hello",
|
||||
language: "en",
|
||||
author: "Ada",
|
||||
playerIndex: 1,
|
||||
},
|
||||
]);
|
||||
},
|
||||
{ state, game }
|
||||
);
|
||||
await page.locator(".chat-translate").click();
|
||||
await page.getByRole("button", { name: "Translated · Show original", exact: true }).waitFor();
|
||||
assert.equal(await page.locator('[data-message-id="000000000000000000000070"] img').count(), 0);
|
||||
assert.match(
|
||||
await page.locator('[data-message-id="000000000000000000000070"]').textContent(),
|
||||
/Bonjour <img src=x>/
|
||||
);
|
||||
await page.locator(".chat-translate").click();
|
||||
assert.match(await page.locator('[data-message-id="000000000000000000000070"]').textContent(), /Hello/);
|
||||
assert.equal(await page.evaluate(() => requests.length), 1);
|
||||
await page.evaluate(() => e.emit("preferences", { sound: false, analysis: true }));
|
||||
await page.waitForFunction(() => !document.querySelector(".chat-translate")?.getBoundingClientRect().height);
|
||||
assert.equal(await page.getByRole("button", { name: /^Chat/ }).count(), 0);
|
||||
assert.equal(await page.getByRole("tab", { name: /^Chat/ }).count(), 0);
|
||||
await page.screenshot({ path: `/tmp/${game}-analysis-${width}.png`, fullPage: true });
|
||||
assert.deepEqual(errors, []);
|
||||
console.log(`${game} ${width}: translation/original/analysis passed`);
|
||||
await page.close();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue