AreWe

Battle Manager

Aug 5th, 2020
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. battleManager = (input = []) => {
  2.     let ppl = {};
  3.     while((cmd = input.shift().split(':'))[0] !== 'Results') {
  4.         if(cmd[0] === 'Add') {
  5.             let [name, health, energy] = [cmd[1], ...cmd.splice(2).map(Number)];
  6.             if(ppl[name]) {
  7.                 ppl[name].health += health;
  8.             } else {
  9.                 ppl[name] = { health, energy };
  10.             }
  11.         } else if(cmd[0] === 'Attack') {
  12.             let [attackerName, defenderName, damage] = [cmd[1], cmd[2], Number(cmd[3])];
  13.             if(ppl[attackerName] && ppl[defenderName]) {
  14.                 if((ppl[defenderName].health -= damage) <= 0) {
  15.                     delete ppl[defenderName];
  16.                     console.log(`${defenderName} was disqualified!`);
  17.                 }
  18.                 if((ppl[attackerName].energy -= 1) === 0) {
  19.                     delete ppl[attackerName];
  20.                     console.log(`${attackerName} was disqualified!`);
  21.                 }
  22.             }
  23.         } else { // 'Delete'
  24.             if(cmd[1] === 'All') {
  25.                 ppl = {};
  26.             } else {
  27.                 delete ppl[cmd[1]];
  28.             }
  29.         }
  30.     }
  31.  
  32.     console.log(`People count: ${Object.keys(ppl).length}`);
  33.  
  34.     Object
  35.         .entries(ppl)
  36.         .sort((a, b) => b[1].health - a[1].health || a[0].localeCompare(b[0]))
  37.         .forEach(([name, stats]) => {
  38.             console.log(`${name} - ${stats.health} - ${stats.energy}`);
  39.         });
  40. };
Advertisement
Add Comment
Please, Sign In to add comment