Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

140 рядки
2.4 KiB

  1. module.exports = {
  2. list: [],
  3. waiting: [],
  4. loaded: false,
  5. init: async function () {
  6. await this.getList();
  7. },
  8. requestList: function (msg) {
  9. let prophecyFilter = msg.data.prophecies;
  10. let offset = msg.data.offset;
  11. let result = this.list;
  12. let length = result.length;
  13. if (prophecyFilter) {
  14. let pLen = prophecyFilter.length;
  15. result = result
  16. .filter(function (r) {
  17. let rProphecies = r.prophecies || [];
  18. let match = true;
  19. for (let i = 0; i < pLen; i++) {
  20. if (!rProphecies.includes(prophecyFilter[i])) {
  21. match = false;
  22. break;
  23. }
  24. }
  25. return match;
  26. });
  27. length = result.length;
  28. result = result
  29. .filter(function (r, i) {
  30. return (
  31. (i >= offset) &&
  32. (i < offset + 10)
  33. );
  34. });
  35. }
  36. msg.callback({
  37. list: result,
  38. length: length
  39. });
  40. },
  41. getList: async function () {
  42. let list = await io.getAllAsync({
  43. table: 'leaderboard',
  44. isArray: true
  45. });
  46. this.list = list.map(l => ({
  47. //This is a bit of a hack. RethinkDB uses 'id' whereas Sqlite uses 'key'
  48. name: l.key || l.id,
  49. level: l.value.level,
  50. prophecies: l.value.prophecies
  51. }));
  52. this.sort();
  53. this.loaded = true;
  54. },
  55. getLevel: function (name) {
  56. if (!this.list)
  57. return null;
  58. let result = this.list.find(l => (l.name === name));
  59. if (result)
  60. return result.level;
  61. return null;
  62. },
  63. setLevel: function (name, level, prophecies) {
  64. let exists = this.list.find(l => l.name === name);
  65. if (exists)
  66. exists.level = level;
  67. else {
  68. exists = {
  69. name: name,
  70. level: level,
  71. prophecies: prophecies
  72. };
  73. this.list.push(exists);
  74. }
  75. this.sort();
  76. this.save(exists);
  77. },
  78. deleteCharacter: async function (name) {
  79. this.list.spliceWhere(l => (l.name === name));
  80. await io.deleteAsync({
  81. key: name,
  82. table: 'leaderboard'
  83. });
  84. },
  85. killCharacter: async function (name) {
  86. let character = this.list.find(l => (l.name === name));
  87. if (!character)
  88. return;
  89. character.dead = true;
  90. this.save(character);
  91. },
  92. sort: function () {
  93. this.list.sort(function (a, b) {
  94. return (b.level - a.level);
  95. }, this);
  96. },
  97. save: async function (character) {
  98. let value = {
  99. level: character.level,
  100. prophecies: character.prophecies || []
  101. };
  102. if (character.dead)
  103. value.dead = true;
  104. await io.setAsync({
  105. key: character.name,
  106. table: 'leaderboard',
  107. value: character,
  108. serialize: true
  109. });
  110. }
  111. };