IAPCtlShopUI.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // IAPCtlShopUI.cpp
  3. // TileManor
  4. //
  5. // Created by 徐炼新 on 2024/1/16.
  6. //
  7. #include "IAPCtlShopUI.hpp"
  8. #include "IAPCtlShopItem.hpp"
  9. #include "RUUtils.h"
  10. #include "RUPlatform.h"
  11. #include "IAPCtlShop.hpp"
  12. #include "IAPUserData.hpp"
  13. #include <regex>
  14. IAPCtlShopUI* IAPCtlShopUI::_instance = nullptr;
  15. IAPCtlShopUI* IAPCtlShopUI::getInstance() {
  16. if (_instance == nullptr) {
  17. _instance = new IAPCtlShopUI();
  18. _instance->autorelease();
  19. _instance->retain();
  20. }
  21. return _instance;
  22. }
  23. IAPCtlShopUI::IAPCtlShopUI() {
  24. }
  25. IAPCtlShopUI::~IAPCtlShopUI() {
  26. }
  27. bool IAPCtlShopUI::isShowing() {
  28. return _bUIShowing;
  29. }
  30. void IAPCtlShopUI::clear() {
  31. _goodsInfo.clear();
  32. for (auto& gi : _goodsItems) {
  33. gi->release();
  34. }
  35. _goodsItems.clear();
  36. _allSizes.clear();
  37. _cb = nullptr;
  38. }
  39. bool IAPCtlShopUI::create(Node* pNode, IAPConf* cfg, int placementId) {
  40. redutils::Platform::getInstance()->reportLog("open iap");
  41. _cfg = cfg;
  42. _bUIShowing = true;
  43. _placementId = placementId;
  44. clear();
  45. _layer = redutils::RUReboltLayer::createReboltLayer("shop_interface.redream");
  46. pNode->addChild(_layer, 3);
  47. _layer->registerOnNotify([this](const redutils::ReboltNotifyData& data){
  48. _onNotifyDevelopment(data);
  49. });
  50. _layer->runBehaviacWhitFunName("初始化");
  51. // _igCoin->doOwnInit();
  52. if (_goodsInfo.size() == 0) {
  53. IAPConf::getInstance()->getAllGoods(_goodsInfo);
  54. }
  55. // 构建 list 区域
  56. constructShopItem(false);
  57. cocos2d::Size size = _ndScrollArea->getContentSize();
  58. _tableView = redutils::RUTableView::create(&_tableData, cocos2d::Size(size.width, size.height));
  59. _tableView->setCascadeOpacityEnabled(true);
  60. _tableView->setCascadeColorEnabled(true);
  61. _tableView->setDirection(ScrollViewSmooth::Direction::VERTICAL);
  62. _tableView->setVerticalFillOrder(TableViewSmooth::VerticalFillOrder::TOP_DOWN, false);
  63. _tableView->setDelegate(&_tableData);
  64. _tableView->setBounceable(true);
  65. // _tableView->setOutSightClean(false);
  66. _ndScrollArea->addChild(_tableView);
  67. return true;
  68. }
  69. bool IAPCtlShopUI::create(Node* pNode, IAPConf* cfg, iap::ShopRequirement& requirement,int placementId) {
  70. redutils::Platform::getInstance()->reportLog("open iap");
  71. _cfg = cfg;
  72. _bUIShowing = true;
  73. _placementId = placementId;
  74. clear();
  75. _layer = redutils::RUReboltLayer::createReboltLayer("shop_interface.redream");
  76. pNode->addChild(_layer, 3);
  77. _layer->registerOnNotify([this](const redutils::ReboltNotifyData& data){
  78. _onNotifyDevelopment(data);
  79. });
  80. _layer->runBehaviacWhitFunName("初始化");
  81. // _igCoin->doOwnInit();
  82. if (_goodsInfo.size() == 0) {
  83. IAPConf::getInstance()->getAllGoods(_goodsInfo);
  84. }
  85. // 过滤掉不符合的金币
  86. filterGoods(requirement);
  87. // 构建 list 区域
  88. constructShopItem(false);
  89. cocos2d::Size size = _ndScrollArea->getContentSize();
  90. _tableView = redutils::RUTableView::create(&_tableData, cocos2d::Size(size.width, size.height));
  91. _tableView->setCascadeOpacityEnabled(true);
  92. _tableView->setCascadeColorEnabled(true);
  93. _tableView->setDirection(ScrollViewSmooth::Direction::VERTICAL);
  94. _tableView->setVerticalFillOrder(TableViewSmooth::VerticalFillOrder::TOP_DOWN, false);
  95. _tableView->setDelegate(&_tableData);
  96. _tableView->setBounceable(true);
  97. // _tableView->setOutSightClean(false);
  98. _ndScrollArea->addChild(_tableView);
  99. return true;
  100. }
  101. void IAPCtlShopUI::_onNotifyDevelopment(const redutils::ReboltNotifyData& data) {
  102. if (data.notify == "绑定显示区域") {
  103. _ndScrollArea = data.outNode;
  104. } else if (data.notify == "绑定coin节点") {
  105. // _igCoin = new MapIGCoin(data.reboltRedManager);
  106. } else if (data.notify == "退出") {
  107. auto rm = RemoveSelf::create();
  108. auto cf = CallFunc::create([=]() {
  109. _layer->removeFromParent();
  110. _layer = nullptr;
  111. _bUIShowing = false;
  112. redutils::Platform::getInstance()->reportLog("iap closed");
  113. });
  114. _layer->runAction(Sequence::create(rm, cf, NULL));
  115. if (_cb) {
  116. _cb();
  117. }
  118. _cb = nullptr;
  119. }
  120. }
  121. void IAPCtlShopUI::constructShopItem(bool bShowAll) {
  122. int index = 0;
  123. _tableData.setCascadeOpacity(true);
  124. _tableData.clear();
  125. cocos2d::Size size = _ndScrollArea->getContentSize();
  126. auto delayIdx = 0;
  127. cocos2d::Vec2 offset(size.width, 0);
  128. float dur = 0.4;
  129. int height = 0;
  130. // 加入一个表头
  131. {
  132. Node* nd = Node::create();
  133. _tableData.insertCell(index++, nd);
  134. int h = size.height*0.08;
  135. _allSizes.push_back(cocos2d::Size(size.width, h));
  136. height += h;
  137. }
  138. // 重新获取
  139. if(bShowAll)IAPConf::getInstance()->getAllGoods(_goodsInfo);
  140. // 根据用户类型进行礼包排序
  141. switch (iap::IAPUserData::getInstance()->getUserBuyType()) {
  142. case iap::UserBuyType::NoShopping:
  143. sortGoods(true);
  144. break;
  145. case iap::UserBuyType::LittleShopping:
  146. sortGoods(true);
  147. break;
  148. case iap::UserBuyType::NormalShopping:
  149. sortGoods(true);
  150. break;
  151. case iap::UserBuyType::LotShopping:
  152. sortGoods(false);
  153. break;
  154. default:
  155. break;
  156. }
  157. // 当前有效的区域
  158. int bigPackCount,SmallPackCount; // 需要显示的礼包
  159. auto iapCtlShop = iap::IAPCtlShop::createWith();
  160. int leval = iapCtlShop->getDeviceLevel();
  161. size_t placementCount = iapCtlShop->getPlacementCount();
  162. switch(leval){
  163. case 1:
  164. // 低等级
  165. bigPackCount = 2;
  166. SmallPackCount = 1;
  167. break;
  168. case 2:
  169. // 中等级
  170. break;
  171. case 3:
  172. if(placementCount >= 2){
  173. bigPackCount = 3;
  174. SmallPackCount = 0;
  175. }else{
  176. bigPackCount = 2;
  177. SmallPackCount = 2;
  178. }
  179. // 高等级
  180. break;
  181. default:
  182. break;
  183. }
  184. // 显示商品
  185. for (const auto& gi : _goodsInfo) {
  186. // 判断是否显示
  187. bool isShow = false;
  188. if (bShowAll) {
  189. isShow = true;
  190. }else{
  191. if(gi.type == "panel" && bigPackCount > 0){
  192. isShow = true;
  193. bigPackCount--;
  194. }
  195. if(gi.type == "bar" && SmallPackCount > 0){
  196. isShow = true;
  197. SmallPackCount--;
  198. }
  199. }
  200. // 商品显示
  201. if (isShow) {
  202. bool bNeedAnim = height <= size.height;
  203. Node* nd = Node::create();
  204. nd->setCascadeOpacityEnabled(true);
  205. nd->setCascadeColorEnabled(true);
  206. auto si = IAPCtlShopItem::create(gi, _placementId);
  207. auto sz = si->getSize();
  208. cocos2d::Vec2 pos(size.width/2.0, sz.height/2.0);
  209. nd->addChild(si);
  210. si->setPosition(bNeedAnim ? pos+offset : pos);
  211. _goodsItems.push_back(si);
  212. _tableData.insertCell(index++, nd);
  213. _allSizes.push_back(sz);
  214. height += sz.height;
  215. if (bNeedAnim) {
  216. // 加入动画
  217. auto d = DelayTime::create(delayIdx*dur/8.0);
  218. auto m = EaseBackOut::create(MoveBy::create(dur, cocos2d::Vec2(-offset.x, offset.y)));
  219. si->runAction(Sequence::create(d, m, NULL));
  220. delayIdx ++;
  221. }
  222. }
  223. }
  224. // 添加展示所有按钮
  225. if (!bShowAll) {
  226. // 加入show All btn, 这个是一定会有动画的
  227. auto sz = cocos2d::Size(584, 130);
  228. Node* nd = Node::create();
  229. nd->setCascadeOpacityEnabled(true);
  230. nd->setCascadeColorEnabled(true);
  231. _btnShowAll = redutils::RUReboltLayer::createReboltLayer("shop_more_btn.redream");
  232. nd->addChild(_btnShowAll);
  233. _btnShowAll->setPosition(Vec2(size.width/2.0, sz.height/2.0) + offset);
  234. _tableData.insertCell(index++, nd);
  235. _btnShowAll->registerOnNotify([this](const redutils::ReboltNotifyData& data){
  236. if (data.notify == "点击") {
  237. auto rm = RemoveSelf::create();
  238. auto cf = CallFunc::create([=](){
  239. _btnShowAll = NULL;
  240. _tableData.clear();
  241. _goodsItems.clear();
  242. _allSizes.clear();
  243. constructShopItem(true);
  244. _tableView->reloadData(true);
  245. });
  246. _btnShowAll->runAction(Sequence::create(rm, cf, NULL));
  247. }
  248. });
  249. redutils::iterateNode(nd, [](Node* nd){
  250. nd->setCascadeOpacityEnabled(true);
  251. nd->setCascadeColorEnabled(true);
  252. });
  253. _btnShowAll->runBehaviacWhitFunName("初始化");
  254. _allSizes.push_back(sz);
  255. // 加入动画
  256. auto d = DelayTime::create(delayIdx*dur/8.0);
  257. auto m = EaseBackOut::create(MoveBy::create(dur, cocos2d::Vec2(-offset.x, offset.y)));
  258. _btnShowAll->runAction(Sequence::create(d, m, NULL));
  259. delayIdx ++;
  260. } else {
  261. // 底部添加一个额外节点(一个是底部的广告,还有就是美观一些)
  262. _tableData.insertCell(index++, Node::create());
  263. _allSizes.push_back(cocos2d::Size(584, 150));
  264. }
  265. _tableData._numberOfCellsInTableView = [=](cocos2d::extension::TableViewSmooth*)->ssize_t{
  266. return _allSizes.size();
  267. };
  268. _tableData._tableCellSizeForIndex = [=](cocos2d::extension::TableViewSmooth*, ssize_t idx)->Size{
  269. if (idx < _allSizes.size()) {
  270. return _allSizes[idx];
  271. } else {
  272. return Size(0,0);
  273. }
  274. };
  275. // 防止拉动的时候才创建
  276. _layer->scheduleOnce([=](float){
  277. for (int i=0; i<_allSizes.size(); i++) {
  278. _tableView->updateCellAtIndex(i);
  279. }
  280. }, 0.05, "SCH_Update_Cells");
  281. }
  282. void IAPCtlShopUI::filterGoods(iap::ShopRequirement &requirement){
  283. std::map<int, bool> m;
  284. int cnt = 0;
  285. for(const auto& goodsInfo : _goodsInfo){
  286. for(const auto area : goodsInfo.areas){
  287. bool flag = false;
  288. for(int i = 0;i < area.size(); i++){
  289. if(area[i].name != "coin")continue;
  290. if(std::stoi(area[i].count) >= requirement.coinsMin){
  291. m[cnt] = true;
  292. flag = true;
  293. break;
  294. }else{
  295. m[cnt] = false;
  296. flag = true;
  297. break;
  298. }
  299. }
  300. if(flag)break;
  301. }
  302. cnt++;
  303. }
  304. std::vector<GoodsInfo> newGoodsInfo;
  305. // 添加符合条件的礼包
  306. for(int i = 0;i < _goodsInfo.size(); i++){
  307. if(m[i]){
  308. newGoodsInfo.push_back(_goodsInfo[i]);
  309. }
  310. }
  311. // 添加不符合条件的礼包(不清楚是不是需要显示,暂放到列表末尾)
  312. // for(int i = 0;i < _goodsInfo.size(); i++){
  313. // if(!m[i]){
  314. // newGoodsInfo.push_back(_goodsInfo[i]);
  315. // }
  316. // }
  317. _goodsInfo = newGoodsInfo;
  318. }
  319. void IAPCtlShopUI::sortGoods(bool flag){
  320. sort(_goodsInfo.begin(), _goodsInfo.end(), [flag](GoodsInfo a, GoodsInfo b){
  321. if(a.type != b.type){
  322. if(a.type == "panel"){
  323. return true;
  324. }else if(b.type == "panel"){
  325. return false;
  326. }
  327. }
  328. float val_a,val_b;
  329. std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字
  330. std::smatch match;
  331. if (std::regex_search(a.cost, match, re)) {
  332. val_a = stof(match[0]); // 提取价格
  333. } else {
  334. log("IAPUserData::getUserBuyType : 转换失败");
  335. }
  336. if (std::regex_search(b.cost, match, re)) {
  337. val_b = stof(match[0]); // 提取价格
  338. } else {
  339. log("IAPUserData::getUserBuyType : 转换失败");
  340. }
  341. if(flag) return val_a < val_b;
  342. else return val_a > val_b;
  343. });
  344. }
  345. void IAPCtlShopUI::quit() {
  346. if (_layer) {
  347. _layer->runBehaviacWhitFunName("退出");
  348. }
  349. }