Skip to content
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
33 changes: 23 additions & 10 deletions pages/rounds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,28 @@ export default function Rounds() {
const [courtsModal, setCourtsModal] = useState(false);

const [roundIndex, setRoundIndex] = useState(0);
const prevRoundCount = React.useRef(state.rounds.length);

// Handle rounds loading into state.
// Jump to latest round when a new round is appended (not when regenerating).
useEffect(() => {
if (state.rounds.length && roundIndex === 0) {
setRoundIndex(Math.max(state.rounds.length - 1, 0));
if (state.rounds.length > prevRoundCount.current) {
setRoundIndex(state.rounds.length - 1);
window.scrollTo(0, 0);
} else if (roundIndex >= state.rounds.length && state.rounds.length > 0) {
setRoundIndex(state.rounds.length - 1);
}
prevRoundCount.current = state.rounds.length;
}, [state.rounds.length, roundIndex]);

useEffect(() => {
if (state.rounds.length && roundIndex === 0 && prevRoundCount.current <= 1) {
setRoundIndex(Math.max(state.rounds.length - 1, 0));
}
}, [state.rounds]);
}, [state.rounds.length, roundIndex]);

const displayIndex = Math.max(
0,
Math.min(roundIndex, state.rounds.length - 1)
Math.min(roundIndex, Math.max(state.rounds.length - 1, 0))
);
const round = state.rounds[displayIndex];
const volunteers = state.volunteerSitoutsByRound[displayIndex];
Expand Down Expand Up @@ -84,7 +95,6 @@ export default function Rounds() {
fixedPairs,
regenerate,
});
if (!regenerate && roundIndex) setRoundIndex((index) => index + 1);
setPlayersModal(false);
}}
/>
Expand All @@ -96,7 +106,6 @@ export default function Rounds() {
regenerate,
courts,
});
if (!regenerate && roundIndex) setRoundIndex((index) => index + 1);
setCourtsModal(false);
}}
/>
Expand Down Expand Up @@ -138,6 +147,9 @@ export default function Rounds() {
)}
</div>

