Skip to content

Quickstart

パッケージ@consenger/[email protected]
Protocolcompanion.plugin.v1
実行環境Vifu Web iframe
状態公開 alpha

この guide では、static browser game に companion integration を追加します。Vifu の外でも game は動き、Vifu host が bridge を初期化したときだけ companion feature が有効になります。

Install

bash
npm install @consenger/[email protected]

bundler を使わない場合は、package の browser bundle を game に同梱します。

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().hostConnectedtrue になります。local browser tab では false のままで問題ありません。