score.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const currentScores = {};
  2. const rescore = (target, { 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 rescoreAll = target => pokemonData.forEach(pkmn => rescore(target, pkmn));
  15. const getBestClusterIndex = (pkmn, space, { sortMetric, scaleOption, sortOrder }) => {
  16. // get the scales
  17. const scales = scaleOption(pkmn);
  18. // and multiply with the intended metric, and find the best value
  19. return argComp(sortOrder)(
  20. currentScores[pkmn.name][space].map((c, i) => c[sortMetric] * scales[i])
  21. );
  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. // and then get the *actual* score according to the sort metric
  29. return scaleOption(pkmn)[index] * clusters[index][sortMetric];
  30. };
  31. } else {
  32. // ignore scaleOption if not using clusters
  33. valueExtractor = pkmn => currentScores[pkmn.name][space].total[sortMetric];
  34. }
  35. return pokemonData
  36. .slice()
  37. .sort((a, b) => sortOrder(valueExtractor(a), valueExtractor(b)))
  38. .slice(0, number);
  39. };