change Ui

This commit is contained in:
Suvodip
2025-01-23 19:16:56 +05:30
parent 9809ca86d9
commit eb7d7e9da8
20 changed files with 2086 additions and 1003 deletions

View File

@@ -92,7 +92,10 @@ import Layout from "../../layouts/Layout.astro";
<div id="scoreBoard" class=" w-full h-screen bg-[#00000070] absolute" style="display: none;">
<div class="flex absolute left-1/2 top-1/2 -translate-y-1/2 -translate-x-1/2 max-w-lg flex-col rounded-[8px] bg-white space-y-2 w-full p-6" >
<div class="flex flex-row space-x-4 items-center justify-center">
<div id="star-container"></div>
<div id="star-container" class="relative inline-flex items-center justify-center">
<img src="/assets/bg-star.svg" alt="" class="block" />
<p id="get-star-value" class="absolute inset-0 flex items-center justify-center text-[#E76408] text-[40px] font-[900]"></p>
</div>
</div>
<div class="flex flex-row" style="margin-top: 15px; margin-bottom: 15px;">
<img class="z-10" src="/assets/clip-art.svg" alt="" />
@@ -141,6 +144,7 @@ import Layout from "../../layouts/Layout.astro";
const userId = params.get('userId');
const gameName = params.get('gameName');
const assignmentId = params.get('assignmentId');
const worksheetId = params.get('worksheetId');
let startTime = Date.now();
let gameData = null;
let totalPoints = 0;
@@ -149,7 +153,7 @@ import Layout from "../../layouts/Layout.astro";
// let gameName = urlSplit[3] + '-' + urlSplit[4].split('?')[0];
let wrongCount = 0;
let starValue;
let maxStarValue = 5;
let maxScore = 6;
let progress = 0;
const progressBar = document.createElement('div');
@@ -276,125 +280,29 @@ import Layout from "../../layouts/Layout.astro";
element: element
};
});
// Count points based on checkbox values and "greenBorder" class
checkboxValues.forEach(checkbox => {
if (checkbox.checked && checkbox.element.classList.contains('greenBorder')) {
totalPoints += 1;
}
});
let snapImage = '';
const div = document.getElementById("snapshotDiv");
const images = div.getElementsByTagName('img');
let loadedImagesCount = 0;
// Check if all images in the div are loaded
for (let img of images) {
if (img.complete) {
loadedImagesCount++;
} else {
img.onload = () => {
loadedImagesCount++;
if (loadedImagesCount === images.length) {
takeSnapshot();
}
};
}
let starValue = Math.round((totalPoints / maxScore) * 10);
starValue = starValue === 0 ? 3 : starValue;
let feedbackMessage = '';
if (starValue >= 1 && starValue <= 3) {
feedbackMessage = "You're making progress! Let's keep going!";
} else if (starValue >= 4 && starValue <= 6) {
feedbackMessage = "Nice work! You're improving every day!";
} else if (starValue >= 7 && starValue <= 8) {
feedbackMessage = "Amazing work! Keep it up!";
} else if (starValue >= 9 && starValue <= 10) {
feedbackMessage = "Outstanding! You're a true superstar!";
} else {
feedbackMessage = "Invalid star value.";
}
// If images are already loaded
if (loadedImagesCount === images.length) {
takeSnapshot();
}
function takeSnapshot() {
html2canvas(div, {
useCORS: true,
allowTaint: true,
})
.then((canvas) => {
snapImage = canvas.toDataURL("image/jpeg");
// Automatically download the snapshot
const link = document.createElement("a");
link.href = snapImage;
link.download = "snapshot.jpg";
link.click();
// Call the function to save the screenshot after it is generated
saveScreenshot(snapImage);
})
.catch((error) => {
console.error('Error taking snapshot:', error);
});
}
function saveScreenshot(snapImage) {
const saveUserDataForScreenshot = {
gameName,
gameID: gameId,
screenShot: snapImage,
userId,
gameTime: timeDifferenceInSeconds,
};
fetch('http://localhost:5175/api/saveGalleryImage', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(saveUserDataForScreenshot),
})
.then((response) => response.json())
.then((res) => {
if (res.status === 200) {
const userAnotherData = {
assignmentId,
worksheetId: gameId,
studentId: userId,
submissionFile: res.screenshotUrl,
};
return fetch('https://preschool-curriculum.in/api/one/v1/akademy/worksheet/submissions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userAnotherData),
});
} else {
throw new Error('Failed to save the screenshot');
}
})
.then((response) => response.json())
.then((data) => {
console.log('Successfully submitted:', data);
})
.catch((error) => {
console.error('Error saving or submitting the screenshot:', error);
});
}
wrongCount === 0 ? starValue = 5 : wrongCount === 1 ? starValue = 4 : starValue = 3;
let feedbackMessage; starValue === 5 ? feedbackMessage = `Fantastic! You earned ${starValue} stars! <br> Keep up the amazing work you're a superstar!` : starValue === 4 ? feedbackMessage = `Great job! ${starValue} stars you're so close to <br> perfection! ` : starValue === 3 ? feedbackMessage = `Well done! ${starValue} stars for your effort! <br> Keep pushing, youve got this!` : '';
let userData = {
'gameName': gameName,
'gameID': gameId,
'userId': userId,
'gameTime': timeDifferenceInSeconds,
'score': totalPoints,
'gameStar': starValue
};
progressIncrement();
addStarScalingStyles();
showClipArt();
const blankStar = `<svg width="61" height="58" viewBox="0 0 61 58" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M60.0193 21.917C59.8274 21.3224 59.4648 20.7975 58.9767 20.4076C58.4885 20.0177 57.8964 19.7801 57.2741 19.7244L39.9564 18.1518L33.1086 2.12366C32.6035 0.949062 31.4537 0.188965 30.176 0.188965C28.8984 0.188965 27.7484 0.94918 27.2436 2.12659L20.3957 18.1519L3.07528 19.7244C2.45372 19.7813 1.86254 20.0194 1.37503 20.4092C0.887526 20.799 0.525135 21.3232 0.332789 21.917C-0.0622597 23.132 0.302554 24.4646 1.26527 25.3046L14.3555 36.7846L10.4954 53.7878C10.213 55.038 10.6982 56.3304 11.7355 57.0801C12.293 57.483 12.9454 57.6881 13.6032 57.6881C14.1704 57.6881 14.7329 57.5352 15.238 57.2331L30.176 48.305L45.1087 57.2331C46.2014 57.8905 47.5788 57.8304 48.6139 57.0801C49.1206 56.7134 49.5083 56.2057 49.7288 55.6204C49.9492 55.035 49.9928 54.3977 49.854 53.7878L45.9941 36.7846L59.0841 25.307C59.555 24.8957 59.8947 24.3552 60.0609 23.7525C60.2272 23.1498 60.2127 22.5115 60.0193 21.917Z" fill="#E3E3E3"/></svg>`
const starSVG = `<svg width="61" height="58" viewBox="0 0 61 58" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M60.0193 21.917C59.8274 21.3224 59.4648 20.7975 58.9767 20.4076C58.4885 20.0177 57.8964 19.7801 57.2741 19.7244L39.9564 18.1518L33.1086 2.12366C32.6035 0.949062 31.4537 0.188965 30.176 0.188965C28.8984 0.188965 27.7484 0.94918 27.2436 2.12659L20.3957 18.1519L3.07528 19.7244C2.45372 19.7813 1.86254 20.0194 1.37503 20.4092C0.887526 20.799 0.525135 21.3232 0.332789 21.917C-0.0622597 23.132 0.302554 24.4646 1.26527 25.3046L14.3555 36.7846L10.4954 53.7878C10.213 55.038 10.6982 56.3304 11.7355 57.0801C12.293 57.483 12.9454 57.6881 13.6032 57.6881C14.1704 57.6881 14.7329 57.5352 15.238 57.2331L30.176 48.305L45.1087 57.2331C46.2014 57.8905 47.5788 57.8304 48.6139 57.0801C49.1206 56.7134 49.5083 56.2057 49.7288 55.6204C49.9492 55.035 49.9928 54.3977 49.854 53.7878L45.9941 36.7846L59.0841 25.307C59.555 24.8957 59.8947 24.3552 60.0609 23.7525C60.2272 23.1498 60.2127 22.5115 60.0193 21.917Z" fill="#FECE00"/></svg>`;
const starContainer = document.getElementById('star-container');
let scoreBoard = document.getElementById('scoreBoard');
let loadingMainContainer = document.getElementById('loadingMainContainer');
@@ -442,52 +350,137 @@ import Layout from "../../layouts/Layout.astro";
calculationText.style.color = 'rgba(0, 0, 0, 0.38)';
containerDiv.appendChild(calculationText);
fetch(`https://api.teachertrainingkolkata.in/api/saveGameScore2`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
})
.then(response => {
if (response.ok) {
setTimeout(() => {
if (response.ok) {
if (response.status == 200) {
document.getElementById('allParentDiv').style.display = 'block';
loadingMainContainer.style.display = 'none';
loadingDiv.style.display = 'none';
starContainer.style.display = 'flex';
starContainer.style.flexDirection = 'row';
document.getElementById('starFeedbackMessage').innerHTML = feedbackMessage;
for (let i = 0; i < starValue; i++) {
const starDiv = document.createElement('div');
starDiv.innerHTML = starSVG;
starDiv.classList.add('star');
starContainer.appendChild(starDiv);
setTimeout(() => {
starDiv.style.opacity = '1';
starDiv.style.animationDelay = `${i * 0.5}s`;
}, i * 500);
}
for (let i = 0; i < maxStarValue - starValue; i++) {
const starDiv = document.createElement('div');
starDiv.innerHTML = blankStar;
starContainer.appendChild(starDiv);
}
scoreBoard.style.display = 'flex';
}
} else {
console.log('Something went wrong', response);
}
}, 100);
} else {
// console.log('Something Wrong', response);
let snapImage = '';
const div = document.getElementById("snapshotDiv");
const images = div.getElementsByTagName('img');
let loadedImagesCount = 0;
for (let img of images) {
if (img.complete) {
loadedImagesCount++;
} else {
img.onload = () => {
loadedImagesCount++;
if (loadedImagesCount === images.length) {
takeSnapshot();
}
};
}
}
if (loadedImagesCount === images.length) {
takeSnapshot();
}
async function takeSnapshot() {
try {
const canvas = await html2canvas(div, {
useCORS: true,
allowTaint: true,
});
snapImage = canvas.toDataURL("image/jpeg");
await saveScreenshot(snapImage);
} catch (error) {
console.error('Error in takeSnapshot:', error.message || error);
}
}
async function saveScreenshot(snapImage) {
const saveUserDataForScreenshot = {
gameName: gameName,
gameID: gameId,
screenShot: snapImage,
userId: userId,
gameTime: timeDifferenceInSeconds
};
try {
const saveResponse = await fetch('http://localhost:5175/api/saveGalleryImage', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(saveUserDataForScreenshot),
});
if (!saveResponse.ok) {
const errorDetails = await saveResponse.text();
console.error(`SaveGalleryImage failed: ${saveResponse.status} ${saveResponse.statusText}`);
console.error('Response Body:', errorDetails);
throw new Error(`Failed to save screenshot: ${saveResponse.statusText}`);
}
})
.catch(error => {
console.error('An error occurred', error);
});
const saveResult = await saveResponse.json();
if (saveResult.screenshotUrl) {
let userData = {
'gameName': gameName,
'gameID': gameId,
'userId': userId,
'gameTime': timeDifferenceInSeconds,
'score': totalPoints,
'gameStar': starValue
};
fetch(`https://api.teachertrainingkolkata.in/api/saveGameScore2`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
})
.then(response => {
if (response.ok) {
setTimeout(() => {
if (response.ok) {
if (response.status == 200) {
document.getElementById('allParentDiv').style.display = 'block';
loadingMainContainer.style.display = 'none';
loadingDiv.style.display = 'none';
starContainer.style.display = 'flex';
starContainer.style.flexDirection = 'row';
document.getElementById('starFeedbackMessage').innerHTML = feedbackMessage;
document.getElementById('get-star-value').innerHTML = starValue;
scoreBoard.style.display = 'flex';
}
} else {
// console.log('Something went wrong', response);
}
}, 100);
} else {
// console.log('Something Wrong', response);
}
})
.catch(error => {
console.error('An error occurred', error);
});
const userAnotherData = {
assignmentId: assignmentId,
worksheetId: worksheetId,
studentId: userId,
submissionFile: saveResult.screenshotUrl,
};
const authToken = 'Bearer eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkZW50aXR5SWQiOiI2NjNjNGMzNjRiNmYzZjU0YmExZjcxMTUiLCJfaWQiOiI2NjNjNGMzNjBiOWYyZDk5MGQyNWYwYjMiLCJ0eXBlIjoiQUtBREVNWSIsImRldmljZUlkIjoiMDBqNXJtODl5b3BvNjh1a2Yyb2c5cTdoIn0sImlhdCI6MTczNjM5ODkwNCwiZXhwIjoxNzM3MDAzNzA0LCJpc3MiOiJidXp6T25lIn0.vhH0zTkVisnbK6n5aPgV3RrtmpP095Gtz6ayUc29XerEV-XPVhYwg06JQnm2FWGPtadEJ2Ea5z6_YNp5Kz9DENL9LxpYYuXRUOGvMEaVM5YZOYQ88viqxM7SIvxElIsc8cQeJr5v-4ov4MyOuXws7R5MDDO2PuzYdwaA52GXbQ0LO3aFGOQL7nWPKVYqFWCQe_T-1JphbsGStHivcmUy8_DAdjs0VpXFJKiQm6jcWQO3j3uOUJSi14af4ZvrUPkjIT92OOHiZTL7CUbbFt4yktnw3Gm2Fv6lS5pamSJpVJnGJQ6ebjd70IXnE49Mn9IK9AssnjTBh2DaNeVc0C1z9Q'; // Your token
try {
const submitResponse = await fetch('https://preschool-curriculum.in/api/one/v1/akademy/worksheet/submissions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': authToken,
},
body: JSON.stringify(userAnotherData),
});
if (!submitResponse.ok) {
const errorDetails = await submitResponse.text();
console.error(`SubmitWorksheet failed: ${submitResponse.status} ${submitResponse.statusText}`);
console.error('Response Body:', errorDetails);
throw new Error(`Failed to submit worksheet: ${submitResponse.statusText}`);
}
const submitResult = await submitResponse.json();
// console.log('SubmitWorksheet API success:', submitResult);
} catch (error) {
console.error('Error in SubmitWorksheet API call:', error.message || error);
}
} else {
console.error('SaveGalleryImage did not return a valid screenshotUrl:', saveResult);
}
} catch (error) {
console.error('Error in saveScreenshot:', error.message || error);
throw error;
}
}
}
document.addEventListener('DOMContentLoaded', function () {
const contactForm = document.getElementById('contactForm');