gammatools/web/mentalattack.js

54 lines
2.0 KiB
JavaScript
Raw Normal View History

2024-06-28 22:00:49 +00:00
window.onload = function () {
document.getElementById('mentalAttackForm').addEventListener('submit', function (event) {
event.preventDefault(); // Prevent the form from submitting the normal way
const ams = parseInt(document.getElementById('ams').value, 10);
const dms = parseInt(document.getElementById('dms').value, 10);
const modifier = parseInt(document.getElementById('modifier').value, 0);
fetch('https://gammaworld.gmgauthier.com/roll/attack/mental', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
ams: ams,
dms: dms,
modifier: modifier
}),
})
.then(response => response.json())
.then(data => {
const resultsDiv = document.getElementById('results');
let html = '';
if (data['needed']) {
html += `<p><b>Needed: </b>${data['needed']} &emsp;&emsp;&emsp;&emsp;&emsp;`;
}
if (data['original-roll']) {
html += `<b>Original Roll: </b>${data['original-roll']}</p>`;
}
if (data['modifier']) {
html += `<p><b>Modifier: </b>${data['modifier']} &emsp;&emsp;&emsp;&emsp;&emsp;`;
}
if (data['adjusted-roll']) {
html += `<b>Adjusted Roll: </b> ${data['adjusted-roll']}</p>`;
}
html += `<br/>`;
if (data['outcome']) {
let outcomeText = data['outcome'];
html += `<p><h2>${outcomeText}</h2></p>`;
// adding img tag
let outcomeImage = 'img/' + outcomeText.replace(' ', '%20') + '.png';
2024-06-28 22:26:42 +00:00
html += `<img src="${outcomeImage}" width="350px" height="350px"/>`
2024-06-28 22:00:49 +00:00
resultsDiv.innerHTML = html;
}
})
});
}