refactor: extended map popup - use /api/v2
ci/woodpecker/push/test Pipeline was successful Details

This commit is contained in:
Dawid Wysokiński 2024-04-02 07:29:59 +02:00
parent 93fa246f14
commit a2fb6425a0
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
5 changed files with 514 additions and 497 deletions

2
.env
View File

@ -1 +1 @@
TWHELP_API_BASE_URL=https://twhelp.app TWHELP_API_BASE_URL=https://twhelp.app/api

View File

@ -4,7 +4,7 @@
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"prepare": "husky install", "prepare": "husky install",
"generate-client": "openapi --input ./src/lib/twhelpv2/openapi3.json --output ./src/lib/twhelpv2 --name TWHelpV2 --useOptions --client axios", "generate-client": "openapi --input ./src/lib/twhelpv2/openapi3.json --output ./src/lib/twhelpv2 --name TWHelpV2Client --useOptions --client axios",
"build": "rm -rf dist && yarn generate-client && parcel build ./src/*.user.ts ./src/*.quickbar.ts", "build": "rm -rf dist && yarn generate-client && parcel build ./src/*.user.ts ./src/*.quickbar.ts",
"postbuild": "PUBLIC_URL=https://scripts.twhelp.app node postbuild.js", "postbuild": "PUBLIC_URL=https://scripts.twhelp.app node postbuild.js",
"lint": "eslint src/**/*.ts" "lint": "eslint src/**/*.ts"
@ -21,13 +21,13 @@
"url": "https://dwysokinski.me/" "url": "https://dwysokinski.me/"
}, },
"browserslist": [ "browserslist": [
"since 2017-06" "since 2020-01"
], ],
"devDependencies": { "devDependencies": {
"@commitlint/cli": "^18.0.0", "@commitlint/cli": "^18.0.0",
"@commitlint/config-conventional": "^18.0.0", "@commitlint/config-conventional": "^18.0.0",
"@parcel/validator-eslint": "^2.9.1", "@parcel/validator-eslint": "^2.12.0",
"@parcel/validator-typescript": "^2.9.1", "@parcel/validator-typescript": "^2.12.0",
"@types/jquery": "^3.5.14", "@types/jquery": "^3.5.14",
"@types/lodash": "^4.14.191", "@types/lodash": "^4.14.191",
"@types/node": "^20.0.0", "@types/node": "^20.0.0",
@ -38,7 +38,7 @@
"eslint-config-prettier": "^9.0.0", "eslint-config-prettier": "^9.0.0",
"husky": "^8.0.3", "husky": "^8.0.3",
"openapi-typescript-codegen": "^0.28.0", "openapi-typescript-codegen": "^0.28.0",
"parcel": "^2.9.1", "parcel": "^2.12.0",
"prettier": "^3.0.0", "prettier": "^3.0.0",
"typescript": "^5.0.0" "typescript": "^5.0.0"
}, },

View File

@ -9,7 +9,7 @@ const metadata = {
// @name Extended player profile // @name Extended player profile
// @version 1.1.6 // @version 1.1.6
// @description Adds additional info and actions on a player overview. // @description Adds additional info and actions on a player overview.
// @author Dawid Wysokiński - Kichiyaki - contact@dwysokinski.me // @author Dawid Wysokiński - Kichiyaki - contact@twhelp.app
// @match https://*/game.php?*screen=info_player* // @match https://*/game.php?*screen=info_player*
// @downloadURL ${process.env.PUBLIC_URL}/extended-player-profile.user.js // @downloadURL ${process.env.PUBLIC_URL}/extended-player-profile.user.js
// @updateURL ${process.env.PUBLIC_URL}/extended-player-profile.user.js // @updateURL ${process.env.PUBLIC_URL}/extended-player-profile.user.js
@ -19,9 +19,9 @@ const metadata = {
// ==/UserScript==`, // ==/UserScript==`,
'extended-map-popup': `// ==UserScript== 'extended-map-popup': `// ==UserScript==
// @name Extended map popup // @name Extended map popup
// @version 1.0.5 // @version 1.1.0
// @description Extends the map popup with additional info. // @description Extends the map popup with additional info.
// @author Dawid Wysokiński - Kichiyaki - contact@dwysokinski.me // @author Dawid Wysokiński - Kichiyaki - contact@twhelp.app
// @match https://*/game.php?*screen=map* // @match https://*/game.php?*screen=map*
// @downloadURL ${process.env.PUBLIC_URL}/extended-map-popup.user.js // @downloadURL ${process.env.PUBLIC_URL}/extended-map-popup.user.js
// @updateURL ${process.env.PUBLIC_URL}/extended-map-popup.user.js // @updateURL ${process.env.PUBLIC_URL}/extended-map-popup.user.js
@ -33,7 +33,7 @@ const metadata = {
// @name Extended village profile // @name Extended village profile
// @version 1.0.4 // @version 1.0.4
// @description Adds additional info and actions on a village overview. // @description Adds additional info and actions on a village overview.
// @author Dawid Wysokiński - Kichiyaki - contact@dwysokinski.me // @author Dawid Wysokiński - Kichiyaki - contact@twhelp.app
// @match https://*/game.php?*screen=info_village* // @match https://*/game.php?*screen=info_village*
// @downloadURL ${process.env.PUBLIC_URL}/extended-village-profile.user.js // @downloadURL ${process.env.PUBLIC_URL}/extended-village-profile.user.js
// @updateURL ${process.env.PUBLIC_URL}/extended-village-profile.user.js // @updateURL ${process.env.PUBLIC_URL}/extended-village-profile.user.js
@ -45,7 +45,7 @@ const metadata = {
// @name Extended tribe profile // @name Extended tribe profile
// @version 1.0.4 // @version 1.0.4
// @description Adds additional info and actions on a tribe overview. // @description Adds additional info and actions on a tribe overview.
// @author Dawid Wysokiński - Kichiyaki - contact@dwysokinski.me // @author Dawid Wysokiński - Kichiyaki - contact@twhelp.app
// @match https://*/game.php?*screen=info_ally* // @match https://*/game.php?*screen=info_ally*
// @downloadURL ${process.env.PUBLIC_URL}/extended-tribe-profile.user.js // @downloadURL ${process.env.PUBLIC_URL}/extended-tribe-profile.user.js
// @updateURL ${process.env.PUBLIC_URL}/extended-tribe-profile.user.js // @updateURL ${process.env.PUBLIC_URL}/extended-tribe-profile.user.js

View File

@ -1,13 +1,13 @@
import addSeconds from 'date-fns/addSeconds'; import addSeconds from 'date-fns/addSeconds';
import {
Ennoblement,
ServerConfig,
TWHelpClient,
UnitInfo,
} from './lib/twhelp';
import { Cache, InMemoryStorage } from './lib/cache'; import { Cache, InMemoryStorage } from './lib/cache';
import { calcDistance, calcLoyalty } from './lib/tw'; import { calcDistance, calcLoyalty } from './lib/tw';
import { createTranslationFunc } from './utils'; import { createTranslationFunc } from './utils';
import {
TWHelpV2Client,
Ennoblement,
UnitInfo,
ServerConfig,
} from './lib/twhelpv2';
declare global { declare global {
interface Window { interface Window {
@ -43,25 +43,38 @@ const t = createTranslationFunc({
class TWHelpConnector { class TWHelpConnector {
private static readonly SERVER_CONFIG_CACHE_KEY = private static readonly SERVER_CONFIG_CACHE_KEY =
'extended_map_popup_server_config'; 'extended_map_popup_server_config_v2';
private static readonly UNIT_INFO_CACHE_KEY = 'extended_map_popup_unit_info'; private static readonly UNIT_INFO_CACHE_KEY =
'extended_map_popup_unit_info_v2';
private static readonly VILLAGE_CACHE_KEY_PREFIX = private static readonly VILLAGE_CACHE_KEY_PREFIX =
'extended_map_popup_village_'; 'extended_map_popup_village_v2_';
private readonly localStorageCache = new Cache(localStorage); private readonly localStorageCache = new Cache(localStorage);
private readonly inMemoryCache = new Cache(new InMemoryStorage()); private readonly inMemoryCache = new Cache(new InMemoryStorage());
private readonly client: TWHelpV2Client;
constructor( constructor(
private readonly client: TWHelpClient, private readonly baseUrl: string,
private readonly version: string, private readonly version: string,
private readonly server: string private readonly server: string
) {} ) {
this.client = new TWHelpV2Client({
BASE: baseUrl,
});
}
serverConfig() { serverConfig() {
return this.localStorageCache.load( return this.localStorageCache.load(
TWHelpConnector.SERVER_CONFIG_CACHE_KEY, TWHelpConnector.SERVER_CONFIG_CACHE_KEY,
3600, 3600,
() => { async () => {
return this.client.serverConfig(this.version, this.server); return (
await this.client.servers.getServerConfig({
serverKey: this.server,
versionCode: this.version,
})
).data;
} }
); );
} }
@ -70,8 +83,13 @@ class TWHelpConnector {
return this.localStorageCache.load( return this.localStorageCache.load(
TWHelpConnector.UNIT_INFO_CACHE_KEY, TWHelpConnector.UNIT_INFO_CACHE_KEY,
3600, 3600,
() => { async () => {
return this.client.unitInfo(this.version, this.server); return (
await this.client.servers.getUnitInfo({
serverKey: this.server,
versionCode: this.version,
})
).data;
} }
); );
} }
@ -84,15 +102,14 @@ class TWHelpConnector {
} }
return this.inMemoryCache.load(key, 86400, async () => { return this.inMemoryCache.load(key, 86400, async () => {
const ennoblements = await this.client.villageEnnoblements( const ennoblements =
this.version, await this.client.ennoblements.listVillageEnnoblements({
this.server, versionCode: this.version,
id, serverKey: this.server,
{
limit: 1,
sort: ['createdAt:DESC'], sort: ['createdAt:DESC'],
} villageId: id,
); limit: 1,
});
return ennoblements.data.length > 0 ? ennoblements.data[0] : null; return ennoblements.data.length > 0 ? ennoblements.data[0] : null;
}); });
} }
@ -245,9 +262,9 @@ class Popup {
class ExtendedMapPopup { class ExtendedMapPopup {
connector: TWHelpConnector; connector: TWHelpConnector;
constructor(client: TWHelpClient) { constructor(baseApiUrl: string) {
this.connector = new TWHelpConnector( this.connector = new TWHelpConnector(
client, baseApiUrl,
window.game_data.market, window.game_data.market,
window.game_data.world window.game_data.world
); );
@ -273,9 +290,7 @@ class ExtendedMapPopup {
return; return;
} }
await new ExtendedMapPopup( await new ExtendedMapPopup(process.env.TWHELP_API_BASE_URL ?? '')
new TWHelpClient(process.env.TWHELP_API_BASE_URL ?? '')
)
.run() .run()
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);

918
yarn.lock

File diff suppressed because it is too large Load Diff