123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #include "levelextinputdata.h"
- using namespace ArduinoJson ;
- #include <fstream>
- using std::ifstream;
- using std::ofstream;
- LevelExtInputData::LevelExtInputData() {
- _seed = 1234567 ;
- _hasMovement = 0;
- _firstMoveType = ""; // A1, B1,B2,B3, C1,C2...
- _firstMoveLyrCnt = 0 ;
- _firstMoveDirection = 0 ; //0-fromRight2Left 1-fromLeft2Right
- _secondMoveType = "" ;
- _secondMoveLyrCnt = 0 ;
- _secondMoveDirection =0 ;
- _bigPlatePercent = 50; //不移动盘子中大盘子百分比
- _midPlatePercent = 50; //不移动盘子中中盘子百分比
- _lyr1Percent = 0 ; //不移动盘子中有一层的盘子百分比
- _lyr2Percent = 100; //不移动盘子中有两层的盘子百分比
- _lyr3Percent = 0 ; //不移动盘子中有三层的盘子百分比
- _matchLyr1 = 70; //第一层可配百分比,第一层是点击层、第二层是可见层、第三层为隐藏层
- _matchLyr2 = 70; //第二层可配百分比
- _matchLyr3 = 70; //第三层可配百分比
- }
- bool LevelExtInputData::readFromFile(string filename)
- {
- *this = LevelExtInputData() ;
- ifstream ifs( filename.c_str() ) ;
- if( ifs.good() == false ) return false ;
- DynamicJsonBuffer buffer ;
- JsonObject& root = buffer.parse(ifs) ;
- _seed = root["seed"].as<int>() ;
- _hasMovement = root["HasMovement"].as<int>() ;
- if( _hasMovement ) {
- _firstMoveType = root["movement"]["firstType"].as<char*>() ; // A1, B1,B2,B3, C1,C2...
- _firstMoveLyrCnt = root["movement"]["firstLyrCnt"].as<int>() ;
- _firstMoveDirection =root["movement"]["firstDirection"].as<int>() ; //0-fromRight2Left 1-fromLeft2Right
- _secondMoveType = root["movement"]["secondType"].as<char*>() ;
- _secondMoveLyrCnt = root["movement"]["secondLyrCnt"].as<int>() ;
- _secondMoveDirection =root["movement"]["secondDirection"].as<int>() ;
- }
- _bigPlatePercent = root["BigPlatePercent"].as<int>() ; //不移动盘子中大盘子百分比
- _midPlatePercent = root["MidPlatePercent"].as<int>() ; //不移动盘子中中盘子百分比
- _lyr1Percent = root["Lyr1Percent"].as<int>() ; //不移动盘子中有一层的盘子百分比
- _lyr2Percent = root["Lyr2Percent"].as<int>() ; //不移动盘子中有两层的盘子百分比
- _lyr3Percent = root["Lyr3Percent"].as<int>() ; //不移动盘子中有三层的盘子百分比
- _matchLyr1 = root["MatchLyr1"].as<int>() ; //第一层可配百分比,第一层是点击层、第二层是可见层、第三层为隐藏层
- _matchLyr2 = root["MatchLyr2"].as<int>() ; //第二层可配百分比
- _matchLyr3 = root["MatchLyr3"].as<int>() ; //第三层可配百分比
- ifs.close();
- return true;
- }
- bool LevelExtInputData::writeToFile(string filename)
- {
- DynamicJsonBuffer buffer ;
- JsonObject& root = buffer.createObject() ;
- root["seed"] = _seed ;
- root["HasMovement"] = _hasMovement ;
- JsonObject& movement = root.createNestedObject("movement") ;
- movement["firstType"] = _firstMoveType ;
- movement["firstLyrCnt"] = _firstMoveLyrCnt ;
- movement["firstDirection"] = _firstMoveDirection ;
- movement["secondType"] = _secondMoveType ;
- movement["secondLyrCnt"] = _secondMoveLyrCnt ;
- movement["secondDirection"] = _secondMoveDirection ;
- root["BigPlatePercent"] = _bigPlatePercent ; //不移动盘子中大盘子百分比
- root["MidPlatePercent"] = _midPlatePercent ; //不移动盘子中中盘子百分比
- root["Lyr1Percent"] = _lyr1Percent ; //不移动盘子中有一层的盘子百分比
- root["Lyr2Percent"] = _lyr2Percent; //不移动盘子中有两层的盘子百分比
- root["Lyr3Percent"] = _lyr3Percent; //不移动盘子中有三层的盘子百分比
- root["MatchLyr1"] = _matchLyr1 ; //第一层可配百分比,第一层是点击层、第二层是可见层、第三层为隐藏层
- root["MatchLyr2"] = _matchLyr2 ; //第二层可配百分比
- root["MatchLyr3"] = _matchLyr3 ; //第三层可配百分比
- string jsontext ;
- root.printTo(jsontext) ;
- ofstream ofs( filename.c_str() ) ;
- if( ofs.good() == false ) return false ;
- ofs<<jsontext ;
- ofs.close() ;
- return true ;
- }
- string LevelExtInputData::levelInputFilename2ExtFilename( string levelInputFilename )
- {
- size_t dotpos = levelInputFilename.rfind('.') ;
- if( dotpos==string::npos ) return "" ;
- string res = levelInputFilename.substr(0, dotpos ) + "_ext.json";
- return res ;
- }
|