QuestDaily.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.CanGet) {
  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. spriteNode.getComponent('cc.Sprite').spriteFrame = _sprite;
  128. countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(_count)}`;
  129. awardLabelNode.getComponent('cc.Label').string = awardString;
  130. },
  131. updateUserDegree() {
  132. this._setUserDegree();
  133. },
  134. /**
  135. * 领取每日任务最终奖励
  136. */
  137. handleGiftBtn () {
  138. // state = 0, 可领取状态
  139. if(this.degree.state == QuestDailyMissionType.CanGain) {
  140. QuestApi.postGetDailyDegreeReward(this.degree.count, (res) => {
  141. // 更新全局userInfo
  142. this.quest.updateUserInfo(this.degree.coin, this.degree.diamond, this.degree.ticket);
  143. // state = 2, 已领取状态
  144. this.degree.state = QuestDailyMissionType.AlreadyGet;
  145. this.questBtns.forEach(n => {
  146. n.active = false;
  147. });
  148. this.questBtns[2].active = true;
  149. // 显示动画
  150. setTimeout(() => {
  151. this.quest.showActGift({
  152. ticket: this.degree.ticket,
  153. diamond: this.degree.diamond,
  154. coin: this.degree.coin
  155. })
  156. }, 200);
  157. }, (errCode, errMsg) => {
  158. // 领取失败
  159. Global.commonAlert.showCommonErrorAlert("领取奖励失败");
  160. });
  161. }
  162. },
  163. update(dt) {
  164. if (this.updateTime == undefined) {
  165. return;
  166. }
  167. if (this.isLoadData == true) {
  168. var curTimestamp = parseInt(new Date().getTime());
  169. if ((this.updateTime - curTimestamp) > 0) {
  170. this.resetTimeLabel.string = `每日任务重置:${DWTool.calculateTimeString((this.updateTime - curTimestamp) / 1000)}`;
  171. } else {
  172. this.isLoadData = false;
  173. this.loadNetworkData();
  174. }
  175. }
  176. }
  177. // start () {
  178. //
  179. // }
  180. });