From bd6e4fec817bf0e7c3c5c03e6e787c10f5d099d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emily=20B=C3=B6hm?= Date: Sat, 3 Aug 2024 08:57:00 +0200 Subject: [PATCH] Find custom set symbols by name Search for set symbols in both the custom and the official directories, preferring custom over official and svgs over pngs. This way, people can just drop their own set symbols into img/setSymbols/custom/ and use the set code like they'd do for official cards. Unfortunately this will log a 404 error message to console whenever we fail to find an image. I'm not aware of any way to avoid this. --- js/creator-23.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/js/creator-23.js b/js/creator-23.js index b2119dc2..90369396 100644 --- a/js/creator-23.js +++ b/js/creator-23.js @@ -4263,6 +4263,28 @@ function uploadSetSymbol(imageSource, otherParams) { }; } } +async function uploadLocalSetSymbol(imageName, otherParams) { + // Try different paths for local set symbols, prioritizing custom over official and svgs over pngs. + if (otherParams && otherParams == 'resetSetSymbol') { + setSymbol.onload = function() { + resetSetSymbol(); + setSymbol.onload = setSymbolEdited; + }; + } + const variations = [ + `/img/setSymbols/custom/${imageName}.svg`, + `/img/setSymbols/custom/${imageName}.png`, + `/img/setSymbols/official/${imageName}.svg`, + `/img/setSymbols/official/${imageName}.png`, + ] + for (url of variations) { + response = await fetch(url, {method: "HEAD"}); + if (response.ok) { + setSymbol.src = url; + break; + } + } +} function setSymbolEdited() { card.setSymbolSource = setSymbol.src; if (document.querySelector('#lockSetSymbolURL').checked) { @@ -4306,9 +4328,9 @@ function fetchSetSymbol() { } var setRarity = document.querySelector('#set-symbol-rarity').value.toLowerCase().replace('uncommon', 'u').replace('common', 'c').replace('rare', 'r').replace('mythic', 'm') || 'c'; if (['sld', 'a22', 'a23', 'j22'].includes(setCode.toLowerCase())) { - uploadSetSymbol(fixUri(`/img/setSymbols/custom/${setCode.toLowerCase()}-${setRarity}.png`), 'resetSetSymbol'); + uploadLocalSetSymbol(`${setCode.toLowerCase()}-${setRarity}`, 'resetSetSymbol'); } else if (['cc', 'logan', 'joe'].includes(setCode.toLowerCase())) { - uploadSetSymbol(fixUri(`/img/setSymbols/custom/${setCode.toLowerCase()}-${setRarity}.svg`), 'resetSetSymbol'); + uploadLocalSetSymbol(`${setCode.toLowerCase()}-${setRarity}`, 'resetSetSymbol'); } else if (document.querySelector("#set-symbol-source").value == 'gatherer') { if (setSymbolAliases.has(setCode.toLowerCase())) setCode = setSymbolAliases.get(setCode.toLowerCase()); uploadSetSymbol('http://gatherer.wizards.com/Handlers/Image.ashx?type=symbol&set=' + setCode + '&size=large&rarity=' + setRarity, 'resetSetSymbol'); @@ -4316,12 +4338,8 @@ function fetchSetSymbol() { if (setSymbolAliases.has(setCode.toLowerCase())) setCode = setSymbolAliases.get(setCode.toLowerCase()); uploadSetSymbol('https://api.hexproof.io/symbols/set/' + setCode + '/' + setRarity, 'resetSetSymbol'); } else { - var extension = 'svg'; - if (['moc', 'ltr', 'ltc', 'cmm', 'who', 'scd', 'woe', 'wot', 'woc', 'lci', 'lcc', 'mkm', 'mkc', 'otj', 'otc'].includes(setCode.toLowerCase())) { - extension = 'png'; - } if (setSymbolAliases.has(setCode.toLowerCase())) setCode = setSymbolAliases.get(setCode.toLowerCase()); - uploadSetSymbol(fixUri(`/img/setSymbols/official/${setCode.toLowerCase()}-${setRarity}.` + extension), 'resetSetSymbol'); + uploadLocalSetSymbol(`${setCode.toLowerCase()}-${setRarity}`, 'resetSetSymbol'); } } function lockSetSymbolCode() {