Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     input.pop();
  3.     let heroCount = Number(input.shift());
  4.     let heroes = {};
  5.     let maxHealth = 100;
  6.     let maxMana = 200;
  7.     let resultForCheck = 0;
  8.  
  9.     for (let i = 0; i < heroCount; i++) {
  10.         let hero = input.shift().split(' ');
  11.         if (!heroes.hasOwnProperty(hero[0])) {
  12.             heroes[hero[0]] = {
  13.                 health: hero[1],
  14.                 mana: hero[2],
  15.             }
  16.         }
  17.     }
  18.  
  19.     for (const line of input) {
  20.         let command = line.split(' - ');
  21.  
  22.         if (command[0] === 'CastSpell') {
  23.             if (heroes.hasOwnProperty(command[1])) {
  24.                 let spellAndHealth = Object.values(heroes[command[1]])
  25.                 let health = spellAndHealth[0]
  26.                 let decreaseSpell = Number(spellAndHealth[1])
  27.                 if (decreaseSpell >= command[2]) {
  28.                 heroes[command[1]] = {
  29.                     health: health,
  30.                     mana: Number(decreaseSpell) - Number(command[2])
  31.                 }
  32.                 console.log(`${command[1]} has successfully cast ${command[3]} and now has ${Number(decreaseSpell) - Number(command[2])} MP!`)
  33.                 } else {
  34.                     console.log(`${command[1]} does not have enough MP to cast ${command[3]}!`)
  35.                 }
  36.             }
  37.         }
  38.  
  39.         if (command[0] === 'TakeDamage') {
  40.             if (heroes.hasOwnProperty(command[1])) {
  41.                 let spellAndHealth = Object.values(heroes[command[1]])
  42.                 let health = Number(spellAndHealth[0])
  43.                 let decreaseSpell = spellAndHealth[1]
  44.                 if (health >= command[2]) {
  45.                     heroes[command[1]] = {
  46.                         health: Number(health) - Number(command[2]),
  47.                         mana: decreaseSpell
  48.                     }
  49.                     console.log(`${command[1]} was hit for ${command[2]} HP by ${command[3]} and now has ${Number(health) - Number(command[2])} HP left!`)
  50.                 } else {
  51.                     delete heroes[command[1]];
  52.                     console.log(`${command[1]} has been killed by ${command[3]}!`)
  53.                 }
  54.             }
  55.         }
  56.  
  57.         if (command[0] === 'Recharge') {
  58.             if (heroes.hasOwnProperty(command[1])) {
  59.                 let spellAndHealth = Object.values(heroes[command[1]])
  60.                 let health = spellAndHealth[0]
  61.                 let decreaseSpell = Number(spellAndHealth[1])
  62.                 let checker = decreaseSpell + Number(command[2])
  63.                 if (checker > maxMana) {
  64.                     resultForCheck = maxMana - decreaseSpell
  65.                     heroes[command[1]] = {
  66.                     health: health,
  67.                     mana: decreaseSpell + resultForCheck
  68.                 }
  69.                 console.log(`${command[1]} recharged for ${resultForCheck} MP!`)
  70.                 }  else {
  71.                     heroes[command[1]] = {
  72.                     health: health,
  73.                     mana: decreaseSpell + Number(command[2])
  74.                 }
  75.                 console.log(`${command[1]} recharged for ${command[2]} MP!`)
  76.                 }
  77.             }
  78.         }
  79.  
  80.         if (command[0] === 'Heal') {
  81.             if (heroes.hasOwnProperty(command[1])) {
  82.                 let spellAndHealth = Object.values(heroes[command[1]])
  83.                 let health = Number(spellAndHealth[0])
  84.                 let decreaseSpell = spellAndHealth[1]
  85.                 let checker = health + Number(command[2]);
  86.                 if (checker > maxHealth) {
  87.                     resultForCheck = maxHealth - health;
  88.                     heroes[command[1]] = {
  89.                     health: health + resultForCheck,
  90.                     mana: decreaseSpell
  91.                    
  92.                 }
  93.                 console.log(`${command[1]} healed for ${resultForCheck} HP!`)
  94.                 } else {
  95.                     heroes[command[1]] = {
  96.                     health: health + Number(command[2]),
  97.                     mana: decreaseSpell
  98.                 }
  99.                 console.log(`${command[1]} healed for ${command[2]} HP!`)
  100.                 }
  101.             }
  102.         }
  103.     }
  104.  
  105.     function sortByTwoCritters(a, b) {
  106.         let healthA = a[1]['health'];
  107.         let healthB = b[1]['health'];
  108.         if (healthA < healthB) {
  109.             return b[0].localeCompare(a[0])
  110.         } else {
  111.            return healthA - healthB;
  112.         }
  113.     }
  114.  
  115.     let entries = Object.entries(heroes).sort(sortByTwoCritters)
  116.     //.sort(([name1, HPMP1], [name2, HPMP2]) => {
  117.     //    if (HPMP2[0] === HPMP1[0])
  118.     //    return name1.localeCompare(name2)
  119.     //    return HPMP2[0] - HPMP1[0]
  120.     //});
  121.  
  122.     for (let i = 0; i < entries.length; i++) {
  123.         let warrior = entries[i];
  124.         let values = Array.from(Object.values(warrior[1]))
  125.         let hp = '  HP: ' + values[0]
  126.         let mp = '  MP: ' + values[1]
  127.         console.log(`${warrior[0]}`)
  128.         console.log(hp)
  129.         console.log(mp)
  130.        
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement