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

This commit is contained in:
Dawid Wysokiński 2024-04-03 06:46:29 +02:00
parent a740a589be
commit 998deef3aa
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
1 changed files with 102 additions and 70 deletions

View File

@ -1,10 +1,10 @@
// Extended player profile // Extended player profile
import { Player, PlayerSnapshot, TWHelpClient } from './lib/twhelp';
import { DialogTable } from './common/dialog-table';
import { InADayClient } from './lib/tw'; import { InADayClient } from './lib/tw';
import { createTranslationFunc } from './utils'; import { createTranslationFunc } from './utils';
import { buildURL } from './lib/twstats'; import { buildURL } from './lib/twstats';
import { TWHelpV2Client, PlayerSnapshot, Player } from './lib/twhelpv2';
import { DialogTableV2 } from './common/dialog-table-v2';
const t = createTranslationFunc({ const t = createTranslationFunc({
pl_PL: { pl_PL: {
@ -59,59 +59,81 @@ const t = createTranslationFunc({
}); });
class TWHelpConnector { class TWHelpConnector {
private readonly client: TWHelpV2Client;
constructor( constructor(
private readonly client: TWHelpClient, readonly baseUrl: string,
private readonly version: string, private readonly version: string,
private readonly server: string, private readonly server: string,
private readonly id: number private readonly id: number
) {} ) {
this.client = new TWHelpV2Client({
BASE: baseUrl,
});
}
player() { async player() {
return this.client.player(this.version, this.server, this.id); return (
await this.client.players.getPlayer({
versionCode: this.version,
serverKey: this.server,
playerId: this.id,
})
).data;
} }
async latestSnapshot() { async latestSnapshot() {
const snapshot = await this.playerSnapshots(1, 1); const snapshot = await this.playerSnapshots(undefined, 1);
return snapshot.data.length > 0 ? snapshot.data[0] : null; return snapshot.data.length > 0 ? snapshot.data[0] : null;
} }
playerTribeChanges(page: number, limit: number) { playerTribeChanges(cursor: string | undefined, limit: number) {
return this.client.playerTribeChanges(this.version, this.server, this.id, { return this.client.tribeChanges.listPlayerTribeChanges({
offset: (page - 1) * limit, versionCode: this.version,
serverKey: this.server,
playerId: this.id,
limit, limit,
sort: ['createdAt:desc', 'id:asc'], cursor,
sort: ['createdAt:DESC'],
}); });
} }
playerEnnoblements(page: number, limit: number) { playerEnnoblements(cursor: string | undefined, limit: number) {
return this.client.playerEnnoblements(this.version, this.server, this.id, { return this.client.ennoblements.listPlayerEnnoblements({
offset: (page - 1) * limit, versionCode: this.version,
serverKey: this.server,
playerId: this.id,
limit, limit,
sort: ['createdAt:desc'], cursor,
sort: ['createdAt:DESC'],
}); });
} }
playerSnapshots(page: number, limit: number) { playerSnapshots(cursor: string | undefined, limit: number) {
return this.client.playerSnapshots(this.version, this.server, this.id, { return this.client.snapshots.listPlayerPlayerSnapshots({
offset: (page - 1) * limit, versionCode: this.version,
serverKey: this.server,
playerId: this.id,
limit, limit,
sort: ['date:desc'], cursor,
sort: ['date:DESC'],
}); });
} }
playerOtherServers(page: number, limit: number) { playerOtherServers(cursor: string | undefined, limit: number) {
return this.client.playerOtherServers(this.version, this.server, this.id, { return this.client.players.listVersionPlayers({
offset: (page - 1) * limit, versionCode: this.version,
limit, limit,
cursor,
id: [this.id],
}); });
} }
} }
class InADayConnector { class InADayConnector {
constructor( private readonly client = new InADayClient();
private readonly client: InADayClient,
private readonly name: string constructor(private readonly name: string) {}
) {}
player() { player() {
return this.client.player(this.name); return this.client.player(this.name);
@ -160,20 +182,20 @@ class UI {
<tr> <tr>
<td>${t('Best rank')}:</td> <td>${t('Best rank')}:</td>
<td>${this.player.bestRank} (${new Date( <td>${this.player.bestRank} (${new Date(
this.player.bestRankAt this.player.bestRankAt
).toLocaleString()})</td> ).toLocaleString()})</td>
</tr> </tr>
<tr> <tr>
<td>${t('Most points')}:</td> <td>${t('Most points')}:</td>
<td>${this.player.mostPoints.toLocaleString()} (${new Date( <td>${this.player.mostPoints.toLocaleString()} (${new Date(
this.player.mostPointsAt this.player.mostPointsAt
).toLocaleString()})</td> ).toLocaleString()})</td>
</tr> </tr>
<tr> <tr>
<td>${t('Most villages')}:</td> <td>${t('Most villages')}:</td>
<td>${this.player.mostVillages.toLocaleString()} (${new Date( <td>${this.player.mostVillages.toLocaleString()} (${new Date(
this.player.mostVillagesAt this.player.mostVillagesAt
).toLocaleString()})</td> ).toLocaleString()})</td>
</tr> </tr>
` `
); );
@ -204,38 +226,54 @@ class UI {
}, },
{ {
header: t('ODA'), header: t('ODA'),
value: this.player.scoreAtt - (this.latestSnapshot?.scoreAtt ?? 0), value:
this.player.opponentsDefeated.scoreAtt -
(this.latestSnapshot?.opponentsDefeated.scoreAtt ?? 0),
}, },
{ {
header: t('ODA - rank'), header: t('ODA - rank'),
value: this.player.rankAtt - (this.latestSnapshot?.rankAtt ?? 0), value:
this.player.opponentsDefeated.rankAtt -
(this.latestSnapshot?.opponentsDefeated.rankAtt ?? 0),
rank: true, rank: true,
}, },
{ {
header: t('ODD'), header: t('ODD'),
value: this.player.scoreDef - (this.latestSnapshot?.scoreDef ?? 0), value:
this.player.opponentsDefeated.scoreDef -
(this.latestSnapshot?.opponentsDefeated.scoreDef ?? 0),
}, },
{ {
header: t('ODD - rank'), header: t('ODD - rank'),
value: this.player.rankDef - (this.latestSnapshot?.rankDef ?? 0), value:
this.player.opponentsDefeated.rankDef -
(this.latestSnapshot?.opponentsDefeated.rankDef ?? 0),
rank: true, rank: true,
}, },
{ {
header: t('ODS'), header: t('ODS'),
value: this.player.scoreSup - (this.latestSnapshot?.scoreSup ?? 0), value:
this.player.opponentsDefeated.scoreSup -
(this.latestSnapshot?.opponentsDefeated.scoreSup ?? 0),
}, },
{ {
header: t('ODS - rank'), header: t('ODS - rank'),
value: this.player.rankSup - (this.latestSnapshot?.rankSup ?? 0), value:
this.player.opponentsDefeated.rankSup -
(this.latestSnapshot?.opponentsDefeated.rankSup ?? 0),
rank: true, rank: true,
}, },
{ {
header: t('OD'), header: t('OD'),
value: this.player.scoreTotal - (this.latestSnapshot?.scoreTotal ?? 0), value:
this.player.opponentsDefeated.scoreTotal -
(this.latestSnapshot?.opponentsDefeated.scoreTotal ?? 0),
}, },
{ {
header: t('OD - rank'), header: t('OD - rank'),
value: this.player.rankTotal - (this.latestSnapshot?.rankTotal ?? 0), value:
this.player.opponentsDefeated.rankTotal -
(this.latestSnapshot?.opponentsDefeated.rankTotal ?? 0),
rank: true, rank: true,
}, },
]; ];
@ -322,7 +360,7 @@ class UI {
private async showOtherServers(e: Event) { private async showOtherServers(e: Event) {
e.preventDefault(); e.preventDefault();
await new DialogTable( await new DialogTableV2(
DialogId.OTHER_SERVERS, DialogId.OTHER_SERVERS,
[ [
{ {
@ -364,8 +402,8 @@ class UI {
}, },
], ],
30, 30,
(page: number, limit: number) => { (cursor: string | undefined, limit: number) => {
return this.twhelpConnector.playerOtherServers(page, limit); return this.twhelpConnector.playerOtherServers(cursor, limit);
} }
).render(); ).render();
} }
@ -383,7 +421,7 @@ class UI {
conquer: 'Villages conquered', conquer: 'Villages conquered',
}; };
await new DialogTable( await new DialogTableV2(
DialogId.IN_A_DAY_RANKS, DialogId.IN_A_DAY_RANKS,
[ [
{ {
@ -431,7 +469,7 @@ class UI {
private async showTribeChanges(e: Event) { private async showTribeChanges(e: Event) {
e.preventDefault(); e.preventDefault();
await new DialogTable( await new DialogTableV2(
DialogId.TRIBE_CHANGES, DialogId.TRIBE_CHANGES,
[ [
{ {
@ -454,8 +492,8 @@ class UI {
}, },
], ],
30, 30,
(page: number, limit: number) => { (cursor: string | undefined, limit: number) => {
return this.twhelpConnector.playerTribeChanges(page, limit); return this.twhelpConnector.playerTribeChanges(cursor, limit);
} }
).render(); ).render();
} }
@ -463,7 +501,7 @@ class UI {
private async showHistory(e: Event) { private async showHistory(e: Event) {
e.preventDefault(); e.preventDefault();
await new DialogTable( await new DialogTableV2(
DialogId.HISTORY, DialogId.HISTORY,
[ [
{ {
@ -473,8 +511,8 @@ class UI {
{ {
header: t('Tribe'), header: t('Tribe'),
accessor: (s) => accessor: (s) =>
s.tribe s.player.tribe
? `<a href="${s.tribe.profileUrl}">${s.tribe.tag}</a>` ? `<a href="${s.player.tribe.profileUrl}">${s.player.tribe.tag}</a>`
: '-', : '-',
}, },
{ {
@ -489,29 +527,29 @@ class UI {
{ {
header: t('OD'), header: t('OD'),
accessor: (s) => accessor: (s) =>
`${s.scoreTotal.toLocaleString()} (<strong>${ `${s.opponentsDefeated.scoreTotal.toLocaleString()} (<strong>${
s.rankTotal s.opponentsDefeated.rankTotal
}</strong>)`, }</strong>)`,
}, },
{ {
header: t('ODA'), header: t('ODA'),
accessor: (s) => accessor: (s) =>
`${s.scoreAtt.toLocaleString()} (<strong>${s.rankAtt}</strong>)`, `${s.opponentsDefeated.scoreAtt.toLocaleString()} (<strong>${s.opponentsDefeated.rankAtt}</strong>)`,
}, },
{ {
header: t('ODD'), header: t('ODD'),
accessor: (s) => accessor: (s) =>
`${s.scoreDef.toLocaleString()} (<strong>${s.rankDef}</strong>)`, `${s.opponentsDefeated.scoreDef.toLocaleString()} (<strong>${s.opponentsDefeated.rankDef}</strong>)`,
}, },
{ {
header: t('ODS'), header: t('ODS'),
accessor: (s) => accessor: (s) =>
`${s.scoreSup.toLocaleString()} (<strong>${s.rankSup}</strong>)`, `${s.opponentsDefeated.scoreSup.toLocaleString()} (<strong>${s.opponentsDefeated.rankSup}</strong>)`,
}, },
], ],
30, 30,
(page: number, limit: number) => { (cursor: string | undefined, limit: number) => {
return this.twhelpConnector.playerSnapshots(page, limit); return this.twhelpConnector.playerSnapshots(cursor, limit);
} }
).render(); ).render();
} }
@ -519,7 +557,7 @@ class UI {
private async showEnnoblements(e: Event) { private async showEnnoblements(e: Event) {
e.preventDefault(); e.preventDefault();
await new DialogTable( await new DialogTableV2(
DialogId.ENNOBLEMENTS, DialogId.ENNOBLEMENTS,
[ [
{ {
@ -563,8 +601,8 @@ class UI {
}, },
], ],
30, 30,
(page: number, limit: number) => { (cursor: string | undefined, limit: number) => {
return this.twhelpConnector.playerEnnoblements(page, limit); return this.twhelpConnector.playerEnnoblements(cursor, limit);
} }
).render(); ).render();
} }
@ -574,18 +612,15 @@ class ExtendedPlayerProfile {
private readonly twhelpConnector: TWHelpConnector; private readonly twhelpConnector: TWHelpConnector;
private readonly inADayConnector: InADayConnector; private readonly inADayConnector: InADayConnector;
constructor(twhelpClient: TWHelpClient, inADayClient: InADayClient) { constructor(apiBaseUrl: string) {
this.twhelpConnector = new TWHelpConnector( this.twhelpConnector = new TWHelpConnector(
twhelpClient, apiBaseUrl,
window.game_data.market, window.game_data.market,
window.game_data.world, window.game_data.world,
this.getPlayerId() this.getPlayerId()
); );
this.inADayConnector = new InADayConnector( this.inADayConnector = new InADayConnector(this.getPlayerName());
inADayClient,
this.getPlayerName()
);
} }
async run() { async run() {
@ -627,10 +662,7 @@ class ExtendedPlayerProfile {
return; return;
} }
await new ExtendedPlayerProfile( await new ExtendedPlayerProfile(process.env.TWHELP_API_BASE_URL ?? '')
new TWHelpClient(process.env.TWHELP_API_BASE_URL ?? ''),
new InADayClient()
)
.run() .run()
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);