From 422719dade6e6ef3cad9df989d777b636f986909 Mon Sep 17 00:00:00 2001 From: Snipzil Date: Sun, 18 Oct 2026 00:43:12 -0400 Subject: [PATCH] feat(minigames): implement buddy game bubbles Introduce a bubble system that allows players to signal availability for specific buddy games. Other players in the same room can click the bubble to initiate a game. The implementation handles bubble visibility for new arrivals and ensures bubbles are cleared when players leave rooms or start other activities. --- lib/minigames.js | 99 +++++++++++++++++++++++++++++++++++++++++++--- lib/world.js | 7 ++++ test/buddygames.js | 68 +++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 5 deletions(-) diff --git a/lib/minigames.js b/lib/minigames.js index d0818f0..7989597 100644 --- a/lib/minigames.js +++ b/lib/minigames.js @@ -103,7 +103,7 @@ function serial() { let nextGameRoomId = 500000; // well clear of the world's room ids -module.exports = ({tell, leaveRoom, findOnline, blocks}) => { +module.exports = ({tell, leaveRoom, findOnline, blocks, watchers}) => { // MinigameInfo.init, the 19 fields the "mi" reply carries for a game. const info = game => [ game.id, @@ -145,6 +145,65 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { } }; + // A buddy game's bubble over a player's head, which anyone else in the + // room can click to play them (UserCommXtCommManager.sendCustomPVPMessage: + // a chat line "on|off" of type 12 with the game's id after it). the client + // reads it back as "on|" or "off", and its sender waits for + // their own before going on. `session.pvpBubble` is the game's id. + const BUBBLE = "12"; + const bubbleLine = session => [`on|${session.pvpBubble}`, BUBBLE, 0]; + const bubbleSeers = session => watchers(session).filter(other => !blocks.either(other.account, session.account)); + // the bubble goes for everyone else. the player's own "off" waits, when + // they are in a game, till they are out of it: it would end the game the + // client has only just set up (MinigameManager.readySelfForPvpGame). + const dropBubble = session => { + if (!session.pvpBubble) { + return; + } + session.pvpBubble = null; + if (session.room) { + for (const other of bubbleSeers(session)) { + other.sendXt("uc", session.room.id, session.id, "off", BUBBLE); + } + } + }; + const bubbleOff = session => { + if (session.game) { + session.bubbleOffOwed = true; + } + else if (session.room) { + session.bubbleOffOwed = false; + session.sendXt("uc", session.room.id, session.id, "off", BUBBLE); + } + }; + const bubble = (session, word, gameDefId) => { + if (word !== "on") { + dropBubble(session); + bubbleOff(session); + return; + } + const def = games.get(Number(gameDefId)); + if (!def || !def.buddy || !Object.hasOwn(buddyGames, def.swf) || !session.room || session.game || session.invite + || session.invisible || !accounts.featureOn(session.account, "gameInvites")) { + if (session.room && !session.game) { + tell(session, session.invisible ? "Nobody can see you to play with you." : "You cannot ask for a game right now."); + } + dropBubble(session); + bubbleOff(session); + return; + } + session.pvpBubble = def.id; + for (const other of [session, ...bubbleSeers(session)]) { + other.sendXt("uc", session.room.id, session.id, ...bubbleLine(session)); + } + }; + // a bubble that is up, for a player who has just been shown its owner. + const showBubble = (to, owner) => { + if (owner.pvpBubble && owner.room && to !== owner && !blocks.either(to.account, owner.account)) { + to.sendXt("uc", owner.room.id, owner.id, ...bubbleLine(owner)); + } + }; + // takes the player out of the game they are in, if any, and out of an // invitation. const leave = session => { @@ -154,6 +213,9 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { return; } session.game = null; + if (session.bubbleOffOwed) { + bubbleOff(session); + } game.players = game.players.filter(player => player !== session); if (game.lobby) { if (game.players.length === 0) { @@ -208,6 +270,11 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { // minigamePetMastery) and who is in the game (joinGameResponse). const seat = (session, game, music) => { const avatar = activeAvatar(session.account); + // off to a game: a bubble still up goes (and its owner is told after). + if (session.pvpBubble) { + dropBubble(session); + session.bubbleOffOwed = true; + } session.game = game; session.sendXt("rj", -1, 1, `game${game.def.id}_${game.roomId}`, game.roomId, game.players.length, game.players.length, 0, 0, 0, 1, 0, 1, game.def.type === IN_ROOM ? 1 : 0, // isPrivate, isGame, isLimbo, isTemp, isSubRoom @@ -683,13 +750,29 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { session.invite = target.invite = {def, sender: session, target}; target.sendXt("mp", -1, def.id, 0, session.account.username, 1); }, - // an open challenge to whoever walks up (UserCommXtCommManager.sendCustomPVPMessage); not offered. - mcp() {}, + // a click on another player's game bubble (MinigameXtCommManager + // .sendMinigameCustomPvpJoinMsg): [gameDefId, 0, their username]. the + // game starts at once, as an invitation said yes to; a bubble that is + // gone by now is answered as an invitation said no to + // (MinigameManager.pvpResponse), which puts the clicker back as they were. + mcp(session, roomId, [gameDefId, , username]) { + const def = games.get(Number(gameDefId)); + const host = findOnline(String(username || "")); + const free = player => player && player.room && !player.game && !player.invite; + if (!def || !host || host === session || host.pvpBubble !== def.id || host.room !== session.room + || !free(session) || !free(host) || blocks.either(session.account, host.account) + || !accounts.featureOn(session.account, "gameInvites")) { + session.sendXt("mp", -1, def ? def.id : Number(gameDefId) || 0, 1); + return; + } + startMatch(def, host, session); + }, }; // the HUD's Games window (GameJoinPopup, generic list 341): the playable - // games that have a picture for it, by name. - const library = [...games.values()].filter(game => game.icon && game.ready !== false) + // games that have a picture for it, by name. A buddy game's card picture + // is drawn the same way, and picking one there puts its bubble up. + const library = [...games.values()].filter(game => (game.icon || (game.buddy && game.card)) && game.ready !== false) .sort((one, other) => one.title.localeCompare(other.title)).map(game => game.id); // the buddy games' descriptions, sent unasked when a player arrives: a @@ -704,6 +787,12 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { handlers, leave, greet, + bubble, + showBubble, + // out of the room: nobody there is left to see the bubble. + roomLeft: session => { + session.pvpBubble = null; + }, library: () => library, // the room a player in a room-replacing game came from, for the panel. playing: session => (session.game ? session.game.def.title : null), diff --git a/lib/world.js b/lib/world.js index bf22bb9..b54dd58 100644 --- a/lib/world.js +++ b/lib/world.js @@ -436,6 +436,7 @@ const sendAvatarCreate = (to, target) => { if (target.invisible) { to.sendXt("xiv", -1, target.id, 1); } + minigames.showBubble(to, target); }; // redraws `session`'s animal for everyone else in the room, for changes the @@ -1190,6 +1191,7 @@ const leaveRoom = session => { room.sessions.delete(session); session.room = null; session.pos = null; + minigames.roomLeft(session); for (const other of room.sessions) { // AvatarManager.avatarRemoveResponse if (sees(other, session)) { @@ -1629,6 +1631,11 @@ module.exports = { return; } fields[1] = String(Number(fields[1])); + // A buddy game's bubble over the player's head (lib/minigames.js). + if (fields[1] === "12") { + minigames.bubble(session, fields[0], fields[2]); + return; + } // typed chat shows as "..." over the sender until the server sends // it back (UserCommXtCommManager.sendChatMessage); everything else // the sender has already shown themselves. diff --git a/test/buddygames.js b/test/buddygames.js index ce5db67..2babb97 100644 --- a/test/buddygames.js +++ b/test/buddygames.js @@ -295,6 +295,74 @@ const run = async () => { b.xt("rl", room, room); } + // --- game bubbles: Ann puts up Pairs over her head (a chat line of type + // 12), everyone in the room sees it as "on|", and whoever clicks it + // plays her + const bubble = (client, me, word, game) => client.send(``); + bubble(a, ann, "on", GAMES.pairs); + for (const client of [a, b, c]) { + assert.deepStrictEqual((await client.untilXt("uc")).slice(1), [ann.roomId, ann.id, "on|93", "12", "0"]); + } + // A game that is not the one in the bubble, or a bubble nobody has up: "no". + c.xt("mcp", cat.roomId, GAMES.ticTacToe, 0, "Ann"); + assert.deepStrictEqual((await c.untilXt("mp")).slice(2), ["89", "1"]); + c.xt("mcp", cat.roomId, GAMES.pairs, 0, "Ben"); + assert.deepStrictEqual((await c.untilXt("mp")).slice(2), ["93", "1"]); + // Ben clicks it: the game starts for the two of them and the bubble goes + // for Cat. Ann hears her own "off" only once the game is over. + b.xt("mcp", ben.roomId, GAMES.pairs, 0, "Ann"); + assert.deepStrictEqual((await c.untilXt("uc")).slice(1), [ann.roomId, ann.id, "off", "12"]); + let pairsRoom; + for (const client of [a, b]) { + pairsRoom = (await client.untilXt("rj"))[4]; + assert.deepStrictEqual((await client.untilXt("ms")).slice(1, 5), [pairsRoom, "2", ann.id, ben.id]); + } + // too late for Cat + c.xt("mcp", cat.roomId, GAMES.pairs, 0, "Ann"); + assert.deepStrictEqual((await c.untilXt("mp")).slice(2), ["93", "1"]); + // while in it, Ann's client turning the bubble off is not answered yet + bubble(a, ann, "off", 0); + b.xt("rl", pairsRoom, pairsRoom); + a.xt("rl", pairsRoom, pairsRoom); + assert.deepStrictEqual((await a.untilXt("uc")).slice(1), [ann.roomId, ann.id, "off", "12"]); + // put up and taken down again outside a game: answered at once + bubble(a, ann, "on", GAMES.bowling); + assert.deepStrictEqual((await a.untilXt("uc")).slice(3), ["on|98", "12", "0"]); + assert.deepStrictEqual((await c.untilXt("uc")).slice(3), ["on|98", "12", "0"]); + bubble(a, ann, "off", 0); + assert.deepStrictEqual((await a.untilXt("uc")).slice(3), ["off", "12"]); + assert.deepStrictEqual((await c.untilXt("uc")).slice(3), ["off", "12"]); + // not a buddy game: refused, and the client is put back with an "off" + bubble(a, ann, "on", 4); + assert.deepStrictEqual((await a.untilXt("uc")).slice(3), ["off", "12"]); + await b.untilXt("uc"); // the bowling bubble's two lines, which Ben saw as well + await b.untilXt("uc"); + + // somebody who walks in sees a bubble that is already up, with its owner + bubble(a, ann, "on", GAMES.shellGame); + await a.untilXt("uc"); + for (const client of [b, c]) { + await client.untilXt("uc"); + } + const reg = await fetch(`${BASE}/api/register`, {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({username: "Dee", password: "KettlePass01", avatarType: 1, agreed: true})}); + assert.strictEqual(reg.status, 200); + const dee = await arrive("Dee"); + let seen; + do { + seen = await dee.client.untilXt("uc"); + } while (seen[2] !== ann.id); + assert.deepStrictEqual(seen.slice(3), ["on|92", "12", "0"]); + bubble(a, ann, "off", 0); + await a.untilXt("uc"); + dee.client.socket.destroy(); + + // --- the HUD's Games window lists the buddy games too, Pairs among them + a.xt("gl", ann.roomId, 341); + const gameList = await a.untilXt("gl"); + for (const id of Object.values(GAMES)) { + assert.ok(gameList.includes(String(id)), `game ${id} in the Games window`); + } + // --- a player who logs off takes their invitation with them a.xt("mp", ann.roomId, GAMES.marbles, 0, "Ben"); await b.untilXt("mp");