Support playable analyses and translated chat
This commit is contained in:
parent
c20c47de4d
commit
3bdb1f9295
9 changed files with 152 additions and 11 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "outpost-engine",
|
||||
"version": "0.10.15",
|
||||
"version": "0.10.16",
|
||||
"description": "Unofficial Outpost rules engine for boardgamers.space",
|
||||
"license": "AGPL-3.0",
|
||||
"files": [
|
||||
|
|
|
|||
|
|
@ -207,3 +207,30 @@ test("handValueRange brackets the true value on stripped hands, collapses on vis
|
|||
assert.equal(own.min, ownTruth);
|
||||
assert.equal(own.max, ownTruth);
|
||||
});
|
||||
|
||||
test("analysis restores authoritative decks and RNG across round boundaries", () => {
|
||||
const source = initGame(3, { kicker: true }, "analysis-rounds");
|
||||
const snapshots: GameState[] = [];
|
||||
for (let i = 0; i < 180 && !source.ended; i++) {
|
||||
play(source, 1);
|
||||
if (i === 15 || i === 70 || i === 130) {
|
||||
snapshots.push(structuredClone(source));
|
||||
}
|
||||
}
|
||||
const original = structuredClone(source);
|
||||
for (const expected of snapshots) {
|
||||
const copy = wrapper.createAnalysis(source, { to: expected.log.length, sourceEnded: true });
|
||||
assert.deepEqual(copy.decks, expected.decks);
|
||||
assert.equal(copy.rngCounter, expected.rngCounter);
|
||||
assert.deepEqual(copy.log, expected.log);
|
||||
assert.deepEqual(
|
||||
copy.players.map((p) => p.hand),
|
||||
expected.players.map((p) => p.hand)
|
||||
);
|
||||
play(copy, 5);
|
||||
play(expected, 5);
|
||||
assert.deepEqual(copy.decks, expected.decks);
|
||||
assert.deepEqual(copy.log, expected.log);
|
||||
}
|
||||
assert.deepEqual(source, original);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -405,3 +405,32 @@ export function cancelled(data: GameState): boolean {
|
|||
export function factions(data: GameState): string[] {
|
||||
return data.players.map((p) => p.name);
|
||||
}
|
||||
|
||||
export function createAnalysis(data: GameState, { to }: { to: number; sourceEnded: boolean }): GameState {
|
||||
if (!Number.isInteger(to) || to < 0 || to > data.log.length) {
|
||||
throw new Error("Invalid history position");
|
||||
}
|
||||
const first = data.log[0];
|
||||
if (!first || first.type !== "init") {
|
||||
throw new Error("Missing initial state");
|
||||
}
|
||||
const copy = to === data.log.length ? structuredClone(data) : initGame(first.players, first.options, first.seed);
|
||||
if (to !== data.log.length) {
|
||||
for (const entry of data.log.slice(0, to)) {
|
||||
if (entry.type !== "move") {
|
||||
continue;
|
||||
}
|
||||
copy.players.forEach((player, seat) => {
|
||||
player.settings = { autoPassBids: entry.info?.autoPassed?.includes(seat) ?? false };
|
||||
});
|
||||
applyMove(copy, entry.move, entry.player);
|
||||
}
|
||||
}
|
||||
copy.players.forEach((player, seat) => {
|
||||
player.name = data.players[seat]!.name;
|
||||
player.dropped = false;
|
||||
player.settings = { autoPassBids: false };
|
||||
});
|
||||
copy.messages = [];
|
||||
return copy;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "outpost-viewer",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"description": "Browser viewer for the Outpost game on boardgamers.space",
|
||||
"license": "AGPL-3.0",
|
||||
"files": [
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@boardgamers/protocol": "0.3.3",
|
||||
"@boardgamers/protocol": "0.4.0",
|
||||
"outpost-engine": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,20 @@
|
|||
<script lang="ts">
|
||||
import { chatDateSeparators } from "@boardgamers/protocol/chat";
|
||||
import { chatDateSeparators, chatTranslationTitle } from "@boardgamers/protocol/chat";
|
||||
import type { ViewerStore } from "./store.svelte";
|
||||
import type { ChatMessage } from "@boardgamers/protocol/chat";
|
||||
import { bindChatComposer, bindChatViewport, type ChatSuggestions } from "@boardgamers/protocol/chat/dom";
|
||||
let { store }: { store: ViewerStore } = $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));
|
||||
const messages = $derived(chat.messages);
|
||||
let composer: HTMLInputElement | undefined = $state();
|
||||
|
|
@ -48,13 +58,17 @@
|
|||
});
|
||||
</script>
|
||||
|
||||
{#if chat.enabled}
|
||||
{#if chat.enabled && !store.preferences.analysis}
|
||||
<div class="chat">
|
||||
<div class="caption">Chat{chat.unreadIds.length ? ` · ${chat.unreadIds.length}` : ""}</div>
|
||||
<!-- svelte-ignore a11y_no_noninteractive_tabindex (Keyboard users must be able to scroll chat.) -->
|
||||
<div class="feed" bind:this={feed} role="region" aria-label="Chat messages" tabindex="0">
|
||||
<div bind:this={contents}>
|
||||
{#each messages as message, index (message._id ?? index)}
|
||||
{@const translation = message._id ? chat.translations[message._id] : undefined}
|
||||
{@const displayed = translation?.shown
|
||||
? { text: translation.text ?? message.text, segments: translation.segments }
|
||||
: message}
|
||||
{@const color = store.chatAuthorColor(message)}
|
||||
{@const time = timeOf(message)}
|
||||
{@const day = dates[index]}
|
||||
|
|
@ -70,7 +84,7 @@
|
|||
{:else}<span class="author" style:color>{message.author}</span>{/if}
|
||||
{/if}
|
||||
<span class="text">
|
||||
{#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"}<a href={segment.url} target="_blank" rel="noopener noreferrer"
|
||||
>{segment.text}</a
|
||||
>
|
||||
|
|
@ -89,6 +103,23 @@
|
|||
{:else}{segment.text}{/if}
|
||||
{/each}
|
||||
</span>
|
||||
|
||||
{#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}
|
||||
{#if message.editedAt}<span class="edited" title="Edited {message.editedAt}">(edited)</span>{/if}
|
||||
</div>
|
||||
{/each}
|
||||
|
|
@ -120,6 +151,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;
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ export const mountTutorial: TutorialMount = async (target, { chapter, onProgress
|
|||
},
|
||||
openPlayer: () => false,
|
||||
updatePreference: () => false,
|
||||
updateSetting: () => false,
|
||||
fetchState: () => false,
|
||||
fetchLog: () => false,
|
||||
addLog: () => false,
|
||||
|
|
|
|||
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
|
|
@ -40,8 +40,8 @@ importers:
|
|||
packages/viewer:
|
||||
dependencies:
|
||||
'@boardgamers/protocol':
|
||||
specifier: 0.3.3
|
||||
version: 0.3.3
|
||||
specifier: 0.4.0
|
||||
version: 0.4.0
|
||||
outpost-engine:
|
||||
specifier: workspace:*
|
||||
version: link:../engine
|
||||
|
|
@ -67,8 +67,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==}
|
||||
|
|
@ -772,7 +772,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.3.1 || 0.3.3"
|
||||
|
|
|
|||
|
|
@ -162,6 +162,42 @@ try {
|
|||
await page.getByText("Edited", { exact: true }).waitFor();
|
||||
assert.equal(await page.evaluate(() => events.filter((e) => e.name === "fetchState").length), 2);
|
||||
assert.equal(await page.evaluate(() => events.filter((e) => e.name === "replay:info").length), 2);
|
||||
|
||||
await page.evaluate(() => {
|
||||
window.translationsRequested = [];
|
||||
emitter.on("chat:translate", (request) => {
|
||||
translationsRequested.push(request);
|
||||
emitter.emit("chat:translation", { ...request, ok: true, text: "Bonjour <img src=x>", language: "en" });
|
||||
});
|
||||
emitter.emit("chat:state", {
|
||||
canSend: true,
|
||||
translationTarget: "fr",
|
||||
translationLabels: {
|
||||
translate: "Traduire",
|
||||
translating: "Traduction…",
|
||||
translated: "Traduit",
|
||||
original: "Voir l’original",
|
||||
error: "Indisponible",
|
||||
retry: "Réessayer",
|
||||
},
|
||||
});
|
||||
emitter.emit("chat:messages", [{ _id: "000000000000000000000070", type: "text", text: "Hello", language: "en" }]);
|
||||
});
|
||||
await page.locator(".chat-translate").click();
|
||||
await page.getByRole("button", { name: "Traduit · Voir l’original", exact: true }).waitFor();
|
||||
assert.equal(await page.evaluate(() => translationsRequested.length), 1);
|
||||
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(() => translationsRequested.length), 1, "showing original needs no new request");
|
||||
await page.evaluate(() => emitter.emit("preferences", { sound: false, analysis: true }));
|
||||
await page.waitForFunction(() => !document.querySelector(".chat-translate")?.getBoundingClientRect().height);
|
||||
await page.screenshot({ path: "/tmp/outpost-analysis-" + width + ".png", fullPage: true });
|
||||
await page.evaluate(() => emitter.emit("preferences", { sound: false, analysis: false }));
|
||||
await page.screenshot({ path: `/tmp/outpost-protocol-${width}.png`, fullPage: true });
|
||||
await page.evaluate(() => emitter.emit("chat:disabled", true));
|
||||
assert.equal(await input.count(), 0);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue