// // Created by misaki on 2025/9/14. // #pragma once #include "PetBaseClass.h" #include "SDFileManager.h" #include "cpp_json.h" #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对象 cppjson::Json petToJson(const std::shared_ptr &pet); // 返回 cppjson::Json // 从JSON对象创建宠物 std::shared_ptr petFromJson(const cppjson::Json &json); // 参数改为 cppjson::Json // 将阶段策略转换为JSON对象 cppjson::Json stageStrategyToJson(const std::shared_ptr &strategy); // 从JSON对象创建阶段策略 std::shared_ptr stageStrategyFromJson(const cppjson::Json &json); // 将动作策略转换为JSON对象 cppjson::Json actionStrategyToJson(const std::shared_ptr &strategy); // 从JSON对象创建动作策略 std::shared_ptr actionStrategyFromJson(const cppjson::Json &json); // 文件管理器实例 SDFileManager *fileManager; // 宠物数据存储目录 static constexpr const char *PET_DATA_DIR = "/sdcard/pet_data"; }; /** * 宠物数据结构(JSON) { "name": "芝士雪豹", "hp": 85, "density": 120, "identity": "我是顶真,是妈妈省的", "stage_strategy": { "current_stage": "PET_STAGE_ADULT", "stage_model_map": { "PET_STAGE_YOUNG": "/models/snow_leopard_young.gif", "PET_STAGE_ADULT": "/models/snow_leopard_adult.gif", "PET_STAGE_OLD": "/models/snow_leopard_old.gif" }, "stage_audio_map": { "PET_STAGE_YOUNG": "/audio/snow_leopard_young.mp3", "PET_STAGE_ADULT": "/audio/snow_leopard_adult.mp3", "PET_STAGE_OLD": "/audio/snow_leopard_old.mp3" } }, "action_strategy": { "current_action": "PET_ACTION_SLEEP", "action_model_map": { "PET_ACTION_SLEEP": "/models/actions/sleep.gif", "PET_ACTION_EAT": "/models/actions/eat.gif", "PET_ACTION_HAPPY": "/models/actions/happy.gif", "PET_ACTION_ANGRY": "/models/actions/angry.gif", "PET_ACTION_SAD": "/models/actions/sad.gif", "PET_ACTION_EVOLVE": "/models/actions/evolve.gif", "PET_ACTION_TOUCH": "/models/actions/touch.gif" }, "action_audio_map": { "PET_ACTION_SLEEP": "/audio/actions/sleep.mp3", "PET_ACTION_EAT": "/audio/actions/eat.mp3", "PET_ACTION_HAPPY": "/audio/actions/happy.mp3", "PET_ACTION_ANGRY": "/audio/actions/angry.mp3", "PET_ACTION_SAD": "/audio/actions/sad.mp3", "PET_ACTION_EVOLVE": "/audio/actions/evolve.mp3", "PET_ACTION_TOUCH": "/audio/actions/touch.mp3" } } } */