MainUIController.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import { Component, EventHandler, find, instantiate, Label, macro, Node, Prefab, Vec3, _decorator } from "cc";
  2. import { Http, HttpMethod } from "../core/net/Http";
  3. import { ResourcesUtils } from "../core/resourceManager/ResourcesUtils";
  4. import { BitmapFont } from "../core/ui/BitmapFont";
  5. import { WindowManager } from "../core/ui/window/WindowManager";
  6. import { WindowOpenMode } from "../core/ui/window/WindowOpenMode";
  7. import { ConfigData } from "../Data/ConfigData";
  8. import { g } from "../Data/g";
  9. import { FsUtils } from "../FsUtils/FsUtils";
  10. import { createType } from "./RoleController";
  11. import { SoundSystem } from "./SoundSystem";
  12. const { ccclass, property } = _decorator;
  13. @ccclass('MainUIController')
  14. export class MainUIController extends Component {
  15. @property({ type: BitmapFont, tooltip: "每日分红金额" })
  16. bonusDayBitmapFont: BitmapFont;
  17. @property({ type: Label, tooltip: "金币文本" })
  18. moneyLabel: Label;
  19. @property({ type: Label, tooltip: "神将每秒产出文本" })
  20. gelenalOutPutLabel: Label;
  21. @property({ type: Label, tooltip: "神石文本" })
  22. diamondLabel: Label;
  23. @property({ type: Label, tooltip: "神石兑换金额文本" })
  24. cashLabel: Label;
  25. @property({ type: Node, tooltip: "出售按钮" })
  26. sellButton: Node;
  27. @property({ type: Node, tooltip: "购买按钮" })
  28. buyButton: Node;
  29. @property({ type: Label, tooltip: "购买按钮等级文本" })
  30. buyLvLabel: Label;
  31. @property({ type: BitmapFont, tooltip: "购买按钮价格文本" })
  32. buyPriceBitmapFont: BitmapFont;
  33. @property({ type: Label, tooltip: "神台开放等级文本" })
  34. shenTaiLvLabel: Label;
  35. @property({ type: Label, tooltip: "切磋开放等级文本" })
  36. battleLvLabel: Label;
  37. @property({ type: Label, tooltip: "等级红包领取提示说明" })
  38. lvRedPacketMemoLabel: Label;
  39. @property({ type: Node, tooltip: "神台红点节点" })
  40. shenTaiRedPointNode: Node;
  41. @property({ type: Node, tooltip: "切磋红点节点" })
  42. qieCuoRedPointNode: Node;
  43. @property({ type: Node, tooltip: "抢红包红点节点" })
  44. qianHongBaoRedPointNode: Node;
  45. @property({ type: EventHandler, tooltip: "购买成功后" })
  46. buyHander: EventHandler;
  47. @property({ type: EventHandler, tooltip: "被挑战失败后" })
  48. attackLossHander: EventHandler;
  49. @property({ type: Http, tooltip: "Http服务" })
  50. http: Http;
  51. @property({ tooltip: "等级红包文字提示", type: Label })
  52. public lvRedRedPacket: Label;
  53. @property({ tooltip: "等级红包", type: Node })
  54. public lvRedRedPacketBtn: Node;
  55. @property({ tooltip: "互推按钮节点", type: Node })
  56. public eachBtnNode: Node;
  57. private lvRedPacket = g.gameData.lvRedPacket;//等级红包当前显示已经领取等级
  58. private money = -1;
  59. private diamond = -1;
  60. private bonus = -1;
  61. private userLv = -1;
  62. private addMoney = -1;
  63. private buyGeneralID = -1;
  64. private buyGeneralPrice = -1;
  65. /**金币同眇间隔 */
  66. private updateTime: number = 10000;
  67. /**下次金币同步时间戳 */
  68. private nextUpdateMoneyTime: number = 0;
  69. /**金币增长时间戳 */
  70. private nextAddMoneyTime: number = 0;
  71. private generalConfig: any;
  72. private systemConfig: any;
  73. start() {
  74. this.systemConfig = ConfigData.configMap.get("systemConfig");
  75. this.generalConfig = ConfigData.configMap.get("generalBase");
  76. this.eachBtnNode.active = g.gameData.openRecommend;
  77. // macro.ENABLE_MULTI_TOUCH = false;
  78. SoundSystem.play(0);
  79. if (this.lvRedPacket >= 51) {
  80. this.lvRedRedPacketBtn.active = false;
  81. } else {
  82. this.lvRedRedPacket.string = "达到" + (this.lvRedPacket + 1) + "级可领取";
  83. }
  84. if (g.userData.lv < 2) {
  85. this.addNovice();
  86. } else {
  87. if (g.gameData.differTime) {
  88. WindowManager.open("Prefabs/Windows/视频金币界面", WindowOpenMode.CloseAndCover, [1, g.gameData.totalOutput, 3, g.gameData.differTime]);
  89. }
  90. }
  91. this.getGameBonus();
  92. this.setUpdateTime();
  93. this.getRedPackets();
  94. this.getBeAttackDatas();
  95. this.getFission();
  96. this.getEachData();
  97. }
  98. private async addNovice() {
  99. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/新手引导", Prefab, this.node);
  100. let novice = instantiate(prefab);
  101. novice.setParent(find("Canvas/UI"));
  102. }
  103. /**更新下次同步时间 */
  104. private setUpdateTime() {
  105. let now = Date.now();
  106. this.nextUpdateMoneyTime = now + this.updateTime;
  107. this.nextAddMoneyTime = now + 1000;
  108. }
  109. async update() {
  110. if (this.lvRedPacket != g.gameData.lvRedPacket) {
  111. this.lvRedPacket = g.gameData.lvRedPacket;
  112. if (this.lvRedPacket >= 51) {
  113. this.lvRedRedPacketBtn.active = false;
  114. } else {
  115. this.lvRedRedPacket.string = "达到" + (this.lvRedPacket + 1) + "级可领取";
  116. }
  117. }
  118. if (this.diamond != g.userData.diamond) {
  119. this.diamond = g.userData.diamond;
  120. this.diamondLabel.string = FsUtils.goldFormat(g.userData.diamond)
  121. this.cashLabel.string = '约' + FsUtils.toFixed(g.userData.diamond / this.systemConfig.moneyRate, 2) + '元';
  122. }
  123. if (this.money != g.userData.money) {
  124. this.money = g.userData.money;
  125. this.moneyLabel.string = FsUtils.moneyFormat(g.userData.money);
  126. }
  127. if (this.buyGeneralID != g.gameData.buyLv) {
  128. this.buyGeneralID = g.gameData.buyLv;
  129. this.buyLvLabel.string = `Lv.${this.generalConfig[this.buyGeneralID].lv} ${this.generalConfig[this.buyGeneralID].name} `
  130. }
  131. if (this.buyGeneralPrice != g.gameData.buyPrice) {
  132. this.buyGeneralPrice = g.gameData.buyPrice;
  133. this.buyPriceBitmapFont.string = FsUtils.moneyFormat(this.buyGeneralPrice);
  134. }
  135. if (this.userLv != g.userData.lv) {
  136. this.userLv = g.userData.lv;
  137. if (this.userLv < this.systemConfig.openBattleLv) {
  138. this.battleLvLabel.string = this.systemConfig.openBattleLv + '级开放';
  139. } else {
  140. this.battleLvLabel.string = '';
  141. }
  142. }
  143. if (g.gameData.needSyncMoney) {
  144. g.gameData.needSyncMoney = false;
  145. this.syncMoney();
  146. }
  147. let now = Date.now();
  148. if (now >= this.nextUpdateMoneyTime) {
  149. this.syncMoney();
  150. }
  151. if (now >= this.nextAddMoneyTime) {
  152. let addMoneyCount: number = 0;
  153. let generals = g.gameData.getMyLocalData(g.userData.id).myLocalGenerals;
  154. for (let i = 0; i < generals.length; i++) {
  155. addMoneyCount += this.generalConfig[generals[i].generalLv].output;
  156. }
  157. g.userData.money += addMoneyCount;
  158. if (addMoneyCount != this.addMoney) {
  159. this.addMoney = addMoneyCount;
  160. this.gelenalOutPutLabel.string = FsUtils.moneyFormat(this.addMoney) + '/秒';
  161. }
  162. this.nextAddMoneyTime = now + 1000;
  163. }
  164. //#region 红点更新
  165. //神台红点
  166. let tempBoolen = g.gameData.getAllRevenueAmount();
  167. if (tempBoolen != this.shenTaiRedPointNode.active) {
  168. this.shenTaiRedPointNode.active = tempBoolen;
  169. }
  170. //远征红点
  171. if (g.gameData.hasAvengeListUpdate != this.qieCuoRedPointNode.active) {
  172. this.qieCuoRedPointNode.active = g.gameData.hasAvengeListUpdate;
  173. }
  174. //抢红包红点
  175. if (g.gameData.hasNewRedPacketUpdate) {
  176. if (!this.qianHongBaoRedPointNode.active) {
  177. this.qianHongBaoRedPointNode.active = true;
  178. }
  179. } else {
  180. this.initRedPacketRedPoint();
  181. }
  182. //#endregion
  183. }
  184. private initRedPacketRedPoint() {
  185. let _time = Date.now() / 1000;
  186. let _bool = false;
  187. for (let i = 0; i < g.gameData.myRedPacketDatas.length; i++) {
  188. if (g.gameData.myRedPacketDatas[i].canOpenTime <= _time) {
  189. _bool = true;
  190. break;
  191. }
  192. }
  193. if (this.qianHongBaoRedPointNode.active != _bool) {
  194. this.qianHongBaoRedPointNode.active = _bool;
  195. }
  196. }
  197. /**同步用户金币 */
  198. private async syncMoney() {
  199. this.setUpdateTime();
  200. if (g.gameData.isPlayAddMoneyAnim || g.gameData.isPlayAddDiamondAnim) {
  201. return;
  202. }
  203. // let result = await this.http.send("/api/wealth/SyncMoney");
  204. let result = await this.http.send("/api/wealth/SyncMoney", null, HttpMethod.GET, false);
  205. if (!result.code && result.data) {
  206. g.userData.money = result.data.newMoney;
  207. // g.userData.moneyPower10 = result.data.newMoneyPower10;
  208. // 红点数据比如:[1,0] (0被攻击,请求28号消息;1红包,请求13号消息;2贡献,请求24号消息) |
  209. for (let i = 0; i < result.data.redDotDatas.length; i++) {//数据更新
  210. switch (result.data.redDotDatas[i]) {
  211. case 0://被别人攻击了显示感叹号
  212. this.getBeAttackDatas(true);
  213. // g.gameData.hasBeAttackUpdate = true;
  214. break;
  215. case 1://有新红包数据显示感叹号
  216. this.getRedPackets();
  217. break;
  218. case 2://有新的贡献产生显示感叹号
  219. g.gameData.updateMyFessiongDatas(result.data.fessionDatas);
  220. break;
  221. case 3://被好友挑战数据改变
  222. g.battleData.friendBattleUpdate = true;
  223. break;
  224. }
  225. }
  226. } else {
  227. if (g.CodeMsg[result.code]) {
  228. WindowManager.showTips(g.CodeMsg[result.code]);
  229. }
  230. }
  231. }
  232. /**
  233. * @param data 出售角色
  234. */
  235. public async onRoleCanSell(...data) {
  236. this.sellButton.active = data.length == 1;
  237. this.buyButton.active = data.length == 0;
  238. if (data.length && data[0] == 2) {
  239. this.sellButton.setScale(new Vec3(1.2, 1.2, 1.2));
  240. } else {
  241. this.sellButton.setScale(new Vec3(1, 1, 1));
  242. }
  243. }
  244. private isBuying = false;
  245. /**
  246. * 购买角色
  247. */
  248. public async onBuyRole() {
  249. if (this.isBuying || g.gameData.isMoveing) {
  250. return;
  251. }
  252. g.gameData.isBuying = true;
  253. this.isBuying = true;
  254. if (g.gameData.getMyLocalData(g.userData.id).myLocalGenerals.length > 11) {
  255. // WindowManager.showTips('位置不足');
  256. this.isBuying = false;
  257. g.gameData.isBuying = false;
  258. return;
  259. }
  260. if (g.gameData.buyPrice > g.userData.money) {
  261. // WindowManager.showTips("金币不足,看视频获得金币");
  262. let _lv = g.userData.lv > 0 ? g.userData.lv : 1;
  263. await WindowManager.open("Prefabs/Windows/视频金币界面", WindowOpenMode.CloseAndCover, [2, ConfigData.configMap.get("flyBox").prizeList[_lv - 1].prize * 5]);
  264. this.isBuying = false;
  265. g.gameData.isBuying = false;
  266. return;
  267. }
  268. let result = await this.http.send("/api/general/GeneralBuy", { generalLv: this.buyGeneralID });
  269. this.isBuying = false;
  270. if (!result.code) {
  271. g.gameData.buyLv = result.data.buyLv;
  272. g.gameData.buyPrice = result.data.buyPrice;
  273. g.userData.money = result.data.newMoney;
  274. // g.userData.moneyPower10 = result.data.newMoneyPower10;
  275. g.userData.money = g.userData.money < 0 ? 0 : g.userData.money;
  276. g.userData.lv = result.data.generalLv > g.userData.lv ? result.data.generalLv : g.userData.lv;
  277. this.buyHander.emit([{ lv: result.data.generalLv, index: -1, type: createType.Buy }]);
  278. } else {
  279. g.gameData.isBuying = false;
  280. if (result.code == 104) {
  281. this.syncMoney();
  282. } else {
  283. WindowManager.showTips(g.CodeMsg[result.code]);
  284. }
  285. }
  286. }
  287. /**打开切磋窗口 */
  288. public openBattleUIWindow() {
  289. if (g.userData.lv >= this.systemConfig.openBattleLv) {
  290. WindowManager.open("Prefabs/Battle/BattleUIWindow", WindowOpenMode.NotCloseAndAdd, { tabIndex: 0 });
  291. } else {
  292. WindowManager.showTips(this.systemConfig.openBattleLv + '级开放')
  293. }
  294. }
  295. /**打开神台窗口 */
  296. public openShentaiWindow() {
  297. if (g.userData.lv >= this.systemConfig.openShentaiLv) {
  298. this.getFission();
  299. WindowManager.open("Prefabs/ShenTai/ShentaiWindow", WindowOpenMode.NotCloseAndAdd);
  300. } else {
  301. WindowManager.showTips(this.systemConfig.openShentaiLv + '级开放')
  302. }
  303. }
  304. /**打开转盘窗口 */
  305. public openTurntableWindow() {
  306. if (g.userData.lv >= this.systemConfig.openTurntableLv) {
  307. WindowManager.open("Prefabs/Windows/Turntable", WindowOpenMode.CloseAndAdd);
  308. } else {
  309. WindowManager.showTips(this.systemConfig.openTurntableLv + '级开放')
  310. }
  311. }
  312. /**
  313. * 获取被攻击信息
  314. */
  315. private async getBeAttackDatas(isRedPoint: boolean = false) {
  316. let revengeResult = await this.http.send("/api/battle/GetRevenges");
  317. if (!revengeResult.code) {
  318. g.gameData.myAvengeDatas = revengeResult.data.revengeDatas;
  319. g.gameData.myAvengeDatas.sort((a: any, b: any): number => {
  320. return a.createTime > b.createTime ? -1 : 1;//优先最近战斗
  321. });
  322. } else {
  323. WindowManager.showTips(g.CodeMsg[revengeResult.code]);
  324. }
  325. let result = await this.http.send("/api/redDot/GetBeAttackDatas");
  326. if (!result.code) {
  327. g.gameData.beAttackDatas = result.data.beAttackDatas;
  328. if (isRedPoint) {//更新本地神将数据
  329. let generals = [];
  330. for (let i = 0; i < g.gameData.beAttackDatas.length; i++) {
  331. if (g.gameData.beAttackDatas[i].beAttackLossType == 3) {
  332. generals.push(Number(g.gameData.beAttackDatas[i].beAttackLossNum));
  333. }
  334. }
  335. if (generals.length > 0) {
  336. this.attackLossHander.emit([generals]);
  337. }
  338. }
  339. if (g.gameData.beAttackDatas.length) {
  340. WindowManager.open("Prefabs/Battle/BattleInfoWindow", WindowOpenMode.NotCloseAndAdd);//打开切磋消息
  341. }
  342. } else {
  343. WindowManager.showTips(g.CodeMsg[result.code]);
  344. }
  345. }
  346. /**
  347. * 获取红包信息
  348. */
  349. private async getRedPackets() {
  350. let getRedPacketsResult = await this.http.send("/api/redPacket/GetRedPackets");//获取红包信息
  351. if (!getRedPacketsResult.code) {
  352. g.gameData.myRedPacketDatas = getRedPacketsResult.data.redPacketList;
  353. g.gameData.hasNewRedPacketUpdate = true;
  354. } else {
  355. WindowManager.showTips(g.CodeMsg[getRedPacketsResult.code]);
  356. }
  357. }
  358. /**
  359. * 获取裂变信息
  360. */
  361. private async getFission() {
  362. let result = await this.http.send("/api/fission/GetFission");
  363. if (!result.code) {
  364. g.gameData.myFissionData = result.data.fissionDatas;
  365. }
  366. else {
  367. WindowManager.showTips(g.CodeMsg[result.code]);
  368. }
  369. }
  370. /**
  371. * 获取分红信息
  372. */
  373. private async getGameBonus() {
  374. let result = await this.http.send("/api/bonus/getGameBonus");
  375. if (!result.code) {
  376. g.gameData.gameBonus = result.data;
  377. let bonus = Math.floor(g.gameData.gameBonus.bonus / 100) / 100;
  378. let bonusStr = '';
  379. if (bonus < 10) {
  380. bonusStr = 'ss' + bonus;
  381. } else if (bonus < 100) {
  382. bonusStr = 's' + bonus;
  383. } else {
  384. bonusStr = '' + bonus;
  385. }
  386. this.bonusDayBitmapFont.string = bonusStr;
  387. }
  388. }
  389. private async getEachData() {
  390. let result = await this.http.send("/api/recommend/GetRecommendList");
  391. if (result.code == 0) {
  392. // platform.umUp("eachPush");//互推埋点
  393. for (let i = 0; i < result.data.plug.length; i++) {
  394. let d = result.data.plug[i];
  395. g.downLoadData.recommendData.push({
  396. id: d.placement_id,
  397. icon: d.icon,
  398. title: d.title,
  399. dec: d.introduction,
  400. url: d.downloadUrl,
  401. nebulaAppId: d.nebulaAppId,
  402. packageName: d.packageName
  403. });
  404. }
  405. }
  406. }
  407. }