minecraft-web/backend/lib/db.js
caadiq 527ac1e51b fix: 아이템/몹 번역 분리
- 아이템 통계: items + blocks 테이블 사용 (달걀 → 달걀)
- 몹 통계: entities 테이블 사용 (달걀 → 던져진 달걀)
- 번역 덮어쓰기 버그 수정
2025-12-24 00:53:29 +09:00

128 lines
4 KiB
JavaScript

import mysql from "mysql2/promise";
// MariaDB 연결 풀 - 환경변수에서 로드
const dbPool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
// 캐시 데이터
let translationsCache = {
blocks: {}, // 블록 번역
items: {}, // 아이템 번역
entities: {}, // 엔티티 번역
itemsAndBlocks: {}, // 아이템 통계용 (items + blocks)
};
let iconsCache = {}; // { "type:name": iconUrl } - 타입별로 구분
let gamerulesCache = {};
/**
* 번역 및 아이콘 데이터 로드 (blocks, items, entities 테이블에서)
*/
async function loadTranslations() {
try {
const [blocks] = await dbPool.query(
"SELECT name, name_ko, icon FROM blocks WHERE mod_id = 'minecraft'"
);
const [items] = await dbPool.query(
"SELECT name, name_ko, icon FROM items WHERE mod_id = 'minecraft'"
);
const [entities] = await dbPool.query(
"SELECT name, name_ko, icon FROM entities WHERE mod_id = 'minecraft'"
);
const [gamerules] = await dbPool.query(
"SELECT name, name_ko, description_ko FROM gamerules WHERE mod_id = 'minecraft'"
);
// 캐시 초기화
translationsCache = { blocks: {}, items: {}, entities: {}, all: {} };
iconsCache = {};
// 타입별로 분리하여 저장
blocks.forEach((row) => {
translationsCache.blocks[row.name] = row.name_ko;
if (row.icon) iconsCache[`block:${row.name}`] = row.icon;
});
items.forEach((row) => {
translationsCache.items[row.name] = row.name_ko;
if (row.icon) iconsCache[`item:${row.name}`] = row.icon;
});
entities.forEach((row) => {
translationsCache.entities[row.name] = row.name_ko;
if (row.icon) iconsCache[`entity:${row.name}`] = row.icon;
});
// 아이템 통계용 캐시 (items + blocks, 아이템 우선)
translationsCache.itemsAndBlocks = {};
Object.assign(translationsCache.itemsAndBlocks, translationsCache.blocks);
Object.assign(translationsCache.itemsAndBlocks, translationsCache.items);
// gamerules 캐시
gamerulesCache = {};
gamerules.forEach((row) => {
gamerulesCache[row.name] = {
name: row.name_ko || row.name,
description: row.description_ko || "",
};
});
const iconCount = Object.keys(iconsCache).length;
console.log(
`[DB] 번역 데이터 로드 완료: blocks ${blocks.length}개, items ${items.length}개, entities ${entities.length}개, icons ${iconCount}`
);
} catch (error) {
console.error("[DB] 번역 데이터 로드 실패:", error.message);
}
}
// 캐시 접근 함수
const getTranslations = () => translationsCache;
const getIcons = () => iconsCache;
const getGamerules = () => gamerulesCache;
const setIconCache = (key, value) => {
iconsCache[key] = value;
};
/**
* 모드팩 테이블 초기화
*/
async function initModpacksTable() {
try {
await dbPool.query(`
CREATE TABLE IF NOT EXISTS modpacks (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
version VARCHAR(50) NOT NULL,
minecraft_version VARCHAR(20),
mod_loader VARCHAR(50),
changelog TEXT,
file_key VARCHAR(500) NOT NULL,
file_size BIGINT DEFAULT 0,
original_filename VARCHAR(255),
contents_json LONGTEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY unique_version (name, version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`);
console.log("[DB] modpacks 테이블 초기화 완료");
} catch (error) {
console.error("[DB] modpacks 테이블 초기화 실패:", error.message);
}
}
export {
dbPool,
dbPool as pool,
loadTranslations,
getTranslations,
getIcons,
getGamerules,
setIconCache,
initModpacksTable,
};