Skip to content

Commit

Permalink
Simplified Ably capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
hopperelec committed Mar 3, 2024
1 parent d00ec17 commit 4af157b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/routes/ably-auth/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export const GET = async ({ locals }) => {
500,
"An unexpected error occurred while trying to retrieve your user data",
);
const channels = ["player:*:" + locals.user.id];
for (const player of userData.players) {
channels.push("game:" + player.gameId + ":*");
}
return json(
await ablyServer.auth.createTokenRequest({
capability: userData.players.reduce(
(acc, player) => {
acc["game:" + player.gameId] = ["subscribe"];
acc["game:" + player.gameId + ":announcements"] = ["subscribe"];
acc["game:" + player.gameId + ":" + locals.user.id] = ["subscribe"];
capability: channels.reduce(
(acc, channel) => {
acc[channel] = ["subscribe"];
return acc;
},
{} as { [key: string]: ["subscribe"] },
Expand Down
4 changes: 2 additions & 2 deletions src/routes/game/[gameId=id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ablyClientStore.subscribe(async (ablyClient) => {
if (!ablyClient) return;
await ablyClient.channels
.get("game:" + $page.params.gameId)
.get("game:" + $page.params.gameId + ":positions")
.subscribe((message) => {
switch (message.name) {
case "move":
Expand All @@ -21,7 +21,7 @@
}
});
await ablyClient.channels
.get("game:" + $page.params.gameId + ":" + data.userId)
.get("player:" + $page.params.gameId + ":" + data.userId)
.subscribe((message) => {
switch (message.name) {
case "points":
Expand Down
10 changes: 6 additions & 4 deletions src/routes/game/[gameId=id]/answer/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ export const POST = async ({ request, params, locals }) => {
currMoveId: null,
},
});
await ablyServer.channels.get("game:" + params.gameId).publish("move", {
userId: locals.user.id,
svgRef: player.currMove.svgRef,
});
await ablyServer.channels
.get("game:" + params.gameId + ":positions")
.publish("move", {
userId: locals.user.id,
svgRef: player.currMove.svgRef,
});
}
return json({ correct });
};
2 changes: 1 addition & 1 deletion src/routes/game/[gameId=id]/shop/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ablyClientStore.subscribe(async (ablyClient) => {
if (!ablyClient) return;
await ablyClient.channels
.get("game:" + $page.params.gameId + ":" + data.userId)
.get("player:" + $page.params.gameId + ":" + data.userId)
.subscribe((message) => {
switch (message.name) {
case "points":
Expand Down
12 changes: 7 additions & 5 deletions src/routes/game/[gameId=id]/shop/[itemId=id]/buy/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ async function moveRoom(
where: { id: playerId },
data: { currRoomId: roomId },
});
await ablyServer.channels.get("game:" + gameId).publish("move", {
userId: userId,
svgRef: svgRef,
});
await ablyServer.channels
.get("game:" + gameId + ":positions")
.publish("move", {
userId: userId,
svgRef: svgRef,
});
}

const ACTIONS: { [key: string]: (details: ActionDetails) => Promise<void> } = {
Expand Down Expand Up @@ -129,7 +131,7 @@ const ACTIONS: { [key: string]: (details: ActionDetails) => Promise<void> } = {
data: { points: newPoints },
});
await ablyServer.channels
.get("game:" + gameId + ":" + randPlayer.userId)
.get("player:" + gameId + ":" + randPlayer.userId)
.publish("points", {
points: newPoints,
});
Expand Down

0 comments on commit 4af157b

Please sign in to comment.