Skip to content

Commit b3f8cd3

Browse files
committed
feat: battle log endpoint
1 parent cef806c commit b3f8cd3

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

src/players/dto/battle-log.dto.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class BattleLogDto {
2+
playerTag: string;
3+
opponentTag: string;
4+
battleType: string;
5+
isAttack: boolean;
6+
stars: number;
7+
destruction: number;
8+
trophies: number;
9+
trophyChange: number;
10+
createdAt: string;
11+
}
12+
13+
export class BattleLogItemsDto {
14+
items: BattleLogDto[];
15+
}

src/players/dto/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './battle-log.dto';
12
export * from './clan-history.dto';
23
export * from './clan-war-attack-log.dto';
34
export * from './legend-attacks.dto';

src/players/players.controller.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
AggregateClanWarLeagueHistoryItemsDto,
99
AttackHistoryInputDto,
1010
AttackHistoryItemsDto,
11+
BattleLogItemsDto,
1112
ClanHistoryItemsDto,
1213
} from './dto';
1314
import { PlayersService } from './players.service';
@@ -24,6 +25,12 @@ export class PlayersController {
2425
private globalService: GlobalService,
2526
) {}
2627

28+
@Get('/:playerTag/battle-log')
29+
@Cache(600)
30+
getBattleLog(@Param('playerTag') playerTag: string): Promise<BattleLogItemsDto> {
31+
return this.playersService.getPlayerBattleLog(playerTag);
32+
}
33+
2734
@Get('/:playerTag/history')
2835
@Cache(600)
2936
getClanHistory(@Param('playerTag') playerTag: string): Promise<ClanHistoryItemsDto> {

src/players/players.service.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,49 @@
11
import { ClashClientService } from '@app/clash-client';
2+
import { ClickHouseClient } from '@clickhouse/client';
23
import { Inject, Injectable } from '@nestjs/common';
34
import Redis from 'ioredis';
45
import { Db } from 'mongodb';
5-
import { GO_REDIS_TOKEN, MONGODB_TOKEN } from '../db';
6+
import { CLICKHOUSE_TOKEN, GO_REDIS_TOKEN, MONGODB_TOKEN } from '../db';
7+
import { BattleLogDto } from './dto';
68

79
@Injectable()
810
export class PlayersService {
911
constructor(
1012
@Inject(GO_REDIS_TOKEN) private redis: Redis,
1113
@Inject(MONGODB_TOKEN) private db: Db,
1214
private clashClientService: ClashClientService,
15+
@Inject(CLICKHOUSE_TOKEN) private clickhouse: ClickHouseClient,
1316
) {}
1417

18+
async getPlayerBattleLog(playerTag: string) {
19+
const result = await this.clickhouse.query({
20+
query: `
21+
SELECT
22+
player_tag as playerTag,
23+
opponent_tag as opponentTag,
24+
battle_type as battleType,
25+
toBool(is_attack) as isAttack,
26+
stars,
27+
destruction,
28+
trophies,
29+
trophy_change as trophyChange,
30+
battle_date as battleDate,
31+
ingested_at as ingestedAt
32+
FROM battle_logs FINAL
33+
WHERE player_tag = {playerTag: String}
34+
ORDER BY version LIMIT {limit: Int32}
35+
`,
36+
query_params: {
37+
playerTag,
38+
limit: 300,
39+
},
40+
});
41+
42+
const rows = await result.json<BattleLogDto>();
43+
44+
return { items: (rows.data || []).reverse() };
45+
}
46+
1547
async addPlayer(tag: string) {
1648
const player = await this.clashClientService.getPlayerOrThrow(tag);
1749
await this.redis.sadd('legend_player_tags', player.tag);

0 commit comments

Comments
 (0)