krasizorbov

Arena Tier

Jul 15th, 2020
1,728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arenaTier(input = []) {
  2.   let result = {};
  3.   for (let i = 0; i < input.length; i++) {
  4.     if (input[i].includes("Ave Cesar")) {
  5.       break;
  6.     }
  7.     if (!input[i].includes("vs")) {
  8.       let [name, technique, skill] = input[i].split(" -> ");
  9.       skill = +skill;
  10.       if (!result.hasOwnProperty(name)) {
  11.         result[name] = {};
  12.         result[name][technique] = skill;
  13.       }
  14.       if (!result[name].hasOwnProperty(technique)) {
  15.         result[name][technique] = skill;
  16.       } else {
  17.         if (result[name][technique] < skill) {
  18.           result[name][technique] = skill;
  19.         }
  20.       }
  21.     } else {
  22.       let [name1, name2] = input[i].split(" vs ");
  23.       if (result.hasOwnProperty(name1) && result.hasOwnProperty(name2)) {
  24.         let firstTech = [...Object.keys(result[name1])];
  25.         let secondTech = [...Object.keys(result[name2])];
  26.         let isTrue = false;
  27.         for (let j = 0; j < firstTech.length; j++) {
  28.           if (secondTech.includes(firstTech[j])) {
  29.             isTrue = true;
  30.             break;
  31.           }
  32.         }
  33.         if (isTrue === true) {
  34.           let firstSkill = 0;
  35.           for (const key in result[name1]) {
  36.             if (result[name1].hasOwnProperty(key)) {
  37.               firstSkill += result[name1][key];
  38.             }
  39.           }
  40.           let secondSkill = 0;
  41.           for (const key in result[name2]) {
  42.             if (result[name2].hasOwnProperty(key)) {
  43.               secondSkill += result[name2][key];
  44.             }
  45.           }
  46.           if (firstSkill > secondSkill) {
  47.             delete result[name2];
  48.           } else {
  49.             delete result[name1];
  50.           }
  51.         }
  52.       }
  53.     }
  54.   }
  55.   let skillOrder = {};
  56.   let names = [...Object.keys(result)];
  57.   for (const name of names) {
  58.     let sum = 0;
  59.     for (const tech in result[name]) {
  60.       sum += result[name][tech];
  61.     }
  62.     skillOrder[name] = sum;
  63.   }
  64.   let output = [...Object.entries(skillOrder)].sort((a, b) => b[1] - a[1]);
  65.   for (let i = 0; i < output.length; i++) {
  66.     console.log(`${output[i][0]}: ${output[i][1]} skill`);
  67.     let ss = [...Object.entries(result[output[i][0]])];
  68.     for (const s of ss.sort(
  69.       (a, b) => b[1] - a[1] || a[0].localeCompare(b[0])
  70.     )) {
  71.       console.log(`- ${s[0]} <!> ${s[1]}`);
  72.     }
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment