HearingGame/index.js

458 lines
15 KiB
JavaScript
Raw Normal View History

2023-03-26 11:45:32 +02:00
var ranid = 1;
var cntdrag = 1;
2023-04-02 15:30:42 +02:00
//missing Q, CH, DZ, DŽ, Ä, W, X
let data_tabl = ["A", "Á", "B", "C", "Č", "D", "Ď", "E", "É", "F", "G", "H", "I", "Í", "J", "K", "L", "Ĺ", "Ľ", "M", "N", "Ň", "O", "Ó", "Ô", "P", "R", "Ŕ", "S", "Š", "T", "Ť", "U", "Ú", "V", "Y", "Ý", "Z", "Ž"];
2023-03-26 11:45:32 +02:00
2023-04-02 18:10:55 +02:00
let tocount = 7;
2023-04-01 21:41:38 +02:00
2023-04-01 08:08:45 +02:00
let words = [
2023-04-02 15:30:42 +02:00
"DOM",
"STROM",
"ÚSTA",
"NOHA",
"MEČ",
"ZAJAC",
"MAČKA",
"KOMÍN",
"VÁZA",
"ŠESŤ",
2023-04-02 17:10:06 +02:00
"PEŇ",
2023-04-02 15:30:42 +02:00
"CITRÓN",
"PRST",
"MYŠ",
"HAD",
"HUSLE",
"ŽABA",
"FÉN",
"GUMA",
"BÝK"
2023-04-01 08:08:45 +02:00
];
function makeSpaceWord(word){
let length = word.length;
2023-04-07 09:44:01 +02:00
var lettersky = "";
var imagesky = "";
2023-04-01 08:08:45 +02:00
for (var i = 0; i < length; i++) {
2023-04-07 09:44:01 +02:00
lettersky = lettersky + ('<td><div ondrop="drop(event)" id="letterko' + ranid +'" finalplaceindex="' + i +'" ondragover="allowDrop(event)" class="letterplace finalplace finalletterplace place"></td>');
2023-04-01 13:57:28 +02:00
ranid++;
2023-04-07 09:44:01 +02:00
imagesky = imagesky + ('<td><div ondrop="drop(event)" id="letterko' + ranid +'" finalplaceindex="' + i +'" ondragover="allowDrop(event)" class="imageplace finalplace finalimageplace place"></td>');
2023-04-01 13:57:28 +02:00
ranid++;
2023-04-01 08:08:45 +02:00
}
2023-04-07 09:44:01 +02:00
lettersky = lettersky + ('<td><div id="letterstatus" class="status fixbgimage"></div></td>');
imagesky = imagesky + ('<td><div id="imagestatus" class="status fixbgimage"></div></td>');
$("#lettersky").html(lettersky);
$("#imagesky").html(imagesky);
2023-04-01 08:08:45 +02:00
}
2023-03-26 11:45:32 +02:00
2023-04-01 08:08:45 +02:00
let prefix = "assets/";
2023-03-31 20:47:02 +02:00
2023-03-26 11:45:32 +02:00
function cleanup(){
2023-04-01 11:07:33 +02:00
// var htmls = [];
// var returntos = [];
// $("[returnto]").each(function (index){
// htmls.push($(this).prop("outerHTML"));
// returntos.push($(this).attr("returnto"));
// $(this).remove();
// });
// for (var x = 0 ; x < returntos.length; x++) {
// //$('[returntarget="' + returntos[x] + '"]').html(htmls[x]);
// }
$(".finalplace").html("");
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent("Trash clear", { type: 'trashclear' });
} catch(e) {}
2023-04-01 21:08:05 +02:00
2023-03-26 11:45:32 +02:00
}
function checkAnswers() {
let good = "background-image: url(\"assets/good.png\");";
let fail = "background-image: url(\"assets/fail.png\");";
2023-04-01 10:44:45 +02:00
// $('#letterstatus').attr('style', good);
// $('#letterstatus').animate({opacity:1}, "slow");
// setTimeout(function (){
// $('#letterstatus').animate({opacity:0}, "slow");
// });
var imagecodes = "";
var lettercodes = "";
let vzorcodes = $("#currentvzor").attr("datacode");
$(".finalimageplace").find("img").each(function (){
imagecodes = imagecodes + $(this).attr("datacode");
});
2023-04-02 11:53:55 +02:00
$(".finalletterplace").find("p").each(function (){
2023-04-01 10:44:45 +02:00
lettercodes = lettercodes + $(this).attr("datacode");
});
if (vzorcodes == imagecodes)
{
2023-03-26 11:45:32 +02:00
$('#imagestatus').attr('style', good);
$('#imagestatus').animate({opacity:1}, "slow");
setTimeout(function (){
$('#imagestatus').animate({opacity:0}, "slow");
});
}
2023-04-01 10:44:45 +02:00
else{
2023-03-26 11:45:32 +02:00
$('#imagestatus').attr('style', fail);
$('#imagestatus').animate({opacity:1}, "slow");
setTimeout(function (){
$('#imagestatus').animate({opacity:0}, "slow");
});
}
2023-04-01 10:44:45 +02:00
if (vzorcodes == lettercodes)
{
$('#letterstatus').attr('style', good);
$('#letterstatus').animate({opacity:1}, "slow");
setTimeout(function (){
$('#letterstatus').animate({opacity:0}, "slow");
});
}
else{
$('#letterstatus').attr('style', fail);
$('#letterstatus').animate({opacity:1}, "slow");
setTimeout(function (){
$('#letterstatus').animate({opacity:0}, "slow");
});
}
if (vzorcodes == imagecodes && vzorcodes == lettercodes){
setTimeout(() => {
cleanup();
generatePair();
}, 1200);
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Successful round', { type: 'successround' });
} catch(e) {}
2023-04-02 11:53:55 +02:00
document.getElementById("goodaudio").play();
2023-03-26 11:45:32 +02:00
}
2023-04-01 19:34:07 +02:00
else{
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Unsuccessful round', { type: 'failround' });
} catch(e) {}
2023-04-02 11:53:55 +02:00
document.getElementById("failaudio").play();
2023-04-01 19:34:07 +02:00
}
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent("Round data", { type: 'rounddata', Vzor: vzorcodes, Lettercodes: lettercodes, Imagecodes: imagecodes });
} catch(e) {}
2023-03-26 11:45:32 +02:00
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
}
function getRandomElement(array){
return array[getRandomInt(0, array.length)];
}
2023-04-02 17:37:47 +02:00
function refresh(){
cleanup();
generatePair();
}
2023-04-01 08:08:45 +02:00
function randomize(inarray){
var tmp = [];
const randomly = () => Math.random() - 0.5;
const traitInfo = Array(inarray.length).fill({}); // for the snippet.
const hahainarray = [].concat(inarray).sort(randomly);
traitInfo.forEach((t, i) => {
tmp.push(hahainarray[i]);
});
return tmp;
2023-03-31 20:47:02 +02:00
}
2023-04-01 13:57:28 +02:00
function getEmptyIndex(element) {
var emptyPlaces = [];
var idcka = [];
var finalindex = -1;
var idcko = "";
element.each(function (){
let thiselement = $(this);
if (thiselement.html() == ""){
emptyPlaces.push(thiselement.attr("finalplaceindex"));
idcka.push(thiselement.attr("id"));
}
});
emptyPlaces.forEach(function (value, index) {
if (value < finalindex || finalindex < 0){
finalindex = value;
idcko = idcka[index];
}
});
return [finalindex, idcko];
}
2023-04-01 21:41:38 +02:00
function showImages(word) {
2023-04-02 17:43:13 +02:00
$("#imageholder").html("");
$("#letterholder").html("");
2023-04-01 21:41:38 +02:00
let tmp_tabl = Array.from(word)
2023-03-31 20:47:02 +02:00
var images = [];
var letters = [];
let imageholder = $("#imageholder");
let letterholder = $("#letterholder");
2023-04-01 21:41:38 +02:00
var now_count = tmp_tabl.length;
while (now_count < tocount){
tmp_tabl.push(getRandomElement(data_tabl));
now_count++;
}
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent("Round images", { type: 'roundimages', images: tmp_tabl });
} catch(e) {}
2023-04-01 21:41:38 +02:00
for (let i = 0 ; i < tmp_tabl.length; i++) {
let imagepath = prefix + "images/" + tmp_tabl[i] + ".png";
let letterpath = prefix + "letters/" + tmp_tabl[i] + ".png";
2023-04-02 17:59:42 +02:00
let audiopath = prefix + "audio/" + tmp_tabl[i] + ".mp3";
2023-03-26 11:45:32 +02:00
2023-04-01 10:44:45 +02:00
images.push(
2023-04-01 21:41:38 +02:00
"<div returntarget=\"" + ranid + "\" ranid=\"" + ranid + "\" ondrop=\"drop(event)\" ondragover=\"allowDrop(event)\" id=\"imageholderak" + ranid + "\" datacode=\"" + tmp_tabl[i] + "\" class=\"place originalplace grid-item holderak imageholderak\">" +
"<img returnto=\"" + ranid + "\" datacode=\"" + tmp_tabl[i] + "\" draggable=\"true\" ondragstart=\"drag(event)\" class=\"imagevec\" src=\"" + imagepath + "\">" +
"<audio datacode=\"" + tmp_tabl[i] + "\" id=\"audplay" + tmp_tabl[i] + "\">" +
"<source id=\"audioholderak" + ranid + "\" class=\"holderak\" datacode=\"" + tmp_tabl[i] + "\" src=\"" + audiopath + "\" type=\"audio/ogg\"/>" +
2023-04-01 10:44:45 +02:00
"</audio>" +
"</img>" +
"</div>"
);
2023-03-26 11:45:32 +02:00
ranid += 1;
2023-04-01 10:44:45 +02:00
letters.push(
2023-04-01 21:41:38 +02:00
"<div returntarget=\"" + ranid + "\" ranid=\"" + ranid + "\" ondrop=\"drop(event)\" ondragover=\"allowDrop(event)\" id=\"letterholderak" + ranid + "\" datacode=\"" + tmp_tabl[i] + "\" class=\"place originalplace grid-item holderak letterholderak\">" +
2023-04-02 11:53:55 +02:00
"<p returnto=\"" + ranid + "\" datacode=\"" + tmp_tabl[i] + "\" draggable=\"true\" ondragstart=\"drag(event)\" class=\"lettervec\">" + tmp_tabl[i] + "</p>" +
2023-04-01 10:44:45 +02:00
"</div>"
);
2023-03-26 11:45:32 +02:00
ranid += 1;
}
2023-04-01 08:08:45 +02:00
randomize(images).forEach(function (value, index, array){
imageholder.append(value);
});
2023-03-31 20:47:02 +02:00
2023-04-01 08:08:45 +02:00
randomize(letters).forEach(function (value, index, array){
letterholder.append(value);
});
2023-04-01 13:57:28 +02:00
$(".originalplace").on("click", function(e){
let targetElement = $(this);
if (targetElement.hasClass("imageholderak")){
2023-04-01 21:02:51 +02:00
let [emptyindex, idcko] = getEmptyIndex($(".finalimageplace"));
2023-04-01 13:57:28 +02:00
if (emptyindex > -1){
let changingelement = $("#" + idcko);
changingelement.html(targetElement.html());
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Clicked sign', { type: 'clicksign', datacode: targetElement.attr("datacode") });
} catch(e) {}
2023-04-01 13:57:28 +02:00
}
}
if (targetElement.hasClass("letterholderak")){
2023-04-01 21:02:51 +02:00
let [emptyindex, idcko] = getEmptyIndex($(".finalletterplace"));
2023-04-01 13:57:28 +02:00
if (emptyindex > -1){
let changingelement = $("#" + idcko);
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Clicked letter', { type: 'clickletter', datacode: targetElement.attr("datacode") });
} catch(e) {}
2023-04-01 13:57:28 +02:00
changingelement.html(targetElement.html());
}
}
});
$(".finalplace").on("click", function (){
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Clicked clear', { type: 'clickclear', datacode: $(this).children()[0].getAttribute("datacode") });
} catch(e) {}
2023-04-01 13:57:28 +02:00
$(this).html("");
});
$(".imageholderak, .letterholderak").each(function(){
2023-04-01 08:08:45 +02:00
$(this).on("click", function (e){
let datacode = $(this).attr("datacode");
document.getElementById("audplay"+ datacode).play();
})
});
2023-03-26 11:45:32 +02:00
}
2023-04-01 10:44:45 +02:00
function generateItem(word){
let vzorplace = $("#vzoritem");
let datacode = "datacode=\"" + word + "\" "
let imagesrc = "src=\"assets/items/" + word +".png\" "
let imageid = "id=\"currentvzor\" ";
let audioid = "id = \"audplayitem" + word + "\" ";
let audiosrc = "src=\"assets/items/AUDIO/" + word +".mp3\" "
let sourceid = "id=\"audioitemholderak" + word +"\" "
let sourceclass = "class=\"holderak\" "
let sourcetype = "type=\"audio/mpeg\" "
let sourceend = "</source>";
let audioend = "</audio>";
let imageend = "</img>";
let itemend = "</div>";
let itemid = "id = \"itemholderak" + word + "\" ";
let itemclass = "class=\"itemholderak holderak\"";
let itempart = "<div " + itemid + itemclass + datacode + ">";
let imagepart = "<img " + imagesrc + datacode + imageid + ">";
let audiopart = "<audio " + datacode + audioid + ">";
let sourcepart = "<source " + datacode + audiosrc + sourceclass + sourceid + sourcetype + ">";
let finalpart = itempart + imagepart + audiopart + sourcepart + sourceend + audioend + imageend + itemend;
vzorplace.html(finalpart);
$(".itemholderak").each(function(){
$(this).on("click", function (e){
let datacode = $(this).attr("datacode");
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Listened to image', { type: 'listenimage', datacode: $(this).attr("datacode") });
} catch(e) {}
2023-04-01 10:44:45 +02:00
document.getElementById("audplayitem"+ datacode).play();
})
});
}
2023-03-26 11:45:32 +02:00
function generatePair(){
2023-04-01 10:44:45 +02:00
let word = getRandomElement(words);
2023-03-26 11:45:32 +02:00
2023-04-01 10:44:45 +02:00
makeSpaceWord(word);
generateItem(word);
2023-03-26 11:45:32 +02:00
2023-04-01 21:41:38 +02:00
showImages(word);
2023-03-26 11:45:32 +02:00
}
$(function() {
generatePair();
$("#audiopart").on("click", function (){
document.getElementById("audplay").play();
});
$("#verify").on("click", checkAnswers);
$("#trashcan").on("click", cleanup);
2023-04-02 17:37:47 +02:00
$("#refresh").on("click", refresh);
2023-03-26 11:45:32 +02:00
});
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("cntdrag", cntdrag);
ev.target.setAttribute("cntdrag", cntdrag);
cntdrag += 1;
ev.dataTransfer.setData("text", ev.target.outerHTML);
ev.dataTransfer.setData("returnto", ev.target.getAttribute("returnto"));
ev.dataTransfer.setData("datacode", ev.target.getAttribute("datacode"));
2023-04-01 10:44:45 +02:00
ev.dataTransfer.setData("classy", ev.target.classList);
2023-04-01 08:08:45 +02:00
let datacode = $(ev.target).attr("datacode");
document.getElementById("audplay"+ datacode).play();
2023-04-02 11:53:55 +02:00
if (ev.target.parentElement.classList.contains("originalplace")){
ev.dataTransfer.setData("dragfromoriginal", "YES");
}
else{
ev.dataTransfer.setData("dragfromoriginal", "NO");
}
2023-04-01 19:53:59 +02:00
if (ev.target.classList.contains("imagevec")){
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Dragged sign', { type: 'draggedsign', datacode: ev.target.getAttribute("datacode") });
} catch(e) {}
2023-04-01 19:53:59 +02:00
}
else if (ev.target.classList.contains("lettervec")){
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Dragged letter', { type: 'draggedletter', datacode: ev.target.getAttribute("datacode") });
} catch(e) {}
2023-04-01 19:53:59 +02:00
}
2023-03-26 11:45:32 +02:00
}
function drop(ev) {
ev.preventDefault();
let data = ev.dataTransfer.getData("text");
let cntdrag = ev.dataTransfer.getData("cntdrag");
2023-04-01 10:44:45 +02:00
let classy = ev.dataTransfer.getData("classy");
2023-03-26 11:45:32 +02:00
let datacode = ev.dataTransfer.getData("datacode");
2023-04-02 11:53:55 +02:00
let dragfromoriginal = ev.dataTransfer.getData("dragfromoriginal");
2023-04-01 10:44:45 +02:00
if (ev.target.classList.contains("letterplace")){
if (!classy.includes("lettervec")){
return;
}
}
2023-04-02 11:53:55 +02:00
if (ev.target.parentElement.classList.contains("originalplace")){
return;
}
2023-04-01 10:44:45 +02:00
if (ev.target.classList.contains("imageplace")){
if (!classy.includes("imagevec")){
return;
}
}
2023-04-02 11:53:55 +02:00
if (ev.target.parentElement.innerHTML.includes("<img")){
return;
2023-03-26 11:45:32 +02:00
}
2023-04-02 11:53:55 +02:00
if (ev.target.parentElement.innerHTML.includes("<p")){
return;
2023-03-26 11:45:32 +02:00
}
2023-04-02 11:53:55 +02:00
if (dragfromoriginal == "NO"){
$('[cntdrag="' + cntdrag + '"]').remove();
}
ev.target.setAttribute("datacode", datacode);
ev.target.setAttribute("cntdrag", cntdrag);
cntdrag += 1;
ev.target.innerHTML = data;
2023-04-01 19:53:59 +02:00
if (classy.includes("imagevec")){
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Dropped sign', { type: 'droppedsign', datacode: ev.target.getAttribute("datacode") });
} catch(e) {}
2023-04-01 19:53:59 +02:00
}
else if (classy.includes("lettervec")){
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Dropped letter', { type: 'droppedletter', datacode: ev.target.getAttribute("datacode") });
} catch(e) {}
2023-04-01 19:53:59 +02:00
}
2023-03-26 11:45:32 +02:00
}
function droptrash(ev) {
ev.preventDefault();
let cntdragx = ev.dataTransfer.getData("cntdrag");
2023-04-01 13:57:28 +02:00
$('[cntdrag="' + cntdragx + '"]').remove();
2023-03-26 11:45:32 +02:00
let htmldata = ev.dataTransfer.getData("text");
let returnto = ev.dataTransfer.getData("returnto");
2023-04-01 19:53:59 +02:00
let classy = ev.dataTransfer.getData("classy");
2023-04-01 13:57:28 +02:00
$('[returntarget="' + returnto + '"]').html(htmldata);
2023-04-01 19:53:59 +02:00
if (classy.includes("imagevec")){
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Dragged sign into trashcan', { type: 'dropsigntrash', datacode: ev.target.getAttribute("datacode") });
} catch(e) {}
2023-04-01 19:53:59 +02:00
}
else if (classy.includes("lettervec")){
2023-04-01 22:17:02 +02:00
try{
umami.trackEvent('Dragged letter into trashcan', { type: 'droplettertrash', datacode: ev.target.getAttribute("datacode") });
} catch(e) {}
2023-04-01 19:53:59 +02:00
}
2023-03-26 11:45:32 +02:00
}