diff --git a/src/routes/(app)/mappools/+page.server.ts b/src/routes/(app)/mappools/+page.server.ts index 0120554..888a5a0 100644 --- a/src/routes/(app)/mappools/+page.server.ts +++ b/src/routes/(app)/mappools/+page.server.ts @@ -6,10 +6,12 @@ import { mappool } from '$lib/server/db/schema'; export const load: PageServerLoad = async ({ locals }) => { if (!locals.user) redirect(302, '/'); - const mappools = await db.query.mappool.findMany({ - with: { slots: true }, - orderBy: (m, { desc }) => [desc(m.createdAt)] - }); + const mappools = await db.query.mappool.findMany({ with: { slots: true } }); + + const avgSR = (m: typeof mappools[0]) => + m.slots.length ? m.slots.reduce((s, sl) => s + (sl.starRating ?? 0), 0) / m.slots.length : 0; + + mappools.sort((a, b) => avgSR(b) - avgSR(a)); return { mappools }; };