Skip to content

Commit

Permalink
el ls
Browse files Browse the repository at this point in the history
  • Loading branch information
inker committed Jul 6, 2024
1 parent 1cb2c1b commit d75d004
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 167 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
ko: 2023,
},
el: {
gs: 2023,
ls: 2024,
ko: 2022,
},
ecl: {
Expand Down
File renamed without changes.
File renamed without changes.
170 changes: 170 additions & 0 deletions src/containers/LeagueStage/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { memo, useEffect, useMemo, useState } from 'react';
import styled from 'styled-components';
import { shuffle } from 'lodash';

import usePopup from '#store/usePopup';
import type Team from '#model/team/GsTeam';
import generatePairings from '#engine/dfs/ls/generatePairings/index';
import generateSchedule from '#engine/dfs/ls/generateSchedule/index';
import useAbortSignal from '#utils/hooks/useAbortSignal';
import Button from '#ui/Button';
import Portal from '#ui/Portal';
import Dots from '#ui/Dots';

import Schedule from './Schedule';
import MatchesTable from './MatchesTable';

const Root = styled.div`
display: flex;
flex-direction: column;
margin: 10px;
font-size: 14px;
`;

const MatrixWrapper = styled.div`
display: flex;
flex-wrap: wrap;
gap: 16px;
`;

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

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

const numMatches = useMemo(() => {
const numTeams = initialPots.flat().length;
return (numTeams * numMatchdays) / 2;
}, [initialPots, numMatchdays]);

const [, setPopup] = usePopup();

const [isMatchdayMode, setIsMatchdayMode] = useState(false);

const [pairings, setPairings] = useState<(readonly [Team, Team])[]>([]);
const [schedule, setSchedule] = useState<(readonly [Team, Team])[][]>(
Array.from(
{
length: numMatchdays,
},
() => [],
),
);
const [isFixturesDone, setIsFixturesDone] = useState(false);

const abortSignal = useAbortSignal();

const pots = useMemo(
() =>
initialPots.map(pot =>
pot.map(team => ({
...team,
id: `${team.country}:${team.name}`,
})),
),
[initialPots],
);

const allTeams = useMemo(() => pots.flat(), [pots]);

const matchdaySize = allTeams.length / 2;

useEffect(() => {
setPopup({
waiting: false,
});
}, []);

useEffect(() => {
const formPairings = async () => {
const generator = generatePairings({
pots,
numMatchdays: 8,
isMatchPossible: (a, b) => a.country !== b.country,
});
for await (const pickedMatch of generator) {
setPairings(prev => [...prev, pickedMatch]);
}
setIsFixturesDone(true);
};

formPairings();
}, []);

useEffect(() => {
if (isFixturesDone) {
const formSchedule = async () => {
// setIsMatchdayMode(true);
// setSchedule(chunk(pairings, 18));
const it = await generateSchedule({
matchdaySize,
allGames: pairings,
currentSchedule: schedule,
signal: abortSignal,
});
setSchedule(it.solutionSchedule.map(md => shuffle(md)));
setIsMatchdayMode(true);
};

formSchedule();
}
}, [isFixturesDone]);

const isScheduleDone = useMemo(
() => schedule.some(md => md.length > 0),
[schedule],
);

return (
<Root>
<Portal
tagName="div"
modalRoot={document.getElementById('navbar-left-container')!}
>
<Button
type="button"
disabled={!isScheduleDone}
onClick={() => {
setIsMatchdayMode(prev => !prev);
}}
>
{isMatchdayMode ? 'Display matrix' : 'Display schedule'}
</Button>
</Portal>
{isMatchdayMode ? (
<Schedule schedule={schedule} />
) : (
<MatrixWrapper>
<MatchesTable
allTeams={allTeams}
pairings={pairings}
/>
<div>
{isFixturesDone ? (
<p>All {numMatches} matches have been drawn.</p>
) : (
<p>
Drawn matches: {pairings.length}/{numMatches}
</p>
)}
{isFixturesDone && !isScheduleDone && (
<p>
Schedule creation in progress. This will take a while. Please do
not close the page
<Dots
initialNum={3}
maxNum={3}
interval={1000}
/>
</p>
)}
</div>
</MatrixWrapper>
)}
</Root>
);
}

export default memo(LeagueStage);
154 changes: 154 additions & 0 deletions src/data/el/ls/2024/pots.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
[
[
{
"name": "Roma",
"country": "Italy"
},
{
"name": "Man United",
"country": "England"
},
{
"name": "Porto",
"country": "Portugal"
},
{
"name": "Ajax",
"country": "Netherlands"
},
{
"name": "Frankfurt",
"country": "Germany"
},
{
"name": "Lazio",
"country": "Italy"
},
{
"name": "Tottenham",
"country": "England"
},
{
"name": "Real Sociedad",
"country": "Spain"
},
{
"name": "Salzburg",
"country": "Austria"
}
],
[
{
"name": "AZ",
"country": "Netherlands"
},
{
"name": "Braga",
"country": "Portugal"
},
{
"name": "Olympiacos",
"country": "Greece"
},
{
"name": "Lille",
"country": "France"
},
{
"name": "Lyon",
"country": "France"
},
{
"name": "LASK",
"country": "Austria"
},
{
"name": "Fenerbahçe",
"country": "Turkey"
},
{
"name": "Young Boys",
"country": "Switzerland"
},
{
"name": "Qarabağ",
"country": "Azerbaijan"
}
],
[
{
"name": "Galatasaray",
"country": "Turkey"
},
{
"name": "Slovan",
"country": "Slovakia"
},
{
"name": "Molde",
"country": "Norway"
},
{
"name": "Plzeň",
"country": "Czechia"
},
{
"name": "Bodø/Glimt",
"country": "Norway"
},
{
"name": "Union SG",
"country": "Belgium"
},
{
"name": "Dynamo Kyiv",
"country": "Ukraine"
},
{
"name": "Ludogorets",
"country": "Bulgaria"
},
{
"name": "Midtjylland",
"country": "Denmark"
}
],
[
{
"name": "Partizan",
"country": "Serbia"
},
{
"name": "Sparta",
"country": "Czechia"
},
{
"name": "Sheriff",
"country": "Moldova"
},
{
"name": "Malmö",
"country": "Sweden"
},
{
"name": "Athletic",
"country": "Spain"
},
{
"name": "Hoffenheim",
"country": "Germany"
},
{
"name": "Nice",
"country": "France"
},
{
"name": "APOEL",
"country": "Cyprus"
},
{
"name": "Twente",
"country": "Netherlands"
}
]
]
Loading

0 comments on commit d75d004

Please sign in to comment.