Fix Sikinia Parliament game end

This commit is contained in:
gpt-engineer-app[bot] 2025-07-23 20:51:26 +00:00
parent 2e75dfc30e
commit 6acfc2748b

View file

@ -90,8 +90,7 @@ const SikiniaParliamentPuzzle: React.FC<SikiniaParliamentPuzzleProps> = ({ showS
// Check for violations and update edge colors
const updateViolations = useCallback(() => {
setEdges(currentEdges => {
return currentEdges.map(edge => {
const updatedEdges = edges.map(edge => {
const person1 = people.find(p => p.id === edge.from);
const person2 = people.find(p => p.id === edge.to);
@ -101,14 +100,14 @@ const SikiniaParliamentPuzzle: React.FC<SikiniaParliamentPuzzleProps> = ({ showS
if (person1.color === person2.color) {
// Check if either person has more than 1 enemy of their color
const person1Enemies = currentEdges.filter(e =>
const person1Enemies = edges.filter(e =>
(e.from === person1.id || e.to === person1.id) &&
e !== edge
).map(e => e.from === person1.id ? e.to : e.from)
.map(id => people.find(p => p.id === id))
.filter(p => p && p.color === person1.color);
const person2Enemies = currentEdges.filter(e =>
const person2Enemies = edges.filter(e =>
(e.from === person2.id || e.to === person2.id) &&
e !== edge
).map(e => e.from === person2.id ? e.to : e.from)
@ -120,8 +119,10 @@ const SikiniaParliamentPuzzle: React.FC<SikiniaParliamentPuzzleProps> = ({ showS
return { ...edge, isViolation: false };
});
});
}, [people]);
setEdges(updatedEdges);
return updatedEdges;
}, [people, edges]);
// Check if puzzle is solved
const checkCompletion = useCallback(() => {
@ -142,9 +143,24 @@ const SikiniaParliamentPuzzle: React.FC<SikiniaParliamentPuzzleProps> = ({ showS
}, [people, edges, gameStarted, isComplete, timer, bestTime]);
useEffect(() => {
updateViolations();
checkCompletion();
}, [people, updateViolations, checkCompletion]);
const updatedEdges = updateViolations();
// Check completion with the updated edges
if (gameStarted && !isComplete) {
const allColored = people.every(p => p.color !== 'grey');
const noViolations = updatedEdges.every(e => !e.isViolation);
if (allColored && noViolations) {
setIsComplete(true);
setIsRunning(false);
if (!bestTime || timer < bestTime) {
setBestTime(timer);
localStorage.setItem('sikinia-best-time', timer.toString());
}
}
}
}, [people, updateViolations, gameStarted, isComplete, timer, bestTime]);
// Load best time from localStorage
useEffect(() => {