mirror of
https://github.com/Lapikud/tipilan.git
synced 2026-03-23 21:34:21 +00:00
House rules also machine translated and added loadrules universal lib
This commit is contained in:
165
src/lib/loadRules.ts
Normal file
165
src/lib/loadRules.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export type Locale = "et" | "en";
|
||||
export type RuleType = "cs2" | "lol" | "kodukord";
|
||||
|
||||
/**
|
||||
* Loads any rule content for a specific type and locale
|
||||
* @param ruleType - The type of rules to load (cs2, lol, kodukord)
|
||||
* @param locale - The locale to load rules for (et, en)
|
||||
* @returns Promise<string> The markdown content of the rules file
|
||||
*/
|
||||
export async function loadRules(
|
||||
ruleType: RuleType,
|
||||
locale: Locale,
|
||||
): Promise<string> {
|
||||
// Try to load the file for the current locale first
|
||||
let filePath = path.join(
|
||||
process.cwd(),
|
||||
"src",
|
||||
"data",
|
||||
"rules",
|
||||
locale,
|
||||
`${ruleType}.md`,
|
||||
);
|
||||
|
||||
try {
|
||||
// Check if file exists for current locale
|
||||
if (fs.existsSync(filePath)) {
|
||||
const file = await import("fs").then(() =>
|
||||
fs.readFileSync(filePath, "utf8")
|
||||
);
|
||||
return file;
|
||||
}
|
||||
|
||||
// Fallback to Estonian if English version doesn't exist
|
||||
if (locale !== "et") {
|
||||
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 (fs.existsSync(fallbackPath)) {
|
||||
const fallbackFile = fs.readFileSync(fallbackPath, "utf8");
|
||||
return fallbackFile;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`Rules file not found for ${ruleType}`);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Error loading rules for ${ruleType} in locale ${locale}:`,
|
||||
error,
|
||||
);
|
||||
throw new Error(
|
||||
`Failed to load rules for ${ruleType}: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads rules using Bun.file (for Bun runtime environments)
|
||||
* @param ruleType - The type of rules to load
|
||||
* @param locale - The locale to load rules for
|
||||
* @returns Promise<string> The markdown content
|
||||
*/
|
||||
export async function loadRulesBun(
|
||||
ruleType: RuleType,
|
||||
locale: Locale,
|
||||
): Promise<string> {
|
||||
// Try to load the file for the current locale first
|
||||
let filePath = `src/data/rules/${locale}/${ruleType}.md`;
|
||||
let file = Bun.file(filePath);
|
||||
|
||||
// Check if file exists, if not fallback to Estonian
|
||||
if (!(await file.exists()) && locale !== "et") {
|
||||
console.warn(
|
||||
`Rules file not found for ${ruleType} in ${locale}, falling back to Estonian`,
|
||||
);
|
||||
filePath = `src/data/rules/et/${ruleType}.md`;
|
||||
file = Bun.file(filePath);
|
||||
}
|
||||
|
||||
try {
|
||||
const content = await file.text();
|
||||
return content;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Error loading rules for ${ruleType} in locale ${locale}:`,
|
||||
error,
|
||||
);
|
||||
throw new Error(
|
||||
`Failed to load rules for ${ruleType}: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all available rule types for a given locale
|
||||
* @param locale - The locale to check for available rules
|
||||
* @returns Promise<RuleType[]> Array of available rule types
|
||||
*/
|
||||
export async function getAvailableRules(locale: Locale): Promise<RuleType[]> {
|
||||
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);
|
||||
|
||||
return ruleTypes;
|
||||
} catch (error) {
|
||||
console.warn(`Could not read rules directory for locale ${locale}:`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a specific rule file exists for a given locale
|
||||
* @param ruleType - The type of rules to check
|
||||
* @param locale - The locale to check
|
||||
* @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`,
|
||||
);
|
||||
return fs.existsSync(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the best available locale for a rule type
|
||||
* Prefers the requested locale, falls back to Estonian
|
||||
* @param ruleType - The type of rules to check
|
||||
* @param preferredLocale - The preferred locale
|
||||
* @returns Locale The best available locale for the rule type
|
||||
*/
|
||||
export function getBestAvailableLocale(
|
||||
ruleType: RuleType,
|
||||
preferredLocale: Locale,
|
||||
): Locale {
|
||||
if (ruleExists(ruleType, preferredLocale)) {
|
||||
return preferredLocale;
|
||||
}
|
||||
|
||||
if (ruleExists(ruleType, "et")) {
|
||||
return "et";
|
||||
}
|
||||
|
||||
// If neither exists, return preferred (will throw error when trying to load)
|
||||
return preferredLocale;
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user