docs: explain lightweight game helper functions

This commit is contained in:
nyan404 committed 2026-08-25 04:22:11 +03:00
1 parent bea7c39f7b
commit afa56b46ee
1 file changed
+16
+16
View File
@@ -78,6 +78,7 @@
} }
}; };
// Entry point: wires settings, timers, and page-specific observers together.
function init() { function init() {
if (isGamePage()) { if (isGamePage()) {
if (settings.smellTimer) setupSmellTimer(); if (settings.smellTimer) setupSmellTimer();
@@ -88,10 +89,12 @@
} }
} }
// Returns true when the current URL belongs to the CatWar game view.
function isGamePage() { function isGamePage() {
return /^\/cw3(\/(kns|jagd))?/.test(window.location.pathname); return /^\/cw3(\/(kns|jagd))?/.test(window.location.pathname);
} }
// Detects direct-message pages where the DM helper should be enabled.
function isDmPage() { function isDmPage() {
return /^\/ls/.test(window.location.pathname); return /^\/ls/.test(window.location.pathname);
} }
@@ -101,6 +104,7 @@
* Вспомогательные утилиты * Вспомогательные утилиты
* --------------------- * ---------------------
*/ */
// Loads persisted user preferences, falling back to the built-in defaults.
function loadSettings() { function loadSettings() {
try { try {
const raw = window.localStorage.getItem(STORAGE_KEY); const raw = window.localStorage.getItem(STORAGE_KEY);
@@ -113,6 +117,7 @@
} }
} }
// Persists the supplied preferences for the next page visit.
function saveSettings(value) { function saveSettings(value) {
try { try {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(value)); window.localStorage.setItem(STORAGE_KEY, JSON.stringify(value));
@@ -121,6 +126,7 @@
} }
} }
// Creates a reusable audio element configured for an alert sound.
function createAudio(src, volume) { function createAudio(src, volume) {
const audio = new Audio(); const audio = new Audio();
audio.preload = 'none'; audio.preload = 'none';
@@ -129,6 +135,7 @@
return audio; return audio;
} }
// Converts a visible timer string into seconds, or null when it is invalid.
function parseSeconds(text) { function parseSeconds(text) {
if (!text) return NaN; if (!text) return NaN;
const clean = text.replace(/\u00A0/g, ' '); const clean = text.replace(/\u00A0/g, ' ');
@@ -147,6 +154,7 @@
* Смелл-таймер * Смелл-таймер
* --------------------- * ---------------------
*/ */
// Locates the smell countdown and attaches the alert timer behavior.
function setupSmellTimer() { function setupSmellTimer() {
const menu = document.querySelector('#app > span.small, .small'); const menu = document.querySelector('#app > span.small, .small');
if (!menu || menu.querySelector('#cwshed-lite-smell')) return; if (!menu || menu.querySelector('#cwshed-lite-smell')) return;
@@ -186,6 +194,7 @@
} }
} }
// Starts a one-shot countdown that updates the page and plays an alert.
function startSmellTimer(seconds) { function startSmellTimer(seconds) {
const label = document.getElementById('cwshed-lite-smell'); const label = document.getElementById('cwshed-lite-smell');
if (!label) return; if (!label) return;
@@ -215,6 +224,7 @@
smellTimerHandle = window.setInterval(tick, 1000); smellTimerHandle = window.setInterval(tick, 1000);
} }
// Cancels the active smell countdown and restores its original display.
function clearSmellTimer() { function clearSmellTimer() {
if (smellTimerHandle) { if (smellTimerHandle) {
window.clearInterval(smellTimerHandle); window.clearInterval(smellTimerHandle);
@@ -229,6 +239,7 @@
if (label) label.textContent = 'Нюх: —'; if (label) label.textContent = 'Нюх: —';
} }
// Re-reads the current smell value and starts a fresh countdown if possible.
function restartSmellTimer() { function restartSmellTimer() {
clearSmellTimer(); clearSmellTimer();
if (settings.smellTimer && isGamePage()) { if (settings.smellTimer && isGamePage()) {
@@ -236,6 +247,7 @@
} }
} }
// Formats a number of seconds as the compact MM:SS timer shown to players.
function formatTime(totalSeconds) { function formatTime(totalSeconds) {
const hours = Math.floor(totalSeconds / 3600); const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60); const minutes = Math.floor((totalSeconds % 3600) / 60);
@@ -252,6 +264,7 @@
* Уведомление о конце действия * Уведомление о конце действия
* --------------------- * ---------------------
*/ */
// Observes the action panel and schedules sound/title alerts for completion.
function setupActionWatcher() { function setupActionWatcher() {
if (actionIntervalHandle) window.clearInterval(actionIntervalHandle); if (actionIntervalHandle) window.clearInterval(actionIntervalHandle);
const audio = (settings.actionAlert && settings.actionAlertSound) const audio = (settings.actionAlert && settings.actionAlertSound)
@@ -297,12 +310,14 @@
}, 1000); }, 1000);
} }
// Restores the document title after an action notification has been handled.
function restoreTitle() { function restoreTitle() {
if (storedTitle) { if (storedTitle) {
document.title = storedTitle; document.title = storedTitle;
} }
} }
// Stops and reinitializes action observation after a DOM refresh.
function restartActionWatcher() { function restartActionWatcher() {
if (!isGamePage()) return; if (!isGamePage()) return;
if (actionIntervalHandle) { if (actionIntervalHandle) {
@@ -321,6 +336,7 @@
* ID собеседника в ЛС * ID собеседника в ЛС
* --------------------- * ---------------------
*/ */
// Adds the current conversation identifier to DM links when it is available.
function injectDmId(force) { function injectDmId(force) {
const container = document.getElementById('main'); const container = document.getElementById('main');
if (!container) return; if (!container) return;