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.
 
 
 

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