#include "levelinputdata.h" using namespace ArduinoJson; #include using std::ifstream; using std::ofstream; LevelInputData::LevelInputData() { _id = -1 ; _subid = -1 ; _time = 0 ; _difficulty = 0 ;//0-normal 1-hard 2-veryhard } bool LevelInputData::readFromFile(string filename) { _jewels.clear() ; ifstream ifs( filename.c_str() ) ; if( ifs.good() == false ) return false ; DynamicJsonBuffer buffer ; JsonObject& root = buffer.parse(ifs) ; _id = root["No"].as() ; _name =root["Name"].as() ; if( _name.length()>8 ) { _subid = atof( _name.substr(_name.length()-2).c_str() ) ; }else{ _subid = 0 ; } if( root.containsKey("time") ) { _time = root["time"].as() ; }else{ _time = root["Duration"].as() ; } //如果没有time字段,那么读取Duration字段 _difficulty = root["Difficulty"].as() ;//0-normal 1-hard 2-veryhard JsonArray& goals = root["Goals"].as() ; JsonArray& board = root["Board"].as() ; for(int i = 0 ; i() ; JewelInputData jid ; jid._jewelId = obj1["ItemType"].as() ; jid._count = obj1["Count"].as() ; _jewels.push_back(jid) ; } for(int i = 0 ; i() ; JewelInputData jid ; jid._jewelId = obj1["ItemType"].as() ; jid._count = obj1["Count"].as() ; if( jid._jewelId > 0 ) { //随机宝石暂时不考虑,直接跳过 _jewels.push_back(jid) ; } } return true ; } bool LevelInputData::writeToFile(string filename) { DynamicJsonBuffer buffer ; JsonObject& root = buffer.createObject() ; root["No"] = _id ; root["Name"] = _name ; root["time"] = _time ; root["Duration"] = _time ; root["Difficulty"] = _difficulty ; JsonArray& goals = root.createNestedArray("Goals"); JsonArray& board = root.createNestedArray("Board"); //just leave it empty. for(int i = 0 ; i<_jewels.size();++ i ) { JsonObject& obj1 = goals.createNestedObject(); obj1["ItemType"] = _jewels[i]._jewelId ; obj1["Count"] = _jewels[i]._count ; } string jsontext ; root.printTo(jsontext) ; ofstream ofs( filename.c_str() ) ; if( ofs.good() == false ) return false ; ofs<