2023-09-10 13:05:07 +00:00
|
|
|
import { Handlers } from "$fresh/server.ts";
|
2023-09-10 12:22:22 +00:00
|
|
|
import Counter from "../islands/CounterCard.tsx";
|
|
|
|
import { getGlobalStatistics, setGlobalStatistics } from "../shared/db.ts";
|
2023-10-21 12:00:46 +00:00
|
|
|
import MarkdownContent from "../islands/MarkdownContent.tsx";
|
2023-09-10 12:22:22 +00:00
|
|
|
|
2023-09-19 05:17:36 +00:00
|
|
|
// TODO: This is hardcoded for now, but /assets/audio contains an N amount of files per language
|
|
|
|
// and we want to randomly play one of them when the mascot is squished
|
2023-09-19 06:17:44 +00:00
|
|
|
const kuruAudio: string[] = [];
|
2023-09-19 06:32:38 +00:00
|
|
|
// also hardcodeed for now
|
|
|
|
const mdData = await Deno.readTextFile("home_content/en.md");
|
2023-09-19 05:17:36 +00:00
|
|
|
|
|
|
|
// iterate inside ../static/assets/audio/ja/ and add all files to the array
|
|
|
|
for (const f of Deno.readDirSync("static/assets/audio/ja/")) {
|
|
|
|
if (f.isDirectory) continue;
|
|
|
|
// replace file paths with /assets/audio/ja/filename.mp3
|
|
|
|
kuruAudio.push(`/assets/audio/ja/${f.name}`);
|
2023-09-12 06:28:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 13:05:07 +00:00
|
|
|
export const handler: Handlers = {
|
|
|
|
GET: async (req, ctx) => {
|
2023-10-22 16:34:52 +00:00
|
|
|
let bc = new BroadcastChannel("global-count");
|
2023-09-10 12:22:22 +00:00
|
|
|
|
2023-10-22 16:34:52 +00:00
|
|
|
// check if we're requesting wss:// or ws://, add the response header accordingly
|
|
|
|
if (req.headers.get("upgrade") === "websocket") {
|
|
|
|
const { socket, response } = Deno.upgradeWebSocket(req);
|
|
|
|
|
|
|
|
socket.onopen = () => {
|
|
|
|
bc = new BroadcastChannel("global-count");
|
|
|
|
console.log(
|
|
|
|
`[${new Date().toISOString()}] Connection opened for ${
|
|
|
|
JSON.stringify(ctx.remoteAddr)
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
bc.addEventListener("message", (e) => {
|
|
|
|
socket.send(JSON.stringify({ globalCount: e.data }));
|
2023-09-10 13:05:07 +00:00
|
|
|
});
|
2023-10-22 16:34:52 +00:00
|
|
|
|
|
|
|
socket.onclose = () => {
|
|
|
|
bc.close();
|
|
|
|
console.log(
|
|
|
|
`[${new Date().toISOString()}] Connection closed for ${
|
|
|
|
JSON.stringify(ctx.remoteAddr)
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onerror = (e) => {
|
|
|
|
bc.close();
|
|
|
|
console.error(
|
|
|
|
`[${new Date().toISOString()}] Connection errored for ${
|
|
|
|
JSON.stringify(ctx.remoteAddr)
|
|
|
|
}: ${e}`,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return response;
|
2023-09-10 13:05:07 +00:00
|
|
|
}
|
2023-10-22 16:43:51 +00:00
|
|
|
|
2023-09-19 01:29:06 +00:00
|
|
|
const data = await getGlobalStatistics();
|
2023-09-19 06:17:44 +00:00
|
|
|
const res = await ctx.render({ globalCount: data });
|
2023-09-10 13:05:07 +00:00
|
|
|
return res;
|
2023-09-11 03:31:28 +00:00
|
|
|
},
|
|
|
|
POST: async (req, ctx) => {
|
|
|
|
const body = await req.json();
|
2023-09-12 06:38:05 +00:00
|
|
|
await setGlobalStatistics(body.data);
|
2023-10-22 16:43:51 +00:00
|
|
|
|
|
|
|
const updatedCount = await getGlobalStatistics();
|
2023-09-11 03:55:06 +00:00
|
|
|
|
|
|
|
const bc = new BroadcastChannel("global-count");
|
2023-10-22 16:43:51 +00:00
|
|
|
bc.postMessage(updatedCount.toString());
|
2023-09-11 04:30:34 +00:00
|
|
|
|
2023-09-11 04:57:06 +00:00
|
|
|
return new Response("", {
|
|
|
|
status: 200,
|
2023-09-19 06:17:44 +00:00
|
|
|
statusText: "OK",
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2023-09-10 13:05:07 +00:00
|
|
|
|
2023-09-19 06:17:44 +00:00
|
|
|
export default function Home(
|
2023-10-22 05:56:36 +00:00
|
|
|
{ data: { globalCount } }: { data: { globalCount: bigint } },
|
2023-09-19 06:17:44 +00:00
|
|
|
) {
|
|
|
|
// added a pseudo-div here so I can nest another div inside it smh
|
2023-09-10 12:22:22 +00:00
|
|
|
return (
|
2023-09-19 06:17:44 +00:00
|
|
|
<div>
|
2023-10-21 13:24:38 +00:00
|
|
|
<div class="px-4 py-8 mx-auto bg-[#9d88d3]">
|
2023-10-22 16:34:52 +00:00
|
|
|
<div
|
|
|
|
class="max-w-screen-md mx-auto flex flex-col items-center justify-center"
|
|
|
|
id="mascot-tgt"
|
|
|
|
>
|
2023-09-19 06:43:41 +00:00
|
|
|
<img class="z-10" src="/favicon.png" width="60px" />
|
2023-10-21 11:49:05 +00:00
|
|
|
<h1 class="text-4xl text-white text-center font-bold z-10">
|
2023-10-21 13:24:38 +00:00
|
|
|
Welcome to herta kuru
|
2023-09-19 06:17:44 +00:00
|
|
|
</h1>
|
2023-10-21 11:49:05 +00:00
|
|
|
<p class="my-4 font-bold text-center text-white z-10">
|
2023-09-19 06:17:44 +00:00
|
|
|
The website for Herta, the <del>annoying</del>{" "}
|
|
|
|
cutest genius Honkai: Star Rail character out there.
|
|
|
|
</p>
|
|
|
|
<Counter
|
|
|
|
globalCount={globalCount}
|
|
|
|
audioFiles={kuruAudio}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-10-21 12:00:46 +00:00
|
|
|
<MarkdownContent mdData={mdData} />
|
2023-09-10 12:22:22 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|