Advertisement
dchukova

Untitled

Apr 1st, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. function battleManager(array = []) {
  2. let command = array.shift().split(':');
  3. let battles = {};
  4. let health = 0;
  5. let energy = 0;
  6. let peopleCount = 0;
  7.  
  8. while (command[0] !== 'Results') {
  9.  
  10. switch (command[0]) {
  11. case 'Add':
  12. let personName = command[1];
  13. health = +command[2];
  14. energy = +command[3];
  15. if (!battles.hasOwnProperty(personName)) {
  16. battles[personName] = [];
  17. peopleCount++;
  18. } else {
  19. let health1=battles[personName].shift();
  20. health=health1+health;
  21. energy=battles[personName].shift();
  22.  
  23. }
  24. battles[personName].push(health);
  25. battles[personName].push(energy);
  26. break;
  27.  
  28. case 'Attack':
  29. let attackerName = command[1];
  30. let defenderName = command[2];
  31. let damage = +command[3];
  32. if (battles.hasOwnProperty(attackerName) && battles.hasOwnProperty(defenderName)) {
  33. health = battles[defenderName].shift();
  34. health -= damage;
  35. if (health <= 0) {
  36. delete battles[defenderName];
  37. peopleCount--;
  38. console.log(`${defenderName} was disqualified!`);
  39. } else {
  40. battles[defenderName].unshift(health);
  41. }
  42. energy = battles[attackerName].pop();
  43. energy--;
  44. if (energy === 0) {
  45. delete battles[attackerName];
  46. peopleCount--;
  47. console.log(`${attackerName} was disqualified!`);
  48. } else {
  49. battles[attackerName].push(energy);
  50. }
  51. }
  52.  
  53. break;
  54.  
  55. case 'Delete':
  56. let username = command[1];
  57. if (username === 'All') {
  58. peopleCount = 0;
  59. battles = {};
  60. } else {
  61. if (battles.hasOwnProperty(username)) {
  62. delete battles[username];
  63. peopleCount--;
  64. }
  65. }
  66. break;
  67. }
  68. command = array.shift().split(':');
  69. }
  70. console.log(`People count: ${peopleCount}`);
  71. let sorted=Object.entries(battles).sort((a,b)=>{
  72. if (a[1][0]!==b[1][0]) {
  73. return b[1][0]-a[1][0];
  74. }else{
  75. return a[0].localeCompare(b[0]);
  76. }
  77. });
  78.  
  79. for (let i = 0; i < sorted.length; i++) {
  80. console.log(`${sorted[i][0]} - ${+sorted[i][1][0]} - ${(+sorted[i][1][1])}`);
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement