feat: extended map popup

This commit is contained in:
Dawid Wysokiński 2023-02-03 09:25:52 +01:00
parent 513dbadb00
commit 23fe731ced
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
6 changed files with 118 additions and 2 deletions

View File

@ -10,6 +10,18 @@ const preambles = {
// @icon https://www.google.com/s2/favicons?domain=plemiona.pl
// @grant none
// @run-at document-end
// ==/UserScript==`,
'extended-map-popup': `// ==UserScript==
// @name Extended map popup
// @version 1.0.0
// @description Extends the map popup with additional info.
// @author Dawid Wysokiński - Kichiyaki - contact@dwysokinski.me
// @match https://*/game.php?*screen=map*
// @downloadURL ${process.env.PUBLIC_URL}/extended-map-popup.user.js
// @updateURL ${process.env.PUBLIC_URL}/extended-map-popup.user.js
// @icon https://www.google.com/s2/favicons?domain=plemiona.pl
// @grant none
// @run-at document-end
// ==/UserScript==`,
};

View File

@ -6,6 +6,8 @@
"build-single": "PUBLIC_URL=https://scripts.tribalwarshelp.com parcel build",
"build:extended-player-profile-user": "PREAMBLE=extended-player-profile yarn build-single ./src/extended-player-profile.user.ts",
"build:extended-player-profile-quickbar": "yarn build-single ./src/extended-player-profile.quickbar.ts",
"build:extended-map-popup-user": "PREAMBLE=extended-map-popup yarn build-single ./src/extended-map-popup.user.ts",
"build:extended-map-popup-quickbar": "yarn build-single ./src/extended-map-popup.quickbar.ts",
"build": "npm-run-all build:*",
"lint": "eslint src/**/*.ts"
},

View File

@ -0,0 +1,2 @@
// Parcel doesn't have an option to rename output files
import './extended-map-popup.user';

View File

@ -0,0 +1,82 @@
import differenceInSeconds from 'date-fns/differenceInSeconds';
import { TWHelpClient } from './lib/twhelp';
class TWHelpConnector {
private static readonly SERVER_CONFIG_LOCAL_STORAGE_KEY =
'kichiyaki_server_config';
constructor(
private readonly client: TWHelpClient,
private readonly version: string,
private readonly server: string
) {}
serverConfig() {
return this.withCache(
TWHelpConnector.SERVER_CONFIG_LOCAL_STORAGE_KEY,
3600,
() => {
return this.client.serverConfig(this.version, this.server);
}
);
}
private async withCache<T>(
key: string,
ttl: number,
func: () => Promise<T>
): Promise<T> {
try {
const fromCache = JSON.parse(localStorage.getItem(key) ?? '');
if (
fromCache.date &&
differenceInSeconds(new Date(), new Date(fromCache.date)) <= ttl
) {
return fromCache.data;
}
// eslint-disable-next-line no-empty
} catch (err) {}
const res = await func();
localStorage.setItem(
key,
JSON.stringify({
data: res,
date: new Date(),
})
);
return res;
}
}
class ExtendedMapPopup {
connector: TWHelpConnector;
constructor(client: TWHelpClient) {
this.connector = new TWHelpConnector(
client,
window.game_data.market,
window.game_data.world
);
}
async run() {
const config = await this.connector.serverConfig();
console.log(config);
}
}
(async () => {
if (window.game_data.screen !== 'map' || window.game_data.mode !== null) {
return;
}
await new ExtendedMapPopup(
new TWHelpClient(process.env.TWHELP_API_BASE_URL ?? '')
)
.run()
.catch((err) => {
console.log(err);
});
})();

View File

@ -589,7 +589,7 @@ class ExtendedPlayerProfile {
);
}
async render() {
async run() {
const player = await this.twhelpConnector.player();
const latestSnapshot = await this.twhelpConnector.latestSnapshot();
@ -630,7 +630,7 @@ class ExtendedPlayerProfile {
new TWHelpClient(process.env.TWHELP_API_BASE_URL ?? ''),
new InADayClient()
)
.render()
.run()
.catch((err) => {
console.log(err);
});

View File

@ -100,6 +100,14 @@ export type PlayerSnapshot = {
date: string;
};
export type ServerConfig = {
speed: number;
unitSpeed: number;
snob: {
maxDist: number;
};
};
export type ListResult<T> = {
data: T[];
total: number;
@ -146,6 +154,16 @@ export class TWHelpClient {
};
}
public async serverConfig(
version: string,
server: string
): Promise<ServerConfig> {
const resp = await this.client.get(
`/api/v1/versions/${version}/servers/${server}/config`
);
return resp.data.data;
}
public async player(
version: string,
server: string,