Files
Bionic_sphere/Bionic_Core/PetBaseClass/PetDao.h
T
Misaki 4cc761aab3 1. 完成了语音识别的C++业务层封装,测试通过
2. 试着测试了一下LVGL_GIF渲染+音乐播放+语音识别的组合简单优化后,
          发现lvgl渲染略显卡顿,语音识别有缓冲区空警告,不过无伤大雅,还需要进一步深度优化。
2025-09-16 01:29:17 +08:00

108 lines
3.4 KiB
C++

//
// Created by misaki on 2025/9/14.
//
#pragma once
#include "PetBaseClass.h"
#include "SDFileManager.h"
#include "cJSON.h"
#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";
};
/**
* 宠物数据结构(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"
}
}
}
*/