webgames/static/gogame.js

63 lines
2 KiB
JavaScript
Raw Normal View History

2022-09-30 22:25:07 +02:00
2022-10-02 07:13:46 +02:00
function setupEventHandler() {
let stones = document.getElementsByClassName('go-stone-empty');
let inputRow = document.getElementById('go-input-row');
let inputCol = document.getElementById('go-input-col');
let inputPlace = document.getElementById('go-input-place');
for (let i = 0; i < stones.length; ++i) {
stones[i].onclick = (e) => {
console.log('place');
inputCol.value = stones[i].attributes['data-col'].value;
inputRow.value = stones[i].attributes['data-row'].value;
inputPlace.click();
};
}
2022-09-30 22:25:07 +02:00
}
2022-10-02 07:13:46 +02:00
let updateInterval = null;
if (document.getElementsByClassName('go-territory').length == 0) {
setInterval(update, 500);
}
function update() {
var req = new XMLHttpRequest();
req.addEventListener('load', (ev) => {
let parser = new DOMParser();
let doc = parser.parseFromString(req.responseText, 'text/html');
let currentField = document.querySelector('svg');
let newField = doc.querySelector('svg');
let currentStones = currentField.getElementsByClassName('go-stone');
let newStones = newField.getElementsByClassName('go-stone');
let newTerritory = doc.getElementsByClassName('go-territory');
let currentInfo = document.getElementById('gameinfo');
let newInfo = doc.getElementById('gameinfo');
if (newTerritory.length > 0) {
window.location.reload();
return;
}
for (let i = 0; i < currentStones.length; ++i) {
if (doc.getElementById(currentStones[i].id) === null) {
currentStones[i].remove();
}
}
if (currentInfo.classList[0] != newInfo.classList[0]) {
console.log(newInfo.classList[0]);
currentInfo.innerHTML = newInfo.innerHTML;
currentInfo.classList.remove(currentInfo.classList[0]);
currentInfo.classList.add(newInfo.classList[0]);
}
for (let i = 0; i < newStones.length; ++i) {
if (document.getElementById(newStones[i].id) === null) {
currentField.append(newStones[i]);
}
}
setupEventHandler();
});
req.open('GET', window.location.href);
req.send();
}
setupEventHandler();