2023-06-28 17:41:57 +02:00
|
|
|
import QrScanner from "./qr-scanner/qr-scanner.min.js";
|
2023-06-28 20:38:52 +02:00
|
|
|
import {puzzleSolve} from "./answer.js";
|
2023-06-28 17:41:57 +02:00
|
|
|
|
|
|
|
const video = document.querySelector("video");
|
|
|
|
const camList = document.querySelector("select");
|
2023-06-28 20:38:52 +02:00
|
|
|
// const fileSelector = document.querySelector("input[type='file']");
|
|
|
|
|
|
|
|
// Gestion de la langue :
|
|
|
|
var url_string = window.location.href;
|
|
|
|
var url = new URL(url_string);
|
|
|
|
var lg = url.searchParams.get("lg");
|
|
|
|
var team_id = url.searchParams.get("team");
|
|
|
|
var art_id = url.searchParams.get("id");
|
|
|
|
|
|
|
|
if (lg == null) {
|
|
|
|
lg = "en"; // Langue par défaut
|
|
|
|
// Recherche dans les cookies :
|
|
|
|
var cookies = decodeURIComponent(document.cookie);
|
|
|
|
if (cookies != "") {
|
|
|
|
// On part du principe qu'on n'a qu'un cookie, et que c'est celui de la langue
|
|
|
|
// 9 caractères pour "language", et 2 pour la langue
|
|
|
|
lg = cookies.substring(9, 11);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cam_error = "cam_error";
|
|
|
|
|
|
|
|
if (lg == "en") {
|
|
|
|
var cam_error = "No camera detected !";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lg == "fr") {
|
|
|
|
var cam_error = "Aucune caméra détectée !";
|
|
|
|
}
|
2023-06-28 17:41:57 +02:00
|
|
|
|
|
|
|
function scanAction(result) {
|
2023-06-28 20:38:52 +02:00
|
|
|
puzzleSolve(result.data, team_id, art_id);
|
|
|
|
console.log(result.data); // TODO : retirer
|
|
|
|
scanner.stop();
|
2023-06-28 17:41:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkCam(hasCamera) {
|
2023-06-28 20:38:52 +02:00
|
|
|
const errorP = document.querySelector("article p:nth-of-type(2)");
|
2023-06-28 17:41:57 +02:00
|
|
|
if (!hasCamera) {
|
2023-06-28 20:38:52 +02:00
|
|
|
errorP.innerHTML = "⚠️ " + cam_error;
|
2023-06-28 17:41:57 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
errorP.text = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const scanner = new QrScanner(
|
|
|
|
video,
|
|
|
|
result => scanAction(result),
|
|
|
|
{
|
|
|
|
highlightScanRegion: true,
|
|
|
|
highlightCodeOutline: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.scanner = scanner; // Débogage
|
|
|
|
|
|
|
|
|
|
|
|
// Changement de caméra :
|
|
|
|
camList.addEventListener("change", event => {
|
|
|
|
scanner.setCamera(event.target.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Démarrage du scan :
|
|
|
|
document.querySelector("button").addEventListener("click", () => {
|
|
|
|
scanner.start().then(() => {
|
|
|
|
QrScanner.listCameras(true).then(cameras => cameras.forEach(camera =>
|
|
|
|
{
|
|
|
|
const option = document.createElement("option");
|
|
|
|
option.value = camera.id;
|
|
|
|
option.text = camera.label;
|
|
|
|
camList.add(option);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
});
|
|
|
|
QrScanner.hasCamera().then(hasCamera => checkCam(hasCamera));
|
|
|
|
});
|
|
|
|
|
|
|
|
// // Arrêt du scan :
|
|
|
|
// document.querySelector("nav button:nth-of-type(2)").addEventListener("click", () => {
|
|
|
|
// scanner.stop();
|
|
|
|
// });
|
|
|
|
|
2023-06-28 20:38:52 +02:00
|
|
|
// // Scan de fichier :
|
|
|
|
// fileSelector.addEventListener("change", () => {
|
|
|
|
// const file = fileSelector.files[0];
|
|
|
|
// if (file) {
|
|
|
|
// QrScanner.scanImage(file, {returnDetailedScanResult: true})
|
|
|
|
// .then(result => scanAction(result));
|
|
|
|
// }
|
|
|
|
// });
|