Explorar o código

Show scores for pokemon looked up by name

Kirk Trombley %!s(int64=3) %!d(string=hai) anos
pai
achega
b0ced5a015
Modificáronse 1 ficheiros con 14 adicións e 10 borrados
  1. 14 10
      nearest.js

+ 14 - 10
nearest.js

@@ -79,8 +79,17 @@ const onUpdate = () => {
   document.getElementById("y-vec").textContent = normQY ? "Ŷ(P)" : "Y(P)";
   document.getElementById("close-coeff-display").innerHTML = closeCoeff;
   document.getElementById("num-poke-display").textContent = numPoke;
+  
+  // determine metrics from configuration
+  const targetInSpace = useRGB ? targetRGB : rgb2luv(targetRGB.map(x => x / 255));
+  const xSelector = includeX ? (useRGB ? ({ xRGB }) => xRGB : ({ xLUV }) => xLUV) : () => 0;
+  const ySelector = useRGB ? ({ yRGB }) => yRGB : ({ yLUV }) => yLUV;
+  const yScorer = (normQY ? getNormedScorer : getUnnormedScorer)(closeCoeff, targetInSpace);
+  const totalScorer = info => xSelector(info) - yScorer(ySelector(info));
+
+  const newParams = paramsChanged(includeX, normQY, closeCoeff, useRGB, numPoke, targetColor);
 
-  if (targetColor.length === 7 && paramsChanged(includeX, normQY, closeCoeff, useRGB, numPoke, targetColor)) {
+  if (targetColor.length === 7 && newParams) {
     // calculate luminance to determine if text should be dark or light
     const textColor = vectorDot(targetRGB, [0.3, 0.6, 0.1]) >= 128 ? "#222" : "#ddd";
     document.querySelector("body").setAttribute("style", `background: ${targetColor}; color: ${textColor}`);
@@ -88,15 +97,9 @@ const onUpdate = () => {
     const bestList = document.getElementById("best-list");
     bestList.innerHTML = ''; // do the lazy thing
   
-    // determine metrics from configuration
-    const targetInSpace = useRGB ? targetRGB : rgb2luv(targetRGB.map(x => x / 255));
-    const xSelector = includeX ? (useRGB ? ({ xRGB }) => xRGB : ({ xLUV }) => xLUV) : () => 0;
-    const ySelector = useRGB ? ({ yRGB }) => yRGB : ({ yLUV }) => yLUV;
-    const yScorer = (normQY ? getNormedScorer : getUnnormedScorer)(closeCoeff, targetInSpace);
-  
     // actually score pokemon
     database
-      .map(info => ({ ...info, score: xSelector(info) - yScorer(ySelector(info)) }))
+      .map(info => ({ ...info, score: totalScorer(info) }))
       .sort((a, b) => a.score - b.score)
       .slice(0, numPoke)
       .forEach(info => {
@@ -106,13 +109,14 @@ const onUpdate = () => {
       });
   }
 
-  if (pokemonName.length > 0 && lastPkmnSearch !== pokemonName) {
+  if (pokemonName.length > 0 && (lastPkmnSearch !== pokemonName || newParams)) {
     lastPkmnSearch = pokemonName;
     // lookup by pokemon too
     const searchList = document.getElementById("search-list");
     searchList.innerHTML = '';
     pokemonLookup.search(pokemonName, { limit: 10 })
-      .forEach(({ item }) => {
+      .map(({ item }) => ({ ...item, score: totalScorer(item) }))
+      .forEach(item => {
         const li = document.createElement("li");
         li.appendChild(createPokemon(item))
         searchList.appendChild(li);