DrawContent.js 8.2 KB

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