DrawContent.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. const AlertManager = require('../utils/AlertManager');
  2. const DrawApi = require("../net/DrawApi");
  3. const DWTool = require("../utils/DWTool");
  4. const GameModule = require("../utils/GameModule");
  5. const WeChat = require('../net/WeChat');
  6. const {GameNotificationKey, WechatShareType, GameRedDot } = require('../utils/GameEnum');
  7. const TapTapTool = require("../utils/TapTapTool");
  8. const ADVideo = require('../utils/ADVideo');
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. moreRichText: cc.RichText,
  13. bestRichText: cc.RichText,
  14. normalRichText: cc.RichText,
  15. normalDrawButton: cc.Button,
  16. bestChanceButton: cc.Button,
  17. moreChanceButton: cc.Button,
  18. normalDrawButtonSprite: cc.Sprite,
  19. bestChanceButtonSprite: cc.Sprite,
  20. moreChanceButtonSprite: cc.Sprite,
  21. content: cc.Node,
  22. shareActionNode: cc.Node,
  23. drawMoneyLabel: cc.RichText,
  24. /// 1视频广告 0普通分享
  25. _adState: 0
  26. },
  27. // LIFE-CYCLE CALLBACKS:
  28. onLoad () {
  29. this.setUpUIIsShow(false);
  30. if (Global.isCheck) {
  31. this.shareActionNode.active = false;
  32. }
  33. this.setupNotification();
  34. this.getDrawState().then((respondData) => {
  35. this.setUpUIIsShow(true);
  36. this.respondData = respondData;
  37. this.setUpUI();
  38. this.initDrawAd();
  39. }).catch(({code, msg}) => {
  40. console.log(code, msg);
  41. });
  42. this.drawMoneyLabel.string = `<b><color=#fec808>¥${(Global.userData.hb / 100).toFixed(2)}</c></b>`;
  43. },
  44. onDestroy() {
  45. GameEvent.off(GameNotificationKey.GameShowNotificationKey, this);
  46. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  47. GameEvent.off(GameNotificationKey.AdUpdateStateNotification, this);
  48. },
  49. updatelData() {
  50. // cdTime 已经用了的CD时间 如果 如果为0或者大于和等于总时间 说明可以免费抽奖
  51. // cdTotalTime cd总时间 ,目前是60分钟
  52. this.getDrawState().then((respondData) => {
  53. this.respondData = respondData;
  54. this.updateUI();
  55. }).catch(({code, msg}) => {
  56. console.log(code, msg);
  57. });
  58. },
  59. start () {
  60. },
  61. setupNotification() {
  62. GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
  63. /// 说明是技能的关闭状态
  64. if (adState === 3 && callBack === 'draw') {
  65. this.shareActionCallback();
  66. }
  67. if (adState === 0 || adState === 1) {
  68. this.initDrawAd();
  69. }
  70. });
  71. GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, this.updatelData);
  72. },
  73. setUpUIIsShow(isShow) {
  74. this.normalDrawButton.node.active = isShow;
  75. this.bestChanceButton.node.active = isShow;
  76. this.moreChanceButton.node.active = isShow;
  77. },
  78. updateUI() {
  79. /// 抽奖冷却结束
  80. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  81. this.setUpNormalButton();
  82. } else {
  83. this.setUpUseBtnBg(false, 1);
  84. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  85. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  86. }
  87. },
  88. setUpUI() {
  89. /// 抽奖冷却结束
  90. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  91. this.setUpNormalButton();
  92. } else {
  93. this.setUpUseBtnBg(false, 1);
  94. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  95. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  96. this.schedule(this.updateCD, 1);
  97. }
  98. this.moreRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond2}</c></b>`;
  99. this.bestRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond3}</c></b>`;
  100. let hasDiamod = GameModule.userInfo.diamond;
  101. if (hasDiamod < this.respondData.diamond2) {
  102. this.setUpUseBtnBg(false, 2);
  103. }
  104. if (hasDiamod < this.respondData.diamond3) {
  105. this.setUpUseBtnBg(false, 3);
  106. }
  107. },
  108. setUpNormalButton() {
  109. this.normalDrawButton.interactable = true;
  110. if (this._adState === 0) {
  111. this.normalRichText.string = '<b><color=#ffffff>分享\n免费获得</c></b>';
  112. } else if (this._adState === 1) {
  113. this.normalRichText.string = '<b><color=#ffffff>看视频\n免费获得</c></b>';
  114. }
  115. },
  116. /// 网络请求
  117. getDrawState() {
  118. return new Promise((resolve, reject) => {
  119. DrawApi.getLotteryInfo((respondData) => {
  120. resolve(respondData);
  121. }, (code, msg) => {
  122. reject({code, msg});
  123. });
  124. })
  125. },
  126. startDrawRequest(typeId, diamond = 0) {
  127. return new Promise((resolve, reject) => {
  128. ///开始抽奖
  129. DrawApi.startNewLottery(typeId, diamond, (respondData) => {
  130. resolve(respondData);
  131. }, (code, msg) => {
  132. reject({code, msg});
  133. });
  134. })
  135. },
  136. startDraw(typeId, completion) {
  137. this.startDrawRequest(typeId, 0).then((respondData) => {
  138. /// 把抽奖状态 以及抽奖成功之后的数据加上去
  139. this.respondData.drawSuccessData = respondData;
  140. // this.respondData.propId = respondData.propId;
  141. // this.respondData.propName = respondData.propName;
  142. // this.respondData.roomId = respondData.roomId;
  143. this.closeNodeAction();
  144. completion(true);
  145. AlertManager.showDrawScrollAlert(this.respondData, typeId);
  146. }).catch(({code, msg}) => {
  147. console.log(code, msg);
  148. completion(false);
  149. this.normalDrawButton.interactable = true;
  150. Global.commonAlert.showCommonErrorAlert(msg);
  151. });
  152. },
  153. updateCD() {
  154. this.respondData.cdTime += 1000;
  155. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  156. this.setUpNormalButton();
  157. this.setUpUseBtnBg(true, 1);
  158. this.unschedule(this.updateCD, this);
  159. }
  160. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  161. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  162. },
  163. /// 看广告抽奖
  164. adNodeAction() {
  165. GameModule.audioMng.playClickButton();
  166. if (CC_WECHATGAME) {
  167. if (this._adState === 0) {
  168. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type) => {
  169. if (type != WechatShareType.DrawLottery) { return; }
  170. this.shareActionCallback();
  171. });
  172. this.normalDrawButton.interactable = false;
  173. WeChat.shareAction(WechatShareType.DrawLottery, () => {
  174. }, () => {
  175. this.normalDrawButton.interactable = true;
  176. console.log('分享失败或取消');
  177. });
  178. } else {
  179. Global._adVideo.showVideo('draw');
  180. }
  181. }
  182. },
  183. shareActionCallback() {
  184. this.startDraw(1, () => {
  185. this.normalDrawButton.interactable = true;
  186. TapTapTool.removeRedDot(GameRedDot.draw);
  187. });
  188. if (this._adState === 0) {
  189. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  190. }
  191. },
  192. ///更多砖石抽奖
  193. moreChanceNodeAction() {
  194. GameModule.audioMng.playClickButton();
  195. this.moreChanceButton.interactable = false;
  196. this.startDraw(2, (isSuccess) => {
  197. this.moreChanceButton.interactable = true;
  198. if (isSuccess) {
  199. GameModule.userInfo.diamond -= this.respondData.diamond2;
  200. }
  201. });
  202. },
  203. ///最大概率抽奖
  204. bestChanceNodeAction () {
  205. GameModule.audioMng.playClickButton();
  206. this.bestChanceButton.interactable = false;
  207. this.startDraw(3, (isSuccess) => {
  208. this.bestChanceButton.interactable = true;
  209. if (isSuccess) {
  210. GameModule.userInfo.diamond -= this.respondData.diamond3;
  211. }
  212. });
  213. },
  214. closeNodeAction() {
  215. // let finish = cc.callFunc(() => {
  216. // this.node.destroy();
  217. // }, this);
  218. // let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
  219. // this.content.runAction(sequence);
  220. this.node.destroy();
  221. GameModule.audioMng.playClickButton();
  222. },
  223. /// 设置button是否能点击 type 123
  224. setUpUseBtnBg(isActive, type) {
  225. let path = isActive ? './textures/draw/draw_green_btn_bg' : './textures/draw/draw_gray_btn_bg';
  226. DWTool.loadResSpriteFrame(path)
  227. .then((spriteFrame) => {
  228. if (type == 1) {
  229. this.normalDrawButtonSprite.spriteFrame = spriteFrame;
  230. this.normalDrawButton.interactable = isActive;
  231. } else if (type == 2) {
  232. this.moreChanceButtonSprite.spriteFrame = spriteFrame;
  233. this.moreChanceButton.interactable = isActive;
  234. } else {
  235. this.bestChanceButtonSprite.spriteFrame = spriteFrame;
  236. this.bestChanceButton.interactable = isActive;
  237. }
  238. });
  239. },
  240. showDrawRedAction() {
  241. AlertManager.showDrawRedRecordAlert();
  242. },
  243. initDrawAd() {
  244. if (!CC_WECHATGAME) {
  245. return;
  246. }
  247. //// 说明有广告
  248. if (Global._adVideoState == 0) {
  249. this._adState = 1;
  250. } else if (Global._adVideoState === 1) {
  251. this._adState = 0;
  252. }
  253. if (this.normalDrawButton.interactable) {
  254. this.setUpNormalButton();
  255. }
  256. },
  257. // update (dt) {},
  258. });