ŠACHY COMEBACK!!

This commit is contained in:
Histmy 2024-06-27 19:53:18 +02:00
parent 41f3b94ed3
commit 43c7e1f5b1
Signed by: Histmy
GPG Key ID: AC2E43C463D8F329
8 changed files with 52 additions and 52 deletions

View File

@ -10,5 +10,6 @@
"DBUser": "povinné, string - uživatelské jméno pro připojení do databáze",
"DBPwd": "povinné, string - heslo pro připojení do databáze",
"noGeneralSync": "nepovinné, boolean - nezálohovat generál dobytčárny do databáze",
"ignoreUnknownKeys": "nepovinné, boolean - vypne hlášení neznámých klíčů v config.json"
"ignoreUnknownKeys": "nepovinné, boolean - vypne hlášení neznámých klíčů v config.json",
"sachyDomena": "nepovinné, string - doména pro funkci sachy (základní hodnota je 'šach.ml')"
}

View File

@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chyba - šach.ml</title>
<title>Chyba - $domena</title>
</head>
<body>

View File

@ -5,28 +5,15 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Šach.ml</title>
<title>$domena</title>
</head>
<body>
<h1>Šach.ml</h1>
<h1>$domena</h1>
<p>Vytajte na úvodní stránce DENIM3001 šachů!</p>
<p id="sem"></p>
<script>
// Jo, použivám JS na render :fujjjj:
// Prasárna, jsem si vědom, ale je to nejjednoduší :ksd:
const pecko = document.querySelector("#sem");
if ($logged) {
pecko.innerHTML = "Jsi přihlášen jako $jmeno. Pro odhlášení pokračuj <a href='logout'>sem</a>.";
} else {
pecko.innerHTML = "Pro přihlášení pokračujte <a href='login'>sem</a>.";
}
</script>
<p>$pecko</p>
</body>
</html>

View File

@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Proměna</title>
<title>Proměna - $domena</title>
</head>
<body>

View File

@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Šach.ml - $title</title>
<title>$title - $domena</title>
</head>
<body>

View File

