score.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const currentScores = {};
  2. const currentBestClusterIndices = {};
  3. const rescoreAll = target => pokemonData.forEach(({ name, jab, rgb }) => {
  4. currentScores[name] = {
  5. jab: {
  6. total: applyMetrics(jab.total, target.jab),
  7. clusters: jab.clusters.map(c => applyMetrics(c, target.jab)),
  8. },
  9. rgb: {
  10. total: applyMetrics(rgb.total, target.rgb),
  11. clusters: rgb.clusters.map(c => applyMetrics(c, target.rgb)),
  12. },
  13. };
  14. });
  15. const getBestClusterIndex = (pkmn, space, { sortMetric, scaleOption, sortOrder }) => {
  16. // get the scales
  17. const scales = scaleOption(pkmn[space]);
  18. // and multiply with the intended metric, and find the index of the best value
  19. return currentScores[pkmn.name][space].clusters
  20. .map((c, i) => [c[sortMetric] * scales[i], i])
  21. .reduce((a, b) => sortOrder(a[0], b[0]) > 0 ? b : a)[1];
  22. }
  23. const getBest = (number, space, clusterSettings, { sortMetric, scaleOption, sortOrder }) => {
  24. let valueExtractor;
  25. if (clusterSettings) {
  26. valueExtractor = pkmn => {
  27. const index = getBestClusterIndex(pkmn, space, clusterSettings);
  28. // save the index for rendering
  29. currentBestClusterIndices[pkmn.name] = { ...(currentBestClusterIndices[pkmn.name] || {}), [space]: index };
  30. // and then get the *actual* score according to the sort metric
  31. return scaleOption(pkmn[space])[index] * currentScores[pkmn.name][space].clusters[index][sortMetric];
  32. };
  33. } else {
  34. // ignore scaleOption if not using clusters
  35. valueExtractor = pkmn => currentScores[pkmn.name][space].total[sortMetric];
  36. }
  37. return pokemonData
  38. .slice()
  39. .sort((a, b) => sortOrder(valueExtractor(a), valueExtractor(b)))
  40. .slice(0, number);
  41. };