mirror of
https://github.com/ZZZure/ZZZ-Plugin.git
synced 2025-12-16 13:17:32 +00:00
gacha
This commit is contained in:
parent
0c72964b12
commit
51fb65cdb4
11 changed files with 313 additions and 56 deletions
59
lib/db.js
Normal file
59
lib/db.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { readFileSync, writeFileSync } from 'fs';
|
||||
import path from 'path';
|
||||
import { checkFolderExistAndCreate } from '../utils/file.js';
|
||||
import { dataPath } from './path.js';
|
||||
const dbPath = {
|
||||
gacha: 'gacha',
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} dbName
|
||||
* @param {string} dbFile
|
||||
* @returns {object}
|
||||
*/
|
||||
export function getDB(dbName, dbFile) {
|
||||
const db = dbPath[dbName];
|
||||
const dbFolder = path.join(dataPath, db);
|
||||
try {
|
||||
const dbPath = path.join(dbFolder, `${dbFile}.json`);
|
||||
return JSON.parse(readFileSync(dbPath, 'utf-8'));
|
||||
} catch (error) {
|
||||
logger.mark(`读取数据库失败: ${error.message}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} dbName
|
||||
* @param {string} dbFile
|
||||
* @param {object} data
|
||||
*/
|
||||
export function setDB(dbName, dbFile, data) {
|
||||
const db = dbPath[dbName];
|
||||
const dbFolder = path.join(dataPath, db);
|
||||
try {
|
||||
checkFolderExistAndCreate(dbFolder);
|
||||
const dbPath = path.join(dbFolder, `${dbFile}.json`);
|
||||
writeFileSync(dbPath, JSON.stringify(data, null, 2));
|
||||
} catch (error) {
|
||||
logger.mark(`读取数据库失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} uid
|
||||
* @returns {object}
|
||||
*/
|
||||
export function getGachaLog(uid) {
|
||||
return getDB('gacha', uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} uid
|
||||
* @param {object} data
|
||||
*/
|
||||
export function saveGachaLog(uid, data) {
|
||||
setDB('gacha', uid, data);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue