// // Created by misaki on 2025/9/14. // #pragma once #include "PetBaseClass.h" #include "SDFileManager.h" #include "cJSON.h" #include #include // 辅助函数:枚举类型与字符串的转换 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& pet, const std::string& filename); // 从文件加载宠物数据 std::shared_ptr loadPet(const std::string& filename); // 获取所有保存的宠物文件列表 std::vector listPetFiles(); // 删除宠物文件 bool deletePetFile(const std::string& filename); private: // 将宠物数据转换为JSON对象 cJSON* petToJson(const std::shared_ptr& pet); // 从JSON对象创建宠物 std::shared_ptr petFromJson(cJSON* json); // 将阶段策略转换为JSON对象 cJSON* stageStrategyToJson(const std::shared_ptr& strategy); // 从JSON对象创建阶段策略 std::shared_ptr stageStrategyFromJson(cJSON* json); // 将动作策略转换为JSON对象 cJSON* actionStrategyToJson(const std::shared_ptr& strategy); // 从JSON对象创建动作策略 std::shared_ptr actionStrategyFromJson(cJSON* json); // 文件管理器实例 SDFileManager* fileManager; // 宠物数据存储目录 static constexpr const char* PET_DATA_DIR = "/sdcard/pet_data"; };