Advertisement
divanov94

heroes logic

Jul 21st, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function heroes(input) {
  2.     let heroesCount = Number(input.shift());
  3.     let heroes = input.splice(0, heroesCount);
  4.     let heroesObj = {};
  5.  
  6.     for (let hero of heroes) {
  7.         let [name, health, mana] = hero.split(" ");
  8.         health = Number(health);
  9.         mana = Number(mana);
  10.         if (!heroesObj.hasOwnProperty(name)) {
  11.             heroesObj[name] = {health: health, mana: mana}
  12.         }
  13.     }
  14.     let commands = input.splice(0, input.indexOf("End"));
  15.     for (let line of commands) {
  16.         let [command, heroName, value, spellName] = line.split(" - ");
  17.         value = Number(value);
  18.  
  19.         switch (command) {
  20.             case "CastSpell":
  21.                 let manaNeeded = value;
  22.                 let spell = spellName;
  23.  
  24.  
  25.                 if (heroesObj[heroName].mana < manaNeeded) {
  26.                     console.log(`${heroName} does not have enough MP to cast ${spell}!`);
  27.                 } else if (heroesObj[heroName].mana > manaNeeded) {
  28.                     let manaLeft = heroesObj[heroName].mana -= manaNeeded;
  29.                     console.log(`${heroName} has successfully cast ${spell} and now has ${manaLeft} MP!`)
  30.  
  31.                 }
  32.                 break;
  33.             case "TakeDamage":
  34.                 let attacker = spellName;
  35.                 let damage = value;
  36.                 let currentHealth = heroesObj[heroName].health -= damage;
  37.                 if (currentHealth > 0) {
  38.                     console.log(`${heroName} was hit for ${damage} HP by ${attacker} and now has ${currentHealth} HP left!`);
  39.                 } else {
  40.                     delete heroesObj[heroName];
  41.                     console.log(`${heroName} has been killed by ${attacker}!`);
  42.  
  43.                 }
  44.                 break;
  45.             case "Recharge":
  46.                 let newMana = value;
  47.                 if (heroesObj[heroName].mana + newMana >= 200) {
  48.                     console.log(`${heroName} recharged for ${200 - heroesObj[heroName].mana} MP!`)
  49.                     heroesObj[heroName].mana = 200;
  50.                 } else {
  51.                     heroesObj[heroName].mana += newMana;
  52.                     console.log(`${heroName} recharged for ${newMana} MP!`)
  53.  
  54.                 }
  55.                 break;
  56.  
  57.  
  58.             case "Heal":
  59.                 let newHealth = value;
  60.                 if (heroesObj[heroName].health + newHealth >= 100) {
  61.                     console.log(`${heroName} healed for ${100 - heroesObj[heroName].health} HP!`)
  62.                     heroesObj[heroName].health = 100;
  63.                 } else {
  64.                     heroesObj[heroName].health += newHealth;
  65.                     console.log(`${heroName} healed for ${newHealth} HP!`);
  66.                 }
  67.  
  68.  
  69.         }
  70.  
  71.     }
  72.     let nameSort = Object.entries(heroesObj).sort((a, b) => a[0].localeCompare(b[0]));
  73.     let healthSort = nameSort.sort(((a, b) => b[1].health - a[1].health));
  74.     for (let kvp of healthSort) {
  75.         console.log(kvp[0]);
  76.         console.log(`  HP: ${kvp[1].health}`);
  77.         console.log(`  MP: ${kvp[1].mana}`);
  78.     }
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement