Advertisement
Lulunga

Battle Manager alternative

Aug 3rd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. function solve(input) {
  2. let records = {};
  3. for (let line of input) {
  4. if (line === 'Results') {
  5. break;
  6. }
  7. let tokens = line.split(':');
  8. let command = tokens[0];
  9. if (command === 'Add') {
  10. let person = tokens[1];
  11. let health = Number(tokens[2]);
  12. let energy = Number(tokens[3]);
  13. if (!records.hasOwnProperty(person)) {
  14. records[person] = {};
  15. records[person].health = 0;
  16. records[person].energy = energy;
  17. }
  18. records[person].health += health;
  19.  
  20. } else if (command === 'Attack') {
  21. let attackerName = tokens[1];
  22. let defenderName = tokens[2];
  23. let damage = Number(tokens[3]);
  24. if (records.hasOwnProperty(attackerName) && records.hasOwnProperty(defenderName)) {
  25. records[defenderName].health -= damage;
  26. if (records[defenderName].health <= 0) {
  27. console.log(`${defenderName} was disqualified!`);
  28. delete records[defenderName];
  29. }
  30. records[attackerName].energy -= 1;
  31. if (records[attackerName].energy <= 0) {
  32. console.log(`${attackerName} was disqualified!`);
  33. delete records[attackerName];
  34. }
  35.  
  36. }
  37. } else if (command = 'Delete') {
  38. let username = tokens[1];
  39. if (username === "All") {
  40. for (let prop in records) {
  41. delete records[prop];
  42. }
  43. } else {
  44. if (records.hasOwnProperty(userName)) {
  45. delete records[userName];
  46. }
  47. }
  48. }
  49. else {
  50. continue;
  51. }
  52. }
  53. let count = 0;
  54. if (Object.keys(records) !== undefined) {
  55. count = Object.keys(records).length;
  56. console.log(`People count: ${count}`);
  57. let sorted = Object.entries(records);
  58. if (sorted.length > 0) {
  59. sorted.sort((a, b) => b[1].health - a[1].health || a[0].localeCompare(b[0]));
  60. sorted.forEach(element => {
  61. console.log(`${element[0]} - ${element[1].health} - ${element[1].energy}`);
  62. });
  63. }
  64. }
  65.  
  66.  
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement