105 lines
3.2 KiB
JavaScript
105 lines
3.2 KiB
JavaScript
const redis = require('redis');
|
|
const pomelo = require('pomelo');
|
|
|
|
const { query_game, query_guild } = require('../lib/db');
|
|
|
|
const config = pomelo.app.get('redis');
|
|
|
|
const client = redis.createClient({ url: `redis://${config.host}:${config.port}` });
|
|
|
|
client.on('error', (err) => {
|
|
console.error(err);
|
|
});
|
|
|
|
async function init() {
|
|
await client.connect();
|
|
}
|
|
init();
|
|
|
|
class UserDao {
|
|
|
|
async getUserInfo(userid) {
|
|
const cacheResult = await client.hGet(`t_user:${userid}`, 'value');
|
|
if (cacheResult != null) {
|
|
const userInfo = JSON.parse(cacheResult);
|
|
const idx = await client.hGet(`t_user:${userid}`, 'idx');
|
|
return { ...userInfo, idx: parseInt(idx, 10) };
|
|
} else {
|
|
return await this.fetchAndCacheUserInfoFromGameDB(userid);
|
|
}
|
|
}
|
|
|
|
async getUserId(idx) {
|
|
const cacheResult = await client.hGet(`t_user:${idx}`, 'account_id');
|
|
if (cacheResult != null) {
|
|
return cacheResult;
|
|
} else {
|
|
const query = `SELECT account_id FROM t_users WHERE idx = ?`;
|
|
const results = await query_guild(query, [idx]);
|
|
if (results.length === 0) {
|
|
throw new Error(`User ID not found: ${idx}`);
|
|
} else {
|
|
const userId = results[0].account_id;
|
|
await client.hSet(`t_user:${userId}`, 'account_id', userId);
|
|
await client.hSet(`t_user:${userId}`, 'idx', idx);
|
|
return userId;
|
|
}
|
|
}
|
|
}
|
|
|
|
async getUserIdx(userid) {
|
|
const cacheResult = await client.hGet(`t_user:${userid}`, 'idx');
|
|
if (cacheResult != null) {
|
|
return parseInt(cacheResult, 10);
|
|
} else {
|
|
const userInfo = await this.fetchAndCacheUserInfoFromGameDB(userid);
|
|
return userInfo.idx;
|
|
}
|
|
}
|
|
|
|
async fetchAndCacheUserInfoFromGameDB(userid) {
|
|
const query = `SELECT idx,account_id,name,sex,head_id,head_frame,level,bceg,gold,diamond,rank,ring_id FROM t_user WHERE account_id=?`;
|
|
const results = await query_game(query, [userid]);
|
|
if (results.length === 0) {
|
|
throw new Error(`User not found: ${userid}`);
|
|
} else {
|
|
const userInfo = results[0];
|
|
userInfo.name = userInfo.name.toString();
|
|
const query_insert = `INSERT INTO t_users (account, username, password) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE account=VALUES(account), username=VALUES(username), password=VALUES(password)`;
|
|
const r = await query_guild(query_insert, [userid, userInfo.name, ""]);
|
|
|
|
// 获取新插入的 idx 或已存在的 idx
|
|
const query_idx = `SELECT idx FROM t_users WHERE account = ?`;
|
|
const idxResult = await query_guild(query_idx, [userid]);
|
|
const idx = idxResult[0].idx;
|
|
|
|
await client.hSet(`t_user:${userid}`, 'value', JSON.stringify(userInfo));
|
|
await client.hSet(`t_user:${userid}`, 'idx', idx);
|
|
|
|
return { ...userInfo, idx };
|
|
}
|
|
}
|
|
|
|
async findUser(name, uid) {
|
|
const query = `SELECT account_id, name FROM t_user WHERE CAST(name as CHAR) LIKE ?`;
|
|
const results = await query_game(query, ["%"+name+"%"]);
|
|
|
|
if (results.length === 0) {
|
|
throw new Error(`User not found: ${name}`);
|
|
}
|
|
|
|
const users = results.map((user) => {
|
|
this.getUserInfo(user.account_id);
|
|
return {
|
|
uid: user.account_id,
|
|
name: user.name.toString(),
|
|
};
|
|
});
|
|
|
|
return users;
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new UserDao();
|