didkoslawow

Areba Tier

Mar 15th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arenaTier(gladiatorsInput) {
  2.    const gladiatorsInfo = [...gladiatorsInput];
  3.    const gladiators = {};
  4.    let index = 0;
  5.    let line = gladiatorsInfo[index];
  6.  
  7.    while (line !== 'Ave Cesar') {
  8.      if (line.includes('vs')) {
  9.        const [firsGladiator, secondGladiator] = line.split(' vs ');
  10.  
  11.        duel(gladiators, firsGladiator, secondGladiator);
  12.        index++;
  13.        line = gladiatorsInfo[index];
  14.        continue;
  15.      } else {
  16.        addGladiator(line);
  17.      }
  18.      index++;
  19.      line = gladiatorsInfo[index];
  20.    }
  21.  
  22.    sortAndPrint(gladiators);
  23.  
  24.    // logic for everything bellow:
  25.  
  26.    function addGladiator(line) {
  27.      [gladiatorName, technique, skill] = line.split(' -> ');
  28.      let totalSkill = 'totalSkill';
  29.      skill = Number(skill);
  30.  
  31.      if (!gladiators.hasOwnProperty(gladiatorName)) {
  32.        gladiators[gladiatorName] = {};
  33.        gladiators[gladiatorName][totalSkill] = 0;
  34.      }
  35.  
  36.      if (!gladiators[gladiatorName].hasOwnProperty(technique)) {
  37.        gladiators[gladiatorName][technique] = skill;
  38.        gladiators[gladiatorName][totalSkill] += skill;
  39.      } else {
  40.        if (skill > gladiators[gladiatorName][technique]) {
  41.          let difference = skill - gladiators[gladiatorName][technique];
  42.          gladiators[gladiatorName][technique] = skill;
  43.          gladiators[gladiatorName][totalSkill] += difference;
  44.        }
  45.      }
  46.    }
  47.  
  48.    function duel(gladiators, firstGladiator, secondGladiator) {
  49.      const firstExist = gladiators.hasOwnProperty(firstGladiator);
  50.      const secondExist = gladiators.hasOwnProperty(secondGladiator);
  51.  
  52.      const isValid = (firstG, secondG) => {
  53.        const first = Object.keys(gladiators[firstG]);
  54.        const second = Object.keys(gladiators[secondG]);
  55.  
  56.        return first.some(
  57.          (element) => element !== 'totalSkill' && second.includes(element)
  58.        );
  59.      };
  60.  
  61.      if (firstExist && secondExist && isValid(firstGladiator, secondGladiator)) {
  62.        const totalSkill = 'totalSkill';
  63.        if (
  64.          gladiators[firstGladiator][totalSkill] >
  65.          gladiators[secondGladiator][totalSkill]
  66.        ) {
  67.          delete gladiators[secondGladiator];
  68.        } else {
  69.          delete gladiators[firstGladiator];
  70.        }
  71.      }
  72.    }
  73.  
  74.    function sortAndPrint(gladiators) {
  75.      const sorted = Object.entries(gladiators).sort(
  76.        (a, b) => b[1].totalSkill - a[1].totalSkill || a[0].localeCompare(b[0])
  77.      );
  78.      for (const [name, skills] of sorted) {
  79.        const totalSkills = Object.entries(skills)[0][1];
  80.        delete skills.totalSkill;
  81.  
  82.        const sortedSkills = Object.entries(skills).sort(
  83.          (a, b) => b[1] - a[1] || a[0].localeCompare(b[0])
  84.        );
  85.  
  86.        console.log(`${name}: ${totalSkills} skill`);
  87.  
  88.        for (const [techniqueName, skill] of sortedSkills) {
  89.          console.log(`- ${techniqueName} <!> ${skill}`);
  90.        }
  91.      }
  92.    }
  93.  }
Add Comment
Please, Sign In to add comment