export_s1_ranks.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Checkout commit de1fdc341b09addeabd719e7a058e3ad449398bc and run on old ranks.db backup
  2. import databases from '../database.js';
  3. const RANK_DIVISIONS = [
  4. 'Cardboard',
  5. 'Wood',
  6. 'Wood+',
  7. 'Wood++',
  8. 'Bronze',
  9. 'Bronze+',
  10. 'Bronze++',
  11. 'Silver',
  12. 'Silver+',
  13. 'Silver++',
  14. 'Gold',
  15. 'Gold+',
  16. 'Gold++',
  17. 'Platinum',
  18. 'Platinum+',
  19. 'Platinum++',
  20. 'Diamond',
  21. 'Diamond+',
  22. 'Diamond++',
  23. 'Legendary',
  24. ];
  25. function get_rank_text(rank_float) {
  26. if (rank_float == null || typeof rank_float === 'undefined') {
  27. return 'Unranked';
  28. }
  29. if (rank_float == 1.0) {
  30. return 'The One';
  31. }
  32. // Epic rank distribution algorithm
  33. for (let i = 0; i < RANK_DIVISIONS.length; i++) {
  34. // Turn current 'Cardboard' rank into a value between 0 and 1
  35. const rank_nb = (i + 1) / RANK_DIVISIONS.length;
  36. // To make climbing ranks more satisfying, we make lower ranks more common.
  37. // Visual representation: https://graphtoy.com/?f1(x,t)=1-((cos(x%5E0.8*%F0%9D%9C%8B)/2)+0.5)&v1=true&f2(x,t)=&v2=true&f3(x,t)=&v3=false&f4(x,t)=&v4=false&f5(x,t)=&v5=false&f6(x,t)=&v6=false&grid=true&coords=0.3918011117299855,0.3722110561434862,1.0068654346588846
  38. const cutoff = 1 - ((Math.cos(Math.pow(rank_nb, 0.8) * Math.PI) / 2) + 0.5);
  39. if (rank_float < cutoff) {
  40. return RANK_DIVISIONS[i];
  41. }
  42. }
  43. // Ok, floating point errors, who cares
  44. return RANK_DIVISIONS[RANK_DIVISIONS.length - 1];
  45. }
  46. async function export_ranks() {
  47. const ranked_users = databases.ranks.prepare(`SELECT user_id, games_played FROM user WHERE games_played > 9 ORDER BY elo DESC`).all();
  48. const unranked_users = databases.ranks.prepare(`SELECT user_id, games_played FROM user WHERE games_played <= 9 ORDER BY elo DESC`).all();
  49. console.log('BEGIN TRANSACTION;');
  50. for (let i = 0; i < ranked_users.length; i++) {
  51. const division = get_rank_text(1.0 - i / ranked_users.length);
  52. console.log(`UPDATE rating SET s1_division = '${division}', s1_scores = ${ranked_users[i].games_played} WHERE user_id = ${ranked_users[i].user_id} AND mode = 0;`);
  53. }
  54. for (let i = 0; i < unranked_users.length; i++) {
  55. console.log(`UPDATE rating SET s1_scores = ${unranked_users[i].games_played} WHERE user_id = ${unranked_users[i].user_id} AND mode = 0;`);
  56. }
  57. console.log('COMMIT;');
  58. }
  59. export_ranks();