score.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const currentScores = {};
  2. const currentBestClusterIndices = {};
  3. const rescoreAll = (target) =>
  4. pokemonData.forEach(({ name, jab, rgb }) => {
  5. currentScores[name] = {
  6. jab: {
  7. total: applyMetrics(jab.total, target.jab),
  8. clusters: jab.clusters.map((c) => applyMetrics(c, target.jab)),
  9. },
  10. rgb: {
  11. total: applyMetrics(rgb.total, target.rgb),
  12. clusters: rgb.clusters.map((c) => applyMetrics(c, target.rgb)),
  13. },
  14. };
  15. });
  16. const getBestClusterIndex = (
  17. pkmn,
  18. space,
  19. { sortMetric, scaleOption, sortOrder }
  20. ) => {
  21. // get the scales
  22. const scales = scaleOption(pkmn[space]);
  23. // and multiply with the intended metric, and find the index of the best value
  24. return currentScores[pkmn.name][space].clusters
  25. .map((c, i) => [c[sortMetric] * scales[i], i])
  26. .reduce((a, b) => (sortOrder(a[0], b[0]) > 0 ? b : a))[1];
  27. };
  28. const getBest = (
  29. number,
  30. space,
  31. clusterSettings,
  32. { sortMetric, scaleOption, sortOrder }
  33. ) => {
  34. let valueExtractor;
  35. if (clusterSettings) {
  36. valueExtractor = (pkmn) => {
  37. const index = getBestClusterIndex(pkmn, space, clusterSettings);
  38. // save the index for rendering
  39. currentBestClusterIndices[pkmn.name] = {
  40. ...(currentBestClusterIndices[pkmn.name] || {}),
  41. [space]: index,
  42. };
  43. // and then get the *actual* score according to the sort metric
  44. const clusterScore =
  45. scaleOption(pkmn[space])[index] *
  46. currentScores[pkmn.name][space].clusters[index][sortMetric];
  47. if (!clusterSettings.multWithTotal) {
  48. return clusterScore;
  49. }
  50. // and then multiply it with the total score if that's needed
  51. return clusterScore * currentScores[pkmn.name][space].total[sortMetric];
  52. };
  53. } else {
  54. // ignore scaleOption if not using clusters
  55. valueExtractor = (pkmn) =>
  56. currentScores[pkmn.name][space].total[sortMetric];
  57. }
  58. return pokemonData
  59. .slice()
  60. .sort((a, b) => sortOrder(valueExtractor(a), valueExtractor(b)))
  61. .slice(0, number);
  62. };