Commit cc8603af authored by Malsha Jayakody's avatar Malsha Jayakody

fix message errors

parent 5229c7c7
...@@ -150,50 +150,45 @@ const Game02Level01 = ({navigation, route}) => { ...@@ -150,50 +150,45 @@ const Game02Level01 = ({navigation, route}) => {
if (budget - item.cost >= 0) { if (budget - item.cost >= 0) {
setBudget(budget - item.cost); setBudget(budget - item.cost);
setCollectedItems([...collectedItems, item]); setCollectedItems([...collectedItems, item]);
if (item.type === 'healthy') { if (item.type === 'healthy') {
setHealthyCost(prevCost => prevCost + item.cost); setHealthyCost(prevCost => prevCost + item.cost);
} else { } else {
setUnhealthyCost(prevCost => prevCost + item.cost); setUnhealthyCost(prevCost => prevCost + item.cost);
} }
// Use a setTimeout to ensure state updates have been applied before check // Use a setTimeout to ensure state updates have been applied before check
setTimeout(() => { setTimeout(() => {
if (unhealthyCost > 2000) { if (unhealthyCost + item.cost > 2000) { // ensure the current cost is added to total for the check
Alert.alert('Game lost!', 'Choose healthier foods. 🍇🍌🍉🥬🥕🍒', Alert.alert('Game lost!', 'Choose healthier foods. 🍇🍌🍉🥬🥕🍒',
[ [
{ {
text: 'OK', text: 'OK',
onPress: () => { onPress: () => navigation.navigate(ROUTES.GAME_QUIZ_OPTIONS),
navigation.navigate(ROUTES.GAME_QUIZ_OPTIONS);
},
}, },
]); ]);
} else if (healthyCost > 2000) { } else if (healthyCost + item.cost > 2000) { // ensure the current cost is added to total for the check
Alert.alert('Game won!', Alert.alert('Game won!', 'Congratulations! You have made healthy choices.',
[ [
{ {
text: 'OK', text: 'OK',
onPress: () => { onPress: () => navigation.navigate(ROUTES.Game02ProgressLevel03),
navigation.navigate(ROUTES.Game02ProgressLevel03);
},
}, },
]); ]);
backgroundMusic.current.setVolume(0.0); backgroundMusic.current.setVolume(0.0);
} }
}, 0); }, 0);
} else { } else {
Alert.alert('Not enough budget', Alert.alert('Not enough budget', 'Please try to manage your budget better.',
[ [
{ {
text: 'OK', text: 'OK',
onPress: () => { onPress: () => navigation.navigate(ROUTES.CHOOSE_GAME),
navigation.navigate(ROUTES.CHOOSE_GAME);
},
}, },
]); ]);
} }
}; };
return ( return (
<ScrollView style={styles.container}> <ScrollView style={styles.container}>
......
...@@ -81,44 +81,39 @@ const CravingsPuzzleGame02Level02 = ({navigation}) => { ...@@ -81,44 +81,39 @@ const CravingsPuzzleGame02Level02 = ({navigation}) => {
}; };
const purchaseItem = item => { const purchaseItem = item => {
// Check if the budget allows for the purchase
if (budget - item.cost >= 0) { if (budget - item.cost >= 0) {
// Update the budget and purchased items
setBudget(budget - item.cost); setBudget(budget - item.cost);
setPurchasedItems([...purchasedItems, item]); setPurchasedItems([...purchasedItems, item]);
// Update the cost tracking based on whether the item is essential
if (item.essential === true) { if (item.essential === true) {
setHealthyCost(prevCost => prevCost + item.cost); setHealthyCost(prevCost => prevCost + item.cost);
} else { } else {
setUnhealthyCost(prevCost => prevCost + item.cost); setUnhealthyCost(prevCost => prevCost + item.cost);
} }
setTimeout(() => { // Delay is unnecessary, so directly execute the checks
if (unhealthyCost > 2000) { if (unhealthyCost + item.cost > 2000) {
Alert.alert('Game lost!', 'Choose essintial items!', [ Alert.alert('Game lost!', 'Choose essential items!', [
{ {
text: 'OK', text: 'OK',
onPress: () => { onPress: () => navigation.navigate(ROUTES.GAME_QUIZ_OPTIONS),
navigation.navigate(ROUTES.GAME_QUIZ_OPTIONS); },
}, ]);
}, } else if (healthyCost + item.cost > 2000) {
]); Alert.alert('Game won!', 'Congratulations!', [
} else if (healthyCost > 2000) { {
Alert.alert('Game won!', [ text: 'OK',
{ onPress: () => navigation.navigate(ROUTES.Game02ProgressLevel06),
text: 'OK', },
onPress: () => { ]);
navigation.navigate(ROUTES.Game02ProgressLevel06); backgroundMusic.current.setVolume(0.0);
}, }
},
]);
backgroundMusic.current.setVolume(0.0);
}
}, 0);
} else { } else {
Alert.alert('Not enough budget'); Alert.alert('Not enough budget');
} }
setBudget(currentBudget => currentBudget - item.cost);
setPurchasedItems(currentItems => [...currentItems, item]);
}; };
return ( return (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment