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.
 
 
 

155 lines
3.2 KiB

  1. let io = require('../security/io');
  2. module.exports = {
  3. queue: {},
  4. busy: {},
  5. init: function (instance) {
  6. this.instance = instance;
  7. },
  8. getMail: function (playerName) {
  9. let player = this.instance.objects.objects.find(o => (o.name === playerName));
  10. if (!player) {
  11. process.send({
  12. method: 'callDifferentThread',
  13. playerName: playerName,
  14. data: {
  15. module: 'mail',
  16. method: 'getMail',
  17. args: [playerName]
  18. }
  19. });
  20. this.processQueue(playerName);
  21. return;
  22. }
  23. io.get({
  24. ent: playerName,
  25. field: 'mail',
  26. callback: this.onGetMail.bind(this, player)
  27. });
  28. },
  29. onGetMail: function (player, result) {
  30. if (result === 'null')
  31. result = null;
  32. else if (result) {
  33. result = result.split('`').join('\'');
  34. //Hack for weird google datastore error
  35. if (result[0] === '<')
  36. return;
  37. }
  38. result = JSON.parse(result || '[]');
  39. let sentMessages = [];
  40. let inventory = player.inventory;
  41. let stash = player.stash;
  42. result.forEach(function (r) {
  43. if (r.removeAll) {
  44. for (let i = 0; i < inventory.items.length; i++) {
  45. let item = inventory.items[i];
  46. if ((r.nameLike) && (item.name.indexOf(r.nameLike) > -1)) {
  47. inventory.destroyItem(item.id, item.quantity ? item.quantity : null);
  48. i--;
  49. }
  50. }
  51. if (stash) {
  52. for (let i = 0; i < stash.items.length; i++) {
  53. let item = stash.items[i];
  54. if ((r.nameLike) && (item.name.indexOf(r.nameLike) > -1)) {
  55. stash.destroyItem(item.id);
  56. i--;
  57. }
  58. }
  59. } else
  60. console.log(player.name + ' has no stash');
  61. } else {
  62. if ((r.msg) && (!sentMessages.some(s => (s === r.msg)))) {
  63. player.instance.syncer.queue('onGetMessages', {
  64. id: player.id,
  65. messages: [{
  66. class: 'color-greenB',
  67. message: r.msg,
  68. type: 'info'
  69. }]
  70. }, [player.serverId]);
  71. sentMessages.push(r.msg);
  72. delete r.msg;
  73. }
  74. inventory.getItem(r);
  75. }
  76. });
  77. io.set({
  78. ent: player.name,
  79. field: 'mail',
  80. value: null,
  81. callback: this.processQueue.bind(this, player.name)
  82. });
  83. },
  84. processQueue: function (playerName) {
  85. delete this.busy[playerName];
  86. let queue = this.queue[playerName];
  87. if (!queue)
  88. return;
  89. delete this.queue[playerName];
  90. this.sendMail(playerName, queue);
  91. },
  92. sendMail: function (playerName, items, callback) {
  93. if (this.busy[playerName]) {
  94. let queue = this.queue[playerName];
  95. if (!queue)
  96. queue = this.queue[playerName] = [];
  97. items.forEach(function (i) {
  98. queue.push(extend(true, {}, i));
  99. });
  100. return;
  101. }
  102. this.busy[playerName] = true;
  103. if (!items.push)
  104. items = [items];
  105. let player = null;
  106. if (this.instance)
  107. player = this.instance.objects.objects.find(o => (o.name === playerName));
  108. io.get({
  109. ent: playerName,
  110. field: 'mail',
  111. callback: this.doSendMail.bind(this, playerName, items, callback)
  112. });
  113. },
  114. doSendMail: function (playerName, items, callback, result) {
  115. if (result === 'null')
  116. result = null;
  117. result = JSON.parse(result || '[]');
  118. items.forEach(function (i) {
  119. result.push(i);
  120. });
  121. let itemString = JSON.stringify(result).split('\'').join('`');
  122. io.set({
  123. ent: playerName,
  124. field: 'mail',
  125. value: itemString,
  126. callback: callback || this.getMail.bind(this, playerName)
  127. });
  128. }
  129. };