GameStore.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { APP_FAVO_ADD, APP_FAVO_REMOVE, APP_GAME_HISTORY, APP_GAME_LAUNCH, APP_GAME_LIST } from "@scripts/api";
  2. import { Http } from "@scripts/request";
  3. import { checkResponse, success } from '@scripts/api/ApiHelper';
  4. import { IGameHistory, IGameHistoryRes, IGameInfo, IGameLaunch, IGameListParams } from "./data";
  5. import { CurrencyType, GAME_TABS, GameTabType } from "@scripts/define/Define";
  6. const UPDATE_LIST_CD = 10 * 60 * 1000;
  7. const HISTORY_PAGE = 20;
  8. interface IGameCaches {
  9. list: IGameInfo[],
  10. update_at: number,
  11. }
  12. class GameStore {
  13. private _allGameList: IGameCaches;
  14. private _recentMap: { [key: string]: IGameInfo[] } = {};
  15. private _favoriting: boolean = false;
  16. public get favoriting() { return this._favoriting; }
  17. private _historyTotal = -1;
  18. private _gameHistory: IGameHistory[] = [];
  19. public get isFullHistory() { return this._historyTotal === this._gameHistory.length }
  20. public get gameHistory() { return this._gameHistory; }
  21. public gameID = 0;
  22. public gameLink = "";
  23. clearGame() {
  24. this.gameID = 0;
  25. this.gameLink = "";
  26. }
  27. private getAllGames() {
  28. return this._allGameList?.list || [];
  29. }
  30. async reqAllGames() {
  31. const params: IGameListParams = {
  32. currency: "",
  33. }
  34. const res = await Http.call<IGameInfo[]>(APP_GAME_LIST, params as any);
  35. if (success(res)) {
  36. this._allGameList = {
  37. list: res.data,
  38. update_at: Date.now(),
  39. };
  40. return true;
  41. }
  42. return false;
  43. }
  44. async reqRecentGames(currency: CurrencyType) {
  45. const params: IGameListParams = {
  46. currency,
  47. recent: 1
  48. }
  49. const res = await Http.call<IGameInfo[]>(APP_GAME_LIST, params as any);
  50. if (success(res)) {
  51. this._recentMap[currency] = res.data;
  52. return true;
  53. }
  54. return false;
  55. }
  56. getRecentGames(currency: CurrencyType) {
  57. return this._recentMap[currency];
  58. }
  59. async reqFavoAdd(id: number) {
  60. if (this._favoriting) return;
  61. this._favoriting = true;
  62. const res = await Http.call(APP_FAVO_ADD, { id });
  63. this._favoriting = false;
  64. if (success(res)) {
  65. const gameList = this.getAllGames();
  66. const game = gameList.find(g => g.gameID === id);
  67. if (game) {
  68. game.isFavorite = 1;
  69. return game;
  70. }
  71. }
  72. }
  73. async reqFavoRemove(id: number) {
  74. if (this._favoriting) return;
  75. this._favoriting = true;
  76. const res = await Http.call(APP_FAVO_REMOVE, { id });
  77. this._favoriting = false;
  78. if (success(res)) {
  79. const gameList = this.getAllGames();
  80. const game = gameList.find(g => g.gameID === id);
  81. if (game) {
  82. game.isFavorite = 0;
  83. return game;
  84. }
  85. }
  86. }
  87. filterGames(tag: GameTabType, currency: CurrencyType) {
  88. const allGames = this._allGameList?.list || [];
  89. let gameList = allGames;
  90. switch (tag) {
  91. case GAME_TABS.Hot:
  92. case GAME_TABS.New:
  93. gameList = allGames.filter(v => v.tags.find(t => t.tagCode === tag));
  94. break;
  95. case GAME_TABS.Favs:
  96. gameList = allGames.filter(v => v.isFavorite === 1);
  97. break;
  98. case GAME_TABS.Recent:
  99. return this._recentMap[currency] || [];
  100. }
  101. return gameList.filter(v => v.currencies.includes(currency));
  102. }
  103. async reqLaunchGame(gameCode: string, currency: string) {
  104. const res = await Http.call<IGameLaunch>(APP_GAME_LAUNCH, { gameCode, currency });
  105. if (checkResponse(res)) {
  106. return res.data?.launchUrl;
  107. }
  108. return null;
  109. }
  110. onUserLogout() {
  111. this._recentMap = {};
  112. }
  113. onEnterGame(game: IGameInfo, currency: CurrencyType, url: string) {
  114. this.gameLink = url;
  115. this.gameID = game.gameID;
  116. if (this._recentMap[currency]) {
  117. const recents = this._recentMap[currency];
  118. const item = recents.find(item => item.gameID === game.gameID);
  119. if (!item) recents.push(game);
  120. this._recentMap[currency] = recents;
  121. }
  122. }
  123. async reqHistory(page: number, pageSize = HISTORY_PAGE) {
  124. let res = await Http.call<IGameHistoryRes>(APP_GAME_HISTORY, { page, pageSize })
  125. if (success(res)) {
  126. const list = res.data.data || [];
  127. if (page === 1) {
  128. this._historyTotal = res.data.total;
  129. this._gameHistory = list;
  130. } else {
  131. this._gameHistory = [...this._gameHistory, ...list];
  132. }
  133. }
  134. }
  135. async reqNextHistory() {
  136. if (this.isFullHistory) return;
  137. const length = this._gameHistory.length;
  138. const page = Math.floor(length / HISTORY_PAGE) + 1;
  139. await this.reqHistory(page);
  140. }
  141. }
  142. export const gameStore = new GameStore();