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.
 
 
 

193 lines
3.8 KiB

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