AlexanderHristov

04. MOBA Challenger

Dec 14th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputArr) {
  2.  
  3.     let playersObj = {};
  4.     let dataObj = {};
  5.  
  6.     for (let string of inputArr) {
  7.  
  8.         if (string === 'Season end') {
  9.             break;
  10.         }
  11.  
  12.         if (!string.includes('vs')) {
  13.  
  14.             let [player, position, skill] = string.split(' -> ');
  15.             skill = +skill;
  16.  
  17.             if (!playersObj.hasOwnProperty(player)) {
  18.  
  19.                 playersObj[player] = {};
  20.                 dataObj = playersObj[player];
  21.                 dataObj[position] = skill;
  22.  
  23.             } else {
  24.  
  25.                 if (!playersObj[player].hasOwnProperty(position)) {
  26.                     let currentDataObj = playersObj[player];
  27.                     currentDataObj[position] = skill;
  28.                 } else {
  29.  
  30.                     let currentDataObj = playersObj[player];
  31.                     if (currentDataObj[position] < skill) {
  32.                         currentDataObj[position] = skill;
  33.                     }
  34.                 }
  35.             }
  36.         } else {
  37.             let [firstPlayer, secondPlayer] = string.split(' vs ');
  38.  
  39.             if (playersObj.hasOwnProperty(firstPlayer) && playersObj.hasOwnProperty(secondPlayer)) {
  40.  
  41.                 let firstPlayerPositions = Object.keys(playersObj[firstPlayer]);
  42.                 let firstPlayerSkills = Object.values(playersObj[firstPlayer]);
  43.                 let firstPlayerTotalSkill = firstPlayerSkills.reduce((a, b) => a + b, 0);
  44.  
  45.                 let secondPlayerPositions = Object.keys(playersObj[secondPlayer]);
  46.                 let secondPlayerSkills = Object.values(playersObj[secondPlayer]);
  47.                 let secondPlayerTotalSkill = secondPlayerSkills.reduce((a, b) => a + b, 0);
  48.  
  49.                 for (let currentPosition of firstPlayerPositions) {
  50.  
  51.                     if (secondPlayerPositions.includes(currentPosition)) {
  52.  
  53.                         if (firstPlayerTotalSkill > secondPlayerTotalSkill) {
  54.  
  55.                             delete playersObj[secondPlayer];
  56.  
  57.                         } else if (firstPlayerTotalSkill < secondPlayerTotalSkill) {
  58.  
  59.                             delete playersObj[firstPlayer];
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     let playersArr = Object.keys(playersObj);
  68.  
  69.     for (let player of playersArr) {
  70.  
  71.         let totalSkill = Object.values(playersObj[player]).reduce((a, b) => a + b, 0);
  72.         let currentDataObj = playersObj[player];
  73.  
  74.         currentDataObj['totalSkill'] = totalSkill;
  75.     }
  76.  
  77.     let playersResult = Object.entries(playersObj).sort(sorter);
  78.  
  79.     for (let player of playersResult) {
  80.  
  81.         let playerPositions = Object.entries(playersObj[player[0]]).sort(skillSorter);
  82.         console.log(`${player[0]}: ${player[1].totalSkill} skill`);
  83.  
  84.         for (let position of playerPositions) {
  85.             if (position[0] !== 'totalSkill') {
  86.                 console.log(`- ${position[0]} <::> ${position[1]}`);
  87.             }
  88.         }
  89.     }
  90.  
  91.     function sorter(a, b) {
  92.  
  93.         let [aKey, aValue] = a;
  94.         let [bKey, bValue] = b;
  95.  
  96.         let firstCriteria = bValue.totalSkill - aValue.totalSkill;
  97.  
  98.         if (firstCriteria === 0) {
  99.  
  100.             return aKey.localeCompare(bKey);
  101.         }
  102.  
  103.         return firstCriteria
  104.     }
  105.     function skillSorter(a, b) {
  106.  
  107.         let firstCriteria = b[1] - a[1];
  108.  
  109.         if (firstCriteria === 0) {
  110.             return a[0].localeCompare(b[0]);
  111.         }
  112.  
  113.         return firstCriteria;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment