1. 历时两天,完整且完美的设计了宠物类,使用到了多种设计模式,完成了低耦合,高内聚的完美代码,测试也完美通过。
2. 顺便完善了底层通信类的封装,基于websocket,基准测试通过,但存在一点很小的线程bug,似乎是来自于esp32 idf底层的问题,待解决
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// Created by misaki on 2025/9/14.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "PetBaseClass.h"
|
||||
#include "SDFileManager.h"
|
||||
#include "cJSON.h"
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
// 辅助函数:枚举类型与字符串的转换
|
||||
namespace PetEnumConverter {
|
||||
// PetStageType 转换
|
||||
std::string stageTypeToString(PetStageType stage);
|
||||
PetStageType stringToStageType(const std::string& str);
|
||||
|
||||
// PetActionType 转换
|
||||
std::string actionTypeToString(PetActionType action);
|
||||
PetActionType stringToActionType(const std::string& str);
|
||||
}
|
||||
|
||||
// PetDAO 类 - 负责宠物的数据持久化
|
||||
class PetDAO {
|
||||
public:
|
||||
// 构造函数,需要SDFileManager实例
|
||||
explicit PetDAO(SDFileManager* fileManager);
|
||||
|
||||
// 保存宠物数据到文件
|
||||
bool savePet(const std::shared_ptr<PetBase>& pet, const std::string& filename);
|
||||
|
||||
// 从文件加载宠物数据
|
||||
std::shared_ptr<PetBase> loadPet(const std::string& filename);
|
||||
|
||||
// 获取所有保存的宠物文件列表
|
||||
std::vector<std::string> listPetFiles();
|
||||
|
||||
// 删除宠物文件
|
||||
bool deletePetFile(const std::string& filename);
|
||||
|
||||
private:
|
||||
// 将宠物数据转换为JSON对象
|
||||
cJSON* petToJson(const std::shared_ptr<PetBase>& pet);
|
||||
|
||||
// 从JSON对象创建宠物
|
||||
std::shared_ptr<PetBase> petFromJson(cJSON* json);
|
||||
|
||||
// 将阶段策略转换为JSON对象
|
||||
cJSON* stageStrategyToJson(const std::shared_ptr<PetStageStrategy>& strategy);
|
||||
|
||||
// 从JSON对象创建阶段策略
|
||||
std::shared_ptr<PetStageStrategy> stageStrategyFromJson(cJSON* json);
|
||||
|
||||
// 将动作策略转换为JSON对象
|
||||
cJSON* actionStrategyToJson(const std::shared_ptr<PetActionStrategy>& strategy);
|
||||
|
||||
// 从JSON对象创建动作策略
|
||||
std::shared_ptr<PetActionStrategy> actionStrategyFromJson(cJSON* json);
|
||||
|
||||
// 文件管理器实例
|
||||
SDFileManager* fileManager;
|
||||
|
||||
// 宠物数据存储目录
|
||||
static constexpr const char* PET_DATA_DIR = "/sdcard/pet_data";
|
||||
};
|
||||
Reference in New Issue
Block a user