Skip to content

Commit

Permalink
chore(merge): merge develop into main
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Sep 5, 2024
2 parents 0aae035 + 8cfae86 commit 8bf9987
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 83 deletions.
10 changes: 6 additions & 4 deletions apps/scripts/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { DatabaseModule } from '@badman/backend-database';
import { VisualModule } from '@badman/backend-visual';
import { configSchema, load } from '@badman/utils';
import { Logger, Module, OnModuleInit } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ExportPlayersWithRanking } from './scripts';
import { PlayersWrongRankingRunner } from './scripts';

@Module({
providers: [ExportPlayersWithRanking],
providers: [PlayersWrongRankingRunner],
imports: [
ConfigModule.forRoot({
cache: true,
validationSchema: configSchema,
load: [load],
}),
DatabaseModule,
VisualModule,
],
})
export class ScriptModule implements OnModuleInit {
private readonly logger = new Logger(ScriptModule.name);

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

async onModuleInit() {
this.logger.log('Running script');

await this.fixer.exportPlayersWithRanking();
await this.fixer.process();

this.logger.log('Script finished');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import xlsx from 'xlsx';
export class ExportPlayersWithRanking {
private readonly logger = new Logger(ExportPlayersWithRanking.name);

public async exportPlayersWithRanking() {
public async process() {
const primarySystem = await RankingSystem.findOne({
where: {
primary: true,
Expand Down Expand Up @@ -39,8 +39,8 @@ export class ExportPlayersWithRanking {
model: RankingPlace,
where: {
systemId: primarySystem.id,
rankingDate: {
[Op.in]: ['2024-08-12', '2024-07-01'],
rankingDate: {
[Op.in]: ['2024-09-02', '2024-08-12'],
},
},
},
Expand Down Expand Up @@ -77,49 +77,49 @@ export class ExportPlayersWithRanking {
this.logger.debug(`Mapping ${players.length} data for ${type}`);
const data = [];
for (const player of players) {
const rankingAug = player.rankingPlaces.find(
(rp) => rp.systemId === system.id && moment(rp.rankingDate).isSame('2024-08-12', 'day'),
const rankingSep = player.rankingPlaces.find(
(rp) => rp.systemId === system.id && moment(rp.rankingDate).isSame('2024-09-12', 'day'),
);

const rankingJul = player.rankingPlaces.find(
(rp) => rp.systemId === system.id && moment(rp.rankingDate).isSame('2024-07-01', 'day'),
const rankingAug = player.rankingPlaces.find(
(rp) => rp.systemId === system.id && moment(rp.rankingDate).isSame('2024-08-01', 'day'),
);

const averages = await this.calcaulateAverages(
player,
system,
type,
'2024-08-12',
rankingAug,
'2024-09-02',
rankingSep,
);

const pointsNeededForPromotion =
system.pointsToGoUp[system.pointsToGoUp.length + 1 - (rankingJul?.[type] ?? 0)];
system.pointsToGoUp[system.pointsToGoUp.length + 1 - (rankingAug?.[type] ?? 0)];
const pointsNeededForDowngrade =
system.pointsToGoDown[system.pointsToGoDown.length - (rankingJul?.[type] ?? 0)];
system.pointsToGoDown[system.pointsToGoDown.length - (rankingAug?.[type] ?? 0)];

const shouldHaveGoneUp =
rankingAug?.[type + 'Points'] > pointsNeededForPromotion &&
rankingJul?.[type] == rankingAug?.[type];
rankingSep?.[type + 'Points'] > pointsNeededForPromotion &&
rankingAug?.[type] == rankingSep?.[type];

let shouldHaveGoneDown =
averages.avgDowngrade < pointsNeededForDowngrade &&
rankingJul?.[type] == rankingAug?.[type];
rankingAug?.[type] == rankingSep?.[type];

shouldHaveGoneDown = this.validateShouldHaveGoneDown(
shouldHaveGoneDown,
type,
rankingJul,
rankingAug,
system,
);

data.push({
Name: player.fullName,
Number: player.memberId,
Gender: player.gender,
['Ranking sep']: rankingSep?.[type],
['Ranking aug']: rankingAug?.[type],
['Ranking jul']: rankingJul?.[type],
['Points upgrade']: rankingAug?.[type + 'Points'],
['Points upgrade']: rankingSep?.[type + 'Points'],
['Points downgrade']: averages.avgDowngrade ? Math.round(averages.avgDowngrade) : '',
['Points needed']: pointsNeededForPromotion,
['Should have gone up']: shouldHaveGoneUp ? 'Yes' : 'No',
Expand Down
2 changes: 2 additions & 0 deletions apps/scripts/src/app/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './add-locations-to-cp';
export * from './changed-encounters';
export * from './export-players-with-ranking';
export * from './incorrect-change-encounters';
export * from './players-with-wrong-ranking';
export * from './wrong-dates';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './players-with-wrong-ranking';
1 change: 1 addition & 0 deletions apps/scripts/src/app/scripts/wrong-dates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './wrong-dates.service';
56 changes: 21 additions & 35 deletions apps/scripts/src/app/scripts/wrong-dates/wrong-dates.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { EncounterCompetition } from '@badman/backend-database';
import { VisualService } from '@badman/backend-visual';
import { getSeasonPeriod } from '@badman/utils';
import { Injectable, Logger } from '@nestjs/common';
import moment from 'moment-timezone';
import { Op } from 'sequelize';
import xlsx from 'xlsx';

@Injectable()
export class WrongDatesService {
Expand All @@ -11,14 +13,13 @@ export class WrongDatesService {

constructor(private readonly visualService: VisualService) {}

async fixWrongDates(year: number) {
const startDate = moment([year, 9, 1]).toDate();
const endDate = moment([year + 1, 7, 1]).toDate();
async process(season: number) {
const period = getSeasonPeriod(season) as [string, string];

const encounters = await EncounterCompetition.findAll({
where: {
date: {
[Op.between]: [startDate, endDate],
[Op.between]: period,
},
originalDate: {
[Op.ne]: null,
Expand All @@ -28,7 +29,7 @@ export class WrongDatesService {

this.logger.log(`Found ${encounters.length} encounters`);

const wrongTimezone = [];
const toChange = [];
const notChanged = [];

for (const encounter of encounters) {
Expand All @@ -48,7 +49,7 @@ export class WrongDatesService {
return;
}

const result = await this.visualService.getDate(event.visualCode, encounter.visualCode);
const result = await this.visualService.getDate(event.visualCode, encounter.visualCode, false);

const dateBrussels = moment.tz(result, 'Europe/Brussels');

Expand All @@ -58,23 +59,6 @@ export class WrongDatesService {

const home = await encounter.getHome();
const away = await encounter.getAway();
const gmtWrong = moment.tz(result, 'Etc/GMT+0');
// Check if it was submitted without timezone update
if (gmtWrong.isSame(moment(encounter.date))) {
wrongTimezone.push(
`${home.name},${away.name},${gmtWrong.format(
this.visualFormat,
)},${moment(encounter.date).format(this.visualFormat)}`,
);

// this.visualService.changeDate(
// event.visualCode,
// encounter.visualCode,
// encounter.date
// );

continue;
}

if (dateBrussels.isSame(moment(encounter.originalDate))) {
notChanged.push(
Expand All @@ -83,6 +67,13 @@ export class WrongDatesService {
)},${moment(encounter.date).format(this.visualFormat)}`,
);

toChange.push({
home: home.name,
away: away.name,
visual: dateBrussels.format(this.visualFormat),
badman: moment(encounter.date).format(this.visualFormat),
});

// this.visualService.changeDate(
// event.visualCode,
// encounter.visualCode,
Expand All @@ -91,26 +82,21 @@ export class WrongDatesService {
continue;
}

// this.logger.log(`Changing date for encounter ${encounter.id}`);

// encounter.synced = new Date();
} catch (error) {
this.logger.error(error);
}
}

this.logger.debug(`Wrong timezone: ${wrongTimezone.length}`);
this.logger.debug(`Not changed: ${notChanged.length}`);

this.logger.log('Done');
// wirte to xlsx
const wb = xlsx.utils.book_new();
const ws = xlsx.utils.json_to_sheet(toChange);
xlsx.utils.book_append_sheet(wb, ws, 'To change');
xlsx.writeFile(wb, `wrong-dates-${season}.xlsx`);


// await writeFile(
// 'wrong-timezone.csv',
// `home,away,wrong,right\n${wrongTimezone.join('\n')}`
// );
// await writeFile(
// 'wrong-date.csv',
// `home,away,wrong,right\n${notChanged.join('\n')}`
// );
this.logger.log('Done');
}
}
19 changes: 19 additions & 0 deletions badman.babel
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,25 @@
</translation>
</translations>
</concept_node>
<concept_node>
<name>potential</name>
<description/>
<comment/>
<translations>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-BE</language>
<approved>false</approved>
</translation>
<translation>
<language>nl-BE</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>title</name>
<description/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class AssemblyValidationData {
meta?: MetaEntry;
otherMeta?: MetaEntry[];
team?: Team;
previousSeasonTeam?: Team | null;

teamIndex?: number;
teamPlayers?: Player[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RankingLastPlace,
RankingPlace,
RankingSystem,
Standing,
SubEventCompetition,
Team,
} from '@badman/backend-database';
Expand Down Expand Up @@ -84,13 +85,33 @@ export class AssemblyValidationService extends ValidationService<
const idSubs = args.subtitudes?.filter((p) => p !== undefined && p !== null);

const team = await Team.findByPk(args.teamId, {
attributes: ['id', 'name', 'type', 'teamNumber', 'clubId'],
attributes: ['id', 'name', 'type', 'teamNumber', 'clubId', 'link', 'season'],
});

if (!team) {
if (!team?.season) {
throw new Error('Team not found');
}

const previousSeasonTeam = await Team.findOne({
attributes: ['id'],
where: {
link: team.link,
season: team.season - 1,
},
include: [
{
attributes: ['id'],
model: EventEntry,
include: [
{
attributes: ['id', 'faller'],
model: Standing,
},
],
},
],
});

const encounter = (await EncounterCompetition.findByPk(args.encounterId)) || undefined;

let draw: DrawCompetition | null = null;
Expand Down Expand Up @@ -304,8 +325,6 @@ export class AssemblyValidationService extends ValidationService<
}),
);

// const titularsTeam = Team.baseTeam(players, type);

return {
type,
meta,
Expand All @@ -321,6 +340,7 @@ export class AssemblyValidationService extends ValidationService<
event,

team,
previousSeasonTeam,

system,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ export type TeamSubeventIndexRuleParams = {
export class TeamSubeventIndexRule extends Rule {
static override readonly description = 'all.rules.team-assembly.team-subevent-index';
async validate(assembly: AssemblyValidationData): Promise<AssemblyOutput> {
const { teamIndex, subEvent } = assembly;
const { teamIndex, subEvent, previousSeasonTeam } = assembly;

if (!subEvent?.minBaseIndex) {
throw new Error('Subevent is not defined');
}

// if team is degraded, it can have a lower index
if (previousSeasonTeam?.entry?.standing?.faller) {
return {
valid: true,
};
}

if ((teamIndex ?? 0) < subEvent.minBaseIndex) {
return {
valid: false,
Expand Down
Loading

0 comments on commit 8bf9987

Please sign in to comment.