Skip to content

Commit

Permalink
Fix CWL stats endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Apr 3, 2024
1 parent 63d62ec commit 738a57b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 10 deletions.
33 changes: 26 additions & 7 deletions apps/service-auth/src/clans/clans.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,23 @@ export class ClansService {
);
if (!body) throw new NotFoundException('Clan war league group not found');

const members: Record<string, CWLMemberStatsOutput> = {};
const membersMap: Record<string, CWLMemberStatsOutput> = {};

const wars = await this.clanWarsCollection
.find({ warTag: { $in: body.warTags[clanTag] } })
.find(
{ warTag: { $in: body.warTags[clanTag] } },
{
projection: {
_id: 0,
id: 0,
uid: 0,
leagueGroupId: 0,
warType: 0,
updatedAt: 0,
season: 0,
},
},
)
.toArray();

for (const data of wars) {
Expand All @@ -258,13 +271,13 @@ export class ClansService {

const __attacks = clan.members.flatMap((m) => m.attacks ?? []);
for (const m of clan.members) {
members[m.tag] ??= {
membersMap[m.tag] ??= {
name: m.name,
tag: m.tag,
participated: 0,
attacks: 0,
stars: 0,
trueStars: 0,
newStars: 0,
destruction: 0,
threeStars: 0,
twoStars: 0,
Expand All @@ -276,14 +289,14 @@ export class ClansService {
defenseCount: 0,
};

const member = members[m.tag]!;
const member = membersMap[m.tag]!;
member.participated += 1;

for (const atk of m.attacks ?? []) {
const previousBestAttack = this.getPreviousBestAttack(__attacks, opponent, atk);
member.attacks += 1;
member.stars += atk.stars;
member.trueStars += previousBestAttack
member.newStars += previousBestAttack
? Math.max(0, atk.stars - previousBestAttack.stars)
: atk.stars;
member.destruction += atk.destructionPercentage;
Expand All @@ -303,10 +316,16 @@ export class ClansService {
}
}

const members = Object.values(membersMap);
members.sort((a, b) => b.stars - a.stars);

return {
season: body.season,
clans: body.clans,
members: Object.values(members),
rounds: body.rounds,
warTags: body.warTags[clanTag] ?? [],
members,
wars,
};
}

Expand Down
11 changes: 10 additions & 1 deletion apps/service-auth/src/clans/dto/cwl-stats.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ClanWarsEntity } from '@app/entities';
import { ApiProperty } from '@nestjs/swagger';

export class CWLMemberStatsOutput {
Expand All @@ -6,8 +7,8 @@ export class CWLMemberStatsOutput {
participated: number;
attacks: number;
stars: number;
newStars: number;
destruction: number;
trueStars: number;
threeStars: number;
twoStars: number;
oneStar: number;
Expand All @@ -32,4 +33,12 @@ export class CWLStatsOutput {

@ApiProperty({ isArray: true, type: CWLMemberStatsOutput })
members: CWLMemberStatsOutput[];

warTags: string[];

@ApiProperty({ isArray: true })
rounds: { warTags: string[] }[];

@ApiProperty({ isArray: true })
wars: ClanWarsEntity[];
}
3 changes: 2 additions & 1 deletion apps/service-auth/src/clans/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {};
export * from './cwl-stats.dto';
export * from './pagination.dto';
2 changes: 1 addition & 1 deletion apps/service-auth/src/links/links.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class LinksController {
}

@Post('/')
@Roles(Role.USER)
@Roles(Role.USER, Role.MANAGE_LINKS)
@ApiOperation({ summary: '(Internal)' })
async createLink(@Body() body: CreateLinkInput) {
return this.linksService.createLink(body);
Expand Down
1 change: 1 addition & 0 deletions libs/auth/src/decorators/roles.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum Role {
USER = 'user',
ADMIN = 'admin',
VIEWER = 'viewer',
MANAGE_LINKS = 'manage_links',
}

export const ROLES_KEY = 'roles';
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"mongodb": "^6.1.0",
"morgan": "^1.10.0",
"passport-jwt": "^4.0.1",
"radash": "^12.1.0",
"redis": "^4.6.8",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
Expand Down
2 changes: 2 additions & 0 deletions scripts/make-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const paths = [
'apps/service-auth/src/clans/dto',
'apps/service-auth/src/links/dto',
'apps/service-auth/src/players/dto',

'apps/service-clans/src/dto',
];

(async () => {
Expand Down

0 comments on commit 738a57b

Please sign in to comment.