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
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 {
<tr>
<td>${t('Best rank')}:</td>
<td>${this.player.bestRank} (${new Date(
this.player.bestRankAt
).toLocaleString()})</td>
this.player.bestRankAt
).toLocaleString()})</td>
</tr>
<tr>
<td>${t('Most points')}:</td>
<td>${this.player.mostPoints.toLocaleString()} (${new Date(
this.player.mostPointsAt
).toLocaleString()})</td>
this.player.mostPointsAt
).toLocaleString()})</td>
</tr>
<tr>
<td>${t('Most villages')}:</td>
<td>${this.player.mostVillages.toLocaleString()} (${new Date(
this.player.mostVillagesAt
).toLocaleString()})</td>
this.player.mostVillagesAt
).toLocaleString()})</td>
</tr>
`
);
@ -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
? `<a href="${s.tribe.profileUrl}">${s.tribe.tag}</a>`
s.player.tribe
? `<a href="${s.player.tribe.profileUrl}">${s.player.tribe.tag}</a>`
: '-',
},
{
@ -489,29 +527,29 @@ class UI {
{
header: t('OD'),
accessor: (s) =>
`${s.scoreTotal.toLocaleString()} (<strong>${
s.rankTotal
`${s.opponentsDefeated.scoreTotal.toLocaleString()} (<strong>${
s.opponentsDefeated.rankTotal
}</strong>)`,
},
{
header: t('ODA'),
accessor: (s) =>
`${s.scoreAtt.toLocaleString()} (<strong>${s.rankAtt}</strong>)`,
`${s.opponentsDefeated.scoreAtt.toLocaleString()} (<strong>${s.opponentsDefeated.rankAtt}</strong>)`,
},
{
header: t('ODD'),
accessor: (s) =>
`${s.scoreDef.toLocaleString()} (<strong>${s.rankDef}</strong>)`,
`${s.opponentsDefeated.scoreDef.toLocaleString()} (<strong>${s.opponentsDefeated.rankDef}</strong>)`,
},
{
header: t('ODS'),
accessor: (s) =>
`${s.scoreSup.toLocaleString()} (<strong>${s.rankSup}</strong>)`,
`${s.opponentsDefeated.scoreSup.toLocaleString()} (<strong>${s.opponentsDefeated.rankSup}</strong>)`,
},
],
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);