Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/re sync points #606

Merged
merged 17 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"github-actions.workflows.pinned.workflows": [
".github/workflows/main.yml"
],
"exportall.config.folderListener": []
"exportall.config.folderListener": [],
"sonarlint.connectedMode.project": {
"connectionId": "badman",
"projectKey": "Badminton-Apps_badman"
}
}
6 changes: 3 additions & 3 deletions apps/scripts/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { DatabaseModule } from '@badman/backend-database';
import { configSchema, load } from '@badman/utils';
import { Logger, Module, OnModuleInit } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AssignClubToPlayers } from './scripts/assign-clubs-to-players-group-role/service';
import { PlayersWrongRankingRunner } from './scripts/players-with-wrong-ranking/players-with-wrong-ranking';

@Module({
providers: [AssignClubToPlayers],
providers: [PlayersWrongRankingRunner],
imports: [
ConfigModule.forRoot({
cache: true,
Expand All @@ -18,7 +18,7 @@ import { AssignClubToPlayers } from './scripts/assign-clubs-to-players-group-rol
export class ScriptModule implements OnModuleInit {
private readonly logger = new Logger(ScriptModule.name);

constructor(private fixer: AssignClubToPlayers) {}
constructor(private fixer: PlayersWrongRankingRunner) {}

async onModuleInit() {
this.logger.log('Running script');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import {
DrawTournament,
EventEntry,
EventTournament,
Player,
RankingLastPlace,
RankingSystem,
SubEventTournament,
} from '@badman/backend-database';
import { GameType, getSeason, getSeasonPeriod } from '@badman/utils';
import { Injectable, Logger } from '@nestjs/common';
import { Op } from 'sequelize';
import xlsx from 'xlsx';

/**
if a player with a ranking 12 has played in any offical tournament where the subEvent has a min ranking of 9 or lower,
This means the player has a wrong ranking and was probably not calculated correctly by the ranking service.

The goal of this script is to find all these players and write them to a file
*/
@Injectable()
export class PlayersWrongRankingRunner {
private readonly logger = new Logger(PlayersWrongRankingRunner.name);

async process() {
const getPrimaryRanking = await RankingSystem.findOne({
where: {
primary: true,
},
});

const playerInclude = [
{
model: RankingLastPlace,
attributes: ['id', 'single', 'double', 'mix'],
where: {
systemId: getPrimaryRanking.id,
single: 12,
double: 12,
mix: 12,
},
},
];

const seasonPeriod = getSeasonPeriod(getSeason() - 1);

// start by fetching all offical events and their subevents with a min ranking of 9 or lower of last season
const events = await EventTournament.findAll({
attributes: ['id', 'name', 'firstDay'],
where: {
firstDay: {
[Op.between]: [seasonPeriod[0], seasonPeriod[1]],
},
official: true,
},
include: [
{
model: SubEventTournament,
attributes: ['id', 'name', 'level', 'gameType'],
where: {
level: {
[Op.lt]: 9,
},
},
include: [
{
model: DrawTournament,
attributes: ['id'],
include: [
{
model: EventEntry,
attributes: ['id', 'player1Id', 'player2Id'],
},
],
},
],
},
],
});

// get all the players that played in these events

this.logger.verbose(`Found ${events.length} events`);

const players: Map<string, Player> = new Map();

for (const event of events) {
for (const subEvent of event.subEventTournaments) {
for (const draw of subEvent.drawTournaments) {
for (const entry of draw.eventEntries) {
const entryP = await entry.getPlayers({
attributes: ['id', 'memberId', 'firstName', 'lastName'],
include: playerInclude,
});

for (const player of entryP) {
if (!player?.memberId) {
continue;
}

// skip if the player is already added
if (players.has(player.memberId)) {
continue;
}

let usedLevel = 12;

switch (subEvent.gameType) {
case GameType.S:
usedLevel = player?.rankingLastPlaces?.[0]?.single;
break;
case GameType.D:
usedLevel = player?.rankingLastPlaces?.[0]?.double;
break;
case GameType.MX:
usedLevel = player?.rankingLastPlaces?.[0]?.mix;
break;
default:
break;
}

if (
// check if the player has a wrong ranking
usedLevel - 2 >
subEvent.level
) {
this.logger.verbose(
`Player ${player.memberId} has wrong ranking ${usedLevel} in event ${subEvent.level} (${subEvent.name})`,
);
players.set(player.memberId, player);
}
}
}
}
}
}

if (players.size === 0) {
this.logger.verbose(`No players with wrong ranking found`);
return;
}

this.logger.verbose(`Found ${players.size} players with wrong ranking`);

// write the players to a file
const wb = xlsx.utils.book_new();
const ws = xlsx.utils.json_to_sheet(
Array.from(players.values()).map((player) => ({
memberId: player.memberId,
firstName: player.firstName,
lastName: player.lastName,
})),
);

xlsx.utils.book_append_sheet(wb, ws, 'players');
xlsx.writeFile(wb, 'players-with-wrong-ranking.xlsx');

this.logger.verbose(`Done`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class CompetitionSyncEntryProcessor extends StepProcessor {
transaction: this.transaction,
});

const drawEntries = await draw.getEntries({
const drawEntries = await draw.getEventEntries({
transaction: this.transaction,
});

Expand Down Expand Up @@ -110,7 +110,7 @@ export class CompetitionSyncEntryProcessor extends StepProcessor {
this._entries.push({ entry, xmlTeamName: item });
}

const entries = await draw.getEntries({
const entries = await draw.getEventEntries({
transaction: this.transaction,
include: [{ model: Team }],
});
Expand Down
18 changes: 9 additions & 9 deletions apps/worker/sync/src/app/processors/sync-ranking/ranking-sync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Player, RankingLastPlace, RankingPlace, RankingSystem } from '@badman/backend-database';
import { Sync } from '@badman/backend-queue';
import { VisualService } from '@badman/backend-visual';
import { RankingSystems } from '@badman/utils';
import { RankingSystems, Ranking, Gender } from '@badman/utils';
import { Logger } from '@nestjs/common';
import { Queue } from 'bull';
import { XMLParser } from 'fast-xml-parser';
Expand Down Expand Up @@ -164,7 +164,7 @@ export class RankingSyncer {
date: momentDate,
} as VisualPublication;
});
pubs = pubs?.sort((a, b) => a.date.diff(b.date));
pubs = pubs?.slice()?.sort((a, b) => a.date.diff(b.date));

// get latest publication
const last = pubs?.slice(-1)?.[0];
Expand All @@ -186,7 +186,7 @@ export class RankingSyncer {
return {
visiblePublications: pubs,
hiddenPublications: publications
?.filter((publication) => publication.Visible == false)
?.filter((publication) => !publication.Visible)
?.map((publication) => {
const momentDate = moment(publication.PublicationDate, 'YYYY-MM-DD');
return momentDate;
Expand All @@ -213,8 +213,8 @@ export class RankingSyncer {
category: string | null,
places: Map<string, RankingPlace>,
newPlayers: Map<string, Player>,
type: 'single' | 'double' | 'mix',
gender: 'M' | 'F',
type: Ranking,
gender: Gender,
) => {
if (!category) {
this.logger.error(`No category defined?`);
Expand Down Expand Up @@ -304,7 +304,7 @@ export class RankingSyncer {

// Check if other publication has create the ranking place
if (places.has(foundPlayer.id)) {
const place = places.get(foundPlayer.id) as RankingPlace;
const place = places.get(foundPlayer.id);

place[type] = points.Level;
place[`${type}Points`] = points.Totalpoints;
Expand All @@ -329,7 +329,7 @@ export class RankingSyncer {
const place = foundPlayer.rankingLastPlaces.find(
(r) => r.systemId === ranking.system.id,
);
if (place != null && place[type] != null && place[type] !== points.Level) {
if (place?.[type] != null && place[type] !== points.Level) {
place[type] = points.Level;
await place.save({ transaction: args.transaction });
}
Expand All @@ -346,7 +346,7 @@ export class RankingSyncer {
(!ranking.stopDate || publication.date.isBefore(ranking.stopDate))
) {
if (publication.usedForUpdate) {
this.logger.log(`Updating ranking on ${publication.date}`);
this.logger.log(`Updating ranking on ${publication.date.format('LLL')}`);
}

this.logger.debug(`Getting single levels for ${publication.date.format('LLL')}`);
Expand Down Expand Up @@ -505,7 +505,7 @@ export class RankingSyncer {

// For now we only check if it's the last update

const lastPublication = visiblePublications?.sort(
const lastPublication = visiblePublications.slice()?.sort(
(a, b) => b.date.valueOf() - a.date.valueOf(),
)?.[0];

Expand Down
14 changes: 4 additions & 10 deletions apps/worker/sync/src/app/utils/correctWrongPlayers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Gender } from '@badman/utils';

export const correctWrongPlayers = (player: {
id?: string;
memberId?: string;
firstName?: string;
lastName?: string;
birthDate?: Date;
gender?: 'M' | 'F';
gender?: Gender;
club?: number;
}): {
id?: string;
memberId?: string;
firstName?: string;
lastName?: string;
birthDate?: Date;
gender?: 'M' | 'F';
gender?: Gender;
club?: number;
} => {
// Yaro Van Delsen
Expand Down Expand Up @@ -5260,14 +5262,6 @@ export const correctWrongPlayers = (player: {
};
}

// // Fixme
// if (player.memberId === '') {
// return {
// ...player,
// memberId: 'Fixme'
// };
// }
//

return player;
};
Loading
Loading