DrawContent.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. },
  24. // LIFE-CYCLE CALLBACKS:
  25. onLoad () {
  26. this.setUpUIIsShow(false);
  27. if (GameGlobal.isCheck) {
  28. this.shareActionNode.active = false;
  29. }
  30. this.getDrawState().then((respondData) => {
  31. this.setUpUIIsShow(true);
  32. this.respondData = respondData;
  33. this.setUpUI();
  34. }).catch(({code, msg}) => {
  35. console.log(code, msg);
  36. });
  37. GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, this.updatelData);
  38. },
  39. onDestroy() {
  40. GameEvent.off(GameNotificationKey.GameShowNotificationKey, this);
  41. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  42. },
  43. updatelData() {
  44. // cdTime 已经用了的CD时间 如果 如果为0或者大于和等于总时间 说明可以免费抽奖
  45. // cdTotalTime cd总时间 ,目前是60分钟
  46. this.getDrawState().then((respondData) => {
  47. this.respondData = respondData;
  48. this.updateUI();
  49. }).catch(({code, msg}) => {
  50. console.log(code, msg);
  51. });
  52. },
  53. start () {
  54. },
  55. setUpUIIsShow(isShow) {
  56. this.normalDrawButton.node.active = isShow;
  57. this.bestChanceButton.node.active = isShow;
  58. this.moreChanceButton.node.active = isShow;
  59. },
  60. updateUI() {
  61. /// 抽奖冷却结束
  62. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  63. this.setUpNormalButton();
  64. } else {
  65. this.setUpUseBtnBg(false, 1);
  66. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  67. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  68. }
  69. },
  70. setUpUI() {
  71. /// 抽奖冷却结束
  72. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  73. this.setUpNormalButton();
  74. } else {
  75. this.setUpUseBtnBg(false, 1);
  76. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  77. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  78. this.schedule(this.updateCD, 1);
  79. }
  80. this.moreRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond2}</c></b>`;
  81. this.bestRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond3}</c></b>`;
  82. let hasDiamod = GameModule.userInfo.diamond;
  83. if (hasDiamod < this.respondData.diamond2) {
  84. this.setUpUseBtnBg(false, 2);
  85. }
  86. if (hasDiamod < this.respondData.diamond3) {
  87. this.setUpUseBtnBg(false, 3);
  88. }
  89. },
  90. setUpNormalButton() {
  91. this.normalDrawButton.interactable = true;
  92. this.normalRichText.string = '<b><color=#ffffff>分享\n免费获得</c></b>';
  93. },
  94. /// 网络请求
  95. getDrawState(success, fail) {
  96. return new Promise((resolve, reject) => {
  97. DrawApi.getLotteryInfo((respondData) => {
  98. resolve(respondData);
  99. }, (code, msg) => {
  100. reject({code, msg});
  101. });
  102. })
  103. },
  104. startDrawRequest(typeId, diamond = 0) {
  105. return new Promise((resolve, reject) => {
  106. ///开始抽奖
  107. DrawApi.startLottery(typeId, diamond, (respondData) => {
  108. resolve(respondData);
  109. }, (code, msg) => {
  110. reject({code, msg});
  111. });
  112. })
  113. },
  114. startDraw(typeId, completion) {
  115. this.startDrawRequest(typeId, 0).then((respondData) => {
  116. /// 把抽奖状态 以及抽奖成功之后的数据加上去
  117. // this.respondData.drawSuccessData = respondData;
  118. this.respondData.propId = respondData.propId;
  119. this.respondData.propName = respondData.propName;
  120. this.respondData.roomId = respondData.roomId;
  121. this.closeNodeAction();
  122. completion(true);
  123. AlertManager.showDrawScrollAlert(this.respondData, typeId);
  124. }).catch(({code, msg}) => {
  125. console.log(code, msg);
  126. completion(false);
  127. this.normalDrawButton.interactable = true;
  128. GameGlobal.commonAlert.showCommonErrorAlert(msg);
  129. });
  130. },
  131. updateCD() {
  132. this.respondData.cdTime += 1000;
  133. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  134. this.setUpNormalButton();
  135. this.setUpUseBtnBg(true, 1);
  136. this.unschedule(this.updateCD, this);
  137. }
  138. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  139. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  140. },
  141. /// 看广告抽奖
  142. adNodeAction() {
  143. GameModule.audioMng.playClickButton();
  144. if (CC_WECHATGAME || CC_QQPLAY) {
  145. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type) => {
  146. this.shareActionCallback(type);
  147. });
  148. this.normalDrawButton.interactable = false;
  149. WeChat.shareAction(WechatShareType.DrawLottery, () => {
  150. }, () => {
  151. this.normalDrawButton.interactable = true;
  152. console.log('分享失败或取消');
  153. });
  154. }
  155. },
  156. shareActionCallback(type) {
  157. if (type != WechatShareType.DrawLottery) { return; }
  158. this.startDraw(1, () => {
  159. this.normalDrawButton.interactable = true;
  160. TapTapTool.removeRedDot(GameRedDot.draw);
  161. });
  162. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  163. },
  164. ///更多砖石抽奖
  165. moreChanceNodeAction() {
  166. GameModule.audioMng.playClickButton();
  167. this.moreChanceButton.interactable = false;
  168. this.startDraw(2, (isSuccess) => {
  169. this.moreChanceButton.interactable = true;
  170. if (isSuccess) {
  171. GameModule.userInfo.diamond -= this.respondData.diamond2;
  172. }
  173. });
  174. },
  175. ///最大概率抽奖
  176. bestChanceNodeAction () {
  177. GameModule.audioMng.playClickButton();
  178. this.bestChanceButton.interactable = false;
  179. this.startDraw(3, (isSuccess) => {
  180. this.bestChanceButton.interactable = true;
  181. if (isSuccess) {
  182. GameModule.userInfo.diamond -= this.respondData.diamond3;
  183. }
  184. });
  185. },
  186. closeNodeAction() {
  187. // let finish = cc.callFunc(() => {
  188. // this.node.destroy();
  189. // }, this);
  190. // let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
  191. // this.content.runAction(sequence);
  192. this.node.destroy();
  193. GameModule.audioMng.playClickButton();
  194. },
  195. /// 设置button是否能点击 type 123
  196. setUpUseBtnBg(isActive, type) {
  197. let path = isActive ? './textures/draw/draw_green_btn_bg' : './textures/draw/draw_gray_btn_bg';
  198. DWTool.loadResSpriteFrame(path)
  199. .then((spriteFrame) => {
  200. if (type == 1) {
  201. this.normalDrawButtonSprite.spriteFrame = spriteFrame;
  202. this.normalDrawButton.interactable = isActive;
  203. } else if (type == 2) {
  204. this.moreChanceButtonSprite.spriteFrame = spriteFrame;
  205. this.moreChanceButton.interactable = isActive;
  206. } else {
  207. this.bestChanceButtonSprite.spriteFrame = spriteFrame;
  208. this.bestChanceButton.interactable = isActive;
  209. }
  210. });
  211. },
  212. showDrawRedAction() {
  213. AlertManager.showDrawRedRecordAlert();
  214. }
  215. // update (dt) {},
  216. });