Btwonu

Heroes

Aug 7th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. function heroes(arr) {
  4.   //Define a hero
  5.   class Hero {
  6.     constructor(name, hp, mp) {
  7.       this.name = name;
  8.       this.health = hp;
  9.       this.mana = mp;
  10.     }
  11.  
  12.     CastSpell = castSpell;
  13.     TakeDamage = takeDamage;
  14.     Recharge = recharge;
  15.     Heal = heal;
  16.   }
  17.  
  18.   //Define variables and constants
  19.   let partyMembersNumber = Number(arr.shift());
  20.   const MAX_MANA = 200;
  21.   const MAX_HEALTH = 100;
  22.   const partyMembers = {};
  23.  
  24.   //Create new party members
  25.   let index;
  26.   let l = partyMembersNumber;
  27.  
  28.   for (index = 0; index < l; index++) {
  29.     let [heroName, hp, mp] = arr[index].split(' ');
  30.     hp = Number(hp);
  31.     mp = Number(mp);
  32.  
  33.     let newHero = new Hero(heroName, hp, mp);
  34.     partyMembers[heroName] = newHero;
  35.   }
  36.  
  37.   //Logic
  38.   let input = arr[index];
  39.  
  40.   while (input != 'End') {
  41.     //Get command
  42.     let [spell, heroName, ...args] = arr[index].split(' - ');
  43.  
  44.     partyMembers[heroName][spell](args);
  45.  
  46.     //Delete dead members
  47.     if (partyMembers[heroName].health <= 0) {
  48.       delete partyMembers[heroName];
  49.     }
  50.  
  51.     //Get next input
  52.     index++;
  53.     input = arr[index];
  54.   }
  55.  
  56.   //Sort and output
  57.   const keys = Object.keys(partyMembers);
  58.  
  59.   keys
  60.     .sort((nameA, nameB) => {
  61.       return nameA.localeCompare(nameB);
  62.     })
  63.     .sort((keyA, keyB) => {
  64.       return partyMembers[keyB].health - partyMembers[keyA].health;
  65.     });
  66.  
  67.   const output = [];
  68.  
  69.   keys.forEach((key) => {
  70.     let name = key;
  71.     let hp = '  HP: ' + partyMembers[key].health;
  72.     let mp = '  MP: ' + partyMembers[key].mana;
  73.  
  74.     output.push(name, hp, mp);
  75.   });
  76.  
  77.   console.log(output.join('\n'));
  78.  
  79.   //Declarations
  80.   function castSpell(params) {
  81.     let [manaNeeded, spell] = params;
  82.     manaNeeded = Number(manaNeeded);
  83.  
  84.     if (this.mana >= manaNeeded) {
  85.       //Casts the spell
  86.       this.mana -= manaNeeded;
  87.       console.log(
  88.         `${this.name} has successfully cast ${spell} and now has ${this.mana} MP!`
  89.       );
  90.       return;
  91.     }
  92.  
  93.     console.log(`${this.name} does not have enough MP to cast ${spell}!`);
  94.   }
  95.  
  96.   function takeDamage(params) {
  97.     let [damage, attacker] = params;
  98.     damage = Number(damage);
  99.  
  100.     this.health -= damage;
  101.     if (this.health > 0) {
  102.       console.log(
  103.         `${this.name} was hit for ${damage} HP by ${attacker} and now has ${this.health} HP left!`
  104.       );
  105.       return;
  106.     }
  107.  
  108.     console.log(`${this.name} has been killed by ${attacker}!`);
  109.   }
  110.  
  111.   function recharge(params) {
  112.     let recharged = params;
  113.     recharged = Number(recharged);
  114.     let oldMana = this.mana;
  115.  
  116.     this.mana =
  117.       this.mana + recharged > MAX_MANA ? MAX_MANA : this.mana + recharged;
  118.  
  119.     console.log(`${this.name} recharged for ${this.mana - oldMana} MP!`);
  120.   }
  121.  
  122.   function heal(params) {
  123.     let [healed] = params;
  124.     healed = Number(healed);
  125.     let oldHealth = this.health;
  126.  
  127.     this.health =
  128.       this.health + healed > MAX_HEALTH ? MAX_HEALTH : this.health + healed;
  129.  
  130.     console.log(`${this.name} healed for ${this.health - oldHealth} HP!`);
  131.   }
  132. }
  133.  
  134. let result = heroes([
  135.   '2',
  136.   'Solmyr 85 120',
  137.   'Kyrre 99 50',
  138.   'Heal - Solmyr - 10',
  139.   'Recharge - Solmyr - 50',
  140.   'TakeDamage - Kyrre - 66 - Orc',
  141.   'CastSpell - Kyrre - 15 - ViewEarth',
  142.   'End',
  143. ]);
  144. console.log(result);
  145.  
Add Comment
Please, Sign In to add comment