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.
 
 
 

195 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. events.emit('playerObjChanged', {
  38. obj
  39. });
  40. const simpleObj = obj.getSimple ? obj.getSimple(true, true) : obj;
  41. sendMessageToThread({
  42. threadId: obj.zoneId,
  43. msg: {
  44. method: 'addObject',
  45. args: {
  46. keepPos: keepPos,
  47. obj: simpleObj,
  48. transfer: transfer
  49. }
  50. }
  51. });
  52. },
  53. removeObjectFromInstancedZone: async function (thread, objId, callback) {
  54. await new Promise(res => {
  55. const cb = this.registerCallback(res);
  56. thread.worker.send({
  57. method: 'forceSavePlayer',
  58. args: {
  59. playerId: objId,
  60. callbackId: cb
  61. }
  62. });
  63. });
  64. killThread(thread);
  65. if (callback)
  66. callback();
  67. },
  68. removeObject: async function (obj, skipLocal, callback) {
  69. //We need to store the player id because the calling thread might delete it (connections.unzone)
  70. const playerId = obj.id;
  71. if (!skipLocal)
  72. objects.removeObject(obj);
  73. const thread = getThreadFromId(obj.zoneId);
  74. if (!thread) {
  75. callback();
  76. return;
  77. }
  78. if (thread.instanced && (await gePlayerCountInThread(thread)) === 1) {
  79. this.removeObjectFromInstancedZone(thread, playerId, callback);
  80. return;
  81. }
  82. let callbackId = null;
  83. if (callback)
  84. callbackId = this.registerCallback(callback);
  85. sendMessageToThread({
  86. threadId: obj.zoneId,
  87. msg: {
  88. method: 'removeObject',
  89. args: {
  90. obj: obj.getSimple(true),
  91. callbackId: callbackId
  92. }
  93. }
  94. });
  95. },
  96. updateObject: function (obj, msgObj) {
  97. sendMessageToThread({
  98. threadId: obj.zoneId,
  99. msg: {
  100. method: 'updateObject',
  101. args: {
  102. id: obj.id,
  103. obj: msgObj
  104. }
  105. }
  106. });
  107. },
  108. queueAction: function (obj, action) {
  109. sendMessageToThread({
  110. threadId: obj.zoneId,
  111. msg: {
  112. method: 'queueAction',
  113. args: {
  114. id: obj.id,
  115. action: action
  116. }
  117. }
  118. });
  119. },
  120. performAction: function (obj, action) {
  121. sendMessageToThread({
  122. threadId: obj.zoneId,
  123. msg: {
  124. method: 'performAction',
  125. args: {
  126. id: obj.id,
  127. action: action
  128. }
  129. }
  130. });
  131. },
  132. registerCallback: function (callback) {
  133. return registerCallback(callback);
  134. },
  135. resolveCallback: function (msg) {
  136. const callback = removeCallback(msg.msg.id);
  137. if (!callback)
  138. return;
  139. callback.callback(msg.msg.result);
  140. },
  141. returnWhenZonesIdle: async function () {
  142. await returnWhenThreadsIdle();
  143. },
  144. forceSavePlayer: async function (playerId, zoneId) {
  145. const thread = getThreadFromId(zoneId);
  146. if (!thread)
  147. return;
  148. return new Promise(res => {
  149. const callbackId = this.registerCallback(res);
  150. thread.worker.send({
  151. method: 'forceSavePlayer',
  152. args: {
  153. playerId,
  154. callbackId
  155. }
  156. });
  157. });
  158. }
  159. };