From 998deef3aa7beb3406109e2d1bc37f01d94ac0e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Wed, 3 Apr 2024 06:46:29 +0200 Subject: [PATCH] refactor: extended player profile - use /api/v2 --- src/extended-player-profile.user.ts | 172 +++++++++++++++++----------- 1 file changed, 102 insertions(+), 70 deletions(-) diff --git a/src/extended-player-profile.user.ts b/src/extended-player-profile.user.ts index 69a9a9b..388f3f6 100644 --- a/src/extended-player-profile.user.ts +++ b/src/extended-player-profile.user.ts @@ -1,10 +1,10 @@ // Extended player profile -import { Player, PlayerSnapshot, TWHelpClient } from './lib/twhelp'; -import { DialogTable } from './common/dialog-table'; import { InADayClient } from './lib/tw'; import { createTranslationFunc } from './utils'; import { buildURL } from './lib/twstats'; +import { TWHelpV2Client, PlayerSnapshot, Player } from './lib/twhelpv2'; +import { DialogTableV2 } from './common/dialog-table-v2'; const t = createTranslationFunc({ pl_PL: { @@ -59,59 +59,81 @@ const t = createTranslationFunc({ }); class TWHelpConnector { + private readonly client: TWHelpV2Client; + constructor( - private readonly client: TWHelpClient, + readonly baseUrl: string, private readonly version: string, private readonly server: string, private readonly id: number - ) {} + ) { + this.client = new TWHelpV2Client({ + BASE: baseUrl, + }); + } - player() { - return this.client.player(this.version, this.server, this.id); + async player() { + return ( + await this.client.players.getPlayer({ + versionCode: this.version, + serverKey: this.server, + playerId: this.id, + }) + ).data; } 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; } - playerTribeChanges(page: number, limit: number) { - return this.client.playerTribeChanges(this.version, this.server, this.id, { - offset: (page - 1) * limit, + playerTribeChanges(cursor: string | undefined, limit: number) { + return this.client.tribeChanges.listPlayerTribeChanges({ + versionCode: this.version, + serverKey: this.server, + playerId: this.id, limit, - sort: ['createdAt:desc', 'id:asc'], + cursor, + sort: ['createdAt:DESC'], }); } - playerEnnoblements(page: number, limit: number) { - return this.client.playerEnnoblements(this.version, this.server, this.id, { - offset: (page - 1) * limit, + playerEnnoblements(cursor: string | undefined, limit: number) { + return this.client.ennoblements.listPlayerEnnoblements({ + versionCode: this.version, + serverKey: this.server, + playerId: this.id, limit, - sort: ['createdAt:desc'], + cursor, + sort: ['createdAt:DESC'], }); } - playerSnapshots(page: number, limit: number) { - return this.client.playerSnapshots(this.version, this.server, this.id, { - offset: (page - 1) * limit, + playerSnapshots(cursor: string | undefined, limit: number) { + return this.client.snapshots.listPlayerPlayerSnapshots({ + versionCode: this.version, + serverKey: this.server, + playerId: this.id, limit, - sort: ['date:desc'], + cursor, + sort: ['date:DESC'], }); } - playerOtherServers(page: number, limit: number) { - return this.client.playerOtherServers(this.version, this.server, this.id, { - offset: (page - 1) * limit, + playerOtherServers(cursor: string | undefined, limit: number) { + return this.client.players.listVersionPlayers({ + versionCode: this.version, limit, + cursor, + id: [this.id], }); } } class InADayConnector { - constructor( - private readonly client: InADayClient, - private readonly name: string - ) {} + private readonly client = new InADayClient(); + + constructor(private readonly name: string) {} player() { return this.client.player(this.name); @@ -160,20 +182,20 @@ class UI { ${t('Best rank')}: ${this.player.bestRank} (${new Date( - this.player.bestRankAt - ).toLocaleString()}) + this.player.bestRankAt + ).toLocaleString()}) ${t('Most points')}: ${this.player.mostPoints.toLocaleString()} (${new Date( - this.player.mostPointsAt - ).toLocaleString()}) + this.player.mostPointsAt + ).toLocaleString()}) ${t('Most villages')}: ${this.player.mostVillages.toLocaleString()} (${new Date( - this.player.mostVillagesAt - ).toLocaleString()}) + this.player.mostVillagesAt + ).toLocaleString()}) ` ); @@ -204,38 +226,54 @@ class UI { }, { 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'), - value: this.player.rankAtt - (this.latestSnapshot?.rankAtt ?? 0), + value: + this.player.opponentsDefeated.rankAtt - + (this.latestSnapshot?.opponentsDefeated.rankAtt ?? 0), rank: true, }, { 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'), - value: this.player.rankDef - (this.latestSnapshot?.rankDef ?? 0), + value: + this.player.opponentsDefeated.rankDef - + (this.latestSnapshot?.opponentsDefeated.rankDef ?? 0), rank: true, }, { 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'), - value: this.player.rankSup - (this.latestSnapshot?.rankSup ?? 0), + value: + this.player.opponentsDefeated.rankSup - + (this.latestSnapshot?.opponentsDefeated.rankSup ?? 0), rank: true, }, { 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'), - value: this.player.rankTotal - (this.latestSnapshot?.rankTotal ?? 0), + value: + this.player.opponentsDefeated.rankTotal - + (this.latestSnapshot?.opponentsDefeated.rankTotal ?? 0), rank: true, }, ]; @@ -322,7 +360,7 @@ class UI { private async showOtherServers(e: Event) { e.preventDefault(); - await new DialogTable( + await new DialogTableV2( DialogId.OTHER_SERVERS, [ { @@ -364,8 +402,8 @@ class UI { }, ], 30, - (page: number, limit: number) => { - return this.twhelpConnector.playerOtherServers(page, limit); + (cursor: string | undefined, limit: number) => { + return this.twhelpConnector.playerOtherServers(cursor, limit); } ).render(); } @@ -383,7 +421,7 @@ class UI { conquer: 'Villages conquered', }; - await new DialogTable( + await new DialogTableV2( DialogId.IN_A_DAY_RANKS, [ { @@ -431,7 +469,7 @@ class UI { private async showTribeChanges(e: Event) { e.preventDefault(); - await new DialogTable( + await new DialogTableV2( DialogId.TRIBE_CHANGES, [ { @@ -454,8 +492,8 @@ class UI { }, ], 30, - (page: number, limit: number) => { - return this.twhelpConnector.playerTribeChanges(page, limit); + (cursor: string | undefined, limit: number) => { + return this.twhelpConnector.playerTribeChanges(cursor, limit); } ).render(); } @@ -463,7 +501,7 @@ class UI { private async showHistory(e: Event) { e.preventDefault(); - await new DialogTable( + await new DialogTableV2( DialogId.HISTORY, [ { @@ -473,8 +511,8 @@ class UI { { header: t('Tribe'), accessor: (s) => - s.tribe - ? `${s.tribe.tag}` + s.player.tribe + ? `${s.player.tribe.tag}` : '-', }, { @@ -489,29 +527,29 @@ class UI { { header: t('OD'), accessor: (s) => - `${s.scoreTotal.toLocaleString()} (${ - s.rankTotal + `${s.opponentsDefeated.scoreTotal.toLocaleString()} (${ + s.opponentsDefeated.rankTotal })`, }, { header: t('ODA'), accessor: (s) => - `${s.scoreAtt.toLocaleString()} (${s.rankAtt})`, + `${s.opponentsDefeated.scoreAtt.toLocaleString()} (${s.opponentsDefeated.rankAtt})`, }, { header: t('ODD'), accessor: (s) => - `${s.scoreDef.toLocaleString()} (${s.rankDef})`, + `${s.opponentsDefeated.scoreDef.toLocaleString()} (${s.opponentsDefeated.rankDef})`, }, { header: t('ODS'), accessor: (s) => - `${s.scoreSup.toLocaleString()} (${s.rankSup})`, + `${s.opponentsDefeated.scoreSup.toLocaleString()} (${s.opponentsDefeated.rankSup})`, }, ], 30, - (page: number, limit: number) => { - return this.twhelpConnector.playerSnapshots(page, limit); + (cursor: string | undefined, limit: number) => { + return this.twhelpConnector.playerSnapshots(cursor, limit); } ).render(); } @@ -519,7 +557,7 @@ class UI { private async showEnnoblements(e: Event) { e.preventDefault(); - await new DialogTable( + await new DialogTableV2( DialogId.ENNOBLEMENTS, [ { @@ -563,8 +601,8 @@ class UI { }, ], 30, - (page: number, limit: number) => { - return this.twhelpConnector.playerEnnoblements(page, limit); + (cursor: string | undefined, limit: number) => { + return this.twhelpConnector.playerEnnoblements(cursor, limit); } ).render(); } @@ -574,18 +612,15 @@ class ExtendedPlayerProfile { private readonly twhelpConnector: TWHelpConnector; private readonly inADayConnector: InADayConnector; - constructor(twhelpClient: TWHelpClient, inADayClient: InADayClient) { + constructor(apiBaseUrl: string) { this.twhelpConnector = new TWHelpConnector( - twhelpClient, + apiBaseUrl, window.game_data.market, window.game_data.world, this.getPlayerId() ); - this.inADayConnector = new InADayConnector( - inADayClient, - this.getPlayerName() - ); + this.inADayConnector = new InADayConnector(this.getPlayerName()); } async run() { @@ -627,10 +662,7 @@ class ExtendedPlayerProfile { return; } - await new ExtendedPlayerProfile( - new TWHelpClient(process.env.TWHELP_API_BASE_URL ?? ''), - new InADayClient() - ) + await new ExtendedPlayerProfile(process.env.TWHELP_API_BASE_URL ?? '') .run() .catch((err) => { console.log(err);