feat: add currency conversions to price table

Exchange rates are loaded once per page and passed to price row generation, so we don't hit the cache more than once.
This commit is contained in:
xenticore
2025-04-16 18:24:27 -04:00
parent fcf077c877
commit 1d92d9e20c
3 changed files with 12 additions and 10 deletions

View File

@@ -222,7 +222,7 @@ async function inject() {
} }
const qualityName = itemQualities[quality as unknown as keyof typeof itemQualities].toString() const qualityName = itemQualities[quality as unknown as keyof typeof itemQualities].toString()
const priceRow = createPriceRow(qualityName, data, keyPrice, locale) const priceRow = createPriceRow(qualityName, data, keyPrice, exchangeRates, locale)
priceRows.push({order: quality == 6 ? -1 : quality, row: priceRow, category: PriceRowCategory.None}) priceRows.push({order: quality == 6 ? -1 : quality, row: priceRow, category: PriceRowCategory.None})
}) })
@@ -239,7 +239,7 @@ async function inject() {
log(`Australium ${itemName} is unpriced or unavailable, skipping...`) log(`Australium ${itemName} is unpriced or unavailable, skipping...`)
} }
const priceRow = createPriceRow($T("Australium"), data, keyPrice, locale, "https://wiki.teamfortress.com/wiki/Australium_weapons") const priceRow = createPriceRow($T("Australium"), data, keyPrice, exchangeRates, locale, "https://wiki.teamfortress.com/wiki/Australium_weapons")
priceRows.push({order: 99, row: priceRow, category: PriceRowCategory.None}) priceRows.push({order: 99, row: priceRow, category: PriceRowCategory.None})
resolve() resolve()
@@ -271,7 +271,7 @@ async function inject() {
log(`Festive ${itemName} is unpriced or unavailable, skipping...`) log(`Festive ${itemName} is unpriced or unavailable, skipping...`)
} }
const priceRow = createPriceRow($T("Unique"), data, keyPrice, locale) const priceRow = createPriceRow($T("Unique"), data, keyPrice, exchangeRates, locale)
priceRows.push({order: -1, row: priceRow, category: PriceRowCategory.Festive}) priceRows.push({order: -1, row: priceRow, category: PriceRowCategory.Festive})
resolve() resolve()
@@ -287,7 +287,7 @@ async function inject() {
log(`Strange Festive ${itemName} is unpriced or unavailable, skipping...`) log(`Strange Festive ${itemName} is unpriced or unavailable, skipping...`)
} }
const priceRow = createPriceRow($T("Strange"), data, keyPrice, locale) const priceRow = createPriceRow($T("Strange"), data, keyPrice, exchangeRates, locale)
priceRows.push({order: 11, row: priceRow, category: PriceRowCategory.Festive}) priceRows.push({order: 11, row: priceRow, category: PriceRowCategory.Festive})
resolve() resolve()
@@ -331,7 +331,7 @@ async function inject() {
return return
} }
const priceRow = createPriceRow($T(`kt-${tier}`), data, keyPrice, locale, "https://wiki.teamfortress.com/wiki/Killstreak_Kit") const priceRow = createPriceRow($T(`kt-${tier}`), data, keyPrice, exchangeRates, locale, "https://wiki.teamfortress.com/wiki/Killstreak_Kit")
priceRows.push({order: tier, row: priceRow, category: PriceRowCategory.KillstreakKit}) priceRows.push({order: tier, row: priceRow, category: PriceRowCategory.KillstreakKit})
resolve() resolve()
@@ -378,7 +378,7 @@ async function inject() {
log(`${itemName} is unpriced or unavailable, skipping...`) log(`${itemName} is unpriced or unavailable, skipping...`)
} }
const priceRow = createPriceRow($T(variantName), data, keyPrice, locale, "https://wiki.teamfortress.com/wiki/Botkiller_weapons") const priceRow = createPriceRow($T(variantName), data, keyPrice, exchangeRates, locale, "https://wiki.teamfortress.com/wiki/Botkiller_weapons")
// FIXME: order should be by release // FIXME: order should be by release
// Silver Mk.I, Gold Mk.II, Rust, Blood, Carbonado, Diamond, Silver Mk.II, Gold Mk.II // Silver Mk.I, Gold Mk.II, Rust, Blood, Carbonado, Diamond, Silver Mk.II, Gold Mk.II

View File

@@ -1,10 +1,11 @@
import { ExchangeRates } from "./exchangeRateService";
import { ItemPriceData } from "./priceService"; import { ItemPriceData } from "./priceService";
import { convertTF2PriceToUSD, defaultCurrencyForPageLocale, convertUSD } from "./utils/currency"; import { convertTF2PriceToUSD, defaultCurrencyForPageLocale, convertUSD } from "./utils/currency";
import { formatPrice } from "./utils/formatting"; import { formatPrice } from "./utils/formatting";
import { $T } from "./utils/localization"; import { $T } from "./utils/localization";
import { logError } from "./utils/log"; import { logError } from "./utils/log";
export function createPriceRow(qualityName: string, data: ItemPriceData, keyPrice: ItemPriceData, locale: string, wikiPage: string = null): HTMLTableRowElement { export function createPriceRow(qualityName: string, data: ItemPriceData, keyPrice: ItemPriceData, rates: ExchangeRates, locale: string, wikiPage: string = null): HTMLTableRowElement {
const priceRow = document.createElement("tr"); const priceRow = document.createElement("tr");
const priceLabel = document.createElement("td"); const priceLabel = document.createElement("td");
@@ -32,7 +33,7 @@ export function createPriceRow(qualityName: string, data: ItemPriceData, keyPric
const currency = defaultCurrencyForPageLocale(locale) ?? 'USD' const currency = defaultCurrencyForPageLocale(locale) ?? 'USD'
if(currency !== 'USD') { if(currency !== 'USD') {
try { try {
const realPrice = convertUSD(realPriceUSD, currency) const realPrice = convertUSD(realPriceUSD, currency, rates)
const currencyFormatter = new Intl.NumberFormat(locale, { style: "currency", currency: currency }) const currencyFormatter = new Intl.NumberFormat(locale, { style: "currency", currency: currency })
realPriceString = currencyFormatter.format(realPrice).replace(/\s+/g, '') realPriceString = currencyFormatter.format(realPrice).replace(/\s+/g, '')
} catch (e) { } catch (e) {

View File

@@ -1,4 +1,5 @@
import { conversion_ref_usd } from '../config'; import { conversion_ref_usd } from '../config';
import { ExchangeRates } from '../exchangeRateService';
export function defaultCurrencyForPageLocale(locale: string): string | null { export function defaultCurrencyForPageLocale(locale: string): string | null {
switch (locale) { switch (locale) {
@@ -39,9 +40,9 @@ export function convertTF2PriceToUSD(keys: number, metal: number, keyPrice: numb
return Math.ceil(pureMetal * conversion_ref_usd * 100) / 100 return Math.ceil(pureMetal * conversion_ref_usd * 100) / 100
} }
export function convertUSD(usd: number, currency: string): number { export function convertUSD(usd: number, currency: string, rates: ExchangeRates): number {
const cur = currency.toUpperCase() const cur = currency.toUpperCase()
if(cur === 'USD') return usd if(cur === 'USD') return usd
throw new Error(`Not implemented`) return usd * rates[cur]
} }