-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5c03cd
commit 4a24577
Showing
7 changed files
with
128 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
import ably from "ably"; | ||
import { ABLY_API_KEY } from "$env/static/private"; | ||
|
||
export default new ably.Realtime.Promise({ key: ABLY_API_KEY }); | ||
const ablyServer = new ably.Realtime.Promise({ key: ABLY_API_KEY }); | ||
export default ablyServer; | ||
|
||
export async function updatePoints( | ||
gameId: number, | ||
userId: number, | ||
points: number, | ||
) { | ||
await ablyServer.channels | ||
.get("player:" + gameId + ":" + userId) | ||
.publish("points", { points: points }); | ||
await ablyServer.channels | ||
.get("game:" + gameId + ":points") | ||
.publish("points", { userId, points }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import getPlayer from "$lib/server/get-player"; | ||
import prisma from "$lib/server/prisma"; | ||
import { error } from "@sveltejs/kit"; | ||
|
||
export const load = async ({ params, locals }) => { | ||
const player = await getPlayer(locals.user, +params.gameId); | ||
const ret = await prisma.player.findUnique({ | ||
where: { id: player.id }, | ||
select: { | ||
isHost: true, | ||
user: { select: { id: true } }, | ||
game: { | ||
select: { | ||
players: { | ||
select: { | ||
user: { select: { id: true, name: true } }, | ||
points: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
if (!ret) | ||
error( | ||
500, | ||
"An unexpected error occurred while trying to retrieve your player data", | ||
); | ||
return { | ||
userId: ret.user.id, | ||
isHost: ret.isHost, | ||
players: ret.game.players.map((player) => { | ||
return { | ||
id: player.user.id, | ||
name: player.user.name, | ||
points: player.points, | ||
}; | ||
}), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<script lang="ts"> | ||
import Leaderboard from "$lib/Leaderboard.svelte"; | ||
import ablyClientStore from "$lib/ably-client"; | ||
import { page } from "$app/stores"; | ||
import "$lib/button.css"; | ||
export let data; | ||
const players = data.players.reduce( | ||
(acc, player) => { | ||
acc[player.id] = { | ||
name: player.name || "User " + player.id, | ||
points: player.points, | ||
}; | ||
return acc; | ||
}, | ||
{} as { [key: number]: { name: string; points: number } }, | ||
); | ||
$: orderedPlayers = Object.values(players).sort( | ||
(a, b) => b.points - a.points, | ||
); | ||
ablyClientStore.subscribe(async (ablyClient) => { | ||
if (!ablyClient) return; | ||
await ablyClient.channels | ||
.get("game:" + $page.params.gameId + ":points") | ||
.subscribe((message) => { | ||
switch (message.name) { | ||
case "points": | ||
players[message.data.userId].points = message.data.points; | ||
break; | ||
} | ||
}); | ||
}); | ||
</script> | ||
|
||
<div id="page-container"> | ||
<div id="leaderboard-container"> | ||
<Leaderboard {orderedPlayers} /> | ||
</div> | ||
<a class="button" href="../">Back to game</a> | ||
</div> | ||
|
||
<style> | ||
#page-container { | ||
height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
#leaderboard-container { | ||
height: 100%; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters