From 3d0822f33549e5e29020dd6eba3d9713dab27007 Mon Sep 17 00:00:00 2001 From: hopperelec Date: Sun, 14 Apr 2024 05:01:35 +0100 Subject: [PATCH] Fix unclaiming rooms in realtime --- src/lib/server/ably-server.ts | 2 +- src/routes/game/[gameId=id]/+page.svelte | 44 +++++++++++++------ .../shop/[itemId=id]/buy/+server.ts | 8 +++- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/lib/server/ably-server.ts b/src/lib/server/ably-server.ts index d3ed9fb..611f405 100644 --- a/src/lib/server/ably-server.ts +++ b/src/lib/server/ably-server.ts @@ -75,6 +75,6 @@ export async function unclaimRooms( }); ablyServer.channels.get("game:" + gameId + ":positions").publish( "unclaim", - rooms.map((room) => room.svgRef), + rooms.map((room) => Number(room.svgRef)), ); } diff --git a/src/routes/game/[gameId=id]/+page.svelte b/src/routes/game/[gameId=id]/+page.svelte index 9ba8368..0f0d130 100644 --- a/src/routes/game/[gameId=id]/+page.svelte +++ b/src/routes/game/[gameId=id]/+page.svelte @@ -18,19 +18,31 @@ "game:" + $page.params.gameId + ":positions", ); $: if ($positionsMessage) { - if ($positionsMessage.name == "create") { - data.players[$positionsMessage.data.userId] = { - picture: $positionsMessage.data.picture, - currSvgRef: $positionsMessage.data.svgRef, - }; + switch ($positionsMessage.name) { + case "create": + data.players[$positionsMessage.data.userId] = { + picture: $positionsMessage.data.picture, + currSvgRef: $positionsMessage.data.svgRef, + }; + // fallthrough + case "move": + const svgRef = $positionsMessage.data.svgRef; + if (!data.claimedRooms.includes(svgRef)) { + data.claimedRooms.push(svgRef); + const pointIcon = map.getElmWhere("point", svgRef) as SVGImageElement; + if (pointIcon) map.removeIcon(pointIcon); + } + movePlayer($positionsMessage.data.userId, $positionsMessage.data.svgRef); + break; + + case "unclaim": + data.claimedRooms = data.claimedRooms.filter( + (claimedRoom) => !$positionsMessage.data.includes(claimedRoom) + ); + for (const svgRef of $positionsMessage.data) { + addPointIcon(svgRef); + } } - const svgRef = $positionsMessage.data.svgRef; - if (!data.claimedRooms.includes(svgRef)) { - data.claimedRooms.push(svgRef); - const pointIcon = map.getElmWhere("point", svgRef) as SVGImageElement; - if (pointIcon) map.removeIcon(pointIcon); - } - movePlayer($positionsMessage.data.userId, svgRef); } const playerMessage = getChannel( @@ -53,6 +65,11 @@ } } + function addPointIcon(svgRef: number) { + const icon = map.addIconTo(svgRef, POINT_ICON); + if (icon) icon.dataset.point = svgRef.toString(); + } + function movePlayer(userId: number, svgRef: number) { data.players[userId].currSvgRef = svgRef; // A position update could occur before the map has finished loading. @@ -107,8 +124,7 @@ for (const roomElm of map.getSVG().querySelectorAll("[data-room]")) { const svgRef = (roomElm as HTMLElement).dataset.room; if (svgRef && !data.claimedRooms.includes(+svgRef)) { - const icon = map.addIconTo(+svgRef, POINT_ICON); - if (icon) icon.dataset.point = svgRef; + addPointIcon(+svgRef); } } doors = await fetch("/maps/" + data.map.id + "/doors") diff --git a/src/routes/game/[gameId=id]/shop/[itemId=id]/buy/+server.ts b/src/routes/game/[gameId=id]/shop/[itemId=id]/buy/+server.ts index 853f522..4eb624f 100644 --- a/src/routes/game/[gameId=id]/shop/[itemId=id]/buy/+server.ts +++ b/src/routes/game/[gameId=id]/shop/[itemId=id]/buy/+server.ts @@ -131,8 +131,10 @@ const ACTIONS: { [key: string]: (details: ActionDetails) => Promise } = { // actionParams is an integer number of rooms (e.g: "5" -> unclaim 5 rooms) unclaimAbsoluteRooms: async ({ gameId, actionParams }) => { const rooms: { roomId: bigint; svgRef: bigint }[] = await prisma.$queryRaw` - SELECT roomId + SELECT roomId,Room.svgRef FROM ClaimedRoom + INNER JOIN Room + ON Room.id = roomId WHERE gameId = ${gameId} ORDER BY rand() LIMIT ${+actionParams}`; @@ -143,8 +145,10 @@ const ACTIONS: { [key: string]: (details: ActionDetails) => Promise } = { // actionParams is a float percentage (e.g: "50" -> unclaim 50% of rooms) unclaimPercentageRooms: async ({ gameId, actionParams }) => { const rooms: { roomId: bigint; svgRef: bigint }[] = await prisma.$queryRaw` - SELECT roomId + SELECT roomId,Room.svgRef FROM ClaimedRoom + INNER JOIN Room + ON Room.id = roomId WHERE gameId = ${gameId} ORDER BY rand()`; if (rooms.length === 0) error(500, "No rooms available to unclaim!");