You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

181 lines
2.9 KiB

  1. let io = require('../security/io');
  2. module.exports = {
  3. list: [],
  4. waiting: [],
  5. loaded: false,
  6. init: function () {
  7. this.getList();
  8. },
  9. requestList: function (msg) {
  10. let prophecyFilter = msg.data.prophecies;
  11. let offset = msg.data.offset;
  12. let result = this.list;
  13. let length = result.length;
  14. if (prophecyFilter) {
  15. let pLen = prophecyFilter.length;
  16. result = result
  17. .filter(function (r) {
  18. let rProphecies = r.prophecies || [];
  19. let match = true;
  20. for (let i = 0; i < pLen; i++) {
  21. if (!rProphecies.some(rp => rp === prophecyFilter[i])) {
  22. match = false;
  23. break;
  24. }
  25. }
  26. return match;
  27. });
  28. length = result.length;
  29. result = result
  30. .filter(function (r, i) {
  31. return (
  32. (i >= offset) &&
  33. (i < offset + 10)
  34. );
  35. });
  36. }
  37. msg.callback({
  38. list: result,
  39. length: length
  40. });
  41. },
  42. getList: function () {
  43. io.get({
  44. ent: 'list',
  45. field: 'leaderboard',
  46. callback: this.onGetList.bind(this)
  47. });
  48. },
  49. onGetList: function (result) {
  50. if (!result) {
  51. let list = {
  52. list: []
  53. };
  54. io.set({
  55. ent: 'list',
  56. field: 'leaderboard',
  57. value: JSON.stringify(list)
  58. });
  59. } else
  60. this.parseList(result);
  61. this.loaded = true;
  62. },
  63. parseList: function (result) {
  64. this.list = JSON.parse(result).list;
  65. if (!(this.list instanceof Array))
  66. this.list = [];
  67. this.list.forEach(function (l) {
  68. if (l.name.indexOf('\'') > -1)
  69. l.name = l.name.split('\'').join('');
  70. });
  71. let doSave = false;
  72. this.waiting.forEach(function (w) {
  73. if (!this.list.some(l => l.name === w.name)) {
  74. this.list.push(w);
  75. doSave = true;
  76. }
  77. }, this);
  78. if (doSave)
  79. this.save();
  80. this.waiting = [];
  81. },
  82. getLevel: function (name) {
  83. if (!this.list)
  84. return null;
  85. let result = this.list.find(l => (l.name === name));
  86. if (result)
  87. return result.level;
  88. return null;
  89. },
  90. setLevel: function (name, level, prophecies) {
  91. if (!this.list) {
  92. this.waiting.push({
  93. name: name,
  94. level: level,
  95. prophecies: prophecies
  96. });
  97. return;
  98. }
  99. let exists = this.list.find(l => l.name === name);
  100. if (exists) {
  101. if (exists.level !== level) {
  102. exists.level = level;
  103. this.save();
  104. }
  105. } else {
  106. this.list.push({
  107. name: name,
  108. level: level,
  109. prophecies: prophecies
  110. });
  111. this.save();
  112. }
  113. },
  114. deleteCharacter: function (name) {
  115. this.list.spliceWhere(l => (l.name === name));
  116. this.save();
  117. },
  118. killCharacter: function (name) {
  119. let character = this.list.find(l => (l.name === name));
  120. if (!character)
  121. return;
  122. character.dead = true;
  123. this.save();
  124. },
  125. sort: function () {
  126. this.list.sort(function (a, b) {
  127. return (b.level - a.level);
  128. }, this);
  129. },
  130. save: function () {
  131. this.sort();
  132. if (!this.loaded)
  133. return;
  134. let value = JSON.stringify({
  135. list: this.list
  136. });
  137. io.set({
  138. ent: 'list',
  139. field: 'leaderboard',
  140. value: value
  141. });
  142. }
  143. };