Advertisement
Guest User

idk lawl

a guest
Feb 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. const Entry = require('./Entry');
  2.  
  3. class Listing {
  4.  
  5.  
  6. constructor(){
  7.  
  8. //Array of entrys.
  9. this.data = Array();
  10.  
  11. // all of the users.
  12. this.users = Array();
  13. }
  14. //check to see if user is already present
  15. userPresent(username){
  16. console.log("userPreesent called");
  17.  
  18. if (this.users.length > 0) {
  19. for (var i =0; i < this.users.length; i++){
  20. if (this.users[i] === username){
  21. return true;
  22. }
  23. }
  24. }
  25. return false;
  26. }
  27. //check to see if id is present.
  28. idPresent(id){
  29. console.log("userPreesent called");
  30.  
  31. if(this.data.length > 0){
  32. for (var i = 0; i < this.data.length; i++){
  33. if (this.data[i].id === id){
  34. return true;
  35. }
  36.  
  37. }
  38. }
  39. return false;
  40. }
  41. //new user
  42. addUser(id,username){
  43. console.log("addUser called")
  44.  
  45. for (var i = 0; i < this.data.length; i++){
  46. if (this.data[i].id === id){
  47. this.data[i].users.push(username);
  48. this.users.push(username);
  49. return;
  50. }
  51. }
  52. }
  53.  
  54. //new id
  55. addID(id,username){
  56. console.log("new ID called");
  57. this.data.push(new Entry(id,username));
  58. this.users.push(username);
  59. }
  60. //FIND AND DELETE USERNAME FROM USERS ARRAY.
  61. deleteUser(username){
  62. console.log("deleteUser called");
  63.  
  64. for (var i = 0; i < this.users.length; i++){
  65. if (this.users[1] === username){
  66. let tmp = this.users[i];
  67. this.users[i] = this.users[0];
  68. this.users[0] = tmp;
  69. this.users.shift();
  70. return;
  71. }
  72. }
  73. }
  74.  
  75. deleteUserEntry(username){
  76. console.log("deleteUserEntry called");
  77. if (this.data.length > 0 && this.users.length > 0){
  78. //delete the entry when only 1 id and username is found/
  79. if(this.data.length === 1 && this.data[0].users.lenght === 1 && this.data[0].users === username){
  80. this.data.pop();
  81. this.user.pop();
  82. } else {
  83. for (var i = 0; i < this.data.length; i++){
  84. if (this.data[i].users.length > 0){
  85. for (var j = 0; j < this.data[i].users.length; j++){
  86. if (this.data[i].users[j] === username && this.data[i].users.length > 1){
  87. let tmp = this.data[i].users[j];
  88. this.data[i].users[j] = this.data[i].users[0];
  89. this.data[i].users[0] = tmp;
  90.  
  91. let deleteUser = this.data[i].users.shift();
  92. console.log("User deleted:" + deleteUser);
  93. console.log(`Deleted user : ${deleteUser}`);
  94. this.deleteUser(username);
  95. return;
  96. } else if (this.data[i].users.length === 1 && this.data[i].users[0] === username){
  97. let tmp2 = this.data[i];
  98. this.data[i] = this.data[0];
  99. this.data[0] = tmp2;
  100.  
  101. let deletedID = this.data.shift();
  102. console.log(`deleted id : ${deletedID}`);
  103. this.deleteUser(username);
  104. return;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. // Entry with most players.
  113. sort() {
  114. if(this.data.length >= 2){
  115. for (var i = 0; i < this.data.length - 1; i++){
  116. for (var j = i + 1; j < this.data.length; j++){
  117. if (this.data[i].users.length < this.data[j].users.length){
  118. let tmp = this.data[i];
  119. this.data[i] = this.data[j];
  120. this.data[j] = tmp;
  121.  
  122. }
  123. }
  124.  
  125. }
  126. }
  127. }
  128.  
  129.  
  130. }
  131.  
  132.  
  133. module.exports = Listing;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement