Advertisement
Todorov_Stanimir

04. Heroes JS Advanced Exam - 17 March 2019

Oct 21st, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.    const stat = { 'MAGESA': 70, 'MAGESD': 30, 'FIGHTERSA': 50, 'FIGHTERSD': 50, 'TANKSA': 20, 'TANKSD': 80 }
  3.    const kingdomNames = ["castle", "dungeon", "fortress", "inferno", "necropolis", "rampart", "stronghold", "tower", "conflux"]
  4.    const [rebuiltBut, joinBut, attackBut] = document.querySelectorAll('button');
  5.    const pattern = new RegExp('[A-Za-z]{2,}', 'g');
  6.  
  7.    isKingNameOrCharsCorect = (king, kingElem, pattern) => {
  8.       if (king.match(pattern)) {
  9.          return true;
  10.       } else {
  11.          kingElem.value = '';
  12.          return false;
  13.       }
  14.    }
  15.  
  16.    isKingdomCorrect = (kingdom, kingdomElem) => {
  17.       if (kingdomNames.includes(kingdom)) {
  18.          return true;
  19.       } else {
  20.          kingdomElem.value = '';
  21.          return false;
  22.       }
  23.    }
  24.  
  25.    isKingomRebuild = (kingdom, bool) => {
  26.       if (bool) {
  27.          const kingomRebuildElement = document.getElementById(`${kingdom}`);
  28.  
  29.          if (kingomRebuildElement.getAttribute('style').split(': ')[1] === 'inline-block;') {
  30.             return true;
  31.          } else {
  32.             return false;
  33.          }
  34.       } return false;
  35.    }
  36.  
  37.  
  38.    rebuiltBut.addEventListener('click', () => {
  39.       const [kingdomElem, kingElem] = [...document.querySelectorAll('#kingdom input[type=text]')];
  40.       let [kingdom, king] = [kingdomElem, kingElem].map(el => el.value);
  41.       kingdom = kingdom.toLowerCase();
  42.  
  43.       if (isKingNameOrCharsCorect(king, kingElem, pattern) && isKingdomCorrect(kingdom, kingdomElem)) {
  44.          const kingomElement = document.getElementById(`${kingdom}`);
  45.  
  46.          let h1Elem = document.createElement('h1');
  47.          h1Elem.textContent = kingdom.toUpperCase();
  48.          let divElem = document.createElement('div');
  49.          divElem.setAttribute('class', 'castle');
  50.          let h2Elem = document.createElement('h2');
  51.          h2Elem.textContent = king.toUpperCase();
  52.          let fieldSetElem = document.createElement('fieldset');
  53.          let legElem = document.createElement('legent');
  54.          legElem.textContent = 'Army';
  55.          let p1Elem = document.createElement('p');
  56.          p1Elem.textContent = 'TANKS - 0';
  57.          let p2Elem = document.createElement('p');
  58.          p2Elem.textContent = 'FIGHTERS - 0';
  59.          let p3Elem = document.createElement('p');
  60.          p3Elem.textContent = 'MAGES - 0';
  61.          let divElemArmyOutput = document.createElement('div');
  62.          divElemArmyOutput.setAttribute('class', 'armyOutput');
  63.          fieldSetElem.appendChild(legElem)
  64.          fieldSetElem.appendChild(p1Elem);
  65.          fieldSetElem.appendChild(p2Elem);
  66.          fieldSetElem.appendChild(p3Elem);
  67.          fieldSetElem.appendChild(divElemArmyOutput);
  68.          kingomElement.appendChild(h1Elem);
  69.          kingomElement.appendChild(divElem);
  70.          kingomElement.appendChild(h2Elem);
  71.          kingomElement.appendChild(fieldSetElem);
  72.          kingomElement.style.display = 'inline-block';
  73.       };
  74.    });
  75.  
  76.    joinBut.addEventListener('click', () => {
  77.       const [characterElem, kingdomElem] = [...document.querySelectorAll('#characters input[type=text]')];
  78.       let [chars, kingdom] = [characterElem, kingdomElem].map(el => el.value);
  79.       kingdom = kingdom.toLowerCase();
  80.       // let areCorectChars = true;
  81.       // let areCorectKingdom = true;
  82.       // let isKingdomRebuild = true;
  83.       let army = [...document.querySelectorAll('#characters input[type=radio]')].filter(el => el.checked === true).map(el => el.value.toUpperCase())[0];
  84.  
  85.       // if (!(kingdomNames.includes(kingdom))) {
  86.       //    kingdomElem.value = ''
  87.       //    areCorectKingdom = false;
  88.       // } else {
  89.       //    const kingomRebuildElement = document.getElementById(`${kingdom}`);
  90.       //    if (!(kingomRebuildElement.getAttribute('style').split(': ')[1] === 'inline-block;')) {
  91.       //       isKingdomRebuild = false;
  92.       //    }
  93.       // }
  94.       // areCorectKingdom
  95.       if (isKingNameOrCharsCorect(chars, characterElem, pattern) && isKingomRebuild(kingdom, isKingdomCorrect(kingdom, kingdomElem)) && army) {
  96.          const kingomRebuildElement = document.getElementById(`${kingdom}`);
  97.          let pElement = [...kingomRebuildElement.querySelectorAll('fieldset p')].filter(p => p.textContent.split(' - ')[0] === army + 'S')[0];
  98.          let [textpElement, count] = pElement.textContent.split(' - ');
  99.          pElement.textContent = `${textpElement} - ${++count}`;
  100.          let divElemArmyOutput = kingomRebuildElement.querySelectorAll('div[class=armyOutput]')[0];
  101.          divElemArmyOutput.textContent += `${chars} `;
  102.       }
  103.    });
  104.  
  105.    attackBut.addEventListener('click', () => {
  106.       const [attackElem, defElem] = [...document.querySelectorAll('#actions input[type=text]')];
  107.       let [attackKing, defKing] = [attackElem, defElem].map(el => el.value.toLowerCase());
  108.       let [isAttackKingValid, isAttackKingRebuild, isDefKingdomValid, isDefKingRebuild] = [true, true, true, true];
  109.  
  110.       if (!(kingdomNames.includes(attackKing))) {
  111.          attackElem.value = ''
  112.          isAttackKingValid = false;
  113.       } else {
  114.          const attackKingdomRebuildElement = document.getElementById(`${attackKing}`);
  115.          if (!(attackKingdomRebuildElement.getAttribute('style').split(': ')[1] === 'inline-block;')) {
  116.             isAttackKingRebuild = false;
  117.          }
  118.       }
  119.  
  120.       if (!(kingdomNames.includes(defKing))) {
  121.          defElem.value = ''
  122.          isDefKingdomValid = false;
  123.       } else {
  124.          const defKingdomRebuildElement = document.getElementById(`${defKing}`);
  125.          if (!(defKingdomRebuildElement.getAttribute('style').split(': ')[1] === 'inline-block;')) {
  126.             isDefKingRebuild = false;
  127.          }
  128.       }
  129.  
  130.       function getInfoForKingdom(kingdom, string) {
  131.          return points = [...kingdom.querySelectorAll('fieldset p')].map(p => {
  132.             let [army, count] = p.textContent.split(' - ');
  133.             army += string;
  134.             count = Number(count);
  135.             return count * stat[army];
  136.          }).reduce((a, b) => a + b, 0);
  137.  
  138.       }
  139.       if (isAttackKingValid && isAttackKingRebuild && isDefKingdomValid && isDefKingRebuild) {
  140.          const attackKingomElement = document.getElementById(`${attackKing}`);
  141.          const defKingomElement = document.getElementById(`${defKing}`);
  142.          let pointA = getInfoForKingdom(attackKingomElement, 'A');
  143.          let pointB = getInfoForKingdom(defKingomElement, 'D');
  144.          if (pointA > pointB) {
  145.             defKingomElement.querySelector('h2').textContent = attackKingomElement.querySelector('h2').textContent;
  146.          }
  147.       }
  148.    })
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement