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.
 
 
 

164 lines
2.6 KiB

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