DrawContent.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.propId = respondData.propId;
  118. this.respondData.propName = respondData.propName;
  119. this.respondData.roomId = respondData.roomId;
  120. this.closeNodeAction();
  121. completion(true);
  122. AlertManager.showDrawScrollAlert(this.respondData, typeId);
  123. }).catch(({code, msg}) => {
  124. console.log(code, msg);
  125. completion(false);
  126. this.normalDrawButton.interactable = true;
  127. GameGlobal.commonAlert.showCommonErrorAlert(msg);
  128. });
  129. },
  130. updateCD() {
  131. this.respondData.cdTime += 1000;
  132. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  133. this.setUpNormalButton();
  134. this.setUpUseBtnBg(true, 1);
  135. this.unschedule(this.updateCD, this);
  136. }
  137. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  138. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  139. },
  140. /// 看广告抽奖
  141. adNodeAction() {
  142. GameModule.audioMng.playClickButton();
  143. if (CC_WECHATGAME || CC_QQPLAY) {
  144. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type) => {
  145. this.shareActionCallback(type);
  146. });
  147. this.normalDrawButton.interactable = false;
  148. WeChat.shareAction(WechatShareType.DrawLottery, () => {
  149. }, () => {
  150. this.normalDrawButton.interactable = true;
  151. console.log('分享失败或取消');
  152. });
  153. }
  154. },
  155. shareActionCallback(type) {
  156. if (type != WechatShareType.DrawLottery) { return; }
  157. this.startDraw(1, () => {
  158. this.normalDrawButton.interactable = true;
  159. TapTapTool.removeRedDot(GameRedDot.draw);
  160. });
  161. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  162. },
  163. ///更多砖石抽奖
  164. moreChanceNodeAction() {
  165. GameModule.audioMng.playClickButton();
  166. this.moreChanceButton.interactable = false;
  167. this.startDraw(2, (isSuccess) => {
  168. this.moreChanceButton.interactable = true;
  169. if (isSuccess) {
  170. GameModule.userInfo.diamond -= this.respondData.diamond2;
  171. }
  172. });
  173. },
  174. ///最大概率抽奖
  175. bestChanceNodeAction () {
  176. GameModule.audioMng.playClickButton();
  177. this.bestChanceButton.interactable = false;
  178. this.startDraw(3, (isSuccess) => {
  179. this.bestChanceButton.interactable = true;
  180. if (isSuccess) {
  181. GameModule.userInfo.diamond -= this.respondData.diamond3;
  182. }
  183. });
  184. },
  185. closeNodeAction() {
  186. // let finish = cc.callFunc(() => {
  187. // this.node.destroy();
  188. // }, this);
  189. // let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
  190. // this.content.runAction(sequence);
  191. this.node.destroy();
  192. GameModule.audioMng.playClickButton();
  193. },
  194. /// 设置button是否能点击 type 123
  195. setUpUseBtnBg(isActive, type) {
  196. let path = isActive ? './textures/draw/draw_green_btn_bg' : './textures/draw/draw_gray_btn_bg';
  197. DWTool.loadResSpriteFrame(path)
  198. .then((spriteFrame) => {
  199. if (type == 1) {
  200. this.normalDrawButtonSprite.spriteFrame = spriteFrame;
  201. this.normalDrawButton.interactable = isActive;
  202. } else if (type == 2) {
  203. this.moreChanceButtonSprite.spriteFrame = spriteFrame;
  204. this.moreChanceButton.interactable = isActive;
  205. } else {
  206. this.bestChanceButtonSprite.spriteFrame = spriteFrame;
  207. this.bestChanceButton.interactable = isActive;
  208. }
  209. });
  210. },
  211. // update (dt) {},
  212. });