-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
117 lines (96 loc) · 2.82 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {
positions,
TeamName,
TeamPosition,
} from "@/app/positions-regular-zone";
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import { MatchResult, Round } from "./types";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
const PROMIEDOS_URL = "https://www.promiedos.com.ar";
export const getImageURL = (imageSrc: string) => {
return `${PROMIEDOS_URL}/${imageSrc}`;
};
export const findTeam = (team: TeamName | (string & {})) => {
const teamPosition = [...positions.zoneA, ...positions.zoneB].find(
(teamPosition) => teamPosition.team === team
);
if (!teamPosition) {
throw new Error(`${team} not found`);
}
return teamPosition;
};
export const defineVentajaDeportiva = (
teamA: TeamPosition,
teamB: TeamPosition
) => {
const haveSamePosition = teamA.position === teamB.position;
if (haveSamePosition) {
return teamA.pts > teamB.pts ? teamA : teamB;
}
return teamA.position < teamB.position ? teamA : teamB;
};
export const getNextRound = (round: Round): Round => {
const classifiedTeamsPositions = round
.map((result) => (result.classified === "home" ? result.home : result.away))
.sort((a, b) => {
const betterVentajaDeportiva = defineVentajaDeportiva(a, b);
return a.team === betterVentajaDeportiva.team ? 1 : -1;
});
const nextRoundMatches: Round = [];
for (let i = 0; i < round.length; i += 2) {
const teamA = classifiedTeamsPositions[i];
const teamB =
classifiedTeamsPositions[classifiedTeamsPositions.length - 1 - i];
const home =
classifiedTeamsPositions.indexOf(teamA) >
classifiedTeamsPositions.indexOf(teamB)
? teamA
: teamB;
const away =
classifiedTeamsPositions.indexOf(teamA) >
classifiedTeamsPositions.indexOf(teamB)
? teamB
: teamA;
nextRoundMatches.push({
home,
away,
classified: null,
result: null,
isResultFromReality: false,
});
}
return nextRoundMatches;
};
const REDUCIDO_RESULTS: MatchResult[] = [];
export const replaceWithRealResults = (
round: Round,
skippedRoundsUntilNow: Round[] = []
): {
round: Round;
skippedRounds: Round[];
} => {
const roundWithReality = round.map((match) => {
const { home, away } = match;
const resultInReality = REDUCIDO_RESULTS.find(
(result) =>
result.home.team === home.team && result.away.team === away.team
);
return resultInReality ?? match;
});
const allIsReality = roundWithReality.every(
(match) => match.isResultFromReality
);
if (allIsReality) {
return replaceWithRealResults(getNextRound(roundWithReality as Round), [
...skippedRoundsUntilNow,
roundWithReality,
]);
}
return {
round: roundWithReality,
skippedRounds: skippedRoundsUntilNow,
};
};