127 lines
3.9 KiB
JavaScript
127 lines
3.9 KiB
JavaScript
const queryString = window.location.search;
|
|
const urlParams = new URLSearchParams(queryString);
|
|
const userId = urlParams.get('userId');
|
|
let gameId;
|
|
let submitNotic;
|
|
|
|
console.log(userId);
|
|
let startTime = Date.now();
|
|
|
|
|
|
const url = window.location.href;
|
|
const gameName = url.split('/');
|
|
const gameType = gameName[3].split('?id=');
|
|
let gameVersion;
|
|
|
|
console.log("Here is game name ", gameName)
|
|
|
|
if(gameType[0] == "guided-tracing"){
|
|
gameVersion = gameType[0].split('?')[0];
|
|
gameId = gameName[4];
|
|
console.log('Type - 1');
|
|
} else if(gameName.length == 5){
|
|
gameVersion = gameName[3];
|
|
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(gameType[0])
|
|
|
|
function submitUserData(drawingZone) {
|
|
const endTime = Date.now();
|
|
const timeDifference = endTime - startTime;
|
|
const timeDifferenceInSeconds = timeDifference / 1000;
|
|
// console.log(`Time difference: ${timeDifferenceInSeconds} seconds`);
|
|
|
|
let imageCode;
|
|
let gameScore;
|
|
|
|
if(scoreTotal){
|
|
gameScore = scoreTotal;
|
|
}else{
|
|
gameScore = 0;
|
|
}
|
|
// console.log('This is from main point', scoreTotal);
|
|
drawingZone.renderer.snapshot((image) => {
|
|
if(gameType[0] == 'drawing'){
|
|
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 = '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 starValue;
|
|
if(scoreTotal === maxScore){
|
|
starValue = 5;
|
|
} else if(scoreTotal === maxScore - 1){
|
|
starValue = 4;
|
|
} else{
|
|
starValue = 3;
|
|
}
|
|
|
|
let userData = {
|
|
'gameName': gameVersion,
|
|
'gameID': gameId,
|
|
'screenShot': imageCode,
|
|
'userId' : userId,
|
|
'gameTime' : timeDifferenceInSeconds,
|
|
'score' : scoreTotal,
|
|
'star' : starValue
|
|
// 'starts': formattedDateTime,
|
|
// 'game_start' : gameStartTime,
|
|
};
|
|
console.log(userData);
|
|
|
|
fetch(`https://api.teachertrainingkolkata.in/api/saveGameScore`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type' : 'application/json'
|
|
},
|
|
body: JSON.stringify(userData)
|
|
})
|
|
.then(response => {
|
|
if(response.ok){
|
|
console.log('Data Saved', response.status)
|
|
if(response.status == 200){
|
|
submitNotic.setVisible(true);
|
|
}
|
|
} else{
|
|
// console.log('Something Wrong', response)
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('An error occured', error)
|
|
});
|
|
|
|
// Clear the drawing
|
|
// graphics.clear();
|
|
});
|
|
}; |