HomeView.ts 15 KB

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