Podpora easter eggů
This commit is contained in:
3
server/.gitignore
vendored
3
server/.gitignore
vendored
@@ -3,4 +3,5 @@
|
||||
data.json
|
||||
.env.production
|
||||
.env.development
|
||||
.easter-eggs.json
|
||||
.easter-eggs.json
|
||||
/resources/easterEggs
|
||||
@@ -11,6 +11,7 @@ import { initWebsocket } from "./websocket";
|
||||
import pizzaDayRoutes from "./routes/pizzaDayRoutes";
|
||||
import foodRoutes from "./routes/foodRoutes";
|
||||
import votingRoutes from "./routes/votingRoutes";
|
||||
import easterEggRoutes from "./routes/easterEggRoutes";
|
||||
|
||||
const ENVIRONMENT = process.env.NODE_ENV || 'production';
|
||||
dotenv.config({ path: path.resolve(__dirname, `./.env.${ENVIRONMENT}`) });
|
||||
@@ -128,6 +129,7 @@ app.get("/api/data", async (req, res) => {
|
||||
app.use("/api/pizzaDay", pizzaDayRoutes);
|
||||
app.use("/api/food", foodRoutes);
|
||||
app.use("/api/voting", votingRoutes);
|
||||
app.use("/api/easterEggs", easterEggRoutes);
|
||||
app.use(express.static('public'))
|
||||
|
||||
// Middleware pro zpracování chyb
|
||||
|
||||
156
server/src/routes/easterEggRoutes.ts
Normal file
156
server/src/routes/easterEggRoutes.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import express, { NextFunction } from "express";
|
||||
import { getLogin, getTrusted } from "../auth";
|
||||
import { parseToken } from "../utils";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { EasterEgg } from "../../../types";
|
||||
|
||||
const EASTER_EGGS_JSON_PATH = path.join(__dirname, "../../.easter-eggs.json");
|
||||
const IMAGES_PATH = '../../resources/easterEggs';
|
||||
|
||||
type EasterEggsJson = {
|
||||
[key: string]: EasterEgg[]
|
||||
}
|
||||
|
||||
function generateUrl() {
|
||||
let result = '';
|
||||
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const charactersLength = characters.length;
|
||||
let counter = 0;
|
||||
while (counter < 32) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
counter += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vrátí náhodně jeden z definovaných easter egg obrázků pro přihlášeného uživatele.
|
||||
*
|
||||
* @param req request
|
||||
* @param res response
|
||||
* @param next next
|
||||
* @returns náhodný easter egg obrázek, nebo 404 pokud žádný není definován
|
||||
*/
|
||||
function getEasterEggImage(req: any, res: any, next: NextFunction) {
|
||||
const login = getLogin(parseToken(req));
|
||||
const trusted = getTrusted(parseToken(req));
|
||||
try {
|
||||
// TODO vrátit!
|
||||
// if (trusted) {
|
||||
if (true) {
|
||||
if (login in easterEggs) {
|
||||
const imagePath = easterEggs[login][Math.floor(Math.random() * easterEggs[login].length)].path;
|
||||
res.sendFile(path.join(__dirname, IMAGES_PATH, imagePath));
|
||||
return;
|
||||
}
|
||||
}
|
||||
res.sendStatus(404);
|
||||
} catch (e: any) { next(e) }
|
||||
}
|
||||
|
||||
function getRandomPosition(startOffset: number, endOffset: number) {
|
||||
const choice = Math.floor(Math.random() * 4);
|
||||
|
||||
if (choice === 0) {
|
||||
// Vlevo nahoře
|
||||
return {
|
||||
left: `${startOffset}px`,
|
||||
startLeft: `${startOffset}px`,
|
||||
"--start-left": `${startOffset}px`,
|
||||
top: `${startOffset}px`,
|
||||
startTop: `${startOffset}px`,
|
||||
"--start-top": `${startOffset}px`,
|
||||
endLeft: `${endOffset}px`,
|
||||
"--end-left": `${endOffset}px`,
|
||||
endTop: `${endOffset}px`,
|
||||
"--end-top": `${endOffset}px`,
|
||||
rotate: '135deg',
|
||||
}
|
||||
} else if (choice === 1) {
|
||||
// Vpravo nahoře
|
||||
return {
|
||||
right: `${startOffset}px`,
|
||||
startRight: `${startOffset}px`,
|
||||
"--start-right": `${startOffset}px`,
|
||||
top: `${startOffset}px`,
|
||||
startTop: `${startOffset}px`,
|
||||
"--start-top": `${startOffset}px`,
|
||||
endRight: `${endOffset}px`,
|
||||
"--end-right": `${endOffset}px`,
|
||||
endTop: `${endOffset}px`,
|
||||
"--end-top": `${endOffset}px`,
|
||||
rotate: '-135deg',
|
||||
}
|
||||
} else if (choice === 2) {
|
||||
// Vpravo dole
|
||||
return {
|
||||
right: `${startOffset}px`,
|
||||
startRight: `${startOffset}px`,
|
||||
"--start-right": `${startOffset}px`,
|
||||
bottom: `${startOffset}px`,
|
||||
startBottom: `${startOffset}px`,
|
||||
"--start-bottom": `${startOffset}px`,
|
||||
endRight: `${endOffset}px`,
|
||||
"--end-right": `${endOffset}px`,
|
||||
endBottom: `${endOffset}px`,
|
||||
"--end-bottom": `${endOffset}px`,
|
||||
rotate: '-45deg',
|
||||
}
|
||||
} else if (choice === 3) {
|
||||
// Vlevo dole
|
||||
return {
|
||||
left: `${startOffset}px`,
|
||||
startLeft: `${startOffset}px`,
|
||||
"--start-left": `${startOffset}px`,
|
||||
bottom: `${startOffset}px`,
|
||||
startBottom: `${startOffset}px`,
|
||||
"--start-bottom": `${startOffset}px`,
|
||||
endLeft: `${endOffset}px`,
|
||||
"--end-left": `${endOffset}px`,
|
||||
endBottom: `${endOffset}px`,
|
||||
"--end-bottom": `${endOffset}px`,
|
||||
rotate: '45deg',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
let easterEggs: EasterEggsJson;
|
||||
|
||||
// Registrace náhodných URL pro všechny existující easter eggy
|
||||
|
||||
if (fs.existsSync(EASTER_EGGS_JSON_PATH)) {
|
||||
const content = fs.readFileSync(EASTER_EGGS_JSON_PATH, 'utf-8');
|
||||
easterEggs = JSON.parse(content);
|
||||
for (const [key, eggs] of Object.entries(easterEggs)) {
|
||||
for (const easterEgg of eggs) {
|
||||
const url = generateUrl();
|
||||
easterEgg.url = url;
|
||||
router.get(`/${url}`, async (req, res, next) => {
|
||||
return getEasterEggImage(req, res, next);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Získání náhodného easter eggu pro přihlášeného uživatele
|
||||
router.get("/", async (req, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
const trusted = getTrusted(parseToken(req));
|
||||
try {
|
||||
// TODO vrátit!
|
||||
// if (trusted) {
|
||||
if (true) {
|
||||
if (easterEggs && login in easterEggs) {
|
||||
const randomEasterEgg = easterEggs[login][Math.floor(Math.random() * easterEggs[login].length)];
|
||||
const { path, startOffset, endOffset, ...strippedEasterEgg } = randomEasterEgg; // Path klient k ničemu nepotřebuje a nemá ho znát
|
||||
return res.status(200).json({ ...strippedEasterEgg, ...getRandomPosition(startOffset, endOffset) });
|
||||
}
|
||||
}
|
||||
return res.status(200).send();
|
||||
} catch (e: any) { next(e) }
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user