House rules also machine translated and added loadrules universal lib

This commit is contained in:
2025-09-15 17:46:34 +03:00
parent a9f33e9ec0
commit 5771581013
6 changed files with 255 additions and 37 deletions

View File

@@ -1,35 +1,58 @@
import fs from 'fs';
import path from 'path';
import fs from "fs";
import path from "path";
export type Locale = 'et' | 'en';
export type RuleType = 'cs2' | 'lol';
export type Locale = "et" | "en";
export type RuleType = "cs2" | "lol" | "kodukord";
/**
* Loads rules content for a specific game and locale
* @param ruleType - The type of rules to load (cs2, lol)
* @param ruleType - The type of rules to load (cs2, lol, kodukord)
* @param locale - The locale to load rules for (et, en)
* @returns The markdown content of the rules file
*/
export async function getRules(ruleType: RuleType, locale: Locale): Promise<string> {
const filePath = path.join(process.cwd(), 'src', 'data', 'rules', locale, `${ruleType}.md`);
export async function getRules(
ruleType: RuleType,
locale: Locale,
): Promise<string> {
const filePath = path.join(
process.cwd(),
"src",
"data",
"rules",
locale,
`${ruleType}.md`,
);
try {
const content = fs.readFileSync(filePath, 'utf8');
const content = fs.readFileSync(filePath, "utf8");
return content;
} catch (error) {
// Fallback to Estonian if English version doesn't exist
if (locale === 'en') {
console.warn(`Rules file not found for ${ruleType} in ${locale}, falling back to Estonian`);
const fallbackPath = path.join(process.cwd(), 'src', 'data', 'rules', 'et', `${ruleType}.md`);
if (locale === "en") {
console.warn(
`Rules file not found for ${ruleType} in ${locale}, falling back to Estonian`,
);
const fallbackPath = path.join(
process.cwd(),
"src",
"data",
"rules",
"et",
`${ruleType}.md`,
);
try {
const fallbackContent = fs.readFileSync(fallbackPath, 'utf8');
const fallbackContent = fs.readFileSync(fallbackPath, "utf8");
return fallbackContent;
} catch (fallbackError) {
throw new Error(`Rules file not found for ${ruleType} in either ${locale} or et locale`);
throw new Error(
`Rules file not found for ${ruleType} in either ${locale} or et locale`,
);
}
}
throw new Error(`Rules file not found for ${ruleType} in ${locale} locale: ${error}`);
throw new Error(
`Rules file not found for ${ruleType} in ${locale} locale: ${error}`,
);
}
}
@@ -39,13 +62,13 @@ export async function getRules(ruleType: RuleType, locale: Locale): Promise<stri
* @returns Array of available rule types
*/
export async function getAvailableRules(locale: Locale): Promise<RuleType[]> {
const rulesDir = path.join(process.cwd(), 'src', 'data', 'rules', locale);
const rulesDir = path.join(process.cwd(), "src", "data", "rules", locale);
try {
const files = fs.readdirSync(rulesDir);
const ruleTypes = files
.filter(file => file.endsWith('.md'))
.map(file => file.replace('.md', '') as RuleType);
.filter((file) => file.endsWith(".md"))
.map((file) => file.replace(".md", "") as RuleType);
return ruleTypes;
} catch (error) {
@@ -61,7 +84,14 @@ export async function getAvailableRules(locale: Locale): Promise<RuleType[]> {
* @returns Boolean indicating if the file exists
*/
export function ruleExists(ruleType: RuleType, locale: Locale): boolean {
const filePath = path.join(process.cwd(), 'src', 'data', 'rules', locale, `${ruleType}.md`);
const filePath = path.join(
process.cwd(),
"src",
"data",
"rules",
locale,
`${ruleType}.md`,
);
return fs.existsSync(filePath);
}
@@ -72,13 +102,16 @@ export function ruleExists(ruleType: RuleType, locale: Locale): boolean {
* @param preferredLocale - The preferred locale
* @returns The best available locale for the rule type
*/
export function getBestAvailableLocale(ruleType: RuleType, preferredLocale: Locale): Locale {
export function getBestAvailableLocale(
ruleType: RuleType,
preferredLocale: Locale,
): Locale {
if (ruleExists(ruleType, preferredLocale)) {
return preferredLocale;
}
if (ruleExists(ruleType, 'et')) {
return 'et';
if (ruleExists(ruleType, "et")) {
return "et";
}
// If neither exists, return preferred (will throw error when trying to load)