Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function battleManager(array = []) {
- let command = array.shift().split(':');
- let battles = {};
- let health = 0;
- let energy = 0;
- let peopleCount = 0;
- while (command[0] !== 'Results') {
- switch (command[0]) {
- case 'Add':
- let personName = command[1];
- health = +command[2];
- energy = +command[3];
- if (!battles.hasOwnProperty(personName)) {
- battles[personName] = [];
- peopleCount++;
- } else {
- let health1=battles[personName].shift();
- health=health1+health;
- energy=battles[personName].shift();
- }
- battles[personName].push(health);
- battles[personName].push(energy);
- break;
- case 'Attack':
- let attackerName = command[1];
- let defenderName = command[2];
- let damage = +command[3];
- if (battles.hasOwnProperty(attackerName) && battles.hasOwnProperty(defenderName)) {
- health = battles[defenderName].shift();
- health -= damage;
- if (health <= 0) {
- delete battles[defenderName];
- peopleCount--;
- console.log(`${defenderName} was disqualified!`);
- } else {
- battles[defenderName].unshift(health);
- }
- energy = battles[attackerName].pop();
- energy--;
- if (energy === 0) {
- delete battles[attackerName];
- peopleCount--;
- console.log(`${attackerName} was disqualified!`);
- } else {
- battles[attackerName].push(energy);
- }
- }
- break;
- case 'Delete':
- let username = command[1];
- if (username === 'All') {
- peopleCount = 0;
- battles = {};
- } else {
- if (battles.hasOwnProperty(username)) {
- delete battles[username];
- peopleCount--;
- }
- }
- break;
- }
- command = array.shift().split(':');
- }
- console.log(`People count: ${peopleCount}`);
- let sorted=Object.entries(battles).sort((a,b)=>{
- if (a[1][0]!==b[1][0]) {
- return b[1][0]-a[1][0];
- }else{
- return a[0].localeCompare(b[0]);
- }
- });
- for (let i = 0; i < sorted.length; i++) {
- console.log(`${sorted[i][0]} - ${+sorted[i][1][0]} - ${(+sorted[i][1][1])}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement