DrawContent.js 11 KB

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