From 533d7c52a41a4d3f354d3affe5dd74c51026218d Mon Sep 17 00:00:00 2001 From: metamethods Date: Thu, 2 Jan 2025 23:53:10 -0800 Subject: [PATCH] other: fill empty slots in archived rooms with cpus on the server --- src/lib/array.ts | 4 +++- src/routes/+page.server.ts | 18 ++++++++++++++++-- src/routes/+page.svelte | 11 ++--------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/lib/array.ts b/src/lib/array.ts index 2fd18ff..80dcb7c 100644 --- a/src/lib/array.ts +++ b/src/lib/array.ts @@ -1,3 +1,5 @@ export function arrayPadEnd(array: T[], amount: number, data: T) { - for (let i = 0; i < amount - array.length; i++) array.push(data); + if (amount > array.length) { + array.push(...Array(amount - array.length).fill(data)); + } } diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 8702055..a7ab860 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,6 +1,20 @@ -import type { PageServerLoad } from './$types'; import { fetchRooms } from '$lib/server/yukiotoko'; +import { arrayPadEnd } from '$lib/array'; +import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ fetch }) => { - return await fetchRooms(); + const { activeRooms, archivedRooms } = await fetchRooms(); + + archivedRooms.forEach((archivedRoom) => + arrayPadEnd(archivedRoom.players, 4, { + name: 'CPU', + battleRank: 0, + rating: 0 + }) + ); + + return { + activeRooms, + archivedRooms + }; }; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index ec7e7fe..cdbb9bb 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,19 +1,12 @@