Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function gameOfEpicness(arrKingdoms, matrixBattles) {
- let objKingdoms = {};
- for (let curObj of arrKingdoms) {
- let currentKingdom = curObj.kingdom;
- let currentGeneral = curObj.general;
- let currentArmy = curObj.army;
- if (!objKingdoms[currentKingdom]) {
- objKingdoms[currentKingdom] = {};
- }
- if (!objKingdoms[currentKingdom][currentGeneral]) {
- objKingdoms[currentKingdom][currentGeneral] = {};
- if (!objKingdoms[currentKingdom][currentGeneral].army) {
- objKingdoms[currentKingdom][currentGeneral].army = currentArmy;
- objKingdoms[currentKingdom][currentGeneral].wins = 0;
- objKingdoms[currentKingdom][currentGeneral].losses = 0;
- }
- } else {
- objKingdoms[currentKingdom][currentGeneral].army += currentArmy;
- }
- }
- for (let arr of matrixBattles) {
- let attackKingdom = arr[0];
- let attackGeneral = arr[1];
- let defendKingdom = arr[2];
- let defendGeneral = arr[3];
- if (attackKingdom !== defendKingdom) {
- if (objKingdoms[attackKingdom][attackGeneral].army > objKingdoms[defendKingdom][defendGeneral].army) {
- objKingdoms[attackKingdom][attackGeneral].army = Math.floor(objKingdoms[attackKingdom][attackGeneral].army * 1.1);
- objKingdoms[attackKingdom][attackGeneral].wins += 1;
- objKingdoms[defendKingdom][defendGeneral].army =Math.floor(objKingdoms[defendKingdom][defendGeneral].army * 0.9);
- objKingdoms[defendKingdom][defendGeneral].losses += 1;
- } else if (objKingdoms[attackKingdom][attackGeneral].army < objKingdoms[defendKingdom][defendGeneral].army) {
- objKingdoms[attackKingdom][attackGeneral].army = Math.floor(objKingdoms[attackKingdom][attackGeneral].army * 0.9);
- objKingdoms[attackKingdom][attackGeneral].losses += 1;
- objKingdoms[defendKingdom][defendGeneral].army = Math.floor(objKingdoms[defendKingdom][defendGeneral].army * 1.1);
- objKingdoms[defendKingdom][defendGeneral].wins += 1;
- }
- }
- }
- let winsKingdom = Object.entries(objKingdoms).sort((a, b) => {
- let totalWinsA = Object.entries(a[1]).map(el => { return el[1].wins }).reduce((x, y) => x + y, 0);
- let totalWinsB = Object.entries(b[1]).map(el => { return el[1].wins }).reduce((x, y) => x + y, 0);
- let totalLossesA = Object.entries(a[1]).map(el => { return el[1].losses }).reduce((x, y) => x + y, 0);
- let totalLossesB = Object.entries(b[1]).map(el => { return el[1].losses }).reduce((x, y) => x + y, 0);
- return totalWinsB - totalWinsA || totalLossesA - totalLossesB || a[0].localeCompare(b[0])
- })[0];
- winsKingdom.forEach((element, index) => {
- if (index === 0) {
- console.log(`Winner: ${winsKingdom[0]}`)
- } else {
- Object.entries(element)
- .sort((generalA, generalB) => generalB[1].army - generalA[1].army)
- .forEach(general => {
- console.log(`/\\general: ${general[0]}`);
- console.log(`---army: ${general[1].army}`);
- console.log(`---wins: ${general[1].wins}`);
- console.log(`---losses: ${general[1].losses}`);
- });
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment