Advertisement
kstoyanov

11. Arena Tier

Sep 25th, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arenaArena(input) {
  2.   function Gladiator(name = '', technique = '', skill = 0) {
  3.     this.setTechnique = (technique = '', skill = 0) => {
  4.       if (this.tech[technique] === undefined || this.tech[technique] < skill) {
  5.         this.tech[technique] = skill;
  6.       }
  7.     };
  8.  
  9.     this.getTotalSkill = () => Object.values(this.tech).reduce((total, skill) => total + skill, 0);
  10.  
  11.     [this.name, this.tech] = [name, {}];
  12.     this.setTechnique(technique, skill);
  13.   }
  14.  
  15.   function Arena() {
  16.     this.addGladiator = (name = '', technique = '', skill = '') => {
  17.       const gladiator = this.gladiators[name];
  18.       if (!gladiator) {
  19.         this.gladiators[name] = new Gladiator(name, technique, Number(skill));
  20.       } else {
  21.         gladiator.setTechnique(technique, Number(skill));
  22.       }
  23.     };
  24.  
  25.     this.battle = (gladiator1Name, gladiator2Name) => {
  26.       const [gladiator1, gladiator2] = [this.gladiators[gladiator1Name], this.gladiators[gladiator2Name]];
  27.       if (!gladiator1 || !gladiator2) {
  28.         return;
  29.       }
  30.  
  31.       const alltech = Object.keys(gladiator1.tech).concat(Object.keys(gladiator2.tech));
  32.       if (alltech.length !== new Set(alltech).size) {
  33.         const loser = gladiator1.getTotalSkill() > gladiator2.getTotalSkill() ? gladiator2.name : gladiator1.name;
  34.         delete this.gladiators[loser];
  35.       }
  36.     };
  37.  
  38.     this.executeCommand = (line = '') => {
  39.       line.includes('->') ? this.addGladiator(...line.split(' -> ')) : this.battle(...line.split(' vs '));
  40.     };
  41.  
  42.     this.gladiatorsInfo = () => Object.values(this.gladiators)
  43.       .sort((a, b) => b.getTotalSkill() - a.getTotalSkill() || a.name.localeCompare(b.name))
  44.       .map((gladiator) => {
  45.         const tech = Object.entries(gladiator.tech)
  46.           .sort(([tech1, skill1], [tech2, skill2]) => skill2 - skill1 || tech1 - tech2)
  47.           .map(([technique, skill]) => `- ${technique} <!> ${skill}`)
  48.           .join('\n');
  49.         return `${gladiator.name}: ${gladiator.getTotalSkill()} skill\n${tech}`;
  50.       })
  51.       .join('\n');
  52.  
  53.     this.gladiators = {};
  54.   }
  55.  
  56.   const tier = new Arena();
  57.  
  58.   let line = input.shift();
  59.  
  60.   while (line !== 'Ave Cesar') {
  61.     tier.executeCommand(line);
  62.     line = input.shift();
  63.   }
  64.  
  65.   return tier.gladiatorsInfo();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement