mirror of
https://github.com/ChuXunYu/OnlineRpg.git
synced 2026-01-31 05:21:26 +00:00
172 lines
3.5 KiB
C++
172 lines
3.5 KiB
C++
#pragma once
|
|
#include <string>
|
|
#include <vector>
|
|
#include <mutex>
|
|
#include <memory>
|
|
|
|
// 前向声明 SQLite 结构
|
|
struct sqlite3;
|
|
|
|
/**
|
|
* @brief 用户数据结构
|
|
*/
|
|
struct UserData {
|
|
int userId;
|
|
std::string username;
|
|
std::string passwordHash;
|
|
std::string createdAt;
|
|
};
|
|
|
|
/**
|
|
* @brief 角色数据结构
|
|
*/
|
|
struct CharacterData {
|
|
int characterId;
|
|
int userId;
|
|
std::string className;
|
|
int level;
|
|
int hp;
|
|
int mp;
|
|
int attack;
|
|
int speed;
|
|
};
|
|
|
|
/**
|
|
* @brief 数据库管理类
|
|
*
|
|
* 技术点: 数据库 (Database) - SQLite3
|
|
* 线程安全: 使用 std::mutex 保护所有数据库操作
|
|
*/
|
|
class Database {
|
|
private:
|
|
sqlite3* m_db;
|
|
std::string m_dbPath;
|
|
mutable std::mutex m_mutex; // 保证线程安全 (STL)
|
|
bool m_isOpen;
|
|
|
|
public:
|
|
/**
|
|
* @brief 构造函数
|
|
* @param dbPath 数据库文件路径
|
|
*/
|
|
explicit Database(const std::string& dbPath);
|
|
|
|
/**
|
|
* @brief 析构函数
|
|
*/
|
|
~Database();
|
|
|
|
// 禁止拷贝
|
|
Database(const Database&) = delete;
|
|
Database& operator=(const Database&) = delete;
|
|
|
|
/**
|
|
* @brief 打开数据库连接
|
|
*/
|
|
bool open();
|
|
|
|
/**
|
|
* @brief 关闭数据库连接
|
|
*/
|
|
void close();
|
|
|
|
/**
|
|
* @brief 初始化数据库表结构
|
|
*/
|
|
bool initializeTables();
|
|
|
|
/**
|
|
* @brief 检查数据库是否已打开
|
|
*/
|
|
bool isOpen() const { return m_isOpen; }
|
|
|
|
// ===== 用户相关操作 =====
|
|
|
|
/**
|
|
* @brief 注册新用户
|
|
* @param username 用户名
|
|
* @param password 密码 (将被哈希)
|
|
* @return bool 是否成功
|
|
*/
|
|
bool registerUser(const std::string& username, const std::string& password);
|
|
|
|
/**
|
|
* @brief 验证用户登录
|
|
* @param username 用户名
|
|
* @param password 密码
|
|
* @return bool 是否验证成功
|
|
*/
|
|
bool verifyUser(const std::string& username, const std::string& password);
|
|
|
|
/**
|
|
* @brief 检查用户名是否存在
|
|
*/
|
|
bool userExists(const std::string& username);
|
|
|
|
/**
|
|
* @brief 获取用户ID
|
|
*/
|
|
int getUserId(const std::string& username);
|
|
|
|
/**
|
|
* @brief 获取用户数据
|
|
*/
|
|
bool getUserData(const std::string& username, UserData& userData);
|
|
|
|
// ===== 角色相关操作 =====
|
|
|
|
/**
|
|
* @brief 创建角色
|
|
* @param userId 用户ID
|
|
* @param className 职业名称
|
|
* @return bool 是否成功
|
|
*/
|
|
bool createCharacter(int userId, const std::string& className);
|
|
|
|
/**
|
|
* @brief 获取用户的角色数据
|
|
*/
|
|
bool getCharacterData(int userId, CharacterData& charData);
|
|
|
|
/**
|
|
* @brief 更新角色数据
|
|
*/
|
|
bool updateCharacter(const CharacterData& charData);
|
|
|
|
/**
|
|
* @brief 检查用户是否有角色
|
|
*/
|
|
bool hasCharacter(int userId);
|
|
|
|
// ===== 工具函数 =====
|
|
|
|
/**
|
|
* @brief 执行 SQL 语句
|
|
* @param sql SQL 语句
|
|
* @return bool 是否成功
|
|
*/
|
|
bool executeSql(const std::string& sql);
|
|
|
|
/**
|
|
* @brief 获取最后插入的行 ID
|
|
*/
|
|
int getLastInsertId();
|
|
|
|
/**
|
|
* @brief 获取最后的错误信息
|
|
*/
|
|
std::string getLastError();
|
|
|
|
private:
|
|
/**
|
|
* @brief 密码哈希函数 (简单实现)
|
|
* 注意: 生产环境应使用更安全的哈希算法 (如 bcrypt, argon2)
|
|
*/
|
|
std::string hashPassword(const std::string& password);
|
|
|
|
/**
|
|
* @brief 验证密码
|
|
*/
|
|
bool verifyPassword(const std::string& password, const std::string& hash);
|
|
};
|