HomeView.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import { _decorator, find, Label, Node, Prefab, Sprite, Toggle, UIOpacity, Widget } from 'cc';
  2. import { AppKernel } from '@scripts/app/AppKernel';
  3. import { UIView } from '@scripts/framework/ui/UIView';
  4. import { UIViewID } from '@scripts/define/UIDefine';
  5. import { addBtnClick, addTogClick, setAlpha } from '@scripts/utils/UIHelper';
  6. import { formatVipLv } from '@scripts/utils/VipHelper';
  7. import { CURRENCY_TYPE, CurrencyType, GAME_TABS } from '@scripts/define/Define';
  8. import { UISwiper } from '@scripts/components/swiper/UISwiper';
  9. import VirtualList from '@scripts/components/virtualList/VirtualList';
  10. import { IGameInfo } from '@scripts/store/gameStore/data';
  11. import { GameItem } from './GameItem';
  12. import { RED_POINTS } from '@scripts/app/AppRedpoint';
  13. import { formatGCNum, formatSCNum } from '@scripts/utils/Helper';
  14. import { HomeNotices } from './HomeNotices';
  15. import { setSpriteFrame } from '@scripts/utils/ResHelper';
  16. const { ccclass, property } = _decorator;
  17. const GameTabValus = [
  18. GAME_TABS.Home,
  19. GAME_TABS.Hot,
  20. GAME_TABS.New,
  21. GAME_TABS.Recent,
  22. GAME_TABS.Favs,
  23. ]
  24. @ccclass('HomeView')
  25. export class HomeView extends UIView {
  26. readonly viewId = UIViewID.HomeView;
  27. @property(UISwiper)
  28. bannerNode: UISwiper;
  29. @property(Node)
  30. homeGamesNode: Node;
  31. @property(VirtualList)
  32. homeGamesScroll: VirtualList;
  33. @property(Node)
  34. tabGamesNode: Node;
  35. @property(VirtualList)
  36. tabGamesScroll: VirtualList;
  37. @property(HomeNotices)
  38. homeNotices: HomeNotices;
  39. private userNode: UserNode;
  40. private gcModel: CoinModel;
  41. private scModel: CoinModel;
  42. private gameTabs: GameTab[] = [];
  43. private tabGameFlags: { [key: string]: Node } = {};
  44. private gameTabIndex = -1;
  45. private gameDatas: IGameInfo[] = [];
  46. private menuPopup: MenuPopup;
  47. private btnFirst: Node;
  48. protected onCreate(): void | Promise<void> {
  49. AppKernel.events.on('user:update', this.updateUserInfo, this);
  50. AppKernel.events.on('firstPack:update', this.onUpdateFirstPack, this);
  51. this.homeGamesScroll.renderEvent.target = this.node;
  52. this.homeGamesScroll.renderEvent.component = "HomeView";
  53. this.homeGamesScroll.renderEvent.handler = "onGameItemRender";
  54. this.tabGamesScroll.renderEvent.target = this.node;
  55. this.tabGamesScroll.renderEvent.component = "HomeView";
  56. this.tabGamesScroll.renderEvent.handler = "onGameItemRender";
  57. this.tabGameFlags = {
  58. [GAME_TABS.Hot]: find("tabGames/sptHot", this.node),
  59. [GAME_TABS.New]: find("tabGames/sptNew", this.node),
  60. [GAME_TABS.Recent]: find("tabGames/sptRecent", this.node),
  61. [GAME_TABS.Favs]: find("tabGames/sptFavs", this.node),
  62. }
  63. this.menuPopup = new MenuPopup(find("menuNode", this.node));
  64. this.userNode = new UserNode(find("topNode/userNode", this.node));
  65. this.gcModel = new CoinModel(find("topNode/coinModel/gcNode", this.node), "lblGC");
  66. this.scModel = new CoinModel(find("topNode/coinModel/scNode", this.node), "lblSC");
  67. addBtnClick(this.gcModel.node, this.onGCModelClick, this);
  68. addBtnClick(this.scModel.node, this.onSCModelClick, this);
  69. const homeTab = new GameTab(find("tabsNode/btnHome", this.node));
  70. const hotTab = new GameTab(find("tabsNode/btnHot", this.node));
  71. const newTab = new GameTab(find("tabsNode/btnNew", this.node));
  72. const recentTab = new GameTab(find("tabsNode/btnRecent", this.node));
  73. const favsTab = new GameTab(find("tabsNode/btnFavs", this.node));
  74. this.gameTabs = [homeTab, hotTab, newTab, recentTab, favsTab];
  75. addBtnClick(homeTab.node, this.onHomeTabClick, this);
  76. addBtnClick(hotTab.node, this.onHotTabClick, this);
  77. addBtnClick(newTab.node, this.onNewTabClick, this);
  78. addBtnClick(recentTab.node, this.onRecentTabClick, this);
  79. addBtnClick(favsTab.node, this.onFavsTabClick, this);
  80. const btnBuy = find("topNode/btnBuy", this.node);
  81. this.btnFirst = find("topNode/btnFirst", this.node);
  82. const btnInvite = find("topNode/btnInvite", this.node);
  83. const btnMail = find("topNode/btnMail", this.node);
  84. const btnService = find("topNode/btnService", this.node);
  85. const btnMenu = find("topNode/btnMenu", this.node);
  86. const btnTask = find("bottomNode/btnTask", this.node);
  87. const btnDaily = find("bottomNode/btnDaily", this.node);
  88. const btnRedeem = find("bottomNode/btnRedeem", this.node);
  89. const btnWeekly = find("bottomNode/btnWeekly", this.node);
  90. const btnVip = find("bottomNode/btnVip", this.node);
  91. addBtnClick(this.userNode.node, this.onUserNodeClick, this);
  92. addBtnClick(this.gcModel.node, this.onGCModelClick, this);
  93. addBtnClick(this.scModel.node, this.onSCModelClick, this);
  94. addBtnClick(btnBuy, this.onBtnBuyClick, this);
  95. addBtnClick(this.btnFirst, this.onBtnFirstClick, this);
  96. addBtnClick(btnInvite, this.onBtnInviteClick, this);
  97. addBtnClick(btnMail, this.onBtnMailClick, this);
  98. addBtnClick(btnService, this.onBtnServiceClick, this);
  99. addBtnClick(btnMenu, this.onBtnMenuClick, this);
  100. addBtnClick(btnTask, this.onBtnTaskClick, this);
  101. addBtnClick(btnDaily, this.onBtnDailyClick, this);
  102. addBtnClick(btnRedeem, this.onBtnRedeemClick, this);
  103. addBtnClick(btnWeekly, this.onBtnWeeklyClick, this);
  104. addBtnClick(btnVip, this.onBtnVipClick, this);
  105. const mailPoint = find("redPoint", btnMail);
  106. const taskPoint = find("redPoint", btnTask);
  107. const dailyPoint = find("redPoint", btnDaily);
  108. AppKernel.redpoint.on(RED_POINTS.Mail, mailPoint, this);
  109. AppKernel.redpoint.on(RED_POINTS.DailyTask, taskPoint, this);
  110. AppKernel.redpoint.on(RED_POINTS.DailyReward, dailyPoint, this);
  111. }
  112. protected onOpen(_params: unknown) {
  113. this.updateView();
  114. this.homeNotices?.updateNotices();
  115. }
  116. private onUpdateFirstPack() {
  117. const payStore = AppKernel.stores.payStore;
  118. this.btnFirst.active = payStore.firstPackVisible;
  119. }
  120. private updateGameModel(type: CurrencyType) {
  121. const mainStore = AppKernel.stores.mainStore;
  122. mainStore.currencyKind = type;
  123. this.gcModel.setActive(type === CURRENCY_TYPE.GC);
  124. this.scModel.setActive(type === CURRENCY_TYPE.SC);
  125. }
  126. private updateGameTabs(index: number) {
  127. if (index === this.gameTabIndex) return;
  128. if (!GameTabValus[index]) return;
  129. this.gameTabIndex = index;
  130. this.gameTabs.forEach((tab, i) => {
  131. tab.setActive(index === i);
  132. });
  133. }
  134. private async getRecentGames(tabIdx = -1) {
  135. tabIdx = tabIdx === -1 ? this.gameTabIndex : tabIdx;
  136. if (tabIdx !== 3) return;
  137. const mainStore = AppKernel.stores.mainStore;
  138. const gameStore = AppKernel.stores.gameStore;
  139. const games = gameStore.getRecentGames(mainStore.currencyKind);
  140. if (!games) {
  141. AppKernel.ui.loading(true);
  142. await gameStore.reqRecentGames(mainStore.currencyKind);
  143. AppKernel.ui.loading(false);
  144. }
  145. }
  146. private async updateGameList() {
  147. const currency = AppKernel.stores.mainStore.currencyKind;
  148. const tag = GameTabValus[this.gameTabIndex];
  149. if (tag === GAME_TABS.Recent) {
  150. await this.getRecentGames();
  151. }
  152. const isHome = tag === GAME_TABS.Home;
  153. const gameStore = AppKernel.stores.gameStore;
  154. const games = gameStore.filterGames(tag, currency);
  155. this.homeGamesNode.active = isHome;
  156. this.tabGamesNode.active = !isHome;
  157. this.gameDatas = games;
  158. const scrollView = isHome ? this.homeGamesScroll : this.tabGamesScroll;
  159. scrollView.numItems = games.length;
  160. scrollView.scrollTo(0, 0.2);
  161. if (!isHome) {
  162. for (const key in this.tabGameFlags) {
  163. const flag = this.tabGameFlags[key];
  164. flag.active = key === tag;
  165. }
  166. }
  167. }
  168. onGameItemRender(item: Node, index: number) {
  169. const data = this.gameDatas[index];
  170. if (!item["_gameItem"]) {
  171. item["_gameItem"] = item.getComponent(GameItem);
  172. }
  173. const gameItem = item['_gameItem'] as GameItem;
  174. gameItem.updateItem(data, index);
  175. }
  176. private onHomeTabClick() {
  177. this.updateGameTabs(0);
  178. this.updateGameList();
  179. }
  180. private onHotTabClick() {
  181. this.updateGameTabs(1);
  182. this.updateGameList();
  183. }
  184. private onNewTabClick() {
  185. this.updateGameTabs(2);
  186. this.updateGameList();
  187. }
  188. private onRecentTabClick() {
  189. this.updateGameTabs(3);
  190. this.updateGameList();
  191. }
  192. private onFavsTabClick() {
  193. this.updateGameTabs(4);
  194. this.updateGameList();
  195. }
  196. private onBtnDailyClick() {
  197. AppKernel.ui.open(UIViewID.CheckInView);
  198. }
  199. private onBtnTaskClick() {
  200. AppKernel.ui.open(UIViewID.SeasonPassView);
  201. }
  202. private onBtnRedeemClick() {
  203. const coinModel = AppKernel.stores.mainStore.currencyKind;
  204. if (coinModel !== CURRENCY_TYPE.SC) {
  205. AppKernel.ui.toast("Redeem is only available in SC mode.");
  206. return;
  207. }
  208. AppKernel.ui.open(UIViewID.RedeemView);
  209. }
  210. private onBtnWeeklyClick() {
  211. }
  212. private onBtnVipClick() {
  213. AppKernel.ui.open(UIViewID.ClubView);
  214. }
  215. private onBtnMenuClick() {
  216. this.menuPopup.open();
  217. }
  218. private onBtnServiceClick() {
  219. }
  220. private onBtnInviteClick() {
  221. AppKernel.ui.open(UIViewID.UIInvite);
  222. }
  223. private onBtnFirstClick() {
  224. AppKernel.ui.open(UIViewID.UIFirstPack);
  225. }
  226. private onBtnBuyClick() {
  227. AppKernel.ui.open(UIViewID.StoreView);
  228. }
  229. private onUserNodeClick() {
  230. AppKernel.ui.open(UIViewID.UserInfoView);
  231. }
  232. private onGCModelClick() {
  233. const coinModel = AppKernel.stores.mainStore.currencyKind;
  234. if (coinModel === CURRENCY_TYPE.GC) return;
  235. this.updateGameModel(CURRENCY_TYPE.GC);
  236. this.updateGameList();
  237. }
  238. private onSCModelClick() {
  239. const coinModel = AppKernel.stores.mainStore.currencyKind;
  240. if (coinModel === CURRENCY_TYPE.SC) return;
  241. this.updateGameModel(CURRENCY_TYPE.SC);
  242. this.updateGameList();
  243. }
  244. private updateUserInfo() {
  245. const mainStore = AppKernel.stores.mainStore;
  246. const userInfo = mainStore.userInfo;
  247. this.userNode.lblVip.string = formatVipLv(userInfo.vip);
  248. const { FaceID, Level } = userInfo;
  249. if (FaceID >= 1) setSpriteFrame(this.userNode.sptAvatar, "common/avatars/" + FaceID);
  250. this.userNode.lblVip.string = "LV." + Level;
  251. this.scModel.lblValue.string = formatSCNum(mainStore.scScore);
  252. this.gcModel.lblValue.string = formatGCNum(mainStore.gcScore);
  253. }
  254. private updateView() {
  255. const mainStore = AppKernel.stores.mainStore;
  256. this.updateUserInfo();
  257. this.updateGameTabs(0);
  258. this.updateGameModel(mainStore.currencyKind);
  259. this.updateGameList();
  260. this.bannerNode.init(5);
  261. this.onUpdateFirstPack();
  262. }
  263. private onBtnMailClick() {
  264. AppKernel.ui.open(UIViewID.Mail);
  265. }
  266. }
  267. class UserNode {
  268. lblVip: Label;
  269. sptAvatar: Sprite;
  270. constructor(readonly node: Node) {
  271. this.lblVip = find("lblLv", node).getComponent(Label);
  272. this.sptAvatar = find("sptAvatar", node).getComponent(Sprite);
  273. }
  274. }
  275. class CoinModel {
  276. lblValue: Label;
  277. private sptIcon: Sprite;
  278. private sptActive: Sprite;
  279. constructor(readonly node: Node, lblName: string) {
  280. this.lblValue = find(lblName, node.parent).getComponent(Label);
  281. this.sptActive = find("sptActive", node).getComponent(Sprite);
  282. this.sptIcon = find("sptIcon", node).getComponent(Sprite);
  283. }
  284. setActive(active) {
  285. setAlpha(this.sptActive, active ? 1 : 0);
  286. setAlpha(this.lblValue, active ? 1 : 0.54);
  287. setAlpha(this.sptIcon, active ? 1 : 0.54);
  288. }
  289. }
  290. class GameTab {
  291. private sptActive: UIOpacity;
  292. constructor(readonly node: Node) {
  293. this.sptActive = find("sptActive", node).getComponent(UIOpacity);
  294. }
  295. setActive(active: boolean) {
  296. this.sptActive.opacity = active ? 255 : 0;
  297. }
  298. }
  299. class MenuPopup {
  300. private togSound: Toggle;
  301. private togMusic: Toggle;
  302. constructor(readonly node: Node) {
  303. node.active = false;
  304. this.togSound = find("content/togSound", node).getComponent(Toggle);
  305. this.togMusic = find("content/togMusic", node).getComponent(Toggle);
  306. this.togSound.isChecked = AppKernel.sound.soundEnabled;
  307. this.togMusic.isChecked = AppKernel.sound.musicEnabled;
  308. addTogClick(this.togSound, this.onTogSoundClick, this);
  309. addTogClick(this.togMusic, this.onTogMusicClick, this)
  310. const btnFAQ = find("content/btnFAQ", node);
  311. const btnPhone = find("content/btnPhone", node);
  312. const btnEmail = find("content/btnEmail", node);
  313. const btnVerify = find("content/btnVerify", node);
  314. const btnSafe = find("content/btnSafe", node);
  315. const btnHistory = find("content/btnHistory", node);
  316. const btnInfo = find("content/btnInfo", node);
  317. const btnLogout = find("content/btnLogout", node);
  318. addBtnClick(btnFAQ, this.onBtnFAQClick, this);
  319. addBtnClick(btnPhone, this.onBtnPhoneClick, this);
  320. addBtnClick(btnEmail, this.onBtnEmailClick, this);
  321. addBtnClick(btnVerify, this.onBtnVerifyClick, this);
  322. addBtnClick(btnSafe, this.onBtnSafeClick, this);
  323. addBtnClick(btnHistory, this.onBtnHistoryClick, this);
  324. addBtnClick(btnInfo, this.onBtnInfoClick, this);
  325. addBtnClick(btnLogout, this.onBtnLogoutClick, this);
  326. const mask = find("mask", node);
  327. mask.on(Node.EventType.TOUCH_END, this.onMaskClick, this);
  328. }
  329. private onTogSoundClick(tog: Toggle) {
  330. if (AppKernel.sound.soundEnabled !== tog.isChecked) {
  331. AppKernel.sound.soundEnabled = tog.isChecked;
  332. }
  333. }
  334. private onTogMusicClick(tog: Toggle) {
  335. if (AppKernel.sound.musicEnabled !== tog.isChecked) {
  336. AppKernel.sound.musicEnabled = tog.isChecked;
  337. }
  338. }
  339. private onMaskClick() {
  340. this.node.active = false;
  341. }
  342. private onBtnFAQClick() {
  343. }
  344. private onBtnPhoneClick() {
  345. }
  346. private onBtnEmailClick() {
  347. }
  348. private onBtnVerifyClick() {
  349. }
  350. private onBtnSafeClick() {
  351. }
  352. private onBtnHistoryClick() {
  353. AppKernel.ui.open(UIViewID.PlayHistory);
  354. }
  355. private onBtnInfoClick() {
  356. AppKernel.ui.open(UIViewID.GamePolicyView);
  357. }
  358. private onBtnLogoutClick() {
  359. AppKernel.ui.dialog({
  360. message: AppKernel.i18n.t("home.logout.content"),
  361. title: AppKernel.i18n.t("home.logout.title"),
  362. showClose: true,
  363. callBack: async (res) => {
  364. if (res) {
  365. this.node.active = false;
  366. AppKernel.appLobby.logOut();
  367. }
  368. return true;
  369. }
  370. })
  371. }
  372. open() {
  373. this.node.active = true;
  374. this.togMusic.isChecked = true;
  375. this.togSound.isChecked = true;
  376. }
  377. }