I made a feature and it is done D:

This commit is contained in:
Lucas Schröder 2023-12-18 13:42:50 +01:00
parent 74df1a555e
commit 2ec71df24b
8 changed files with 140 additions and 3 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
.idea/*

4
website/.gitignore vendored
View File

@ -4,8 +4,8 @@ node_modules
/.svelte-kit
/package
.env
.env.*
!.env.example
.env
!.env
vite.config.js.timestamp-*
vite.config.ts.timestamp-*

View File

@ -0,0 +1,15 @@
<script>
export let announcementJson ;
export let announcementStyle;
</script>
<div id="background" style={announcementStyle}>
<div id="window">
<div id="title">{announcementJson['title']}</div>
<div id="time">{announcementJson['time']}</div>
<div id="content">{announcementJson['content']}</div>
</div>
</div>
<style>
</style>

View File

@ -0,0 +1,60 @@
<script lang="js">
import AnnouncementPopup from '$lib/client/components/AnnouncementPopup.svelte';
import { onMount } from 'svelte';
let announcement;
let placeholder = {
'title': 'None',
'time': 'undefined',
'content': 'None'
}
let intervalId;
async function readSavedFile() {
const response = await fetch('api/announcements');
const savedAnnouncements = await response.json();
console.log(savedAnnouncements)
let date = new Date()
let time = date.getHours() + ':' + date.getMinutes()
console.log(time)
for (let object of savedAnnouncements) {
if (object['time'] === time) {
announcement = object;
}
else {
announcement = undefined
}
}
console.log("Read saved file!")
}
function checkForSavedFile() {
intervalId = setTimeout(
() => {
readSavedFile();
checkForSavedFile();
},
announcement ? 3000000 : 10000
);
}
function cancelTimeout() {
if (intervalId) {
clearTimeout(intervalId);
intervalId = undefined;
}
}
onMount(() => {
intervalId = setInterval(readSavedFile, 10000);
checkForSavedFile();
return cancelTimeout();
});
</script>
<AnnouncementPopup
announcementJson={announcement ? announcement: placeholder}
announcementStyle={announcement ? 'display:flex' : 'display:none'}
/>
<slot />

View File

@ -0,0 +1,32 @@
import { redirect } from '@sveltejs/kit';
import fs from 'fs';
import { SAVED_FOLDER_LOCATION } from '$env/static/private';
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
let upload = Object.fromEntries(data)
let fileData = []
if (!fs.existsSync(SAVED_FOLDER_LOCATION))
fs.mkdirSync(SAVED_FOLDER_LOCATION, { recursive: true });
if (!fs.existsSync(SAVED_FOLDER_LOCATION + 'announcements.json')) {
fileData.push(upload)
fs.appendFile(SAVED_FOLDER_LOCATION + 'announcements.json', JSON.stringify(fileData),(err) => {
if (err) throw err;
}) }
else {
let temp = fs.readFileSync(SAVED_FOLDER_LOCATION + 'announcements.json')
fileData.push(JSON.parse(temp))
fileData.push(upload)
fs.writeFile(SAVED_FOLDER_LOCATION + 'announcements.json', JSON.stringify(fileData), (err) => {
if (err) throw err;
})
}
throw redirect(302, '/')
}
};

View File

@ -0,0 +1,13 @@
<div id="ann-container">
<form method="POST">
<label for="ann-title"> Titel: </label>
<input type="text" name="title" id="ann-title" />
<label for="ann-time"> Uhrzeit: </label>
<input type="time" name="time" id="ann-time" />
<label for="ann-content"> Inhalt: </label>
<input type="text" name="content" id="ann-content" />
<label for="ann-reminder"> Wann erinnern? </label>
<input type="text" name="reminder" id="ann-reminder" />
<input type="submit">
</form>
</div>

View File

@ -0,0 +1,15 @@
import fs from 'fs';
import { SAVED_FOLDER_LOCATION } from '$env/static/private';
export async function GET() {
if (!fs.existsSync(SAVED_FOLDER_LOCATION))
fs.mkdirSync(SAVED_FOLDER_LOCATION, { recursive: true });
if (!fs.existsSync(SAVED_FOLDER_LOCATION + 'announcements.json'))
return new Response({ status: 404 });
const announcementList = fs.readFileSync(SAVED_FOLDER_LOCATION + 'announcements.json');
return new Response(announcementList, { status: 200 });
}

View File

@ -6,7 +6,7 @@ export async function GET() {
if (!fs.existsSync(TEMP_FOLDER_LOCATION)) fs.mkdirSync(TEMP_FOLDER_LOCATION, { recursive: true });
if (!fs.existsSync(TEMP_FOLDER_LOCATION + 'gameserverstatus.json'))
return new Response({status: 404});
return new Response({ status: 404 });
const serverList = fs.readFileSync(TEMP_FOLDER_LOCATION + 'gameserverstatus.json');