{state.generating && !round ? (
<p className="text-center text-lg my-8">Jumbling the next round…</p>
) : null}
<div className="flex gap-4 items-stretch justify-center flex-wrap">
{/* Sitting out */}{" "}
<div className="basis-full sm:basis-64 md:basis-64 xl:basis-64">
Expand Down Expand Up @@ -224,16 +236,17 @@ export default function Rounds() {
<div className="flex justify-around">
<Button
size="lg"
isDisabled={state.generating}
onPress={async () => {
await newRound(dispatch, state, worker, {
volunteerSitouts: [],
});
setRoundIndex(state.rounds.length);
window.scrollTo(0, 0);
}}
className="bg-gradient-to-l from-blue-600 to-pink-600 text-white"
>
Start round {state.rounds.length + 1}!
{state.generating
? "Jumbling…"
: `Start round ${state.rounds.length + 1}!`}
</Button>
</div>
<Spacer y={2} />
Expand Down
10 changes: 8 additions & 2 deletions src/CourtsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ export function CourtsModal({
Cancel
</Button>
<Button
onPress={() => onSubmit(parseInt(courts), true)}
onPress={() => {
const count = parseInt(courts, 10);
if (count >= 1) onSubmit(count, true);
}}
color="danger"
>
Redo round
</Button>
<Button
onPress={() => onSubmit(parseInt(courts), false)}
onPress={() => {
const count = parseInt(courts, 10);
if (count >= 1) onSubmit(count, false);
}}
color="primary"
>
New round
Expand Down
56 changes: 40 additions & 16 deletions src/matching/heuristics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,10 @@ const getSitOuts = (
const players = allPlayers.filter(
(player) => !expandedVolunteers.includes(player)
);
const capacity = courts * 4;
const rawSitouts =
players.length > capacity
? players.length - courts * 4
: players.length % 4;
const capacity = Math.max(courts, 1) * 4;
const playable = Math.min(players.length, capacity);
const playableRounded = playable - (playable % 4);
const rawSitouts = players.length - playableRounded;

const units = buildSitOutUnits(players, fixedPairs);
const sitouts = adjustSitOutCountForUnits(rawSitouts, units);
Expand Down Expand Up @@ -942,8 +941,12 @@ async function getNextRound(
if (!bestMatches) {
throw new Error("no matches found");
}
const maxMatches = Math.max(courts, 1);
return [
{ sitOuts: bestTeams.sitOuts, matches: bestMatches },
{
sitOuts: bestTeams.sitOuts,
matches: bestMatches.slice(0, maxMatches),
},
{ bestTeamScore, bestMatchesScore },
];
}
Expand Down Expand Up @@ -993,7 +996,7 @@ async function getNextBestRound(
for (let attempt = 0; attempt < ROUND_ATTEMPTS; attempt++) {
await new Promise((resolve) => resolve(undefined));
let newHeuristics = heuristics;
let newRounds = [];
let newRounds: Round[] = [];
let backToBackOpponents = Infinity;
let partnerScore = 0;
let opponentScore = Infinity;
Expand All @@ -1005,20 +1008,30 @@ async function getNextBestRound(
roundGeneration++
) {
try {
const [newRound, roundStats] = await getNextRound(
rounds,
const [candidateRound, roundStats]: [
Round,
{ bestTeamScore: number; bestMatchesScore: number },
] = await getNextRound(
[...rounds, ...newRounds],
players,
courts,
volunteerSitouts,
heuristics,
roundGeneration === 0 ? volunteerSitouts : [],
newHeuristics,
fixedPairs
);
const [, newDuplicates] = getUniqueMatchCounts([newRound], matchCounts);
newHeuristics = getHeuristics([newRound], players, newHeuristics);
newRounds.push(newRound);
const [, newDuplicates] = getUniqueMatchCounts(
[candidateRound],
matchCounts
);
newHeuristics = getHeuristics(
[candidateRound],
players,
newHeuristics
);
newRounds.push(candidateRound);
if (roundGeneration === 0) {
backToBackOpponents = countBackToBackOpponentRepeats(
newRound,
candidateRound,
heuristics
);
}
Expand Down Expand Up @@ -1062,7 +1075,18 @@ async function getNextBestRound(
}
}

return selectedRound!;
if (!selectedRound) {
const [fallbackRound] = await getNextRound(
rounds,
players,
courts,
volunteerSitouts,
heuristics,
fixedPairs
);
return fallbackRound;
}
return selectedRound;
}

export {
Expand Down
46 changes: 34 additions & 12 deletions src/useShuffler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Action =
round: Round;
courts?: number;
volunteerSitouts: PlayerId[];
regenerate?: boolean;
};
}
| {
Expand Down Expand Up @@ -216,30 +217,42 @@ function shufflerReducer(state: State, action: Action): State {
return loadFromCache(state);
}
case "start-generation":
const { regenerate } = action.payload;
return {
...state,
generating: true,
rounds: regenerate ? state.rounds.slice(0, -1) : state.rounds,
volunteerSitoutsByRound: regenerate
? state.volunteerSitoutsByRound.slice(0, -1)
: state.volunteerSitoutsByRound,
players: action.payload.players || state.players,
playersById: action.payload.playersById || state.playersById,
fixedPairs: action.payload.fixedPairs ?? state.fixedPairs,
};
case "new-round": {
const { regenerate } = action.payload;
const rounds = regenerate
? [...state.rounds.slice(0, -1), action.payload.round]
: [...state.rounds, action.payload.round];
const volunteerSitoutsByRound = regenerate
? [
...state.volunteerSitoutsByRound.slice(0, -1),
action.payload.volunteerSitouts,
]
: [
...state.volunteerSitoutsByRound,
action.payload.volunteerSitouts,
];
return cacheState({
...state,
generating: false,
rounds: [...state.rounds, action.payload.round],
courts: action.payload.courts || state.courts,
volunteerSitoutsByRound: [
...state.volunteerSitoutsByRound,
action.payload.volunteerSitouts,
],
rounds,
courts: action.payload.courts ?? state.courts,
volunteerSitoutsByRound,
});
}
case "new-round-fail":
case "new-game-fail": {
return {
...state,
generating: false,
};
}
}
return state;
}
Expand All @@ -264,9 +277,16 @@ async function newRound(
payload.volunteerSitouts,
state.fixedPairs
);
if (!nextRound?.matches) {
throw new Error("Round generation returned an empty round");
}
dispatch({
type: "new-round",
payload: { round: nextRound, volunteerSitouts: payload.volunteerSitouts },
payload: {
round: nextRound,
volunteerSitouts: payload.volunteerSitouts,
regenerate: payload.regenerate ?? false,
},
});
} catch (error) {
dispatch({ type: "new-round-fail", payload: { error: error as Error } });
Expand Down Expand Up @@ -374,6 +394,7 @@ async function editCourts(
round,
volunteerSitouts,
courts,
regenerate,
},
});
} catch (error) {
Expand Down Expand Up @@ -423,6 +444,7 @@ async function editPlayers(
payload: {
round: nextRound,
volunteerSitouts,
regenerate,
},
});
} catch (error) {
Expand Down
Loading