diff --git a/example.config.json b/example.config.json index 03237b5..5c0ec13 100644 --- a/example.config.json +++ b/example.config.json @@ -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')" } diff --git a/res/sachy/error.html b/res/sachy/error.html index 23746db..f9914df 100644 --- a/res/sachy/error.html +++ b/res/sachy/error.html @@ -5,7 +5,7 @@ - Chyba - šach.ml + Chyba - $domena diff --git a/res/sachy/index.html b/res/sachy/index.html index 0877e65..26d1909 100644 --- a/res/sachy/index.html +++ b/res/sachy/index.html @@ -5,28 +5,15 @@ - Šach.ml + $domena -

Šach.ml

+

$domena

Vytajte na úvodní stránce DENIM3001 šachů!

-

- - +

$pecko

diff --git a/res/sachy/promotion.html b/res/sachy/promotion.html index e06cff6..a15e616 100644 --- a/res/sachy/promotion.html +++ b/res/sachy/promotion.html @@ -5,7 +5,7 @@ - Proměna + Proměna - $domena diff --git a/res/sachy/tah.html b/res/sachy/tah.html index 678da3d..519362d 100644 --- a/res/sachy/tah.html +++ b/res/sachy/tah.html @@ -5,7 +5,7 @@ - Šach.ml - $title + $title - $domena diff --git a/src/modules/sachy.ts b/src/modules/sachy.ts index 3052437..e8ca13f 100644 --- a/src/modules/sachy.ts +++ b/src/modules/sachy.ts @@ -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 = {}; -const hraci: SRecord = {}; +const hry = new Map(); // gameID -> Hra +const hraci = new Map(); // 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; diff --git a/src/utils/sachyServer.ts b/src/utils/sachyServer.ts index 765fdaa..cc94eb7 100644 --- a/src/utils/sachyServer.ts +++ b/src/utils/sachyServer.ts @@ -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 sem.` : `Pro přihlášení pokračuj sem.`); return res.end(response); } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 1e6375c..86d0360 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -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 };