ExpropriationGrainItem.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { _decorator, Component, Node, Sprite, SpriteFrame, Label, EventHandler, Button, RichText } from 'cc';
  2. import { RewardVideoSystem } from '../ad/RewardVideoSystem';
  3. import { DataSystem } from '../core/data/DataSystem';
  4. import { Http } from '../core/net/Http';
  5. import { HttpSystem } from '../core/net/HttpSystem';
  6. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  7. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  8. import { WindowSystem } from '../core/ui/window/WindowSystem';
  9. import { HttpErrorCode } from '../data/HttpErrorCode';
  10. import { ADData } from '../data/jsb/ADData';
  11. import { RedPointData } from '../main/RedPointData';
  12. import { ReportThinking } from '../ReportThinking';
  13. const { ccclass, property, requireComponent } = _decorator;
  14. @ccclass('ExpropriationGrainItem')
  15. @requireComponent(ResourceLoader)
  16. export class ExpropriationGrainItem extends Component {
  17. @property({ tooltip: "背景图", type: Sprite }) private bg: Sprite;
  18. @property({ tooltip: "奖励值", type: Label }) private prizeNum: Label;
  19. @property({ tooltip: "征收按钮", type: Node }) private toGetBtn: Node;
  20. @property({ tooltip: "征收按钮免费文本", type: Node }) private toGetBtnFree: Node;
  21. @property({ tooltip: "征收按钮视频文本", type: Node }) private toGetBtnVideo: Node;
  22. @property({ tooltip: "领取按钮", type: Node }) private getBtn: Node;
  23. @property({ tooltip: "加速按钮", type: Node }) private quickenBtn: Node;
  24. @property({ tooltip: "倒计时文本", type: Label }) private timeTxt: Label;
  25. @property({ tooltip: "配置所需时间文本", type: Node }) private configTime: Node;
  26. @property({ tooltip: "加速按钮文本", type: Label }) private quickTxt: Label;
  27. private index = 0;
  28. private isHadToGet = false;//是否有出征状态
  29. public initToGetState: EventHandler;
  30. public timesTxt: RichText;//次数文本
  31. public finishTime: number = 0;//征粮结束时间
  32. start() {
  33. // [3]
  34. }
  35. public async initUI(data: { index: number, rewardNum: number, time: number, finishTime?: number, isReceive?: boolean }) {
  36. this.prizeNum.string = data.rewardNum + "";
  37. this.configTime.getComponent(Label).string = this.formatTime(data.time * 1000);
  38. this.index = data.index;
  39. this.bg.spriteFrame = await this.getComponent(ResourceLoader).load<SpriteFrame>("image/getGrain/banner" + data.index + "/spriteFrame", SpriteFrame);
  40. if (!data.finishTime) {
  41. this.initToGetState && this.initToGetState.emit([this.index, 0]);
  42. return;
  43. }
  44. let date = HttpSystem.serverTime / 1000;
  45. if (date >= data.finishTime) {
  46. if (!data.isReceive) {
  47. this.toGetBtn.active = false;
  48. this.configTime.active = false;
  49. this.getBtn.active = true;
  50. this.timeTxt.string = "已完成";
  51. }
  52. this.initToGetState && this.initToGetState.emit([this.index, 0]);
  53. } else {
  54. this.toGetBtn.active = false;
  55. this.configTime.active = false;
  56. this.quickenBtn.active = true;
  57. this.finishTime = data.finishTime;
  58. this.initToGetState && this.initToGetState.emit([this.index, 1]);
  59. }
  60. }
  61. public initTogetBtn(isToGet: boolean) {
  62. this.toGetBtnFree.active = !isToGet;
  63. this.toGetBtnVideo.active = isToGet;
  64. this.isHadToGet = isToGet;
  65. }
  66. //征粮
  67. public async toGetGrain() {
  68. this.toGetBtn.getComponent(Button).interactable = false;
  69. if (this.isHadToGet) {
  70. let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
  71. if (result && result.code == 0) {
  72. this.ad_init();
  73. let adData = await RewardVideoSystem.show();
  74. if (adData) {//视频观看成功
  75. this.requestToGetGrain(adData);
  76. } else {
  77. //视频观看失败
  78. WindowSystem.showTips("视频观看时间不足");
  79. this.toGetBtn.getComponent(Button).interactable = true;
  80. }
  81. } else {//申请看视频回复错误信息
  82. WindowSystem.showTips("视频次数不足");
  83. this.toGetBtn.getComponent(Button).interactable = true;
  84. }
  85. } else {
  86. this.requestToGetGrain()
  87. }
  88. }
  89. private async requestToGetGrain(adData?: ADData) {
  90. let data = adData ? { index: this.index, adData: adData.obj } : { index: this.index };
  91. let resul = await this.getComponent(Http).send("/api/grainLevies/StartGrainLevies", data);
  92. if (resul && resul.code == 0) {
  93. adData && this.ad_end();
  94. !adData && ReportThinking.field();
  95. this.timesTxt && (this.timesTxt.string = "<b><color=#FFFFFF>剩余次数:</color><color=#DA5E5F>" + resul.data.times + "</color>");
  96. this.finishTime = resul.data.finishTime;
  97. //初始化界面
  98. this.toGetBtn.active = false;
  99. this.configTime.active = false;
  100. this.quickenBtn.active = true;
  101. this.initToGetState && this.initToGetState.emit([this.index, 1]);
  102. DataSystem.getData(RedPointData).grainLevies.times = resul.data.times;//更新红点数据,剩余出征次数
  103. DataSystem.getData(RedPointData).grainLevies = DataSystem.getData(RedPointData).grainLevies;
  104. } else {//服务器返回错误消息
  105. if (resul && resul.code == HttpErrorCode.NoTimes) {
  106. WindowSystem.showTips("征粮次数不足");
  107. } else if (resul && resul.code == HttpErrorCode.VideoADCD) {
  108. WindowSystem.showTips('视频观看时间不足');
  109. } else {
  110. WindowSystem.showTips("征粮失败");
  111. }
  112. }
  113. this.toGetBtn.getComponent(Button).interactable = true;
  114. }
  115. //领取征粮粮草
  116. public async getGrain() {
  117. this.getBtn.getComponent(Button).interactable = false;
  118. let resul = await this.getComponent(Http).send("/api/grainLevies/ReceiveGrainLevies", { index: this.index });
  119. if (resul && resul.code == 0) {//领取粮草
  120. await WindowSystem.open("prefabs/ui/turntable/turntablePrizePopup", WindowOpenMode.NotCloseAndCover, [{ icon: 4, type: 1, num: resul.data }]);
  121. //初始化界面
  122. this.getBtn.active = false;
  123. this.toGetBtn.active = true;
  124. this.configTime.active = true;
  125. this.timeTxt.string = "";
  126. this.redPointChange(true);
  127. }
  128. this.getBtn.getComponent(Button).interactable = true;
  129. }
  130. //cd减半
  131. public async quicken() {
  132. this.quickenBtn.getComponent(Button).interactable = false;
  133. let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
  134. if (result && result.code == 0) {
  135. let adData = await RewardVideoSystem.show();
  136. this.ad_init(true);
  137. if (adData) {//视频观看成功
  138. let _result = await this.getComponent(Http).send("/api/grainLevies/HalfGrainLevies", { index: this.index, adData: adData.obj });
  139. if (_result && _result.code == 0) {
  140. this.ad_end(true);
  141. this.finishTime = _result.data;
  142. } else if (_result && _result.code == HttpErrorCode.VideoADCD) {
  143. WindowSystem.showTips("视频观看时间不足");
  144. }
  145. } else {
  146. WindowSystem.showTips("视频观看时间不足");
  147. }
  148. } else {
  149. WindowSystem.showTips("视频次数不足");
  150. }
  151. this.quickenBtn.getComponent(Button).interactable = true;
  152. }
  153. /**
  154. * 格式化倒计时
  155. */
  156. public formatCountDown(time: number): string {
  157. let ss = Math.floor(time / 1000);
  158. let day = Math.floor(ss / 3600 / 24);
  159. let h = Math.floor(ss / 3600 % 24) < 10 ? "0" + Math.floor(ss / 3600 % 24) : Math.floor(ss / 3600 % 24);
  160. let m = Math.floor(ss / 60 % 60) < 10 ? "0" + Math.floor(ss / 60 % 60) : Math.floor(ss / 60 % 60);
  161. let s = Math.floor(ss % 60) < 10 ? "0" + Math.floor(ss % 60) : Math.floor(ss % 60);
  162. let result = "";
  163. if (day > 0) {
  164. result = day + "天" + h + ":" + m + ":" + s;
  165. } else {
  166. result = h + ":" + m + ":" + s;
  167. }
  168. return result;
  169. }
  170. /**
  171. * 时间格式化
  172. */
  173. public formatTime(time: number): string {
  174. let ss = Math.floor(time / 1000);
  175. let day = Math.floor(ss / 3600 / 24);
  176. let h = Math.floor(ss / 3600 % 24);
  177. let m = Math.floor(ss / 60 % 60);
  178. let s = Math.floor(ss % 60);
  179. let result = "";
  180. day > 0 && (result = day + "天");
  181. h > 0 && (result = result + h + "小时");
  182. m > 0 && (result = result + m + "分钟");
  183. s > 0 && (result = result + s + "秒");
  184. return result;
  185. }
  186. update() {
  187. let _time = Math.round(HttpSystem.serverTime / 1000);
  188. if (_time <= this.finishTime) {
  189. this.timeTxt.string = this.formatCountDown((this.finishTime - _time) * 1000);
  190. if (this.quickTxt.string != "-50%时间" && (this.finishTime - _time > (60 * 30))) {
  191. this.quickTxt.string = "-50%时间";
  192. } else if (this.quickTxt.string != "立即完成" && (this.finishTime - _time <= (60 * 30))) {
  193. this.quickTxt.string = "立即完成";
  194. }
  195. } else {
  196. if (this.timeTxt.string != "已完成" && this.timeTxt.string != "") {
  197. this.timeTxt.string = "已完成";
  198. this.quickenBtn.active = false;
  199. this.getBtn.active = true;
  200. this.initToGetState && this.initToGetState.emit([this.index, 0]);
  201. this.redPointChange(false);
  202. }
  203. }
  204. }
  205. //修改红点已领取数据
  206. private redPointChange(isReceive: boolean) {
  207. for (let i = 0; i < DataSystem.getData(RedPointData).grainLevies.leviesDatas.length; i++) {
  208. if (DataSystem.getData(RedPointData).grainLevies.leviesDatas[i].index == this.index) {
  209. DataSystem.getData(RedPointData).grainLevies.leviesDatas[i].isReceive = isReceive;
  210. DataSystem.getData(RedPointData).grainLevies = DataSystem.getData(RedPointData).grainLevies;
  211. break;
  212. }
  213. }
  214. }
  215. /**上报数数 ad_init*/
  216. private ad_init(isCd: boolean = false) {
  217. let p_name = this.getName(isCd);
  218. ReportThinking.ad_init(p_name);
  219. }
  220. /**上报数数 ad_end*/
  221. private ad_end(isCd: boolean = false) {
  222. let p_name = this.getName(isCd);
  223. ReportThinking.ad_end(p_name);
  224. }
  225. private getName(isCd: boolean) {
  226. let p_name = '';
  227. switch (this.index) {
  228. case 0: p_name = isCd ? 'skip_5min' : 'collect_5min'; break;
  229. case 1: p_name = isCd ? 'skip_15min' : 'collect_15min'; break;
  230. case 2: p_name = isCd ? 'skip_1hour' : 'collect_1hour'; break;
  231. case 3: p_name = isCd ? 'skip_12hour' : 'collect_12hour'; break;
  232. }
  233. return p_name;
  234. }
  235. }