diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-08-22 15:01:03 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-08-22 15:01:03 +0200 |
commit | c0354b250f84d578b609a7f25d71dee7fc24e9ca (patch) | |
tree | af586e0a4c44a2f2c8df956ca3b992be15daaba3 /resources/js/ui.js | |
parent | 54e5ffaeb79f989463c144e58dfcd677b752c5a9 (diff) |
currency, save/load
Diffstat (limited to 'resources/js/ui.js')
-rw-r--r-- | resources/js/ui.js | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/resources/js/ui.js b/resources/js/ui.js index bfd9204..92ca313 100644 --- a/resources/js/ui.js +++ b/resources/js/ui.js @@ -684,7 +684,10 @@ const UI = { drawStatus () { const currentArea = Memory.state.currentArea; - UI.elements.status.querySelector('[data-template-slot="money"]').textContent = `${Memory.state.money} €`; + UI.elements.status.querySelector('[data-template-slot="money"]').textContent = + `${Memory.state.money.toFixed(DB.currencies.map[Memory.state.Settings.currency].decimals)}` + + ' ' + + `${DB.currencies.map[Memory.state.Settings.currency].symbol}`; UI.elements.status.querySelector('[data-template-slot="monsterProgress"]').textContent = `${currentArea.monsterProgress} / ${currentArea.requiredEncounters}`; UI.elements.status.querySelector('[data-template-slot="trainerProgress"]').textContent = `${currentArea.trainerProgress} / ${currentArea.trainers.length}`; @@ -943,6 +946,50 @@ const UI = { }); + /* Currency */ + + const currencySelectNode = template.querySelector('[data-template-slot="currency"]'); + const currencyCodeMap = Object.keys(DB.currencies.map).sort((a, b) => { + const nameA = DB.currencies.map[a].name; + const nameB = DB.currencies.map[b].name; + + return nameA > nameB ? 1 : -1; + }); + + for (const currencyCode of currencyCodeMap) { + const currency = DB.currencies.map[currencyCode]; + const currencyOptionNode = document.createElement('option'); + + currencyOptionNode.value = currencyCode, + currencyOptionNode.textContent = `${currency.symbol} - ${currency.name}`; + + if (currencyCode === Memory.state.Settings.currency) { + currencyOptionNode.selected = true; + } + + currencySelectNode.appendChild(currencyOptionNode); + } + + currencySelectNode.addEventListener('change', async () => { + const selected = [...currencySelectNode.children].find((node) => node.selected === true); + const previousCurrencyCode = Memory.state.Settings.currency; + const newCurrencyCode = selected.value; + + Memory.state.Settings.currency = newCurrencyCode; + + // re-calculate money + const previousCurrency = DB.currencies.map[previousCurrencyCode]; + const newCurrency = DB.currencies.map[newCurrencyCode]; + const baseRateMoney = Memory.state.money / previousCurrency.rate; + const exchangedMoney = baseRateMoney * newCurrency.rate; + Memory.state.money = Number(exchangedMoney.toFixed(newCurrency.decimals)); + + UI.drawStatus(); + }); + + template.querySelector('[data-template-slot="currency.lastUpdated"]').textContent = DB.currencies.last_updated; + + popup.querySelector('.popup').appendChild(template); UI.drawPopup(popup); }, |