Skip to content

Commit

Permalink
[Issue-54] Mythical_Game - Do not load event to play
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiendekaco committed Dec 10, 2024
1 parent 9e89d26 commit 2253cfc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/extension-koni-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@subwallet/keyring": "^0.1.5",
"@subwallet/react-ui": "5.1.2-b77",
"@subwallet/ui-keyring": "^0.1.5",
"@types/pako": "^2.0.3",
"@walletconnect/types": "^2.13.1",
"bip39": "^3.1.0",
"boring-avatars": "^1.7.0",
Expand All @@ -55,6 +56,7 @@
"humanize-duration": "^3.31.0",
"i18next": "^21.9.2",
"lodash": "^4.17.21",
"pako": "^2.1.0",
"phosphor-react": "^1.4.1",
"querystring": "^0.2.1",
"rc-input": "^0.2.2",
Expand Down
36 changes: 29 additions & 7 deletions packages/extension-koni-ui/src/connector/booka/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import { signRaw } from '@subwallet/extension-koni-ui/messaging';
import { populateTemplateString } from '@subwallet/extension-koni-ui/utils';
import { formatDateFully } from '@subwallet/extension-koni-ui/utils/date';
import fetch from 'cross-fetch';
import { deflate, inflate } from 'pako';
import { BehaviorSubject } from 'rxjs';

import { stringToU8a, u8aToString } from '@polkadot/util';

export const DEFAULT_INIT_DATA = process.env.DEFAULT_INIT_DATA;
export const GAME_API_HOST = process.env.GAME_API_HOST || 'https://game-api.anhmtv.xyz';
export const MYTHICAL_API_HOST = process.env.MYTHICAL_API_HOST || 'https://nflrivals.client.mythical.dev';
Expand All @@ -38,11 +41,15 @@ const CACHE_KEYS = {
nflRivalCardList: 'data--nfl-rival-cards-cache'
};

function parseCache<T> (key: string): T | undefined {
const data = localStorage.getItem(key);
function parseCache<T> (key: string, useDecompress?: boolean): T | undefined {
let data = localStorage.getItem(key);

if (data) {
try {
if (useDecompress) {
data = decompressData(data);
}

return JSON.parse(data) as T;
} catch (e) {
console.error('Failed to parse cache', e);
Expand All @@ -52,6 +59,20 @@ function parseCache<T> (key: string): T | undefined {
return undefined;
}

function compressData (data: any) {
const jsonData = JSON.stringify(data);

const compressed = deflate(jsonData);

return u8aToString(compressed);
}

function decompressData (data: string) {
const compressed = stringToU8a(data);

return inflate(compressed, { to: 'string' });
}

const metadataHandler = MetadataHandler.instance;

export class BookaSdk {
Expand Down Expand Up @@ -105,7 +126,7 @@ export class BookaSdk {
const rankInfoMap = parseCache<Record<AccountRankType, RankInfo>>(CACHE_KEYS.rankInfoMap);
const leaderboardConfigSubject = parseCache<Record<string, object>>(CACHE_KEYS.leaderboardConfigSubject);
const gameEventList = parseCache<GameEvent[]>(CACHE_KEYS.gameEventList);
const nflRivalCardList = parseCache<NFLRivalCard[]>(CACHE_KEYS.nflRivalCardList);
const nflRivalCardList = parseCache<NFLRivalCard[]>(CACHE_KEYS.nflRivalCardList, true);

account && this.accountSubject.next(account);
taskCategoryList && this.taskCategoryListSubject.next(taskCategoryList);
Expand All @@ -120,12 +141,13 @@ export class BookaSdk {
nflRivalCardList && this.nflRivalCardListSubject.next(nflRivalCardList);
} else {
console.debug('Clearing cache');
storage.removeItems(Object.keys(CACHE_KEYS).concat(['cache-version'])).catch(console.error);
Object.keys(CACHE_KEYS).forEach((key) => {
localStorage.removeItem(key);
});

localStorage.setItem('cache-version', cacheVersion);
storage.removeItems(Object.keys(CACHE_KEYS)
.concat(['cache-version']))
.catch(console.error)
.finally(() => localStorage.setItem('cache-version', cacheVersion));
}
}

Expand Down Expand Up @@ -416,7 +438,7 @@ export class BookaSdk {

if (response?.cards) {
this.nflRivalCardListSubject.next(response.cards);
localStorage.setItem(CACHE_KEYS.gameEventList, JSON.stringify(response.cards));
localStorage.setItem(CACHE_KEYS.nflRivalCardList, compressData(response.cards));
}

this.cardListHandler.resolve();
Expand Down
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6283,6 +6283,7 @@ __metadata:
"@types/enzyme-adapter-react-16": ^1.0.6
"@types/file-saver": ^2.0.5
"@types/lodash": ^4.17.0
"@types/pako": ^2.0.3
"@types/react-copy-to-clipboard": ^5.0.4
"@types/react-dom": ^18.0.9
"@types/react-lazy-load-image-component": ^1.5.3
Expand All @@ -6307,6 +6308,7 @@ __metadata:
humanize-duration: ^3.31.0
i18next: ^21.9.2
lodash: ^4.17.21
pako: ^2.1.0
phosphor-react: ^1.4.1
querystring: ^0.2.1
rc-input: ^0.2.2
Expand Down Expand Up @@ -7373,6 +7375,13 @@ __metadata:
languageName: node
linkType: hard

"@types/pako@npm:^2.0.3":
version: 2.0.3
resolution: "@types/pako@npm:2.0.3"
checksum: 0746dd5d29eccf5b2e6cceb3ccb093851219e78bd2e2e20d25757e247987139e061e5d4ba37cb5295493f06e3c683c74f8876011cd8a3f3748a09244fbc841d9
languageName: node
linkType: hard

"@types/parse-json@npm:^4.0.0":
version: 4.0.0
resolution: "@types/parse-json@npm:4.0.0"
Expand Down Expand Up @@ -21691,6 +21700,13 @@ __metadata:
languageName: node
linkType: hard

"pako@npm:^2.1.0":
version: 2.1.0
resolution: "pako@npm:2.1.0"
checksum: 71666548644c9a4d056bcaba849ca6fd7242c6cf1af0646d3346f3079a1c7f4a66ffec6f7369ee0dc88f61926c10d6ab05da3e1fca44b83551839e89edd75a3e
languageName: node
linkType: hard

"param-case@npm:^3.0.4":
version: 3.0.4
resolution: "param-case@npm:3.0.4"
Expand Down

0 comments on commit 2253cfc

Please sign in to comment.