From 2253cfc2d2c1d539a034df68bd9d3e1e55240296 Mon Sep 17 00:00:00 2001 From: Thiendekaco Date: Tue, 10 Dec 2024 11:39:46 +0700 Subject: [PATCH] [Issue-54] Mythical_Game - Do not load event to play --- packages/extension-koni-ui/package.json | 2 ++ .../src/connector/booka/sdk.ts | 36 +++++++++++++++---- yarn.lock | 16 +++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/extension-koni-ui/package.json b/packages/extension-koni-ui/package.json index 0f6355316f..75280b289b 100644 --- a/packages/extension-koni-ui/package.json +++ b/packages/extension-koni-ui/package.json @@ -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", @@ -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", diff --git a/packages/extension-koni-ui/src/connector/booka/sdk.ts b/packages/extension-koni-ui/src/connector/booka/sdk.ts index 7049ad58c1..c7302800ee 100644 --- a/packages/extension-koni-ui/src/connector/booka/sdk.ts +++ b/packages/extension-koni-ui/src/connector/booka/sdk.ts @@ -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'; @@ -38,11 +41,15 @@ const CACHE_KEYS = { nflRivalCardList: 'data--nfl-rival-cards-cache' }; -function parseCache (key: string): T | undefined { - const data = localStorage.getItem(key); +function parseCache (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); @@ -52,6 +59,20 @@ function parseCache (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 { @@ -105,7 +126,7 @@ export class BookaSdk { const rankInfoMap = parseCache>(CACHE_KEYS.rankInfoMap); const leaderboardConfigSubject = parseCache>(CACHE_KEYS.leaderboardConfigSubject); const gameEventList = parseCache(CACHE_KEYS.gameEventList); - const nflRivalCardList = parseCache(CACHE_KEYS.nflRivalCardList); + const nflRivalCardList = parseCache(CACHE_KEYS.nflRivalCardList, true); account && this.accountSubject.next(account); taskCategoryList && this.taskCategoryListSubject.next(taskCategoryList); @@ -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)); } } @@ -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(); diff --git a/yarn.lock b/yarn.lock index 17bbe63423..ed25f09fd6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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 @@ -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" @@ -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"