@ -1,18 +1,19 @@
// Jo, šachec, :sjetec:
import { APIEmbed, Message } from "discord.js";
import { Modul, SRecord } from "../utils/types";
import { Modul } from "../utils/types";
import { fokinLookupTable, klikance, pocetKeStrane } from "../utils/sachyEtc";
import { emiter } from "../utils/sachyServer";
import { emiter, sachyDomena } from "../utils/sachyServer";
const hry: SRecord<Hra> = {};
const hraci: SRecord<string> = {};
const hry = new Map<number, Hra>(); // gameID -> Hra
const hraci = new Map<string, number>(); // hrac -> gameID
let nextGameID = 0;
const smery = [-8, 1, 8, -1, -7, 9, 7, -9];
const mkPis = (barva: Figurka["barva"], typ: Figurka["typ"]) => ({ barva, typ, netahnuta: true, enPassantovatelna: false });
function createGame(kanel: string, bilej: string, cernej: string, mes: Message) {
function createGame(gameID: number, bilej: string, cernej: string, mes: Message) {
const deska: Deska = [...new Array(64)];
@ -43,6 +44,7 @@ function createGame(kanel: string, bilej: string, cernej: string, mes: Message)
deska[59] = mkPis("bila", "queen");
const hra: Hra = {
id: gameID,
bila: bilej,
cerna: cernej,
deska,
@ -51,7 +53,7 @@ function createGame(kanel: string, bilej: string, cernej: string, mes: Message)
message: mes
};
hry[kanel] = hra;
hry.set(gameID, hra);
return hra;
}
@ -330,7 +332,7 @@ function renderHra(hra: Hra, konec?: true) {
if (!figurka) {
field.value += hra.tahy.includes(i)
? (cernyPozadi ? `[<:h:1066376051422404628>](http://šach.ml/${klikanec})` : `[<:h:1066376343807336488>](http://šach.ml/${klikanec})`)
? (cernyPozadi ? `[<:h:1066376051422404628>](http://${sachyDomena}/${klikanec})` : `[<:h:1066376343807336488>](http://${sachyDomena}/${klikanec})`)
: (cernyPozadi ? "🟩" : "⬜");
} else {
@ -342,7 +344,7 @@ function renderHra(hra: Hra, konec?: true) {
const figurkaStr = `<:h:${emout}>`;
if (!konec && figurka.barva == hra.hraje || hra.tahy.includes(i)) {
field.value += `[${figurkaStr}](http://šach.ml/${klikanec})`;
field.value += `[${figurkaStr}](http://${sachyDomena}/${klikanec})`;
} else {
field.value += figurkaStr;
}
@ -360,9 +362,11 @@ function renderHra(hra: Hra, konec?: true) {
function konec(mes: Message, hra: Hra, duvod: string) {
mes.edit({ content: `hra zkoncila ${duvod}`, embeds: [renderHra(hra, true)] });
delete hraci[hra.bila];
delete hraci[hra.cerna];
delete hry[mes.channelId];
const gameID = hra.id;
hraci.delete(hra.bila);
hraci.delete(hra.cerna);
hry.delete(gameID);
}
function prekreslitHru(hra: Hra) {
@ -398,10 +402,10 @@ function prekreslitHru(hra: Hra) {
}
emiter.on("tah", (hrac, policko, respond, promo) => {
const channelId = hraci[hrac];
if (!channelId) return respond("Však vůbec nehraješ ty magore");
const gameID = hraci.get(hrac);
if (!gameID) return respond("Však vůbec nehraješ ty magore");
const hra = hry[channelId];
const hra = hry.get(gameID)!;
const barva = hra.bila == hrac ? "bila" : "cerna";
if (hra.hraje != barva) return respond("Teď nehraješ ty ty magore");
@ -467,20 +471,18 @@ const exp: Modul = {
DMUnsafe: true,
run: async mes => {
if (hry[mes.channelId]) return "nejze tady se us hraej";
const druhej = mes.mentions.members?.first()?.id;
if (!druhej) return "no sam asi hrat nebudez vet";
for (const player of [mes.author.id, druhej]) {
if (hraci[player]) return "zakaz symultanki";
if (hraci.has(player)) return "zakaz symultanki";
}
const message = await mes.channel.send("hraj zopiciva");
const hra = createGame(mes.channelId, mes.author.id, druhej, message);
hraci[mes.author.id] = mes.channelId;
hraci[druhej] = mes.channelId;
const hra = createGame(++nextGameID, mes.author.id, druhej, message);
hraci.set(mes.author.id, nextGameID);
hraci.set(druhej, nextGameID);
message.edit({ content: "", embeds: [renderHra(hra)] });
@ -489,10 +491,10 @@ const exp: Modul = {
jasevzdavam: mes => {
if (!hraci[mes.author.id]) return "vsak nehrajes magore";
const gameID = hraci.get(mes.author.id);
if (!gameID) return "vsak nehrajes magore";
const hra = hry[mes.channelId];
if (!hra) return "tadi nehrajes";
const hra = hry.get(gameID)!;
konec(hra.message, hra, `${mes.author} je pusi`);
}
@ -503,6 +505,7 @@ const exp: Modul = {
module.exports = exp;
type Hra = {
id: number;
bila: string;
cerna: string;
deska: Deska;

View File

@ -15,6 +15,8 @@ const tahStranka = readFileSync(`${basePath}/tah.html`).toString();
const promoStranka = readFileSync(`${basePath}/promotion.html`).toString();
const chybaStranka = readFileSync(`${basePath}/error.html`).toString();
export const sachyDomena = process.env.sachyDomena ?? "šach.ml";
interface Eventy {
tah: (playerId: string, square: string, verify: (err?: string) => void, promotionType?: string) => void;
}
@ -92,7 +94,11 @@ function logout(session: Session, res: ServerResponse) {
}
function loginErr(res: ServerResponse) {
res.end(chybaStranka.replaceAll("$error", "Prihlaseni se nezdarilo, zkus to znovu a jestli to nefunguje furt, tak si holt stezuj no"));
res.end(
chybaStranka
.replace("$domena", sachyDomena)
.replace("$error", "Prihlaseni se nezdarilo, zkus to znovu a jestli to nefunguje furt, tak si holt stezuj no")
);
}
async function overeni(url: string, session: Session, res: ServerResponse) {
@ -112,7 +118,7 @@ async function overeni(url: string, session: Session, res: ServerResponse) {
session.logedIn = true;
session.discordId = uzivatel.id;
session.discordName = `${uzivatel.username}#${uzivatel.discriminator}`;
session.discordName = uzivatel.username;
redirect(res, session.redirect || "/");
}
@ -126,9 +132,11 @@ function validateMove(res: ServerResponse, session: Session, url: string) {
function overeni(err?: string) {
if (err == "--promote--")
return res.end(promoStranka);
return res.end(promoStranka.replace("$domena", sachyDomena));
const response = tahStranka.replace("$title", err ? "Neplatej tah" : "Úspšný tah")
const response = tahStranka
.replace("$domena", sachyDomena)
.replace("$title", err ? "Neplatej tah" : "Úspšný tah")
.replace("$content", err ?? "Kvalitní tah!")
.replace("$podminka", err ? "0" : "1");
@ -141,17 +149,18 @@ function validateMove(res: ServerResponse, session: Session, url: string) {
const server = createServer((req, res) => {
const session = startSession(req, res);
// TODO: Natipovaný, protože vscode jebe (možná někdy přestane, v tom případě odebrat pls)
const url: URL = new URL(req.url!, "http://šach.ml");
const url = new URL(req.url!, `http://${sachyDomena}`);
// Zpracování tahu
if (url.pathname.length == 2 || url.pathname.length == 4) return validateMove(res, session, req.url!);
if (req.url?.startsWith("/oauth")) return overeni(req.url, session, res);
switch (url.pathname) {
case "/": {
const response = indexStranka.replace("$logged", session.logedIn ? "1" : "0")
.replace("$jmeno", session.discordName ?? "");
const response = indexStranka
.replaceAll("$domena", sachyDomena)
.replace("$pecko", session.logedIn ? `Jsi přihlášen jako ${session.discordName}. Pro odhlášení pokračuj <a href="logout">sem</a>.` : `Pro přihlášení pokračuj <a href="login">sem</a>.`);
return res.end(response);
}

View File

@ -6,7 +6,7 @@ if (!existsSync("config.json")) throw new Error("config.json neexistuje");
const konfig = require("../../config.json");
if (!konfig.ignoreUnknownKeys)
for (const klic of Object.keys(konfig)) {
if (!["adminChannel", "adminID", "DBPwd", "DBUser", "dieOnError", "ignoreMess", "ignorePresence", "ignoreSpink", "ignoreUnknownKeys", "noGeneralSync", "prefix", "statPass", "token"].includes(klic)) throw new Error(`config.json obsahuje neznámý klíč: ${klic}`);
if (!["adminChannel", "adminID", "DBPwd", "DBUser", "dieOnError", "ignoreMess", "ignorePresence", "ignoreSpink", "ignoreUnknownKeys", "noGeneralSync", "prefix", "sachyDomena", "statPass", "token"].includes(klic)) throw new Error(`config.json obsahuje neznámý klíč: ${klic}`);
}
process.env = { ...process.env, ...konfig };