Skip to content

Commit

Permalink
Fix unclaiming rooms in realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
hopperelec committed Apr 14, 2024
1 parent 72cbc1e commit 3d0822f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/lib/server/ably-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
}
44 changes: 30 additions & 14 deletions src/routes/game/[gameId=id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 29 in src/routes/game/[gameId=id]/+page.svelte

View workflow job for this annotation

GitHub Actions / eslint

Unexpected lexical declaration in case block
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(
Expand All @@ -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.
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 6 additions & 2 deletions src/routes/game/[gameId=id]/shop/[itemId=id]/buy/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ const ACTIONS: { [key: string]: (details: ActionDetails) => Promise<void> } = {
// 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}`;
Expand All @@ -143,8 +145,10 @@ const ACTIONS: { [key: string]: (details: ActionDetails) => Promise<void> } = {
// 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!");
Expand Down

0 comments on commit 3d0822f

Please sign in to comment.