StarItem.js 14 KB

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