StarItem.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. const GameModule = require("../utils/GameModule");
  2. const DWTool = require("../utils/DWTool");
  3. const StarApi = require('../net/StarApi');
  4. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  5. const TapTapTool = require("../utils/TapTapTool");
  6. const ArtistManager = require('../utils/ArtistManager');
  7. const HomeApi = require('../net/HomeApi');
  8. // const ADVideo = require('../utils/ADVideo');
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. starIcon: cc.Sprite,
  13. starNameLabel: cc.Label,
  14. subTitleRichText: cc.RichText,
  15. subTitleRichText1: cc.RichText,
  16. starUseTitleRichText: cc.RichText,
  17. starUseBtn: cc.Button,
  18. starUserSprite: cc.Sprite,
  19. countLabel: cc.Label,
  20. countNode: cc.Node,
  21. starIconBlackBg: cc.Node,
  22. lightSpriteFrame: cc.SpriteFrame,
  23. graySpriteFrame: cc.SpriteFrame,
  24. lockedSpriteFrame: cc.SpriteFrame,
  25. /// 0说明是用金币 1 说明是用分享 2 看广告
  26. _adState: 0,
  27. //// 当前使用的广告状态
  28. _useAdState: 0,
  29. },
  30. // LIFE-CYCLE CALLBACKS:
  31. onLoad () {
  32. GameEvent.on('StarItem_updateUI', this, () => {
  33. this._buyGold = this._listViewAdapter.buyGold;
  34. this._starTypeCount = this._listViewAdapter.starTypeCount;
  35. this.updateData();
  36. });
  37. // GameEvent.on('StarItem_AD_updateState', this, (id) => {
  38. // if (id < 3) {
  39. // this._adState = id;
  40. // this._video = this.listAdapter._video;
  41. // /// 说明是关闭
  42. // } else if (id === 3) {
  43. // this.finishAction();
  44. // }
  45. // });
  46. // GameEvent.on(GameNotificationKey.AdStarStateUpdate, this, () => {
  47. // this.initStarAd();
  48. // })
  49. GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
  50. /// 说明是技能的关闭状态
  51. if (adState === 3 && callBack === 'buyStar' + this._starData.starId) {
  52. this.finishAction();
  53. }
  54. if (adState === 0 || adState === 1) {
  55. this.initStarAd();
  56. }
  57. });
  58. },
  59. start () {
  60. this._isAction = false;
  61. },
  62. onDestroy() {
  63. GameEvent.off('StarItem_updateUI', this);
  64. GameEvent.off(GameNotificationKey.AdUpdateStateNotification, this);
  65. },
  66. update (dt) {
  67. /// 如果是锁住的 那就什么都不做嘛 这里就是更新金币
  68. if (this._isRoomLocked == false && this._isStarLocked == false && this._isAction == false) {
  69. /// 如果是没反应的 说明金币不够
  70. // if (this.starUseBtn.interactable == false) {
  71. // if () {
  72. // this.setUpUseBtnBg(true);
  73. // }
  74. // }
  75. let isCanUse = TapTapTool.compare(GameModule.userInfo.gold, this._buyGold);
  76. let isAd = DWTool.checkIsOldUser() && Global._buyStarInfo.maxCount > Global._buyStarInfo.count && Global._buyStarInfo.cdTime <= 0;
  77. if (isAd && !this._isInitAd) {
  78. this._isInitAd = true;
  79. this.initStarAd();
  80. }
  81. if (isAd && !isCanUse) {
  82. if (this._adState != this._useAdState) {
  83. this._useAdState = this._adState;
  84. this.updateBuyStarUI();
  85. this.setUpUseBtnBg(true);
  86. }
  87. } else {
  88. if (this._useAdState != 0) {
  89. this._useAdState = 0;
  90. this.updateBuyStarUI();
  91. }
  92. let inter = this.starUseBtn.interactable;
  93. if (isCanUse != inter) {
  94. this.setUpUseBtnBg(!inter);
  95. }
  96. }
  97. }
  98. },
  99. initStarAd() {
  100. if (!CC_WECHATGAME) {
  101. return;
  102. }
  103. //// 说明有广告
  104. if (Global._adVideoState == 0) {
  105. this._adState = 2;
  106. } else if (Global._adVideoState === 1) {
  107. this._adState = 1;
  108. }
  109. },
  110. finishAction() {
  111. HomeApi.reportInformation(3, () => {
  112. Global._buyStarInfo.count += 1;
  113. /// 时间重置为5分钟
  114. Global._buyStarInfo.cdTime = 5 * 60 * 1000;
  115. /// 恢复到正常的状态
  116. this._adState = 0;
  117. this._useAdState = 0;
  118. this._isAcion = false;
  119. this.buyStarRequest();
  120. }, (code, msg) => {
  121. console.log(msg);
  122. })
  123. },
  124. setListViewAdapter (listViewAdapter) {
  125. this._listViewAdapter = listViewAdapter;
  126. this._buyGold = listViewAdapter.buyGold;
  127. this._starTypeCount = listViewAdapter.starTypeCount;
  128. },
  129. updateItem(userInfo, itemId) {
  130. this._itemId = itemId;
  131. this._starData = userInfo;
  132. // console.log(this._starData.itemId);
  133. this._starInfo = Global.BuildingManager.getStarInfo(this._starData.starId);
  134. this.updateData();
  135. this.starNameLabel.string = this._starInfo.name;
  136. let imageId = 50000 + this._starData.starId;
  137. ArtistManager.loadStarAvatarSpriteFrame(imageId, this.starIcon);
  138. },
  139. deleteItem() {
  140. this._listViewAdapter.removeItem(this);
  141. },
  142. /// 实时更新数据
  143. updateData() {
  144. this.setUpIsCanSign();
  145. this.undateStarCount();
  146. },
  147. undateStarCount() {
  148. if (this._starData.starCount > 0) {
  149. this.countNode.active = true;
  150. if (this._starData.starRoomCount <= this._starData.starCount) {
  151. this.countLabel.string = this._starData.starRoomCount + '/' + this._starData.starCount;
  152. } else {
  153. this.countLabel.string = this._starData.starCount + '/' + this._starData.starCount;
  154. }
  155. } else {
  156. this.countNode.active = false;
  157. }
  158. },
  159. /// 设置是否能签约
  160. setUpIsCanSign() {
  161. if (this._starData.type === 3 || this._starData.type === 4) {
  162. this._isRoomLocked = false;
  163. this._isStarLocked = false;
  164. this.subTitleRichText1.string = "";
  165. this.subTitleRichText.string = '';
  166. } else {
  167. let buyStarCount = GameModule.userInfo.buyStarCount;
  168. let roomId = this._starData.signRoomId;
  169. let isUnlocked = Global.BuildingManager.getRoomIsUnlocked(roomId);
  170. let roomName = Global.BuildingManager.getRoomName(roomId);
  171. this._isRoomLocked = false;
  172. this._isStarLocked = false;
  173. /// 需要的房间锁住的。
  174. if (isUnlocked == 0) {
  175. this.subTitleRichText.string = "<img src='star_false'/><color=#2E2E2E> 开启 " + roomName + "</c>";
  176. this._isRoomLocked = true;
  177. } else {
  178. this.subTitleRichText.string = "<img src='star_true'/><color=#2E2E2E> 开启 " + roomName + "</c>";
  179. }
  180. let needCount = this._starData.itemCount;
  181. if (needCount > 0) {
  182. /// 如要明星的个数为解锁条件
  183. if (this._starData.itemId == 101) {
  184. /// 如果明星数量足够
  185. let newBuyStarCount = buyStarCount <= needCount ? buyStarCount : needCount;
  186. if (buyStarCount >= needCount) {
  187. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newBuyStarCount + "/" + needCount + ")" + "</c>";
  188. } else {
  189. this._isStarLocked = true;
  190. this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newBuyStarCount + "/" + needCount + ")" + "</c>";
  191. }
  192. } else if (this._starData.itemId == 102) {
  193. let newStarTypeCount = this._starTypeCount <= needCount ? this._starTypeCount : needCount;
  194. if (this._starTypeCount >= needCount) {
  195. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newStarTypeCount + "/" + needCount + ")" + "</c>";
  196. } else {
  197. this._isStarLocked = true;
  198. this.subTitleRichText1.string = "<img src='star_fale'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newStarTypeCount + "/" + needCount + ")" + "</c>";
  199. }
  200. } else {
  201. let newAreaStarCount = this._starData.areaStarCount <= needCount ? this._starData.areaStarCount : needCount;
  202. if (this._starData.areaStarCount >= needCount) {
  203. this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newAreaStarCount + "/" + needCount + ")" + "</c>";
  204. } else {
  205. this._isStarLocked = true;
  206. this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newAreaStarCount + "/" + needCount + ")" + "</c>";
  207. }
  208. }
  209. } else {
  210. this.subTitleRichText1.string = "";
  211. }
  212. }
  213. ///说明满足明星和房间的条件 已经解锁 那么只需要判断金币进行预约了
  214. if (this._isStarLocked == false && this._isRoomLocked == false) {
  215. /// 如果金币满足
  216. this.starUseTitleRichText.node.active = true;
  217. if (this._useAdState === 0) {
  218. this.setUpUseBtnBg(TapTapTool.compare(GameModule.userInfo.gold, this._buyGold));
  219. } else {
  220. this.setUpUseBtnBg(true);
  221. }
  222. this.updateBuyStarUI();
  223. // this.starUseTitleRichText.string = `<color=#ffffff>签约</c><br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._buyGold)}</c>`;
  224. this.starIconBlackBg.active = false;
  225. } else {
  226. this.starUseTitleRichText.node.active = false;
  227. this.starUseBtn.interactable = false;
  228. this.starUserSprite.spriteFrame = this.lockedSpriteFrame;
  229. this.starIconBlackBg.active = true;
  230. }
  231. },
  232. /// 根据视频 分享 升级更新使用的UI
  233. updateBuyStarUI() {
  234. /// 如果是直接使用金币
  235. if (this._useAdState === 0) {
  236. this.starUseTitleRichText.string = `<color=#ffffff>签约</c><br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._buyGold)}</c>`;
  237. /// 如果是分享
  238. } else if (this._useAdState === 1) {
  239. this.starUseTitleRichText.string = '<color=#ffffff> 分享\n免费签约' + '</c>';
  240. } else {
  241. this.starUseTitleRichText.string = '<color=#ffffff> 看视频\n免费签约' + '</c>';
  242. }
  243. },
  244. /// 设置签约的button是否能点击
  245. setUpUseBtnBg(isActive) {
  246. this.starUseBtn.interactable = isActive;
  247. this.starUserSprite.spriteFrame = isActive ? this.lightSpriteFrame : this.graySpriteFrame;
  248. },
  249. // 签约明星
  250. signUseBtnAction() {
  251. GameModule.audioMng.playClickButton();
  252. this.starUseBtn.interactable = false;
  253. this._isAction = true;
  254. if (CC_WECHATGAME) {
  255. if (this._useAdState == 0) {
  256. /// 只能同时买一个
  257. if (this._listViewAdapter.isBuying) {
  258. return;
  259. }
  260. this._listViewAdapter.isBuying = true;
  261. this.buyStarRequest();
  262. /// 说明是分享
  263. } else if (this._useAdState == 1) {
  264. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type) => {
  265. if (type == WechatShareType.BuyStar) {
  266. this.finishAction();
  267. }
  268. });
  269. WeChat.shareAction(WechatShareType.BuyStar, () => {
  270. }, () => {
  271. this.starUseBtn.interactable = true;
  272. this._isAction = false;
  273. console.log('分享失败或取消');
  274. });
  275. /// 如果是看视频
  276. } else {
  277. this.starUseBtn.interactable = true;
  278. this._isAction = false;
  279. Global._adVideo.showVideo('buyStar' + this._starData.starId);
  280. }
  281. } else {
  282. this.buyStarRequest();
  283. }
  284. },
  285. buyStarRequest() {
  286. this.buyStar().then((respondData) => {
  287. this.starUseBtn.interactable = true;
  288. this._isAction = false;
  289. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this._buyGold);
  290. /// 本地购买的明星数量加1
  291. GameModule.userInfo.buyStarCount += 1;
  292. // this._starData.starRoomCount += 1;
  293. this._starData.starCount += 1;
  294. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': 2});
  295. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': 2});
  296. Global._buyStarGold = respondData.buyGold;
  297. this.undateStarCount();
  298. this._listViewAdapter.isBuying = false;
  299. GameEvent.fire(GameNotificationKey.StarEnterRoom, this._starData.starId, respondData.roomId);
  300. GameEvent.fire("Star_Buy_success", this._starInfo.areaType, respondData.buyGold, respondData.starTypeCount);
  301. GameEvent.fire('Star_Buy_success_Alert', this._starInfo.starId, this._starInfo.name);
  302. }).catch((error) => {
  303. this.starUseBtn.interactable = true;
  304. this._isAction = false;
  305. Global.commonAlert.showCommonErrorAlert("签约明星失败");
  306. console.error(error);
  307. });
  308. },
  309. buyStar() {
  310. return new Promise((resolve, reject) => {
  311. //// 购买明星
  312. StarApi.buyStar(this._starData.starId, (respondData) => {
  313. resolve(respondData);
  314. }, (code, msg) => {
  315. reject({code, msg});
  316. });
  317. });
  318. },
  319. });