HomeView.ts 15 KB

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