Advertisement
kstoyanov

03. Heroes of Code and Logic VII

Aug 8th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   let heroNumber = Number(args.shift());
  3.   const heroes = {};
  4.  
  5.   while (heroNumber > 0) {
  6.     let [heroName, health, mana] = args.shift().split(' ');
  7.     health = Number(health);
  8.     mana = Number(mana);
  9.     heroes[heroName] = { hp: health, mp: mana };
  10.  
  11.     heroNumber--;
  12.   }
  13.  
  14.   let command = args.shift();
  15.  
  16.   while (command !== 'End') {
  17.     const tokens = command.split(' - ');
  18.  
  19.     switch (tokens[0]) {
  20.       case 'CastSpell': castSpell(tokens[1], tokens[2], tokens[3]); break;
  21.       case 'TakeDamage': takeDamage(tokens[1], tokens[2], tokens[3]); break;
  22.       case 'Recharge': recharge(tokens[1], tokens[2]); break;
  23.       case 'Heal': heal(tokens[1], tokens[2]); break;
  24.     }
  25.  
  26.     command = args.shift();
  27.   }
  28.  
  29.   function castSpell(heroName, mpNeeded, spellName) {
  30.     mpNeeded = Number(mpNeeded);
  31.  
  32.     if (heroes[heroName].mp - mpNeeded >= 0) {
  33.       heroes[heroName].mp -= mpNeeded;
  34.       console.log(`${heroName} has successfully cast ${spellName} and now has ${heroes[heroName].mp} MP!`);
  35.     } else {
  36.       console.log(`${heroName} does not have enough MP to cast ${spellName}!`);
  37.     }
  38.   }
  39.  
  40.   function takeDamage(heroName, damage, attacker) {
  41.     damage = Number(damage);
  42.  
  43.     if (heroes[heroName].hp - damage > 0) {
  44.       heroes[heroName].hp -= damage;
  45.       console.log(`${heroName} was hit for ${damage} HP by ${attacker} and now has ${heroes[heroName].hp} HP left!`);
  46.     } else {
  47.       delete heroes[heroName];
  48.       console.log(`${heroName} has been killed by ${attacker}!`);
  49.     }
  50.   }
  51.  
  52.   function recharge(heroName, amount) {
  53.     amount = Number(amount);
  54.  
  55.     if (heroes[heroName].mp + amount > 200) {
  56.       const buff = 200 - heroes[heroName].mp;
  57.       heroes[heroName].mp = 200;
  58.       console.log(`${heroName} recharged for ${buff} MP!`);
  59.     } else {
  60.       heroes[heroName].mp += amount;
  61.       console.log(`${heroName} recharged for ${amount} MP!`);
  62.     }
  63.   }
  64.  
  65.   function heal(heroName, amount) {
  66.     amount = Number(amount);
  67.  
  68.     if (heroes[heroName].hp + amount > 100) {
  69.       const buff = 100 - heroes[heroName].hp;
  70.       heroes[heroName].hp = 100;
  71.       console.log(`${heroName} healed for ${buff} HP!`);
  72.     } else {
  73.       heroes[heroName].hp += amount;
  74.       console.log(`${heroName} healed for ${amount} HP!`);
  75.     }
  76.   }
  77.  
  78.   const nameSort = Object.entries(heroes).sort((a, b) => a[0].localeCompare(b[0]));
  79.   const hpSort = nameSort.sort((a, b) => b[1].hp - a[1].hp);
  80.  
  81.  
  82.   for (const kvp of hpSort) {
  83.     console.log(kvp[0]);
  84.     console.log(`  HP: ${kvp[1].hp}`);
  85.     console.log(`  MP: ${kvp[1].mp}`);
  86.   }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement