DrawContent.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. Global.commonAlert.showCommonErrorAlert(msg);
  150. });
  151. },
  152. updateCD() {
  153. this.respondData.cdTime += 1000;
  154. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  155. this.setUpNormalButton();
  156. this.setUpUseBtnBg(true, 1);
  157. this.unschedule(this.updateCD, this);
  158. } else {
  159. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  160. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  161. }
  162. },
  163. /// 看广告抽奖
  164. adNodeAction() {
  165. GameModule.audioMng.playClickButton();
  166. if (CC_WECHATGAME) {
  167. if (this._adState === 0) {
  168. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type, isOk) => {
  169. if (type != WechatShareType.DrawLottery) { return; }
  170. if (isOk) {
  171. this.shareActionCallback();
  172. } else {
  173. this.normalDrawButton.interactable = true;
  174. }
  175. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  176. });
  177. this.normalDrawButton.interactable = false;
  178. WeChat.shareAction(WechatShareType.DrawLottery, () => {
  179. }, () => {
  180. this.normalDrawButton.interactable = true;
  181. console.log('分享失败或取消');
  182. });
  183. } else {
  184. Global._adVideo.showVideo('draw');
  185. }
  186. }
  187. },
  188. shareActionCallback() {
  189. this.normalDrawButton.interactable = false;
  190. this.startDraw(1, (isSuccess) => {
  191. if (isSuccess) {
  192. TapTapTool.removeRedDot(GameRedDot.draw);
  193. } else {
  194. this.normalDrawButton.interactable = true;
  195. }
  196. });
  197. if (this._adState === 0) {
  198. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  199. }
  200. },
  201. ///更多砖石抽奖
  202. moreChanceNodeAction() {
  203. GameModule.audioMng.playClickButton();
  204. this.moreChanceButton.interactable = false;
  205. this.startDraw(2, (isSuccess) => {
  206. if (isSuccess) {
  207. GameModule.userInfo.diamond -= this.respondData.diamond2;
  208. } else {
  209. this.moreChanceButton.interactable = true;
  210. }
  211. });
  212. },
  213. ///最大概率抽奖
  214. bestChanceNodeAction () {
  215. GameModule.audioMng.playClickButton();
  216. this.bestChanceButton.interactable = false;
  217. this.startDraw(3, (isSuccess) => {
  218. if (isSuccess) {
  219. GameModule.userInfo.diamond -= this.respondData.diamond3;
  220. } else {
  221. this.bestChanceButton.interactable = true;
  222. }
  223. });
  224. },
  225. closeNodeAction() {
  226. // let finish = cc.callFunc(() => {
  227. // this.node.destroy();
  228. // }, this);
  229. // let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
  230. // this.content.runAction(sequence);
  231. this.node.destroy();
  232. GameModule.audioMng.playClickButton();
  233. },
  234. /// 设置button是否能点击 type 123
  235. setUpUseBtnBg(isActive, type) {
  236. let path = isActive ? './textures/draw/draw_green_btn_bg' : './textures/draw/draw_gray_btn_bg';
  237. DWTool.loadResSpriteFrame(path)
  238. .then((spriteFrame) => {
  239. if (type == 1) {
  240. this.normalDrawButtonSprite.spriteFrame = spriteFrame;
  241. this.normalDrawButton.interactable = isActive;
  242. } else if (type == 2) {
  243. this.moreChanceButtonSprite.spriteFrame = spriteFrame;
  244. this.moreChanceButton.interactable = isActive;
  245. } else {
  246. this.bestChanceButtonSprite.spriteFrame = spriteFrame;
  247. this.bestChanceButton.interactable = isActive;
  248. }
  249. });
  250. },
  251. showDrawRedAction() {
  252. AlertManager.showDrawRedRecordAlert();
  253. },
  254. initDrawAd() {
  255. if (!CC_WECHATGAME) {
  256. return;
  257. }
  258. //// 说明有广告
  259. if (Global._adVideoState == 0) {
  260. this._adState = 1;
  261. } else if (Global._adVideoState === 1) {
  262. this._adState = 0;
  263. }
  264. /// 如果时间到了 那么更新显示的UI
  265. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  266. this.setUpNormalButton();
  267. }
  268. },
  269. // update (dt) {},
  270. });