352 lines
16 KiB
JavaScript
352 lines
16 KiB
JavaScript
function retryGame(){
|
|
window.location.reload();
|
|
}
|
|
const queryString = window.location.search;
|
|
const urlParams = new URLSearchParams(queryString);
|
|
const userId = urlParams.get('userId');
|
|
const gameVersion = urlParams.get('gameName');
|
|
const gameId = urlParams.get('id');
|
|
const spriteName = urlParams.get('spriteName');
|
|
const assignmentId = urlParams.get('assignmentId');
|
|
const worksheetId = urlParams.get('worksheetId');
|
|
let submitNotic;
|
|
let startTime = Date.now();
|
|
|
|
|
|
const url = window.location.href;
|
|
const gameName = url.split('/');
|
|
const gameType = gameName[3].split('?id=');
|
|
// let gameVersion;
|
|
// if(gameType[0] == "guided-tracing"){
|
|
// gameVersion = gameType[0].split('?')[0];
|
|
// gameId = gameName[4];
|
|
// console.log('Type - 1');
|
|
|
|
// } else if(gameName.length == 4){
|
|
// gameVersion = gameName[3].split('?')[0];
|
|
// gameId = urlParams.get('id');
|
|
// }
|
|
// else if(gameName.length == 5){
|
|
// gameVersion = gameName[3] + '-' + gameName[4].split('?')[0];
|
|
// gameId = urlParams.get('id');
|
|
// console.log('Type - 2');
|
|
// }else if(gameName.length == 6){
|
|
// gameVersion = gameType[0] + '-' + gameName[4];
|
|
// gameId = urlParams.get('id');
|
|
// console.log('Type - 3');
|
|
// }
|
|
// console.log("Here is game name ", gameType[0])
|
|
|
|
// console.log(gameVersion)
|
|
let imageCode;
|
|
let gameScore;
|
|
let timeDifferenceInSeconds;
|
|
|
|
// if(scoreTotal){
|
|
// gameScore = scoreTotal;
|
|
// }else{
|
|
// gameScore = 0;
|
|
// }
|
|
|
|
|
|
function submitUserData(drawingZone) {
|
|
submitButton.setVisible(false);
|
|
galleryButton.setVisible(false);
|
|
customCursor.setVisible(false);
|
|
snapshotButton.setVisible(false);
|
|
if(!gameType[0] === 'guided-tracing'){
|
|
snapshotButton.setVisible(false);
|
|
customCursor.setVisible(false);
|
|
galleryButton.setVisible(false);
|
|
}
|
|
muteIcon.setVisible(false);
|
|
retryButton.setVisible(false);
|
|
|
|
const endTime = Date.now();
|
|
const timeDifference = endTime - startTime;
|
|
timeDifferenceInSeconds = timeDifference / 1000;
|
|
// console.log(`Time difference: ${timeDifferenceInSeconds} seconds`);
|
|
|
|
|
|
// console.log('This is from main point', scoreTotal);
|
|
drawingZone.renderer.snapshot((image) => {
|
|
submitButton.setVisible(true);
|
|
galleryButton.setVisible(true);
|
|
customCursor.setVisible(true);
|
|
snapshotButton.setVisible(true);
|
|
if(!gameType[0] == "guided-tracing"){
|
|
snapshotButton.setVisible(true);
|
|
customCursor.setVisible(true);
|
|
galleryButton.setVisible(true);
|
|
}
|
|
|
|
muteIcon.setVisible(true);
|
|
retryButton.setVisible(true);
|
|
|
|
if(gameType[0] == 'drawing'){
|
|
// submitButton.setVisible(true);
|
|
// snapshotButton.setVisible(true);
|
|
// customCursor.setVisible(true);
|
|
// galleryButton.setVisible(true);
|
|
image.style.width = '160px';
|
|
image.style.height = '120px';
|
|
image.style.paddingLeft = '2px';
|
|
document.body.appendChild(image);
|
|
// Download the snapshot as an image
|
|
// const link = document.createElement('a');
|
|
// link.href = image.src;
|
|
// link.download = 'my_drawing.png';
|
|
// link.click();
|
|
document.body.removeChild(image);
|
|
imageCode = image.src;
|
|
}else if( gameType[0] == "guided-tracing"){
|
|
// submitButton.setVisible(true);
|
|
// snapshotButton.setVisible(true);
|
|
// customCursor.setVisible(true);
|
|
image.style.width = '160px';
|
|
image.style.height = '120px';
|
|
image.style.paddingLeft = '2px';
|
|
document.body.appendChild(image);
|
|
// Download the snapshot as an image
|
|
// const link = document.createElement('a');
|
|
// link.href = image.src;
|
|
// link.download = `guided-tracing-${gameId}.png`;
|
|
// link.click();
|
|
document.body.removeChild(image);
|
|
imageCode = image.src;
|
|
}
|
|
|
|
|
|
let userData = {
|
|
'gameName': gameVersion,
|
|
'gameID': gameId,
|
|
'screenShot': imageCode,
|
|
'userId' : userId,
|
|
'gameTime' : timeDifferenceInSeconds
|
|
|
|
};
|
|
console.log('userData', userData)
|
|
fetch(`https://api.teachertrainingkolkata.in/api/saveGalleryImage`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type' : 'application/json'
|
|
},
|
|
body: JSON.stringify(userData)
|
|
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
console.log('data from save gallery', data);
|
|
|
|
if(data.screenshotUrl){
|
|
fetch(`https://api.teachertrainingkolkata.in/api/saveGameScore2`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(userData)
|
|
})
|
|
.then(response => {
|
|
setTimeout(() => {
|
|
if (response.ok) {
|
|
if (response.status == 200) {
|
|
document.getElementById('saved-toast').classList.remove('hidden');
|
|
setTimeout(() => {
|
|
document.getElementById('saved-toast').classList.add('hidden');
|
|
}, 2000);
|
|
|
|
// console.log(response.status, " Data Saved Succesfully");
|
|
}
|
|
} else {
|
|
// console.log('Something went wrong', response);
|
|
}
|
|
}, 100);
|
|
})
|
|
.catch(error => {
|
|
console.error('An error occurred', error);
|
|
});
|
|
|
|
const userAnotherData = {
|
|
assignmentId: assignmentId,
|
|
worksheetId: worksheetId,
|
|
studentId: userId,
|
|
submissionFile: data.screenshotUrl,
|
|
};
|
|
const authToken = 'Bearer eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkZW50aXR5SWQiOiI2NjNjNGMzNjRiNmYzZjU0YmExZjcxMTUiLCJfaWQiOiI2NjNjNGMzNjBiOWYyZDk5MGQyNWYwYjMiLCJ0eXBlIjoiQUtBREVNWSIsImRldmljZUlkIjoiMDBqNXJtODl5b3BvNjh1a2Yyb2c5cTdoIn0sImlhdCI6MTczNjM5ODkwNCwiZXhwIjoxNzM3MDAzNzA0LCJpc3MiOiJidXp6T25lIn0.vhH0zTkVisnbK6n5aPgV3RrtmpP095Gtz6ayUc29XerEV-XPVhYwg06JQnm2FWGPtadEJ2Ea5z6_YNp5Kz9DENL9LxpYYuXRUOGvMEaVM5YZOYQ88viqxM7SIvxElIsc8cQeJr5v-4ov4MyOuXws7R5MDDO2PuzYdwaA52GXbQ0LO3aFGOQL7nWPKVYqFWCQe_T-1JphbsGStHivcmUy8_DAdjs0VpXFJKiQm6jcWQO3j3uOUJSi14af4ZvrUPkjIT92OOHiZTL7CUbbFt4yktnw3Gm2Fv6lS5pamSJpVJnGJQ6ebjd70IXnE49Mn9IK9AssnjTBh2DaNeVc0C1z9Q'; // Your token
|
|
fetch(`https://preschool-curriculum.in/api/one/v1/akademy/worksheet/submissions`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type' : 'application/json',
|
|
'Authorization' : authToken
|
|
},
|
|
body: JSON.stringify(userAnotherData)
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
console.log('submissions api response', data)
|
|
})
|
|
.catch(error => {
|
|
console.error('An error occured', error)
|
|
})
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('An error occured', error);
|
|
})
|
|
});
|
|
|
|
|
|
};
|
|
|
|
function calculateFromAI(){
|
|
let progress = 0;
|
|
let starValue;
|
|
let maxStarValue = 5;
|
|
|
|
function progressIncrement() {
|
|
const interval = setInterval(() => {
|
|
if (progress >= 100) {
|
|
clearInterval(interval);
|
|
} else {
|
|
progress += 10;
|
|
progressBar.style.width = `${progress}%`;
|
|
}
|
|
}, 100);
|
|
}
|
|
progressIncrement();
|
|
function addStarScalingStyles() {
|
|
const style = document.createElement('style');
|
|
style.innerHTML = `
|
|
@keyframes scaleUp {
|
|
0% {
|
|
transform: scale(1.5);
|
|
}
|
|
100% {
|
|
transform: scale(1);
|
|
}
|
|
}
|
|
.star {
|
|
display: inline-block;
|
|
transform-origin: center;
|
|
transform: scale(0); /* Initial state before animation */
|
|
opacity: 0; /* Initial opacity */
|
|
animation: scaleUp 0.5s ease forwards;
|
|
}
|
|
`;
|
|
document.head.appendChild(style);
|
|
}
|
|
addStarScalingStyles();
|
|
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');
|
|
loadingMainContainer.style.display = 'flex';
|
|
|
|
const loadingDiv = document.getElementById('loadingState');
|
|
loadingDiv.style.display = 'flex';
|
|
|
|
const containerDiv = document.createElement('div');
|
|
containerDiv.classList.add('w-full');
|
|
containerDiv.style.display = 'flex';
|
|
containerDiv.style.flexDirection = 'column';
|
|
containerDiv.style.justifyContent = 'center';
|
|
containerDiv.style.alignItems = 'center';
|
|
|
|
const paragraph = document.createElement('p');
|
|
paragraph.textContent = 'HOLD ON.';
|
|
paragraph.style.fontSize = '16px';
|
|
paragraph.style.fontWeight = '600';
|
|
paragraph.style.color = 'rgba(0, 0, 0, 0.38)';
|
|
containerDiv.appendChild(paragraph);
|
|
|
|
const progressBarContainer = document.createElement('div');
|
|
progressBarContainer.style.position = 'relative';
|
|
progressBarContainer.style.display = 'flex';
|
|
progressBarContainer.style.alignItems = 'center';
|
|
progressBarContainer.style.border = '1px solid #AFB8E6';
|
|
progressBarContainer.style.height = '10px';
|
|
progressBarContainer.style.width = '100%';
|
|
progressBarContainer.style.marginTop = '30px';
|
|
progressBarContainer.style.marginBottom = '30px';
|
|
containerDiv.appendChild(progressBarContainer);
|
|
|
|
const progressBar = document.createElement('div');
|
|
progressBar.style.height = '10px';
|
|
progressBar.style.backgroundColor = '#D7DCF2';
|
|
progressBar.style.width = '0%';
|
|
progressBarContainer.appendChild(progressBar);
|
|
|
|
loadingDiv.appendChild(containerDiv);
|
|
|
|
const calculationText = document.createElement('p');
|
|
calculationText.textContent = 'Your stars are being calculated...';
|
|
calculationText.style.fontSize = '14px';
|
|
calculationText.style.fontWeight = '600';
|
|
calculationText.style.color = 'rgba(0, 0, 0, 0.38)';
|
|
containerDiv.appendChild(calculationText);
|
|
let userData = {
|
|
gameName: gameVersion,
|
|
userId: userId,
|
|
gameID: gameId,
|
|
gameTime: timeDifferenceInSeconds,
|
|
screenShot: imageCode
|
|
};
|
|
// console.log(userData);
|
|
fetch(`https://api.teachertrainingkolkata.in/api/aiEvaluateImageToStar`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(userData)
|
|
})
|
|
.then(response => {
|
|
if(!response.ok){
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
return response.json()
|
|
})
|
|
.then(data => {
|
|
// console.log('data', data.data.score);
|
|
setTimeout(() => {
|
|
if (data && data.message === "Game data saved successfully") {
|
|
starValue = data.data.score;
|
|
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.";
|
|
}
|
|
loadingMainContainer.style.display = 'none';
|
|
loadingDiv.style.display = 'none';
|
|
starContainer.style.display = 'flex';
|
|
starContainer.style.flexDirection = 'row';
|
|
document.getElementById('get-star-value').innerHTML = starValue;
|
|
document.getElementById('starFeedbackMessage').innerHTML = feedbackMessage;
|
|
scoreBoard.style.display = 'flex';
|
|
if (scoreBoard.style.display === 'flex') {
|
|
const bottomAnimateClipart = document.getElementById('bottomAnimateClipart');
|
|
bottomAnimateClipart.classList.remove('hidden');
|
|
bottomAnimateClipart.classList.add('-translate-x-full');
|
|
bottomAnimateClipart.classList.add('transition-transform', 'duration-700');
|
|
setTimeout(() => {
|
|
bottomAnimateClipart.classList.add('hidden');
|
|
bottomAnimateClipart.classList.remove('translate-x-full');
|
|
}, 100);
|
|
}
|
|
|
|
|
|
}
|
|
}, 100);
|
|
})
|
|
.catch(error => {
|
|
console.error('An error occurred', error);
|
|
});
|
|
} |