Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function heroes(input) {
- let heroesCount = Number(input.shift());
- let heroes = input.splice(0, heroesCount);
- let heroesObj = {};
- for (let hero of heroes) {
- let [name, health, mana] = hero.split(" ");
- health = Number(health);
- mana = Number(mana);
- if (!heroesObj.hasOwnProperty(name)) {
- heroesObj[name] = {health: health, mana: mana}
- }
- }
- let commands = input.splice(0, input.indexOf("End"));
- for (let line of commands) {
- let [command, heroName, value, spellName] = line.split(" - ");
- value = Number(value);
- switch (command) {
- case "CastSpell":
- let manaNeeded = value;
- let spell = spellName;
- if (heroesObj[heroName].mana < manaNeeded) {
- console.log(`${heroName} does not have enough MP to cast ${spell}!`);
- } else if (heroesObj[heroName].mana > manaNeeded) {
- let manaLeft = heroesObj[heroName].mana -= manaNeeded;
- console.log(`${heroName} has successfully cast ${spell} and now has ${manaLeft} MP!`)
- }
- break;
- case "TakeDamage":
- let attacker = spellName;
- let damage = value;
- let currentHealth = heroesObj[heroName].health -= damage;
- if (currentHealth > 0) {
- console.log(`${heroName} was hit for ${damage} HP by ${attacker} and now has ${currentHealth} HP left!`);
- } else {
- delete heroesObj[heroName];
- console.log(`${heroName} has been killed by ${attacker}!`);
- }
- break;
- case "Recharge":
- let newMana = value;
- if (heroesObj[heroName].mana + newMana >= 200) {
- console.log(`${heroName} recharged for ${200 - heroesObj[heroName].mana} MP!`)
- heroesObj[heroName].mana = 200;
- } else {
- heroesObj[heroName].mana += newMana;
- console.log(`${heroName} recharged for ${newMana} MP!`)
- }
- break;
- case "Heal":
- let newHealth = value;
- if (heroesObj[heroName].health + newHealth >= 100) {
- console.log(`${heroName} healed for ${100 - heroesObj[heroName].health} HP!`)
- heroesObj[heroName].health = 100;
- } else {
- heroesObj[heroName].health += newHealth;
- console.log(`${heroName} healed for ${newHealth} HP!`);
- }
- }
- }
- let nameSort = Object.entries(heroesObj).sort((a, b) => a[0].localeCompare(b[0]));
- let healthSort = nameSort.sort(((a, b) => b[1].health - a[1].health));
- for (let kvp of healthSort) {
- console.log(kvp[0]);
- console.log(` HP: ${kvp[1].health}`);
- console.log(` MP: ${kvp[1].mana}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement