| 
									
										
										
										
											2024-06-29 19:25:55 +00:00
										 |  |  | document.getElementById('terrainForm').addEventListener('submit', function (event) { | 
					
						
							| 
									
										
										
										
											2024-06-26 07:45:20 +00:00
										 |  |  |     event.preventDefault(); | 
					
						
							| 
									
										
										
										
											2024-07-01 21:35:47 +00:00
										 |  |  |     const terrain = document.getElementById('terrainType').value; | 
					
						
							| 
									
										
										
										
											2024-07-01 15:03:40 +00:00
										 |  |  |     const queryParams = new URLSearchParams({ | 
					
						
							|  |  |  |         terrain: terrain, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2024-06-26 07:45:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-29 19:25:55 +00:00
										 |  |  |     // Insert the loading.gif before making the fetch request
 | 
					
						
							|  |  |  |     let resultSection = document.getElementById('resultSection'); | 
					
						
							|  |  |  |     resultSection.innerHTML = ''; | 
					
						
							|  |  |  |     let imgElement = document.createElement('img'); | 
					
						
							|  |  |  |     imgElement.src = 'img/checking.gif'; | 
					
						
							|  |  |  |     imgElement.style.width = '500px'; | 
					
						
							|  |  |  |     imgElement.style.height = '500px'; | 
					
						
							|  |  |  |     imgElement.style.display = 'block'; | 
					
						
							|  |  |  |     imgElement.style.marginLeft = 'auto'; | 
					
						
							|  |  |  |     imgElement.style.marginRight = 'auto'; | 
					
						
							|  |  |  |     resultSection.appendChild(imgElement); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-01 21:35:47 +00:00
										 |  |  |     setTimeout(() => { | 
					
						
							|  |  |  |         fetch(`${window.BASE_URL}/gameplay/encounter?${queryParams}`) | 
					
						
							|  |  |  |             .then((response) => { | 
					
						
							|  |  |  |                 if (!response.ok) { | 
					
						
							|  |  |  |                     throw response; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return response.json(); // we only get here if there is no error
 | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             .then((json) => { | 
					
						
							|  |  |  |                 resultSection.innerHTML = ''; // Clear previous content
 | 
					
						
							|  |  |  |                 if (Object.keys(json).length === 1) { // Short version of response
 | 
					
						
							|  |  |  |                     let name = json.name !== null ? json.name : 'No Encounter'; | 
					
						
							|  |  |  |                     console.log(name); | 
					
						
							|  |  |  |                     setResultImage(name); | 
					
						
							|  |  |  |                 } else { // Long version of the response
 | 
					
						
							|  |  |  |                     setResultTable(json); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             .catch((err) => { | 
					
						
							|  |  |  |                 err.text().then((errorMessage) => { | 
					
						
							|  |  |  |                     imgElement.src = './img/404.jpg'; | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2024-06-27 00:45:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-01 21:35:47 +00:00
										 |  |  |         function setResultImage(name) { | 
					
						
							|  |  |  |             let imgElement = document.createElement('img'); | 
					
						
							|  |  |  |             imgElement.src = './img/' + name + ".jpg"; | 
					
						
							|  |  |  |             console.log(imgElement.src); | 
					
						
							| 
									
										
										
										
											2024-06-29 19:25:55 +00:00
										 |  |  |             imgElement.onerror = function () { | 
					
						
							|  |  |  |                 this.onerror = null; | 
					
						
							|  |  |  |                 this.src = './img/404.jpg'; | 
					
						
							|  |  |  |             }; | 
					
						
							| 
									
										
										
										
											2024-07-01 21:35:47 +00:00
										 |  |  |             resultSection.appendChild(imgElement); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function setResultTable(data) { | 
					
						
							|  |  |  |             let table = document.createElement('table'); | 
					
						
							|  |  |  |             for (let key in data) { | 
					
						
							|  |  |  |                 let tr = document.createElement('tr'); | 
					
						
							|  |  |  |                 let tdKey = document.createElement('td'); | 
					
						
							|  |  |  |                 let tdValue = document.createElement('td'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 tdKey.innerText = key.toUpperCase(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Make the key names bold:
 | 
					
						
							|  |  |  |                 tdKey.style.fontWeight = 'bold'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (Array.isArray(data[key])) { | 
					
						
							|  |  |  |                     if (key === 'environ' || key === 'mutations') { | 
					
						
							|  |  |  |                         tdValue.innerText = data[key].join(', '); | 
					
						
							|  |  |  |                     } else if (key === 'land speed' || key === 'water speed' || key === 'air speed') { | 
					
						
							|  |  |  |                         tdValue.innerText = `${data[key][0]}/${data[key][1]}/${data[key][2]}`; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         tdValue.innerText = `${data[key][0]}d${data[key][1]}${data[key][2] === 0 ? "" : "+" + data[key][2]}`; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } else if (key === 'attacks' && typeof data[key] === 'object') { | 
					
						
							|  |  |  |                     let attacksTable = document.createElement('table'); | 
					
						
							|  |  |  |                     for (let attack in data[key]) { | 
					
						
							|  |  |  |                         let tr = document.createElement('tr'); | 
					
						
							|  |  |  |                         let tdKey = document.createElement('td'); | 
					
						
							|  |  |  |                         let tdValue = document.createElement('td'); | 
					
						
							|  |  |  |                         tdKey.innerText = attack; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         if (data[key][attack].join() === "0,0,0") { | 
					
						
							|  |  |  |                             tdValue.innerText = "See notes below"; | 
					
						
							|  |  |  |                         } else { | 
					
						
							|  |  |  |                             tdValue.innerText = `${data[key][attack][0]}d${data[key][attack][1]}${data[key][attack][2] === 0 ? "" : "+" + data[key][attack][2]}`; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         tr.appendChild(tdKey); | 
					
						
							|  |  |  |                         tr.appendChild(tdValue); | 
					
						
							|  |  |  |                         attacksTable.appendChild(tr); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     tdValue.appendChild(attacksTable); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     tdValue.innerText = data[key]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 tr.appendChild(tdKey); | 
					
						
							|  |  |  |                 tr.appendChild(tdValue); | 
					
						
							|  |  |  |                 table.appendChild(tr); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             resultSection.appendChild(table); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, 1000); | 
					
						
							| 
									
										
										
										
											2024-06-26 07:45:20 +00:00
										 |  |  | }); |