levelextinputdata.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "levelextinputdata.h"
  2. using namespace ArduinoJson ;
  3. #include <fstream>
  4. using std::ifstream;
  5. using std::ofstream;
  6. LevelExtInputData::LevelExtInputData() {
  7. _seed = 1234567 ;
  8. _hasMovement = 0;
  9. _firstMoveType = ""; // A1, B1,B2,B3, C1,C2...
  10. _firstMoveLyrCnt = 0 ;
  11. _firstMoveDirection = 0 ; //0-fromRight2Left 1-fromLeft2Right
  12. _secondMoveType = "" ;
  13. _secondMoveLyrCnt = 0 ;
  14. _secondMoveDirection =0 ;
  15. _bigPlatePercent = 50; //不移动盘子中大盘子百分比
  16. _midPlatePercent = 50; //不移动盘子中中盘子百分比
  17. _lyr1Percent = 0 ; //不移动盘子中有一层的盘子百分比
  18. _lyr2Percent = 100; //不移动盘子中有两层的盘子百分比
  19. _lyr3Percent = 0 ; //不移动盘子中有三层的盘子百分比
  20. _matchLyr1 = 70; //第一层可配百分比,第一层是点击层、第二层是可见层、第三层为隐藏层
  21. _matchLyr2 = 70; //第二层可配百分比
  22. _matchLyr3 = 70; //第三层可配百分比
  23. }
  24. bool LevelExtInputData::readFromFile(string filename)
  25. {
  26. *this = LevelExtInputData() ;
  27. ifstream ifs( filename.c_str() ) ;
  28. if( ifs.good() == false ) return false ;
  29. DynamicJsonBuffer buffer ;
  30. JsonObject& root = buffer.parse(ifs) ;
  31. _seed = root["seed"].as<int>() ;
  32. _hasMovement = root["HasMovement"].as<int>() ;
  33. if( _hasMovement ) {
  34. _firstMoveType = root["movement"]["firstType"].as<char*>() ; // A1, B1,B2,B3, C1,C2...
  35. _firstMoveLyrCnt = root["movement"]["firstLyrCnt"].as<int>() ;
  36. _firstMoveDirection =root["movement"]["firstDirection"].as<int>() ; //0-fromRight2Left 1-fromLeft2Right
  37. _secondMoveType = root["movement"]["secondType"].as<char*>() ;
  38. _secondMoveLyrCnt = root["movement"]["secondLyrCnt"].as<int>() ;
  39. _secondMoveDirection =root["movement"]["secondDirection"].as<int>() ;
  40. }
  41. _bigPlatePercent = root["BigPlatePercent"].as<int>() ; //不移动盘子中大盘子百分比
  42. _midPlatePercent = root["MidPlatePercent"].as<int>() ; //不移动盘子中中盘子百分比
  43. _lyr1Percent = root["Lyr1Percent"].as<int>() ; //不移动盘子中有一层的盘子百分比
  44. _lyr2Percent = root["Lyr2Percent"].as<int>() ; //不移动盘子中有两层的盘子百分比
  45. _lyr3Percent = root["Lyr3Percent"].as<int>() ; //不移动盘子中有三层的盘子百分比
  46. _matchLyr1 = root["MatchLyr1"].as<int>() ; //第一层可配百分比,第一层是点击层、第二层是可见层、第三层为隐藏层
  47. _matchLyr2 = root["MatchLyr2"].as<int>() ; //第二层可配百分比
  48. _matchLyr3 = root["MatchLyr3"].as<int>() ; //第三层可配百分比
  49. ifs.close();
  50. return true;
  51. }
  52. bool LevelExtInputData::writeToFile(string filename)
  53. {
  54. DynamicJsonBuffer buffer ;
  55. JsonObject& root = buffer.createObject() ;
  56. root["seed"] = _seed ;
  57. root["HasMovement"] = _hasMovement ;
  58. JsonObject& movement = root.createNestedObject("movement") ;
  59. movement["firstType"] = _firstMoveType ;
  60. movement["firstLyrCnt"] = _firstMoveLyrCnt ;
  61. movement["firstDirection"] = _firstMoveDirection ;
  62. movement["secondType"] = _secondMoveType ;
  63. movement["secondLyrCnt"] = _secondMoveLyrCnt ;
  64. movement["secondDirection"] = _secondMoveDirection ;
  65. root["BigPlatePercent"] = _bigPlatePercent ; //不移动盘子中大盘子百分比
  66. root["MidPlatePercent"] = _midPlatePercent ; //不移动盘子中中盘子百分比
  67. root["Lyr1Percent"] = _lyr1Percent ; //不移动盘子中有一层的盘子百分比
  68. root["Lyr2Percent"] = _lyr2Percent; //不移动盘子中有两层的盘子百分比
  69. root["Lyr3Percent"] = _lyr3Percent; //不移动盘子中有三层的盘子百分比
  70. root["MatchLyr1"] = _matchLyr1 ; //第一层可配百分比,第一层是点击层、第二层是可见层、第三层为隐藏层
  71. root["MatchLyr2"] = _matchLyr2 ; //第二层可配百分比
  72. root["MatchLyr3"] = _matchLyr3 ; //第三层可配百分比
  73. string jsontext ;
  74. root.printTo(jsontext) ;
  75. ofstream ofs( filename.c_str() ) ;
  76. if( ofs.good() == false ) return false ;
  77. ofs<<jsontext ;
  78. ofs.close() ;
  79. return true ;
  80. }
  81. string LevelExtInputData::levelInputFilename2ExtFilename( string levelInputFilename )
  82. {
  83. size_t dotpos = levelInputFilename.rfind('.') ;
  84. if( dotpos==string::npos ) return "" ;
  85. string res = levelInputFilename.substr(0, dotpos ) + "_ext.json";
  86. return res ;
  87. }