FillGlobalConfig.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // FillGlobalConfig.cpp
  3. // Test
  4. //
  5. // Created by 高慕白 on 2024/12/3.
  6. //
  7. #include "FillGlobalConfig.hpp"
  8. using namespace ArduinoJson;
  9. #include <fstream>
  10. using std::ifstream;
  11. #include <boost/algorithm/string.hpp>
  12. FillGlobalConfig* FillGlobalConfig::_s_instance = nullptr ;
  13. bool FillGlobalConfig::init(string filename)
  14. {
  15. _plateContours.clear();
  16. _jewelContours.clear() ;
  17. ifstream ifs( filename.c_str() );
  18. if( ifs.good()==false ) return false ;
  19. DynamicJsonBuffer buffer ;
  20. JsonObject& root = buffer.parse(ifs) ;
  21. string platedir = root["plate_contour_dir"].as<char*>() ;
  22. string jeweldir = root["jewel_contour_dir"].as<char*>() ;
  23. JsonArray& parr =root["plate_names"].as<JsonArray>() ;
  24. JsonArray& jarr =root["jewel_names"].as<JsonArray>() ;
  25. _plateContours.resize(parr.size());
  26. _jewelContours.resize(jarr.size());
  27. for(int i=0;i<parr.size();++i ) {
  28. string name1 = parr[i].as<char*>() ;
  29. string fullname1 = platedir + name1 ;
  30. _plateContours[i]._name = name1 ;
  31. _plateContours[i]._contour.readFromJsonFile(fullname1) ;
  32. }
  33. for(int i=0;i<jarr.size();++i ) {
  34. string name1 = jarr[i].as<char*>() ;
  35. string fullname1 = jeweldir + name1 ;
  36. _jewelContours[i]._name = name1 ;
  37. _jewelContours[i]._contour.readFromJsonFile(fullname1) ;
  38. }
  39. return true;
  40. }
  41. FillGlobalConfig* FillGlobalConfig::getInstance()
  42. {
  43. if( _s_instance == nullptr ) {
  44. _s_instance = new FillGlobalConfig ;
  45. _s_instance->init("config.json") ;
  46. _s_instance->initJewelItems("sg_jewel_items.csv") ;
  47. _s_instance->initLevelDatas("sg_level_data.csv") ;
  48. _s_instance->initPlateItems("sg_plate_items.csv") ;
  49. }
  50. return _s_instance ;
  51. }
  52. ContourData* FillGlobalConfig::getContourDataByPngName(string& pngname)
  53. {
  54. for(auto it = _jewelContours.begin(); it!=_jewelContours.end();++it ) {
  55. if( it->_name.find(pngname) != string::npos ) {
  56. return &(it->_contour) ;
  57. }
  58. }
  59. return nullptr ;
  60. }
  61. bool FillGlobalConfig::initJewelItems( string filename )
  62. {
  63. _mapJewelItems.clear() ;
  64. ifstream ifs( filename.c_str() ) ;
  65. if( ifs.good()==false ) return false ;
  66. string line ;
  67. //跳过第一行
  68. std::getline(ifs, line) ;
  69. vector<string> tokens ;
  70. while( std::getline(ifs, line) ) {
  71. if( line.length() > 2 ) {
  72. tokens.clear() ;
  73. boost::split(tokens, line, boost::is_any_of(","));
  74. if( tokens.size()>=13 ) {
  75. JewelItem ji ;
  76. ji._id = atof(tokens[0].c_str()) ;
  77. ji._category = tokens[1] ;
  78. ji._jewelName = tokens[2] ;
  79. ji._code1 = tokens[3] ;
  80. ji._code2 = tokens[4] ;
  81. ji._size = (tokens[5].compare("SM")==0)?FILLGLOBALCONFIG_JEWELSIZE_SM:((tokens[5].compare("MD")==0)?FILLGLOBALCONFIG_JEWELSIZE_MD:FILLGLOBALCONFIG_JEWELSIZE_LG) ;
  82. ji._scale = atof( tokens[6].c_str() ) ;
  83. ji._pngName = tokens[7] ;
  84. ji._yzName = tokens[8] ;
  85. ji._contourName = tokens[9] ;
  86. ji._bbWidth = atof( tokens[10].c_str() ) ;
  87. ji._bbHeight = atof( tokens[11].c_str() ) ;
  88. ji._etc = tokens[12] ;
  89. _mapJewelItems[ji._id] = ji ;
  90. }
  91. }
  92. }
  93. return true ;
  94. }
  95. bool FillGlobalConfig::initLevelDatas( string filename )
  96. {
  97. _mapLevelDatas.clear() ;
  98. ifstream ifs( filename.c_str() ) ;
  99. if( ifs.good()==false ) return false ;
  100. string line ;
  101. //跳过第一行
  102. std::getline(ifs, line) ;
  103. LevelData tempLevelData ;
  104. tempLevelData._id = -1 ;//初始化
  105. vector<string> tokens ;
  106. while( std::getline(ifs, line) ) {
  107. bool isDataLine = false;
  108. if( line.length() > 2 ) {
  109. tokens.clear() ;
  110. boost::split(tokens, line, boost::is_any_of(","));
  111. if( tokens.size()>=6 ) {
  112. isDataLine = true ;
  113. int lid = atof(tokens[0].c_str()) ;
  114. int subid = atof(tokens[1].c_str()) ;
  115. int difficulty = atof(tokens[2].c_str()) ;
  116. int jewId = atof(tokens[3].c_str()) ;
  117. string jewSz = tokens[4]; //ignored
  118. int grpcnt = atof( tokens[5].c_str() ) ;
  119. if( tempLevelData._id == -1 ) {
  120. tempLevelData._id = lid ;
  121. tempLevelData._subId = subid ;
  122. tempLevelData._difficulty = difficulty ;
  123. }
  124. tempLevelData._jewelIds.push_back(jewId) ;
  125. tempLevelData._cnts.push_back(grpcnt*3) ;
  126. }
  127. }
  128. if( isDataLine==false )
  129. {
  130. //出现空行, 记录临时关卡
  131. if( tempLevelData._id>0 ) {
  132. _mapLevelDatas[tempLevelData._id].push_back(tempLevelData) ;
  133. }
  134. //清空临时关卡数据
  135. tempLevelData._id = -1 ;
  136. tempLevelData._subId = -1 ;
  137. tempLevelData._difficulty = -1;
  138. tempLevelData._cnts.clear() ;
  139. tempLevelData._jewelIds.clear() ;
  140. }
  141. }
  142. //最后一个临时对象是否是有效关卡数据
  143. if( tempLevelData._id>0 ) {
  144. _mapLevelDatas[tempLevelData._id].push_back(tempLevelData) ;
  145. tempLevelData._id = -1 ;
  146. tempLevelData._cnts.clear() ;
  147. tempLevelData._jewelIds.clear() ;
  148. }
  149. return true ;
  150. }
  151. bool FillGlobalConfig::initPlateItems( string filename )
  152. {
  153. _mapPlateItems.clear() ;
  154. _mapPlateIdArray.clear() ;
  155. ifstream ifs( filename.c_str() ) ;
  156. if( ifs.good()==false ) return false ;
  157. string line ;
  158. //跳过第一行
  159. std::getline(ifs, line) ;
  160. vector<string> tokens ;
  161. while( std::getline(ifs, line) ) {
  162. if( line.length() > 2 ) {
  163. tokens.clear() ;
  164. boost::split(tokens, line, boost::is_any_of(","));
  165. if( tokens.size()>=13 ) {
  166. PlateItem pi ;
  167. pi._id = atof(tokens[0].c_str()) ;
  168. pi._name = tokens[1] ;
  169. int sz = 0 ;
  170. string szStr = tokens[2] ;
  171. if( szStr.compare("SM")==0 ) sz = FILLGLOBALCONFIG_PLATESIZE_SM ;
  172. else if( szStr.compare("MD")==0 ) sz = FILLGLOBALCONFIG_PLATESIZE_MD;
  173. else if( szStr.compare("LG")==0 ) sz = FILLGLOBALCONFIG_PLATESIZE_LG;
  174. else continue; //无效盘子跳过
  175. pi._size = sz ;
  176. pi._pngName = tokens[3] ;
  177. pi._contourName = tokens[4] ;
  178. pi._blx = atof( tokens[5].c_str() ) ;//bottom left
  179. pi._bly = atof( tokens[6].c_str() ) ;
  180. pi._trx = atof( tokens[7].c_str() ) ;//top right
  181. pi._try = atof( tokens[8].c_str() ) ;
  182. pi._bigJewCap = atof( tokens[9].c_str() ) ;
  183. pi._bbwid = atof( tokens[10].c_str()) ;
  184. pi._bbhei = atof( tokens[11].c_str()) ;
  185. pi._etc = tokens[12] ;
  186. _mapPlateItems[pi._id] = pi ;
  187. _mapPlateIdArray[sz].push_back(pi._id) ;
  188. }
  189. }
  190. }
  191. return true ;
  192. }