| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  | window.addEventListener('load', () => { | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |   Promise.all([fetchSettingData(), fetchSpriteData()]).then((results) => { | 
					
						
							|  |  |  |     // Page setup
 | 
					
						
							|  |  |  |     createDefaultSettings(results[0]); | 
					
						
							|  |  |  |     buildUI(results[0]); | 
					
						
							|  |  |  |     adjustHeaderWidth(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Event listeners
 | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |     document.getElementById('export-settings').addEventListener('click', () => exportSettings()); | 
					
						
							|  |  |  |     document.getElementById('generate-race').addEventListener('click', () => generateGame(true)) | 
					
						
							|  |  |  |     document.getElementById('generate-game').addEventListener('click', () => generateGame()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |     // Name input field
 | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |     const playerSettings = JSON.parse(localStorage.getItem('playerSettings')); | 
					
						
							|  |  |  |     const nameInput = document.getElementById('player-name'); | 
					
						
							|  |  |  |     nameInput.addEventListener('keyup', (event) => updateSetting(event)); | 
					
						
							|  |  |  |     nameInput.value = playerSettings.name; | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Sprite options
 | 
					
						
							|  |  |  |     const spriteData = JSON.parse(results[1]); | 
					
						
							|  |  |  |     const spriteSelect = document.getElementById('sprite'); | 
					
						
							| 
									
										
										
										
											2020-12-05 21:34:15 -05:00
										 |  |  |     spriteData.sprites.forEach((sprite) => { | 
					
						
							|  |  |  |       if (sprite.name.trim().length === 0) { return; } | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |       const option = document.createElement('option'); | 
					
						
							| 
									
										
										
										
											2020-12-05 21:34:15 -05:00
										 |  |  |       option.setAttribute('value', sprite.name.trim()); | 
					
						
							|  |  |  |       if (playerSettings.rom.sprite === sprite.name.trim()) { option.selected = true; } | 
					
						
							|  |  |  |       option.innerText = sprite.name; | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |       spriteSelect.appendChild(option); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }).catch((error) => { | 
					
						
							|  |  |  |     console.error(error); | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const fetchSettingData = () => new Promise((resolve, reject) => { | 
					
						
							|  |  |  |   const ajax = new XMLHttpRequest(); | 
					
						
							|  |  |  |   ajax.onreadystatechange = () => { | 
					
						
							|  |  |  |     if (ajax.readyState !== 4) { return; } | 
					
						
							|  |  |  |     if (ajax.status !== 200) { | 
					
						
							|  |  |  |       reject(ajax.responseText); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     try{ resolve(JSON.parse(ajax.responseText)); } | 
					
						
							|  |  |  |     catch(error){ reject(error); } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |   ajax.open('GET', `${window.location.origin}/static/static/playerSettings.json`, true); | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |   ajax.send(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const createDefaultSettings = (settingData) => { | 
					
						
							|  |  |  |   if (!localStorage.getItem('playerSettings')) { | 
					
						
							|  |  |  |     const newSettings = {}; | 
					
						
							|  |  |  |     for (let roSetting of Object.keys(settingData.readOnly)){ | 
					
						
							|  |  |  |       newSettings[roSetting] = settingData.readOnly[roSetting]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |     for (let generalOption of Object.keys(settingData.generalOptions)){ | 
					
						
							|  |  |  |       newSettings[generalOption] = settingData.generalOptions[generalOption]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |     for (let gameOption of Object.keys(settingData.gameOptions)){ | 
					
						
							|  |  |  |       newSettings[gameOption] = settingData.gameOptions[gameOption].defaultValue; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |     newSettings.rom = {}; | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |     for (let romOption of Object.keys(settingData.romOptions)){ | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |       newSettings.rom[romOption] = settingData.romOptions[romOption].defaultValue; | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     localStorage.setItem('playerSettings', JSON.stringify(newSettings)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const buildUI = (settingData) => { | 
					
						
							|  |  |  |   // Game Options
 | 
					
						
							|  |  |  |   const leftGameOpts = {}; | 
					
						
							|  |  |  |   const rightGameOpts = {}; | 
					
						
							|  |  |  |   Object.keys(settingData.gameOptions).forEach((key, index) => { | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |     if (index < Object.keys(settingData.gameOptions).length / 2) { leftGameOpts[key] = settingData.gameOptions[key]; } | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |     else { rightGameOpts[key] = settingData.gameOptions[key]; } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   document.getElementById('game-options-left').appendChild(buildOptionsTable(leftGameOpts)); | 
					
						
							|  |  |  |   document.getElementById('game-options-right').appendChild(buildOptionsTable(rightGameOpts)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // ROM Options
 | 
					
						
							|  |  |  |   const leftRomOpts = {}; | 
					
						
							|  |  |  |   const rightRomOpts = {}; | 
					
						
							|  |  |  |   Object.keys(settingData.romOptions).forEach((key, index) => { | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |     if (index < Object.keys(settingData.romOptions).length / 2) { leftRomOpts[key] = settingData.romOptions[key]; } | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |     else { rightRomOpts[key] = settingData.romOptions[key]; } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |   document.getElementById('rom-options-left').appendChild(buildOptionsTable(leftRomOpts, true)); | 
					
						
							|  |  |  |   document.getElementById('rom-options-right').appendChild(buildOptionsTable(rightRomOpts, true)); | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  | const buildOptionsTable = (settings, romOpts = false) => { | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |   const currentSettings = JSON.parse(localStorage.getItem('playerSettings')); | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |   const table = document.createElement('table'); | 
					
						
							|  |  |  |   const tbody = document.createElement('tbody'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Object.keys(settings).forEach((setting) => { | 
					
						
							|  |  |  |     const tr = document.createElement('tr'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // td Left
 | 
					
						
							|  |  |  |     const tdl = document.createElement('td'); | 
					
						
							|  |  |  |     const label = document.createElement('label'); | 
					
						
							|  |  |  |     label.setAttribute('for', setting); | 
					
						
							|  |  |  |     label.setAttribute('data-tooltip', settings[setting].description); | 
					
						
							|  |  |  |     label.innerText = `${settings[setting].friendlyName}:`; | 
					
						
							|  |  |  |     tdl.appendChild(label); | 
					
						
							|  |  |  |     tr.appendChild(tdl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // td Right
 | 
					
						
							|  |  |  |     const tdr = document.createElement('td'); | 
					
						
							|  |  |  |     const select = document.createElement('select'); | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |     select.setAttribute('id', setting); | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |     select.setAttribute('data-key', setting); | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |     if (romOpts) { select.setAttribute('data-romOpt', '1'); } | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |     settings[setting].options.forEach((opt) => { | 
					
						
							|  |  |  |       const option = document.createElement('option'); | 
					
						
							|  |  |  |       option.setAttribute('value', opt.value); | 
					
						
							|  |  |  |       option.innerText = opt.name; | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |       if ((isNaN(currentSettings[setting]) && (parseInt(opt.value, 10) === parseInt(currentSettings[setting]))) || | 
					
						
							|  |  |  |         (opt.value === currentSettings[setting])) { | 
					
						
							|  |  |  |         option.selected = true; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |       select.appendChild(option); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |     select.addEventListener('change', (event) => updateSetting(event)); | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |     tdr.appendChild(select); | 
					
						
							|  |  |  |     tr.appendChild(tdr); | 
					
						
							|  |  |  |     tbody.appendChild(tr); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   table.appendChild(tbody); | 
					
						
							|  |  |  |   return table; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  | const updateSetting = (event) => { | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |   const options = JSON.parse(localStorage.getItem('playerSettings')); | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  |   if (event.target.getAttribute('data-romOpt')) { | 
					
						
							|  |  |  |     options.rom[event.target.getAttribute('data-key')] = isNaN(event.target.value) ? | 
					
						
							|  |  |  |       event.target.value : parseInt(event.target.value, 10); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     options[event.target.getAttribute('data-key')] = isNaN(event.target.value) ? | 
					
						
							|  |  |  |       event.target.value : parseInt(event.target.value, 10); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-03 18:27:32 -05:00
										 |  |  |   localStorage.setItem('playerSettings', JSON.stringify(options)); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | const exportSettings = () => { | 
					
						
							|  |  |  |   const settings = JSON.parse(localStorage.getItem('playerSettings')); | 
					
						
							|  |  |  |   if (!settings.name || settings.name.trim().length === 0) { settings.name = "noname"; } | 
					
						
							|  |  |  |   const yamlText = jsyaml.safeDump(settings, { noCompatMode: true }).replaceAll(/'(\d+)':/g, (x, y) => `${y}:`); | 
					
						
							|  |  |  |   download(`${document.getElementById('player-name').value}.yaml`, yamlText); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** Create an anchor and trigger a download of a text file. */ | 
					
						
							|  |  |  | const download = (filename, text) => { | 
					
						
							|  |  |  |   const downloadLink = document.createElement('a'); | 
					
						
							|  |  |  |   downloadLink.setAttribute('href','data:text/yaml;charset=utf-8,'+ encodeURIComponent(text)) | 
					
						
							|  |  |  |   downloadLink.setAttribute('download', filename); | 
					
						
							|  |  |  |   downloadLink.style.display = 'none'; | 
					
						
							|  |  |  |   document.body.appendChild(downloadLink); | 
					
						
							|  |  |  |   downloadLink.click(); | 
					
						
							|  |  |  |   document.body.removeChild(downloadLink); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const generateGame = (raceMode = false) => { | 
					
						
							|  |  |  |   axios.post('/api/generate', { | 
					
						
							|  |  |  |     weights: { player: localStorage.getItem('playerSettings') }, | 
					
						
							|  |  |  |     presetData: { player: localStorage.getItem('playerSettings') }, | 
					
						
							|  |  |  |     playerCount: 1, | 
					
						
							|  |  |  |     race: raceMode ? '1' : '0', | 
					
						
							| 
									
										
										
										
											2020-12-04 00:42:33 -05:00
										 |  |  |   }).then((response) => { | 
					
						
							|  |  |  |     window.location.href = response.data.url; | 
					
						
							| 
									
										
										
										
											2020-12-04 00:40:57 -05:00
										 |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-12-04 05:30:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | const fetchSpriteData = () => new Promise((resolve, reject) => { | 
					
						
							|  |  |  |   const ajax = new XMLHttpRequest(); | 
					
						
							|  |  |  |   ajax.onreadystatechange = () => { | 
					
						
							|  |  |  |     if (ajax.readyState !== 4) { return; } | 
					
						
							|  |  |  |     if (ajax.status !== 200) { | 
					
						
							|  |  |  |       reject('Unable to fetch sprite data.'); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     resolve(ajax.responseText); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   ajax.open('GET', `${window.location.origin}/static/static/spriteData.json`, true); | 
					
						
							|  |  |  |   ajax.send(); | 
					
						
							|  |  |  | }); |