Advertisement
AreWe

Arena Tier 100/100

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