score.js 1.5 KB

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