Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #0000 fix: Lives mechanics changes for getset result #29

Merged
merged 1 commit into from
Apr 5, 2024
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
4 changes: 4 additions & 0 deletions src/components/DiscoverSentance/DiscoverSentance.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const SpeakSentenceComponent = () => {
const [disableScreen, setDisableScreen] = useState(false);
const [play] = useSound(LevelCompleteAudio);
const [openMessageDialog, setOpenMessageDialog] = useState("");
const [totalSyllableCount, setTotalSyllableCount] = useState('');

Comment on lines +42 to +43
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider initializing totalSyllableCount with a numerical value (e.g., 0) instead of an empty string if it's meant to store a count.

-  const [totalSyllableCount, setTotalSyllableCount] = useState('');
+  const [totalSyllableCount, setTotalSyllableCount] = useState(0);

This change ensures type consistency and prevents potential type coercion issues when performing arithmetic operations or comparisons.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const [totalSyllableCount, setTotalSyllableCount] = useState('');
const [totalSyllableCount, setTotalSyllableCount] = useState(0);


const callConfettiAndPlay = () => {
play();
Expand Down Expand Up @@ -159,6 +161,7 @@ const SpeakSentenceComponent = () => {
session_id: localStorage.getItem("sessionId"),
user_id: localStorage.getItem("virtualId"),
collectionId: currentCollectionId,
totalSyllableCount: totalSyllableCount,
language: localStorage.getItem("lang"),
}
);
Expand Down Expand Up @@ -269,6 +272,7 @@ const SpeakSentenceComponent = () => {
`${process.env.REACT_APP_LEARNER_AI_APP_HOST}/${config.URLS.GET_PAGINATION}?page=1&limit=5&collectionId=${sentences?.content?.[0]?.collectionId}`
);
setCurrentContentType("Sentence");
setTotalSyllableCount(resPagination?.data?.totalSyllableCount)
setCurrentCollectionId(sentences?.content?.[0]?.collectionId);
setAssessmentResponse(resAssessment);
localStorage.setItem("storyTitle", sentences?.name);
Expand Down
23 changes: 11 additions & 12 deletions src/utils/VoiceAnalyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function VoiceAnalyser(props) {
);
data = updateLearnerData;
responseText = data.responseText;
newThresholdPercentage = data?.targetsPercentage || 0;
newThresholdPercentage = data?.subsessionTargetsCount || 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that subsessionTargetsCount is validated and has a fallback value to prevent potential division by zero or other calculation errors in handlePercentageForLife.

-        newThresholdPercentage = data?.subsessionTargetsCount || 0;
+        newThresholdPercentage = data?.subsessionTargetsCount || 1; // Consider using a default value that prevents division by zero.

This change helps to ensure that the application behaves predictably even when subsessionTargetsCount is not provided or is zero.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
newThresholdPercentage = data?.subsessionTargetsCount || 0;
newThresholdPercentage = data?.subsessionTargetsCount || 1; // Consider using a default value that prevents division by zero.

handlePercentageForLife(newThresholdPercentage);
}

Expand Down Expand Up @@ -388,21 +388,12 @@ function VoiceAnalyser(props) {
};

