From e19e7ef13a0f554ea309c0823e555bb21911ea68 Mon Sep 17 00:00:00 2001 From: DevIos01 Date: Sat, 27 Apr 2024 17:48:42 +0200 Subject: [PATCH] Implement Playable Community Game On HomePage as requested on #1568, i have implemented so that it takes random game from "New Tag" and render the game --- src/pages/index.astro | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/pages/index.astro b/src/pages/index.astro index 52f63fa38..197216423 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -6,6 +6,10 @@ import '../global.css' import '../legacy/homepage.scss' import { firestore, getSession } from '../lib/game-saving/account' import { getConsolesRemaining } from '../lib/remaining-consoles' +import fs from "fs"; +import path from "path"; +import { dirname } from "path"; +import { fileURLToPath } from "url"; const remainingConsoles = await getConsolesRemaining() @@ -28,6 +32,29 @@ if (session && session.session.full) { .get() gameCount = res.data().count } + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +async function getGameSourceCode() { + const response = await fetch("http://localhost:3000/api/gallery?new"); + const gameList = await response.json(); + const randomIndex = Math.floor(Math.random() * gameList.length); + const selectedGame = gameList[randomIndex]; + let gameTitle = selectedGame.filename; + gameTitle = gameTitle.replace(/\s/g, ""); + const gameContentPath = path.resolve( + __dirname, + `../../games/${gameTitle}.js` + ); + + console.log(gameContentPath); + let gameSourceCode = fs.readFileSync(gameContentPath, "utf-8").toString(); + + return gameSourceCode; +} + +const gamesource = await getGameSourceCode(); --- @@ -294,7 +321,7 @@ if (session && session.session.full) { }) } - +