Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- input.pop();
- let heroCount = Number(input.shift());
- let heroes = {};
- let maxHealth = 100;
- let maxMana = 200;
- let resultForCheck = 0;
- for (let i = 0; i < heroCount; i++) {
- let hero = input.shift().split(' ');
- if (!heroes.hasOwnProperty(hero[0])) {
- heroes[hero[0]] = {
- health: hero[1],
- mana: hero[2],
- }
- }
- }
- for (const line of input) {
- let command = line.split(' - ');
- if (command[0] === 'CastSpell') {
- if (heroes.hasOwnProperty(command[1])) {
- let spellAndHealth = Object.values(heroes[command[1]])
- let health = spellAndHealth[0]
- let decreaseSpell = Number(spellAndHealth[1])
- if (decreaseSpell >= command[2]) {
- heroes[command[1]] = {
- health: health,
- mana: Number(decreaseSpell) - Number(command[2])
- }
- console.log(`${command[1]} has successfully cast ${command[3]} and now has ${Number(decreaseSpell) - Number(command[2])} MP!`)
- } else {
- console.log(`${command[1]} does not have enough MP to cast ${command[3]}!`)
- }
- }
- }
- if (command[0] === 'TakeDamage') {
- if (heroes.hasOwnProperty(command[1])) {
- let spellAndHealth = Object.values(heroes[command[1]])
- let health = Number(spellAndHealth[0])
- let decreaseSpell = spellAndHealth[1]
- if (health >= command[2]) {
- heroes[command[1]] = {
- health: Number(health) - Number(command[2]),
- mana: decreaseSpell
- }
- console.log(`${command[1]} was hit for ${command[2]} HP by ${command[3]} and now has ${Number(health) - Number(command[2])} HP left!`)
- } else {
- delete heroes[command[1]];
- console.log(`${command[1]} has been killed by ${command[3]}!`)
- }
- }
- }
- if (command[0] === 'Recharge') {
- if (heroes.hasOwnProperty(command[1])) {
- let spellAndHealth = Object.values(heroes[command[1]])
- let health = spellAndHealth[0]
- let decreaseSpell = Number(spellAndHealth[1])
- let checker = decreaseSpell + Number(command[2])
- if (checker > maxMana) {
- resultForCheck = maxMana - decreaseSpell
- heroes[command[1]] = {
- health: health,
- mana: decreaseSpell + resultForCheck
- }
- console.log(`${command[1]} recharged for ${resultForCheck} MP!`)
- } else {
- heroes[command[1]] = {
- health: health,
- mana: decreaseSpell + Number(command[2])
- }
- console.log(`${command[1]} recharged for ${command[2]} MP!`)
- }
- }
- }
- if (command[0] === 'Heal') {
- if (heroes.hasOwnProperty(command[1])) {
- let spellAndHealth = Object.values(heroes[command[1]])
- let health = Number(spellAndHealth[0])
- let decreaseSpell = spellAndHealth[1]
- let checker = health + Number(command[2]);
- if (checker > maxHealth) {
- resultForCheck = maxHealth - health;
- heroes[command[1]] = {
- health: health + resultForCheck,
- mana: decreaseSpell
- }
- console.log(`${command[1]} healed for ${resultForCheck} HP!`)
- } else {
- heroes[command[1]] = {
- health: health + Number(command[2]),
- mana: decreaseSpell
- }
- console.log(`${command[1]} healed for ${command[2]} HP!`)
- }
- }
- }
- }
- function sortByTwoCritters(a, b) {
- let healthA = a[1]['health'];
- let healthB = b[1]['health'];
- if (healthA < healthB) {
- return b[0].localeCompare(a[0])
- } else {
- return healthA - healthB;
- }
- }
- let entries = Object.entries(heroes).sort(sortByTwoCritters)
- //.sort(([name1, HPMP1], [name2, HPMP2]) => {
- // if (HPMP2[0] === HPMP1[0])
- // return name1.localeCompare(name2)
- // return HPMP2[0] - HPMP1[0]
- //});
- for (let i = 0; i < entries.length; i++) {
- let warrior = entries[i];
- let values = Array.from(Object.values(warrior[1]))
- let hp = ' HP: ' + values[0]
- let mp = ' MP: ' + values[1]
- console.log(`${warrior[0]}`)
- console.log(hp)
- console.log(mp)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement