soundbirder/public/javascripts/game.js

52 lines
1.6 KiB
JavaScript

import { geolocationHandler, getCoordinates } from './map.js';
import client from './api-client.js';
const API_VERSION = "0";
function geolocationStep() {
if (document.getElementById('map') != undefined)
geolocationHandler();
let start_button = document.querySelector('.game .start-button');
if (start_button != undefined)
start_button.addEventListener('click', quizzStep);
}
function quizzStep() {
// Start by disallowing geolocation step
document.querySelector('.game-map-step').classList.toggle('none');
// Then allow the quizz step
document.querySelector('.game-quizz-step').classList.remove('none');
// Retrieve coordinates from former done geolocation (TODO: fix the need of cookie)
const coordinates = getCoordinates();
client.getQuizz(coordinates)
.then(quizz => {
// Display the quizz
displayQuizz(quizz);
}).catch(error => {
console.log(error);
});
}
function displayQuizz(quizz) {
let audio = document.querySelector('.game-quizz-step audio');
audio.src = quizz.audio;
audio.play();
let proposals = document.querySelector('.game-quizz-step .proposals');
quizz.species.forEach(sp => {
let proposal = document.createElement('li');
proposal.classList.add('proposal');
let button = document.createElement('button');
button.classList.add('proposal-button');
button.value = sp.code;
button.innerText = sp.comName;
proposal.appendChild(button);
proposals.appendChild(proposal);
});
}
function game() {
geolocationStep();
}
game();