Advertisement
bebo231312312321

Untitled

Mar 15th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arenaTier(commands) {
  2.     let gladiatorsPool = {}
  3.     commands.splice(commands.indexOf("Ave Cesar"), commands.length)
  4.     function tehniqueBattle(a, b) {
  5.         const firstData = Object.entries(gladiatorsPool[a])
  6.         const secondData = Object.entries(gladiatorsPool[b])
  7.         return firstData.some(x => secondData.some(y => y.includes(x[0])))
  8.     }
  9.     function sum(glad) {
  10.         return glad.reduce((a, v) => a + v[1], 0)
  11.     }
  12.     function sortedGladiator (a,b){
  13.         return a.localeCompare(b)
  14.     }
  15.     function sumSkill(glad) {
  16.         return glad.reduce((a, v) => a + v[1], 0)
  17.     }
  18.     function sortSkill(x, y) {
  19.         return sumSkill(Object.entries(y)) - sumSkill(Object.entries(x))
  20.     }
  21.     for (let i = 0; i < commands.length; i++) {
  22.         let [name, techique, skill] = commands[i].split(" -> ");
  23.         if (techique !== undefined) {
  24.             if (!gladiatorsPool[name]) gladiatorsPool[name] = {}
  25.             if (!gladiatorsPool[name][techique]) gladiatorsPool[name][techique] = 0
  26.                 if (gladiatorsPool[name][techique] < Number(skill))
  27.                     gladiatorsPool[name][techique] = Number(skill)    
  28.         }else {
  29.             let [gladiatorA, gladiatorB] = commands[i].split(" vs ")
  30.           if(gladiatorsPool.hasOwnProperty(gladiatorA) && gladiatorsPool.hasOwnProperty(gladiatorB) ){                
  31.               if (tehniqueBattle(gladiatorA, gladiatorB)) {
  32.                     sum(Object.entries(gladiatorsPool[gladiatorA])) > sum(Object.entries(gladiatorsPool[gladiatorB]))
  33.                         ? delete gladiatorsPool[gladiatorB]
  34.                         : delete gladiatorsPool[gladiatorA]
  35.                 }
  36.             }    
  37.         }      
  38.     }
  39.     let  sortedGlad = Object.entries(gladiatorsPool).sort((x, y) =>
  40.     sortSkill(x[1], y[1]) !== 0 ? sortSkill(x[1], y[1]) : sortByName(x[0], y[0])
  41. )
  42.         sortedGlad.forEach(x => {
  43.             const skills = Object.entries(x[1]).sort((x, y) => (y[1] - x[1] !== 0 ? y[1] - x[1] : sortedGladiator(x[0], y[0])))
  44.             console.log(`${x[0]}: ${sumSkill(skills)} skill`)
  45.             skills.forEach(x => console.log(`- ${x[0]} <!> ${x[1]}`))
  46.         })
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement