Skip to content

Quickstart

软件包@consenger/[email protected]
Protocolcompanion.plugin.v1
运行环境Vifu Web iframe
状态公开 alpha

这个 guide 会把 companion integration 加到 static browser game 中。游戏在 Vifu 外仍然可以运行;当 Vifu host 初始化 bridge 后,companion feature 才会生效。

Install

bash
npm install @consenger/[email protected]

不使用 bundler 的游戏,可以复制 package 中的 browser bundle。

txt
node_modules/@consenger/companion-sdk/dist/browser/companion-sdk.js

添加 SDK

html
<script src="./companion-sdk.js"></script>
<script>
  const companion = window.Consenger.companion;
</script>

注册 context

ts
companion.context("question.current", {
  description: "Current question shown to the player",
  read: () => ({
    id: "q-1",
    prompt: "Choose the Japanese word for water.",
    choices: ["みず", "ねこ", "そら"]
  })
});

注册 command

ts
companion.command("question.hint", {
  title: "Show a hint",
  inputSchema: { type: "object", properties: {}, additionalProperties: false },
  handler: () => {
    showHint();
    return { ok: true };
  }
});

发送 observation

ts
companion.observe("answer.wrong", { questionId: "q-1" }, {
  context: "question.current"
});

声明 capability

json
{
  "name": "water-quest",
  "entry": "index.html",
  "capabilities": ["companion.plugin.v1"]
}

在 Vifu 中,companion.status().hostConnected 会变成 true。在普通 local browser tab 中保持 false 是正常的。