const handlePercentageForLife = (percentage) => {
percentage = (percentage / livesData.totalTargets) * 100;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a check to ensure livesData.totalTargets is not zero before performing division to calculate the percentage.

+    if (livesData.totalTargets === 0) return; // Prevent division by zero
     percentage = (percentage / livesData.totalTargets) * 100;

This precaution prevents potential runtime errors due to division by zero.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
percentage = (percentage / livesData.totalTargets) * 100;
if (livesData.totalTargets === 0) return; // Prevent division by zero
percentage = (percentage / livesData.totalTargets) * 100;

try {
const THRESHOLD_PERCENTAGE = 30;
let newLivesData = {};

if (livesData) {
if (percentage > THRESHOLD_PERCENTAGE) {
let redLivesToShow = 0;
let blackLivesToShow = 5;
newLivesData = {
...livesData,
blackLivesToShow,
redLivesToShow,
};
// 5 black , 0 red
} else if (percentage >= 0 && percentage <= 5) {
if (percentage >= 0 && percentage <= 5) {
let redLivesToShow = 5;
let blackLivesToShow = 0;
newLivesData = {
Expand Down Expand Up @@ -447,6 +438,14 @@ function VoiceAnalyser(props) {
redLivesToShow,
};
// 1 red , 4 black
}else{
let redLivesToShow = 0;
let blackLivesToShow = 5;
newLivesData = {
...livesData,
blackLivesToShow,
redLivesToShow,
};
}

var audio = new Audio(
Expand Down
11 changes: 8 additions & 3 deletions src/views/Practice/Practice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const Practice = () => {
const [openMessageDialog, setOpenMessageDialog] = useState("");
const { state } = useLocation();
const lang = getLocalData("lang");
const [totalSyllableCount, setTotalSyllableCount] = useState('');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize totalSyllableCount with a numerical value (e.g., 0) instead of an empty string for consistency and to avoid type coercion issues.

-  const [totalSyllableCount, setTotalSyllableCount] = useState('');
+  const [totalSyllableCount, setTotalSyllableCount] = useState(0);

This ensures that totalSyllableCount is always treated as a number, which is important for calculations and comparisons.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const [totalSyllableCount, setTotalSyllableCount] = useState('');
const [totalSyllableCount, setTotalSyllableCount] = useState(0);


const gameOver = (data) => {
let userWon = livesData?.redLivesToShow > 0;
Expand Down Expand Up @@ -223,6 +224,7 @@ const Practice = () => {
sessionId: sessionId,
subSessionId: sub_session_id,
milestoneLevel: getSetData?.data?.currentLevel,
totalSyllableCount: totalSyllableCount,
language: localStorage.getItem("lang"),
}
);
Expand Down Expand Up @@ -280,9 +282,10 @@ const Practice = () => {
`${process.env.REACT_APP_LEARNER_AI_APP_HOST}/${config.URLS.GET_CONTENT}/${currentGetContent.criteria}/${virtualId}?language=${lang}&contentlimit=${limit}&gettargetlimit=${limit}`
);

setTotalSyllableCount(resGetContent?.data?.totalSyllableCount)
setLivesData({
...livesData,
totalTargets: resGetContent?.data?.totalTargets,
totalTargets: resGetContent?.data?.totalSyllableCount,
targetsForLives:
resGetContent?.data?.totalTargets * TARGETS_PERCENTAGE,
targetPerLive:
Expand Down Expand Up @@ -413,9 +416,10 @@ const Practice = () => {
const resWord = await axios.get(
`${process.env.REACT_APP_LEARNER_AI_APP_HOST}/${config.URLS.GET_CONTENT}/${currentGetContent.criteria}/${virtualId}?language=${lang}&contentlimit=${limit}&gettargetlimit=${limit}`
);
setTotalSyllableCount(resGetContent?.data?.totalSyllableCount)
setLivesData({
...livesData,
totalTargets: resWord?.data?.totalTargets,
totalTargets: resWord?.data?.totalSyllableCount,
targetsForLives: resWord?.data?.totalTargets * TARGETS_PERCENTAGE,
targetPerLive:
(resWord?.data?.totalTargets * TARGETS_PERCENTAGE) / LIVES,
Expand Down Expand Up @@ -502,9 +506,10 @@ const Practice = () => {
const resWord = await axios.get(
`${process.env.REACT_APP_LEARNER_AI_APP_HOST}/${config.URLS.GET_CONTENT}/${currentGetContent.criteria}/${virtualId}?language=${lang}&contentlimit=${limit}&gettargetlimit=${limit}`
);
setTotalSyllableCount(resGetContent?.data?.totalSyllableCount)
setLivesData({
...livesData,
totalTargets: resWord?.data?.totalTargets,
totalTargets: resWord?.data?.totalSyllableCount,
targetsForLives: resWord?.data?.totalTargets * TARGETS_PERCENTAGE,
targetPerLive:
(resWord?.data?.totalTargets * TARGETS_PERCENTAGE) / LIVES,
Expand Down