place_maxlan/server/config.ts
freesemar93 7968b56976 Allow configuration through a file and export images automatically
Introduce a configuration file for all settings, remove the web-based config editor, fix the grid display, and add automatic hourly PNG exports.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 0385ea33-cde8-4bbd-8fce-8d192d30eb41
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/870d08ce-da3b-4822-9874-c2fe2b7628b1/0385ea33-cde8-4bbd-8fce-8d192d30eb41/Zffw2vY
2025-08-18 12:20:13 +00:00

90 lines
2.9 KiB
TypeScript

import { readFileSync } from "fs";
import { join } from "path";
interface Config {
canvasWidth: number;
canvasHeight: number;
defaultCooldown: number;
enableAutomaticEvents: boolean;
eventDurationMinutes: number;
eventIntervalHours: number;
showGridByDefault: boolean;
autoExportIntervalSeconds: number;
exportPath: string;
}
function parseConfigFile(): Config {
try {
const configPath = join(process.cwd(), "config.cfg");
const configContent = readFileSync(configPath, "utf-8");
const config: Partial<Config> = {};
configContent.split("\n").forEach(line => {
line = line.trim();
if (line.startsWith("#") || !line.includes("=")) return;
const [key, value] = line.split("=");
const trimmedKey = key.trim();
const trimmedValue = value.trim();
switch (trimmedKey) {
case "CANVAS_WIDTH":
config.canvasWidth = parseInt(trimmedValue);
break;
case "CANVAS_HEIGHT":
config.canvasHeight = parseInt(trimmedValue);
break;
case "DEFAULT_COOLDOWN":
config.defaultCooldown = parseInt(trimmedValue);
break;
case "ENABLE_AUTOMATIC_EVENTS":
config.enableAutomaticEvents = trimmedValue.toLowerCase() === "true";
break;
case "EVENT_DURATION_MINUTES":
config.eventDurationMinutes = parseInt(trimmedValue);
break;
case "EVENT_INTERVAL_HOURS":
config.eventIntervalHours = parseInt(trimmedValue);
break;
case "SHOW_GRID_BY_DEFAULT":
config.showGridByDefault = trimmedValue.toLowerCase() === "true";
break;
case "AUTO_EXPORT_INTERVAL_SECONDS":
config.autoExportIntervalSeconds = parseInt(trimmedValue);
break;
case "EXPORT_PATH":
config.exportPath = trimmedValue;
break;
}
});
// Set defaults for missing values
return {
canvasWidth: config.canvasWidth || 100,
canvasHeight: config.canvasHeight || 100,
defaultCooldown: config.defaultCooldown || 5,
enableAutomaticEvents: config.enableAutomaticEvents || false,
eventDurationMinutes: config.eventDurationMinutes || 30,
eventIntervalHours: config.eventIntervalHours || 6,
showGridByDefault: config.showGridByDefault !== undefined ? config.showGridByDefault : true,
autoExportIntervalSeconds: config.autoExportIntervalSeconds || 60,
exportPath: config.exportPath || "./exports/",
};
} catch (error) {
console.error("Error reading config file, using defaults:", error);
return {
canvasWidth: 100,
canvasHeight: 100,
defaultCooldown: 5,
enableAutomaticEvents: false,
eventDurationMinutes: 30,
eventIntervalHours: 6,
showGridByDefault: true,
autoExportIntervalSeconds: 60,
exportPath: "./exports/",
};
}
}
export const config = parseConfigFile();