GameStore.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { APP_FAVO_ADD, APP_FAVO_REMOVE, APP_GAME_LIST, success } from "@scripts/api";
  2. import { Http } from "@scripts/request";
  3. import { IGameInfo, IGameListParams } from "./data";
  4. import { CurrencyType } 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 _currencyNow: CurrencyType;
  13. private _favoriting: boolean = false;
  14. public get favoriting() { return this._favoriting; }
  15. public gameID = 0;
  16. public gameLink = "";
  17. clearGame() {
  18. this.gameID = 0;
  19. this.gameLink = "";
  20. }
  21. private findGameList(key = '') {
  22. if (!key) key = this._currencyNow;
  23. return this._gameListMap[key]?.list || [];
  24. }
  25. async reqGameList(currency: CurrencyType, cache = true) {
  26. this._currencyNow = currency;
  27. const gameCache = this._gameListMap[currency];
  28. if (gameCache && cache) {
  29. if (Date.now() - gameCache.update_at < UPDATE_LIST_CD) {
  30. return true;
  31. }
  32. }
  33. const params: IGameListParams = {
  34. currency: currency,
  35. favorite: 0
  36. }
  37. const res = await Http.call<IGameInfo[]>(APP_GAME_LIST, params as any);
  38. if (success(res)) {
  39. this._gameListMap[currency] = {
  40. list: res.data,
  41. update_at: Date.now(),
  42. };
  43. return true;
  44. }
  45. return false;
  46. }
  47. /**
  48. * 获取游戏列表
  49. * @param tagId 0 不筛选标签
  50. * @returns
  51. */
  52. getGameList(tagId = 0) {
  53. const gameList = this.findGameList();
  54. if (tagId === 0) {
  55. return gameList;
  56. }
  57. return gameList.map(v => {
  58. if (v.tags.find(t => t.tagId === tagId)) {
  59. return v;
  60. }
  61. })
  62. }
  63. getFavorites() {
  64. const gameList = this.findGameList();
  65. return gameList.map(v => v.isFavorite === 1);
  66. }
  67. async reqFavoAdd(id: number) {
  68. if (this._favoriting) return;
  69. this._favoriting = true;
  70. const res = await Http.call(APP_FAVO_ADD, { id });
  71. this._favoriting = false;
  72. if (success(res)) {
  73. const gameList = this.findGameList();
  74. const game = gameList.find(g => g.gameID === id);
  75. if (game) {
  76. game.isFavorite = 1;
  77. return game;
  78. }
  79. }
  80. }
  81. async reqFavoRemove(id: number) {
  82. if (this._favoriting) return;
  83. this._favoriting = true;
  84. const res = await Http.call(APP_FAVO_REMOVE, { id });
  85. this._favoriting = false;
  86. if (success(res)) {
  87. const gameList = this.findGameList();
  88. const game = gameList.find(g => g.gameID === id);
  89. if (game) {
  90. game.isFavorite = 0;
  91. return game;
  92. }
  93. }
  94. }
  95. }
  96. export const gameStore = new GameStore();