HomeView.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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('login:success', this.updateView, this);
  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. private onUpdateFirstPack() {
  112. const payStore = AppKernel.stores.payStore;
  113. this.btnFirst.active = payStore.firstPackVisible;
  114. }
  115. private updateGameModel(type: CurrencyType) {
  116. const mainStore = AppKernel.stores.mainStore;
  117. mainStore.currencyKind = type;
  118. this.gcModel.setActive(type === CURRENCY_TYPE.GC);
  119. this.scModel.setActive(type === CURRENCY_TYPE.SC);
  120. }
  121. private updateGameTabs(index: number) {
  122. if (index === this.gameTabIndex) return;
  123. if (!GameTabValus[index]) return;
  124. this.gameTabIndex = index;
  125. this.gameTabs.forEach((tab, i) => {
  126. tab.setActive(index === i);
  127. });
  128. }
  129. private async getRecentGames(tabIdx = -1) {
  130. tabIdx = tabIdx === -1 ? this.gameTabIndex : tabIdx;
  131. if (tabIdx !== 3) return;
  132. const mainStore = AppKernel.stores.mainStore;
  133. const gameStore = AppKernel.stores.gameStore;
  134. const games = gameStore.getRecentGames(mainStore.currencyKind);
  135. if (!games) {
  136. AppKernel.ui.loading(true);
  137. await gameStore.reqRecentGames(mainStore.currencyKind);
  138. AppKernel.ui.loading(false);
  139. }
  140. }
  141. private async updateGameList() {
  142. const currency = AppKernel.stores.mainStore.currencyKind;
  143. const tag = GameTabValus[this.gameTabIndex];
  144. if (tag === GAME_TABS.Recent) {
  145. await this.getRecentGames();
  146. }
  147. const isHome = tag === GAME_TABS.Home;
  148. const gameStore = AppKernel.stores.gameStore;
  149. const games = gameStore.filterGames(tag, currency);
  150. this.homeGamesNode.active = isHome;
  151. this.tabGamesNode.active = !isHome;
  152. this.gameDatas = games;
  153. const scrollView = isHome ? this.homeGamesScroll : this.tabGamesScroll;
  154. scrollView.numItems = games.length;
  155. scrollView.scrollTo(0, 0.2);
  156. if (!isHome) {
  157. for (const key in this.tabGameFlags) {
  158. const flag = this.tabGameFlags[key];
  159. flag.active = key === tag;
  160. }
  161. }
  162. }
  163. onGameItemRender(item: Node, index: number) {
  164. const data = this.gameDatas[index];
  165. if (!item["_gameItem"]) {
  166. item["_gameItem"] = item.getComponent(GameItem);
  167. }
  168. const gameItem = item['_gameItem'] as GameItem;
  169. gameItem.updateItem(data, index);
  170. }
  171. private onHomeTabClick() {
  172. this.updateGameTabs(0);
  173. this.updateGameList();
  174. }
  175. private onHotTabClick() {
  176. this.updateGameTabs(1);
  177. this.updateGameList();
  178. }
  179. private onNewTabClick() {
  180. this.updateGameTabs(2);
  181. this.updateGameList();
  182. }
  183. private onRecentTabClick() {
  184. this.updateGameTabs(3);
  185. this.updateGameList();
  186. }
  187. private onFavsTabClick() {
  188. this.updateGameTabs(4);
  189. this.updateGameList();
  190. }
  191. private onBtnDailyClick() {
  192. }
  193. private onBtnTaskClick() {
  194. }
  195. private onBtnRedeemClick() {
  196. const coinModel = AppKernel.stores.mainStore.currencyKind;
  197. if (coinModel !== CURRENCY_TYPE.SC) {
  198. AppKernel.ui.toast("Redeem is only available in SC mode.");
  199. return;
  200. }
  201. AppKernel.ui.open(UIViewID.UIWithdraw);
  202. }
  203. private onBtnWeeklyClick() {
  204. }
  205. private onBtnVipClick() {
  206. }
  207. private onBtnMenuClick() {
  208. this.menuPopup.open();
  209. }
  210. private onBtnServiceClick() {
  211. }
  212. private onBtnInviteClick() {
  213. AppKernel.ui.open(UIViewID.UIInvite);
  214. }
  215. private onBtnFirstClick() {
  216. AppKernel.ui.open(UIViewID.UIFirstPack);
  217. }
  218. private onBtnBuyClick() {
  219. AppKernel.ui.open(UIViewID.StoreView);
  220. }
  221. private onUserNodeClick() {
  222. }
  223. private onGCModelClick() {
  224. const coinModel = AppKernel.stores.mainStore.currencyKind;
  225. if (coinModel === CURRENCY_TYPE.GC) return;
  226. this.updateGameModel(CURRENCY_TYPE.GC);
  227. this.updateGameList();
  228. }
  229. private onSCModelClick() {
  230. const coinModel = AppKernel.stores.mainStore.currencyKind;
  231. if (coinModel === CURRENCY_TYPE.SC) return;
  232. this.updateGameModel(CURRENCY_TYPE.SC);
  233. this.updateGameList();
  234. }
  235. private updateUserInfo() {
  236. const mainStore = AppKernel.stores.mainStore;
  237. const userInfo = mainStore.userInfo;
  238. this.userNode.lblVip.string = formatVipLv(userInfo.vip);
  239. this.scModel.lblValue.string = formatSCNum(mainStore.scScore);
  240. this.gcModel.lblValue.string = formatGCNum(mainStore.gcScore);
  241. }
  242. private updateView() {
  243. const mainStore = AppKernel.stores.mainStore;
  244. this.updateUserInfo();
  245. this.updateGameTabs(0);
  246. this.updateGameModel(mainStore.currencyKind);
  247. this.updateGameList();
  248. this.bannerNode.init(5);
  249. this.onUpdateFirstPack();
  250. }
  251. private onBtnMailClick() {
  252. AppKernel.ui.open(UIViewID.MailView);
  253. }
  254. }
  255. class UserNode {
  256. lblVip: Label;
  257. sptAvatar: Sprite;
  258. constructor(readonly node: Node) {
  259. this.lblVip = find("lblLv", node).getComponent(Label);
  260. this.sptAvatar = find("sptAvatar", node).getComponent(Sprite);
  261. }
  262. }
  263. class CoinModel {
  264. lblValue: Label;
  265. private sptIcon: Sprite;
  266. private sptActive: Sprite;
  267. constructor(readonly node: Node, lblName: string) {
  268. this.lblValue = find(lblName, node.parent).getComponent(Label);
  269. this.sptActive = find("sptActive", node).getComponent(Sprite);
  270. this.sptIcon = find("sptIcon", node).getComponent(Sprite);
  271. }
  272. setActive(active) {
  273. setAlpha(this.sptActive, active ? 1 : 0);
  274. setAlpha(this.lblValue, active ? 1 : 0.54);
  275. setAlpha(this.sptIcon, active ? 1 : 0.54);
  276. }
  277. }
  278. class GameTab {
  279. private sptActive: UIOpacity;
  280. constructor(readonly node: Node) {
  281. this.sptActive = find("sptActive", node).getComponent(UIOpacity);
  282. }
  283. setActive(active: boolean) {
  284. this.sptActive.opacity = active ? 255 : 0;
  285. }
  286. }
  287. class MenuPopup {
  288. private togSound: Toggle;
  289. private togMusic: Toggle;
  290. constructor(readonly node: Node) {
  291. node.active = false;
  292. this.togSound = find("content/togSound", node).getComponent(Toggle);
  293. this.togMusic = find("content/togMusic", node).getComponent(Toggle);
  294. this.togSound.isChecked = AppKernel.sound.soundEnabled;
  295. this.togMusic.isChecked = AppKernel.sound.musicEnabled;
  296. addTogClick(this.togSound, this.onTogSoundClick, this);
  297. addTogClick(this.togMusic, this.onTogMusicClick, this)
  298. const btnFAQ = find("content/btnFAQ", node);
  299. const btnPhone = find("content/btnPhone", node);
  300. const btnEmail = find("content/btnEmail", node);
  301. const btnVerify = find("content/btnVerify", node);
  302. const btnSafe = find("content/btnSafe", node);
  303. const btnHistory = find("content/btnHistory", node);
  304. const btnInfo = find("content/btnInfo", node);
  305. const btnLogout = find("content/btnLogout", node);
  306. addBtnClick(btnFAQ, this.onBtnFAQClick, this);
  307. addBtnClick(btnPhone, this.onBtnPhoneClick, this);
  308. addBtnClick(btnEmail, this.onBtnEmailClick, this);
  309. addBtnClick(btnVerify, this.onBtnVerifyClick, this);
  310. addBtnClick(btnSafe, this.onBtnSafeClick, this);
  311. addBtnClick(btnHistory, this.onBtnHistoryClick, this);
  312. addBtnClick(btnInfo, this.onBtnInfoClick, this);
  313. addBtnClick(btnLogout, this.onBtnLogoutClick, this);
  314. const mask = find("mask", node);
  315. mask.on(Node.EventType.TOUCH_END, this.onMaskClick, this);
  316. }
  317. private onTogSoundClick(tog: Toggle) {
  318. if (AppKernel.sound.soundEnabled !== tog.isChecked) {
  319. AppKernel.sound.soundEnabled = tog.isChecked;
  320. }
  321. }
  322. private onTogMusicClick(tog: Toggle) {
  323. if (AppKernel.sound.musicEnabled !== tog.isChecked) {
  324. AppKernel.sound.musicEnabled = tog.isChecked;
  325. }
  326. }
  327. private onMaskClick() {
  328. this.node.active = false;
  329. }
  330. private onBtnFAQClick() {
  331. }
  332. private onBtnPhoneClick() {
  333. }
  334. private onBtnEmailClick() {
  335. }
  336. private onBtnVerifyClick() {
  337. }
  338. private onBtnSafeClick() {
  339. }
  340. private onBtnHistoryClick() {
  341. AppKernel.ui.open(UIViewID.PlayHistory);
  342. }
  343. private onBtnInfoClick() {
  344. AppKernel.ui.open(UIViewID.GamePolicyView);
  345. }
  346. private onBtnLogoutClick() {
  347. AppKernel.ui.dialog({
  348. message: AppKernel.i18n.t("home.logout.content"),
  349. title: AppKernel.i18n.t("home.logout.title"),
  350. showClose: true,
  351. callBack: async (res) => {
  352. if (res) {
  353. this.node.active = false;
  354. AppKernel.appLobby.logOut();
  355. }
  356. return true;
  357. }
  358. })
  359. }
  360. open() {
  361. this.node.active = true;
  362. this.togMusic.isChecked = true;
  363. this.togSound.isChecked = true;
  364. }
  365. }