Lia开发者

指南

一个双人房间。在您的服务器上创建它,使用Lia 的好友选择器或链接邀请一个朋友进入其中,让两个玩家都进入同一个房间。

想法:一个房间有一个 ID,这个 ID 会出现在邀请中's startParam。打开邀请的人会用该startParam打开您的应用,您的页面会加入该房间。Lia'自己的Lia XO 是这样工作的。

#1. 在您的服务器上创建一个房间

server.js
app.post("/api/rooms", async (req, res) => {
  const playerId = await playerFrom(req.body.session); // see Verify on your server
  if (!playerId) return res.status(401).end();
  const room = "room-" + crypto.randomUUID().replace(/-/g, "").slice(0, 10);
  await db.createRoom(room, { host: playerId, players: [playerId] });
  res.json({ room });
});

app.post("/api/rooms/join", async (req, res) => {
  const playerId = await playerFrom(req.body.session);
  if (!playerId) return res.status(401).end();
  const room = await db.room(req.body.room);
  if (!room) return res.status(404).json({ error: "no_room" });
  if (room.players.length >= 2 && !room.players.includes(playerId)) return res.status(409).json({ error: "full" });
  await db.addPlayer(req.body.room, playerId);
  res.json({ room: req.body.room });
});

房间 ID 必须符合 startParam:1 到 64 个字母、数字, _ 和 -.

#2. 邀请朋友

game.js
async function playWithAFriend() {
  const { room } = await api("/api/rooms", {});
  showWaiting(room);
  const { invited } = await Lia.inviteFriend({ startParam: room });
  if (!invited) Lia.toast("Share the link to invite someone");
}

// Or a link to send anywhere: opens your app in Lia, or the App Store without it.
async function shareRoom(room) {
  await Lia.share("Play with me: " + Lia.inviteLink(room));
}

每个被选中的朋友会在他们的 Lia 聊天中获得一张卡片。Lia 仅允许人们邀请他们的朋友,并且仅限于可以打开您的应用的朋友(在它'在测试中,只有您的测试者)。

#3.进入房间

在您的应用打开时阅读 startParam,并在它运行时监听到达的'打开(用户点击第二个邀请):

function route(startParam) {
  if (startParam && startParam.startsWith("room-")) joinRoom(startParam);
}

Lia.init().then(function () {
  route(Lia.startParam);
  Lia.ready();
});
Lia.on("startParam", function (data) { route(data.startParam); });

async function joinRoom(room) {
  try {
    await api("/api/rooms/join", { room });
    openTable(room);
  } catch (error) {
    Lia.toast(error.message === "full" ? "That game is full" : "That game has ended");
  }
}

startParam 只是一个提示:任何人都可以使用任何startParam 创建链接,因此您的服务器决定房间是否存在,玩家是否可以加入。

#4. 实时播放

将两个玩家连接到你的服务器(通常是一个WebSocket),并验证每个玩家'的会话在加入房间之前。当游戏进行时:

Lia.enableClosingConfirmation(); // "Leave the game?" if they close
Lia.disableVerticalSwipes();     // a swipe down won't close the sheet mid move
Lia.onPause(function () { socket.send(JSON.stringify({ type: "away" })); });
Lia.onResume(function () { socket.send(JSON.stringify({ type: "back" })); });

#5. 通过通知轮到您了

当它'的某人'的移动,然后他们'已经离开了你的应用,一个 通知 与房间作为其startParam将它们直接带回:

{ "playerId": "p_3f9c0a6d1b2e4f5a6b7c", "text": "@kofi played. Your move.", "startParam": "room-a81c2e09f4" }

#公共表格

邀请是给朋友的。要让Lia上的任何人都能加入,还要在 大厅 当它等待时:

server.js
// When the room is made: list it (as the host, with one seat taken).
await lia("POST", "/lobby/tables", { tableId: room, hostPlayerId: playerId, seats: 2, taken: 1, title: "Quick game" });

// When someone joins from the lobby: seat them through Lia first (it refuses blocked players and full tables).
app.post("/api/rooms/sit", async (req, res) => {
  const playerId = await playerFrom(req.body.session);
  if (!playerId) return res.status(401).end();
  try {
    const { full } = await lia("POST", `/lobby/tables/${encodeURIComponent(req.body.room)}/sit`, { playerId });
    await db.addPlayer(req.body.room, playerId);
    if (full) startGame(req.body.room);
    res.json({ room: req.body.room });
  } catch (error) {
    res.status(409).json({ error: error.data?.error ?? "failed" }); // full, not_found, own_table
  }
});

// When the game starts or the room closes: take it out (safe to repeat).
await lia("DELETE", `/lobby/tables/${encodeURIComponent(room)}`);

在你的页面中,显示开放的表格,并在Lia中安排选择你表格的人'自己的大厅(他们带着startParam lobby-<table id>):

const { tables } = await Lia.lobby.tables();
renderTables(tables, (table) => api("/api/rooms/sit", { room: table.id }));

function route(startParam) {
  if (startParam?.startsWith("lobby-")) return api("/api/rooms/sit", { room: startParam.slice(6) }).then(openTable);
  if (startParam?.startsWith("room-")) return joinRoom(startParam);
}

陌生人可以't 通过Lia聊天,块被尊重,玩家可以 报告 彼此。

#需要了解的限制

  • 一个人每天可以邀请 20 个朋友进入您的应用,因此邀请仍然是人们在聊天中欢迎的东西。
  • 一个主持人可以在你的应用中同时打开3个公开桌子,且一个列表最多持续一小时。
  • Lia.inviteFriend() 答案 { invited },从不谁被邀请:你的服务器会在他们加入时知道谁加入了。

自动翻译。英语是真实来源。 阅读英文

输入以搜索每一页、方法和端点。