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
3.3 KiB

  1. //Imports
  2. const objects = require('../objects/objects');
  3. const events = require('../misc/events');
  4. const {
  5. getThread, killThread, sendMessageToThread, getThreadFromId, returnWhenThreadsIdle
  6. } = require('./threadManager');
  7. //Exports
  8. module.exports = {
  9. nextId: 0,
  10. lastCallbackId: 0,
  11. callbacks: [],
  12. addObject: async function (obj, keepPos, transfer) {
  13. const serverObj = objects.objects.find(o => o.id === obj.id);
  14. if (!serverObj)
  15. return;
  16. events.emit('onBeforePlayerEnterWorld', obj);
  17. const { zoneName, zoneId } = obj;
  18. const { thread, resetObjPosition } = await getThread({
  19. zoneName,
  20. zoneId
  21. });
  22. if (resetObjPosition) {
  23. delete obj.x;
  24. delete obj.y;
  25. }
  26. obj.zoneName = thread.name;
  27. obj.zoneId = thread.id;
  28. serverObj.zoneId = thread.id;
  29. serverObj.zoneName = thread.name;
  30. serverObj.player.broadcastSelf();
  31. const simpleObj = obj.getSimple ? obj.getSimple(true, true) : obj;
  32. sendMessageToThread({
  33. threadId: obj.zoneId,
  34. msg: {
  35. method: 'addObject',
  36. args: {
  37. keepPos: keepPos,
  38. obj: simpleObj,
  39. transfer: transfer
  40. }
  41. }
  42. });
  43. },
  44. removeObjectFromInstancedZone: async function (thread, obj, callback) {
  45. await new Promise(res => {
  46. const cb = this.registerCallback(res);
  47. thread.worker.send({
  48. method: 'forceSavePlayer',
  49. args: {
  50. playerName: obj.name,
  51. callbackId: cb
  52. }
  53. });
  54. });
  55. killThread(thread);
  56. if (callback)
  57. callback();
  58. },
  59. removeObject: function (obj, skipLocal, callback) {
  60. if (!skipLocal)
  61. objects.removeObject(obj);
  62. const thread = getThreadFromId(obj.zoneId);
  63. if (!thread)
  64. return;
  65. if (thread.instanced) {
  66. this.removeObjectFromInstancedZone(thread, obj, callback);
  67. return;
  68. }
  69. let callbackId = null;
  70. if (callback)
  71. callbackId = this.registerCallback(callback);
  72. sendMessageToThread({
  73. threadId: obj.zoneId,
  74. msg: {
  75. method: 'removeObject',
  76. args: {
  77. obj: obj.getSimple(true),
  78. callbackId: callbackId
  79. }
  80. }
  81. });
  82. },
  83. updateObject: function (obj, msgObj) {
  84. sendMessageToThread({
  85. threadId: obj.zoneId,
  86. msg: {
  87. method: 'updateObject',
  88. args: {
  89. id: obj.id,
  90. obj: msgObj
  91. }
  92. }
  93. });
  94. },
  95. queueAction: function (obj, action) {
  96. sendMessageToThread({
  97. threadId: obj.zoneId,
  98. msg: {
  99. method: 'queueAction',
  100. args: {
  101. id: obj.id,
  102. action: action
  103. }
  104. }
  105. });
  106. },
  107. performAction: function (obj, action) {
  108. sendMessageToThread({
  109. threadId: obj.zoneId,
  110. msg: {
  111. method: 'performAction',
  112. args: {
  113. id: obj.id,
  114. action: action
  115. }
  116. }
  117. });
  118. },
  119. registerCallback: function (callback) {
  120. this.callbacks.push({
  121. id: ++this.lastCallbackId,
  122. callback: callback
  123. });
  124. return this.lastCallbackId;
  125. },
  126. resolveCallback: function (msg) {
  127. let callback = this.callbacks.spliceFirstWhere(c => c.id === msg.msg.id);
  128. if (!callback)
  129. return;
  130. callback.callback(msg.msg.result);
  131. },
  132. returnWhenZonesIdle: async function () {
  133. await returnWhenThreadsIdle();
  134. },
  135. forceSavePlayer: async function (playerName, zoneId) {
  136. const thread = getThreadFromId(zoneId);
  137. if (!thread)
  138. return;
  139. return new Promise(res => {
  140. const callbackId = this.registerCallback(res);
  141. thread.worker.send({
  142. method: 'forceSavePlayer',
  143. args: {
  144. playerName,
  145. callbackId
  146. }
  147. });
  148. });
  149. }
  150. };