Skip to content

Commit

Permalink
incompatible countries
Browse files Browse the repository at this point in the history
  • Loading branch information
inker committed Jul 6, 2024
1 parent 8a2ca49 commit 5cca95a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/containers/LeagueStage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ const MatrixWrapper = styled.div`
`;

interface Props {
season: number;
pots: readonly (readonly Team[])[];
}

function LeagueStage({ pots: initialPots }: Props) {
function LeagueStage({ season, pots: initialPots }: Props) {
const numMatchdays = initialPots.length * 2;

const numMatches = useMemo(() => {
Expand Down Expand Up @@ -80,9 +81,9 @@ function LeagueStage({ pots: initialPots }: Props) {
useEffect(() => {
const formPairings = async () => {
const generator = generatePairings({
season,
pots,
numMatchdays: 8,
isMatchPossible: (a, b) => a.country !== b.country,
});
for await (const pickedMatch of generator) {
setPairings(prev => [...prev, pickedMatch]);
Expand Down
20 changes: 15 additions & 5 deletions src/engine/dfs/ls/generatePairings/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { range, shuffle } from 'lodash';

import { type Country } from '#model/types';
import { type UefaCountry } from '#model/types';
import incompatibleCountries from '#engine/predicates/uefa/utils/incompatibleCountries';

import generateFull from './generateFull';
import getFirstSuitableMatch from './getFirstSuitableMatch.wrapper';

interface Team {
country: Country;
country: UefaCountry;
}

export default async function* generatePairings<T extends Team>({
season,
pots,
numMatchdays,
isMatchPossible,
}: {
season: number;
pots: readonly (readonly T[])[];
numMatchdays: number;
isMatchPossible: (h: T, a: T) => boolean;
}) {
const teams = pots.flat();
const numTeamsPerPot = pots[0].length;
Expand All @@ -28,7 +29,16 @@ export default async function* generatePairings<T extends Team>({

allGames = [...allGames, ...allGames.map(([a, b]) => [b, a] as const)];

allGames = allGames.filter(([h, a]) => isMatchPossible(teams[h], teams[a]));
const isCountryIncompatibleWith = incompatibleCountries(season);

allGames = allGames.filter(([h, a]) => {
const hTeam = teams[h];
const aTeam = teams[a];
const isImpossible =
hTeam.country === aTeam.country ||
isCountryIncompatibleWith(hTeam)(aTeam);
return !isImpossible;
});

const matches: (readonly [number, number])[] = [];

Expand Down

0 comments on commit 5cca95a

Please sign in to comment.