| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- import { APP_FAVO_ADD, APP_FAVO_REMOVE, APP_GAME_HISTORY, APP_GAME_LAUNCH, APP_GAME_LIST } from "@scripts/api";
- import { Http } from "@scripts/request";
- import { checkResponse, success } from '@scripts/api/ApiHelper';
- import { IGameHistoryRes, IGameInfo, IGameLaunch, IGameListParams } from "./data";
- import { CurrencyType, GAME_TABS, GameTabType } from "@scripts/define/Define";
- const UPDATE_LIST_CD = 10 * 60 * 1000;
- interface IGameCaches {
- list: IGameInfo[],
- update_at: number,
- }
- class GameStore {
- private _allGameList: IGameCaches;
- private _recentMap: { [key: string]: IGameInfo[] } = {};
- private _favoriting: boolean = false;
- public get favoriting() { return this._favoriting; }
- public gameID = 0;
- public gameLink = "";
- clearGame() {
- this.gameID = 0;
- this.gameLink = "";
- }
- private getAllGames() {
- return this._allGameList?.list || [];
- }
- async reqAllGames() {
- const params: IGameListParams = {
- currency: "",
- }
- const res = await Http.call<IGameInfo[]>(APP_GAME_LIST, params as any);
- if (success(res)) {
- this._allGameList = {
- list: res.data,
- update_at: Date.now(),
- };
- return true;
- }
- return false;
- }
- async reqRecentGames(currency: CurrencyType) {
- const params: IGameListParams = {
- currency,
- recent: 1
- }
- const res = await Http.call<IGameInfo[]>(APP_GAME_LIST, params as any);
- if (success(res)) {
- this._recentMap[currency] = res.data;
- return true;
- }
- return false;
- }
- getRecentGames(currency: CurrencyType) {
- return this._recentMap[currency];
- }
- async reqFavoAdd(id: number) {
- if (this._favoriting) return;
- this._favoriting = true;
- const res = await Http.call(APP_FAVO_ADD, { id });
- this._favoriting = false;
- if (success(res)) {
- const gameList = this.getAllGames();
- const game = gameList.find(g => g.gameID === id);
- if (game) {
- game.isFavorite = 1;
- return game;
- }
- }
- }
- async reqFavoRemove(id: number) {
- if (this._favoriting) return;
- this._favoriting = true;
- const res = await Http.call(APP_FAVO_REMOVE, { id });
- this._favoriting = false;
- if (success(res)) {
- const gameList = this.getAllGames();
- const game = gameList.find(g => g.gameID === id);
- if (game) {
- game.isFavorite = 0;
- return game;
- }
- }
- }
- filterGames(tag: GameTabType, currency: CurrencyType) {
- const allGames = this._allGameList?.list || [];
- let gameList = allGames;
- switch (tag) {
- case GAME_TABS.Hot:
- case GAME_TABS.New:
- gameList = allGames.filter(v => v.tags.find(t => t.tagCode === tag));
- break;
- case GAME_TABS.Favs:
- gameList = allGames.filter(v => v.isFavorite === 1);
- break;
- case GAME_TABS.Recent:
- return this._recentMap[currency] || [];
- }
- return gameList.filter(v => v.currencies.includes(currency));
- }
- async reqLaunchGame(gameCode: string, currency: string) {
- const res = await Http.call<IGameLaunch>(APP_GAME_LAUNCH, { gameCode, currency });
- if (checkResponse(res)) {
- return res.data?.launchUrl;
- }
- return null;
- }
- onUserLogout() {
- this._recentMap = {};
- }
- onEnterGame(game: IGameInfo, currency: CurrencyType, url: string) {
- this.gameLink = url;
- this.gameID = game.gameID;
- if (this._recentMap[currency]) {
- const recents = this._recentMap[currency];
- const item = recents.find(item => item.gameID === game.gameID);
- if (!item) recents.push(game);
- this._recentMap[currency] = recents;
- }
- }
- async reqHistory(page: number, pageSize = 20) {
- let res = await Http.call<IGameHistoryRes>(APP_GAME_HISTORY, { page, pageSize })
- }
- }
- export const gameStore = new GameStore();
|