GameStore.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { APP_FAVO_ADD, APP_FAVO_REMOVE, APP_GAME_LAUNCH, APP_GAME_LIST, success } from "@scripts/api";
  2. import { Http } from "@scripts/request";
  3. import { IGameInfo, IGameLaunch, IGameListParams } from "./data";
  4. import { CURRENCY_TYPE, CurrencyType, GAME_TABS, GameTabType } from "@scripts/define/Define";
  5. const UPDATE_LIST_CD = 10 * 60 * 1000;
  6. interface IGameCaches {
  7. list: IGameInfo[],
  8. update_at: number,
  9. }
  10. class GameStore {
  11. private _gameListMap: { [key: string]: IGameCaches } = {};
  12. private _allGameList: IGameCaches;
  13. private _currencyNow: CurrencyType;
  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. favorite: 0
  29. }
  30. const res = await Http.call<IGameInfo[]>(APP_GAME_LIST, params as any);
  31. if (success(res)) {
  32. this._allGameList = {
  33. list: res.data,
  34. update_at: Date.now(),
  35. };
  36. return true;
  37. }
  38. return false;
  39. }
  40. async reqGameList(currency: CurrencyType, cache = true) {
  41. this._currencyNow = currency;
  42. const gameCache = this._gameListMap[currency];
  43. if (gameCache && cache) {
  44. if (Date.now() - gameCache.update_at < UPDATE_LIST_CD) {
  45. return true;
  46. }
  47. }
  48. const params: IGameListParams = {
  49. currency: currency,
  50. favorite: 0
  51. }
  52. const res = await Http.call<IGameInfo[]>(APP_GAME_LIST, params as any);
  53. if (success(res)) {
  54. this._gameListMap[currency] = {
  55. list: res.data,
  56. update_at: Date.now(),
  57. };
  58. return true;
  59. }
  60. return false;
  61. }
  62. async reqFavoAdd(id: number) {
  63. if (this._favoriting) return;
  64. this._favoriting = true;
  65. const res = await Http.call(APP_FAVO_ADD, { id });
  66. this._favoriting = false;
  67. if (success(res)) {
  68. const gameList = this.getAllGames();
  69. const game = gameList.find(g => g.gameID === id);
  70. if (game) {
  71. game.isFavorite = 1;
  72. return game;
  73. }
  74. }
  75. }
  76. async reqFavoRemove(id: number) {
  77. if (this._favoriting) return;
  78. this._favoriting = true;
  79. const res = await Http.call(APP_FAVO_REMOVE, { id });
  80. this._favoriting = false;
  81. if (success(res)) {
  82. const gameList = this.getAllGames();
  83. const game = gameList.find(g => g.gameID === id);
  84. if (game) {
  85. game.isFavorite = 0;
  86. return game;
  87. }
  88. }
  89. }
  90. filterGames(tag: GameTabType, currency: CurrencyType) {
  91. const allGames = this._allGameList?.list || [];
  92. let gameList = allGames;
  93. switch (tag) {
  94. case GAME_TABS.Hot:
  95. case GAME_TABS.New:
  96. gameList = allGames.filter(v => v.tags.find(t => t.tagCode === tag));
  97. break;
  98. case GAME_TABS.Favs:
  99. gameList = allGames.filter(v => v.isFavorite === 1);
  100. break;
  101. }
  102. return gameList.filter(v => v.currencies.includes(currency));
  103. }
  104. async reqLaunchGame(gameCode: string, currency: string) {
  105. const res = await Http.call<IGameLaunch>(APP_GAME_LAUNCH, { gameCode, currency });
  106. if (res && res.data && res.data.launchUrl) {
  107. return res.data.launchUrl;
  108. }
  109. return null;
  110. }
  111. }
  112. export const gameStore = new GameStore();