audipower

GameOfEpicness-Javascript

Sep 25th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function gameOfEpicness(arrKingdoms, matrixBattles) {
  2.     let objKingdoms = {};
  3.  
  4.     for (let curObj of arrKingdoms) {
  5.         let currentKingdom = curObj.kingdom;
  6.         let currentGeneral = curObj.general;
  7.         let currentArmy = curObj.army;
  8.         if (!objKingdoms[currentKingdom]) {
  9.             objKingdoms[currentKingdom] = {};
  10.         }
  11.         if (!objKingdoms[currentKingdom][currentGeneral]) {
  12.             objKingdoms[currentKingdom][currentGeneral] = {};
  13.             if (!objKingdoms[currentKingdom][currentGeneral].army) {
  14.                 objKingdoms[currentKingdom][currentGeneral].army = currentArmy;
  15.                 objKingdoms[currentKingdom][currentGeneral].wins = 0;
  16.                 objKingdoms[currentKingdom][currentGeneral].losses = 0;
  17.             }
  18.         } else {
  19.             objKingdoms[currentKingdom][currentGeneral].army += currentArmy;
  20.         }
  21.     }
  22.  
  23.     for (let arr of matrixBattles) {
  24.         let attackKingdom = arr[0];
  25.         let attackGeneral = arr[1];
  26.         let defendKingdom = arr[2];
  27.         let defendGeneral = arr[3];
  28.         if (attackKingdom !== defendKingdom) {
  29.             if (objKingdoms[attackKingdom][attackGeneral].army > objKingdoms[defendKingdom][defendGeneral].army) {
  30.                 objKingdoms[attackKingdom][attackGeneral].army = Math.floor(objKingdoms[attackKingdom][attackGeneral].army * 1.1);
  31.                 objKingdoms[attackKingdom][attackGeneral].wins += 1;
  32.                 objKingdoms[defendKingdom][defendGeneral].army =Math.floor(objKingdoms[defendKingdom][defendGeneral].army * 0.9);
  33.                 objKingdoms[defendKingdom][defendGeneral].losses += 1;
  34.                
  35.             } else if (objKingdoms[attackKingdom][attackGeneral].army < objKingdoms[defendKingdom][defendGeneral].army) {
  36.                 objKingdoms[attackKingdom][attackGeneral].army = Math.floor(objKingdoms[attackKingdom][attackGeneral].army * 0.9);
  37.                 objKingdoms[attackKingdom][attackGeneral].losses += 1;
  38.                 objKingdoms[defendKingdom][defendGeneral].army = Math.floor(objKingdoms[defendKingdom][defendGeneral].army * 1.1);
  39.                 objKingdoms[defendKingdom][defendGeneral].wins += 1;
  40.             }
  41.         }
  42.     }
  43.    
  44.     let winsKingdom = Object.entries(objKingdoms).sort((a, b) => {
  45.         let totalWinsA = Object.entries(a[1]).map(el => { return el[1].wins }).reduce((x, y) => x + y, 0);
  46.         let totalWinsB = Object.entries(b[1]).map(el => { return el[1].wins }).reduce((x, y) => x + y, 0);
  47.         let totalLossesA = Object.entries(a[1]).map(el => { return el[1].losses }).reduce((x, y) => x + y, 0);
  48.         let totalLossesB = Object.entries(b[1]).map(el => { return el[1].losses }).reduce((x, y) => x + y, 0);
  49.         return totalWinsB - totalWinsA || totalLossesA - totalLossesB || a[0].localeCompare(b[0])
  50.     })[0];
  51.  
  52.     winsKingdom.forEach((element, index) => {
  53.         if (index === 0) {
  54.             console.log(`Winner: ${winsKingdom[0]}`)
  55.         } else {
  56.             Object.entries(element)
  57.                 .sort((generalA, generalB) => generalB[1].army - generalA[1].army)
  58.                 .forEach(general => {
  59.                     console.log(`/\\general: ${general[0]}`);
  60.                     console.log(`---army: ${general[1].army}`);
  61.                     console.log(`---wins: ${general[1].wins}`);
  62.                     console.log(`---losses: ${general[1].losses}`);
  63.                 });
  64.         }
  65.     });
  66. }
Advertisement
Add Comment
Please, Sign In to add comment