score.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. const clusterScore = scaleOption(pkmn[space])[index] * currentScores[pkmn.name][space].clusters[index][sortMetric];
  32. if (!clusterSettings.multWithTotal) {
  33. return clusterScore;
  34. }
  35. // and then multiply it with the total score if that's needed
  36. return clusterScore * currentScores[pkmn.name][space].total[sortMetric];
  37. };
  38. } else {
  39. // ignore scaleOption if not using clusters
  40. valueExtractor = pkmn => currentScores[pkmn.name][space].total[sortMetric];
  41. }
  42. return pokemonData
  43. .slice()
  44. .sort((a, b) => sortOrder(valueExtractor(a), valueExtractor(b)))
  45. .slice(0, number);
  46. };