Advertisement
bebo231312312321

Untitled

Mar 5th, 2023
122
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.     //console.log(commands)
  5.     function tehniqueBattle(a, b) {
  6.         const firstData = Object.entries(gladiatorsPool[a])
  7.         const secondData = Object.entries(gladiatorsPool[b])
  8.         return firstData.some(x => secondData.some(y => y.includes(x[0])))
  9.     }
  10.  
  11.     function sum(glad) {
  12.         return glad.reduce((a, v) => a + v[1], 0)
  13.     }
  14.     function sortedGladiator (a,b){
  15.         return a.localeCompare(b)
  16.     }
  17.     function sumSkill(glad) {
  18.         return glad.reduce((a, v) => a + v[1], 0)
  19.     }
  20.     function sortSkill(x, y) {
  21.         return sumSkill(Object.entries(y)) - sumSkill(Object.entries(x))
  22.     }
  23.  
  24.     for (let i = 0; i < commands.length; i++) {
  25.         let [name, techique, skill] = commands[i].split(" -> ");
  26.        
  27.         if (techique !== undefined) {
  28.  
  29.             if (!gladiatorsPool[name]) gladiatorsPool[name] = {}
  30.        
  31.            
  32.             if (!gladiatorsPool[name][techique]) gladiatorsPool[name][techique] = 0
  33.                 if (gladiatorsPool[name][techique] < Number(skill))
  34.                     gladiatorsPool[name][techique] = Number(skill)
  35.        
  36.         }else {
  37.             let [gladiatorA, gladiatorB] = commands[i].split(" vs ")
  38.           if(gladiatorsPool.hasOwnProperty(gladiatorA) && gladiatorsPool.hasOwnProperty(gladiatorB) ){
  39.                
  40.                 //console.log(techiqueGladB)
  41.                
  42.               if (tehniqueBattle(gladiatorA, gladiatorB)) {
  43.                     sum(Object.entries(gladiatorsPool[gladiatorA])) > sum(Object.entries(gladiatorsPool[gladiatorB]))
  44.                         ? delete gladiatorsPool[gladiatorB]
  45.                         : delete gladiatorsPool[gladiatorA]
  46.                 }
  47.                 // console.table(gladiatorsPool)
  48.             }
  49.          
  50.         }
  51.        
  52.     }
  53.     let  sortedGlad = Object.entries(gladiatorsPool).sort((x, y) =>
  54.     sortSkill(x[1], y[1]) !== 0 ? sortSkill(x[1], y[1]) : sortByName(x[0], y[0])
  55. )
  56.         sortedGlad.forEach(x => {
  57.             const skills = Object.entries(x[1]).sort((x, y) => (y[1] - x[1] !== 0 ? y[1] - x[1] : sortedGladiator(x[0], y[0])))
  58.             console.log(`${x[0]}: ${sumSkill(skills)} skill`)
  59.             skills.forEach(x => console.log(`- ${x[0]} <!> ${x[1]}`))
  60.         })
  61.    
  62.    // console.table(gladiatorsPool)
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement