GameStore.ts 4.0 KB

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