QuestDaily.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. const QuestApi = require('../net/QuestApi');
  2. const DWTool = require("../utils/DWTool");
  3. const QuestDailyMissionType = require("../utils/GameEnum").QuestDailyMissionType;
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. questWrap: {
  8. tooltip: '每日任务容器',
  9. default: null,
  10. type: cc.Node
  11. },
  12. questBtns: [cc.Node],
  13. userDegreeRichText: {
  14. tooltip: '用户当前活跃度',
  15. default: null,
  16. type: cc.RichText
  17. },
  18. _userDegree: 0,
  19. userDegree: {
  20. get () {
  21. return this._userDegree;
  22. },
  23. set (value) {
  24. this._userDegree = value;
  25. // 设置用户当前活跃度
  26. // this.actProgress.progress = data / 100
  27. // this.userDegreeLabel.string = data
  28. }
  29. },
  30. giftFrames: {
  31. tooltip: '奖励图片素材',
  32. default: [],
  33. type: [cc.SpriteFrame]
  34. },
  35. questItem: cc.Prefab,
  36. resetTimeLabel: cc.Label,
  37. isLoadData: {
  38. default: false,
  39. visible: false
  40. }
  41. },
  42. onLoad () {
  43. this.userDegree = 0;
  44. this.dailyDegreeList = [];
  45. this.giftMap = ['diamond', 'coin', 'ticket'];
  46. },
  47. init (quest) {
  48. this.quest = quest;
  49. this.loadNetworkData();
  50. },
  51. loadNetworkData() {
  52. QuestApi.getDailyTask((responseData) => {
  53. this.dailyDegreeList = responseData.dailyDegreeList.sort((a, b) => {
  54. return a.count - b.count
  55. });
  56. this.degree = this.dailyDegreeList[0];
  57. this.dailyTaskList = responseData.dailyTaskList;
  58. this.userDegree = parseInt(responseData.userDegree);
  59. this._setUserDegree();
  60. this._setTaskList();
  61. this.updateTime = responseData.updateTime;
  62. this.isLoadData = true;
  63. } , (error) => {
  64. console.log('dailytask error' + error);
  65. });
  66. },
  67. /**
  68. * 渲染每日任务列表
  69. */
  70. _setTaskList () {
  71. this.questWrap.removeAllChildren();
  72. this.dailyTaskList.forEach((item, index) => {
  73. let questItem = cc.instantiate(this.questItem);
  74. this.questWrap.addChild(questItem);
  75. questItem.getComponent('QuestDailyItem').init(this, item, index);
  76. })
  77. },
  78. /**
  79. * 设置活跃度奖励进度条
  80. */
  81. _setUserDegree () {
  82. var canGet = false;
  83. var alreadyGetCount = 0;
  84. for (let i = 0; i < this.dailyTaskList.length; ++i) {
  85. let dailyTask = this.dailyTaskList[i];
  86. if (dailyTask.state == QuestDailyMissionType.AlreadyGet){
  87. alreadyGetCount++;
  88. }
  89. }
  90. if (alreadyGetCount >= this.degree.count) {
  91. canGet = true;
  92. }
  93. this.userDegreeRichText.string = `<b><outline color=#2E2E2E width=2>每日任务 最终奖励 </outline></b><size=18><outline color=#2E2E2E width=2>(${alreadyGetCount}/${this.degree.count})</outline></size>`;
  94. this.questBtns.forEach(n => {
  95. n.active = false;
  96. });
  97. let btn;
  98. if (this.degree.state == QuestDailyMissionType.NoFinished) {
  99. btn = this.questBtns[0];
  100. } else if (this.degree.state == QuestDailyMissionType.CanGain) {
  101. if (canGet) {
  102. btn = this.questBtns[1];
  103. } else {
  104. btn = this.questBtns[0];
  105. }
  106. } else {
  107. btn = this.questBtns[2];
  108. }
  109. btn.active = true;
  110. let countNode = cc.find('/awardNode/count_label', btn);
  111. let spriteNode = cc.find('/awardNode/sprite', btn);
  112. let awardLabelNode = cc.find('/awardLabel', btn);
  113. var awardString = '奖励';
  114. if (canGet) {
  115. awardString = '获得';
  116. }
  117. let _count = 0;
  118. let _type = '';
  119. let _sprite = null;
  120. this.giftMap.forEach((value, index) => {
  121. if(this.degree[value] > 0) {
  122. _count = this.degree[value];
  123. _type = value;
  124. _sprite = this.giftFrames[index];
  125. }
  126. });
  127. if (spriteNode) {
  128. spriteNode.getComponent('cc.Sprite').spriteFrame = _sprite;
  129. }
  130. if (countNode) {
  131. countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(_count)}`;
  132. }
  133. if (awardLabelNode) {
  134. awardLabelNode.getComponent('cc.Label').string = awardString;
  135. }
  136. },
  137. updateUserDegree() {
  138. this._setUserDegree();
  139. },
  140. /**
  141. * 领取每日任务最终奖励
  142. */
  143. handleGiftBtn () {
  144. // state = 0, 可领取状态
  145. if(this.degree.state == QuestDailyMissionType.CanGain) {
  146. this.questBtns[1].interactable = false;
  147. QuestApi.postGetDailyDegreeReward(this.degree.count, (res) => {
  148. this.questBtns[1].interactable = true;
  149. // 更新全局userInfo
  150. this.quest.updateUserInfo(this.degree.coin, this.degree.diamond, this.degree.ticket);
  151. // state = 2, 已领取状态
  152. this.degree.state = QuestDailyMissionType.AlreadyGet;
  153. this.questBtns.forEach(n => {
  154. n.active = false;
  155. });
  156. this.questBtns[2].active = true;
  157. // 显示动画
  158. setTimeout(() => {
  159. this.quest.showActGift({
  160. ticket: this.degree.ticket,
  161. diamond: this.degree.diamond,
  162. coin: this.degree.coin
  163. })
  164. }, 200);
  165. if (res.isCancel) {
  166. let isCancel = res.isCancel == 1 ? true : false;
  167. GameEvent.fire('quest_daily_notice', isCancel);
  168. }
  169. }, (errCode, errMsg) => {
  170. this.questBtns[1].interactable = true;
  171. // 领取失败
  172. Global.commonAlert.showCommonErrorAlert("领取奖励失败");
  173. });
  174. }
  175. },
  176. update(dt) {
  177. if (this.updateTime == undefined) {
  178. return;
  179. }
  180. if (this.isLoadData == true) {
  181. var curTimestamp = parseInt(new Date().getTime());
  182. if ((this.updateTime - curTimestamp) > 0) {
  183. this.resetTimeLabel.string = `每日任务重置:${DWTool.calculateTimeString((this.updateTime - curTimestamp) / 1000)}`;
  184. } else {
  185. this.isLoadData = false;
  186. this.loadNetworkData();
  187. }
  188. }
  189. }
  190. // start () {
  191. //
  192. // }
  193. });