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.
 
 
 

194 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. 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. const simple = obj.getSimple(true);
  82. if (!simple.name)
  83. console.log(`Object has no name. Username: ${obj.auth?.username}`);
  84. sendMessageToThread({
  85. threadId: obj.zoneId,
  86. msg: {
  87. method: 'removeObject',
  88. args: {
  89. obj: obj.getSimple(true),
  90. callbackId: callbackId
  91. }
  92. }
  93. });
  94. },
  95. updateObject: function (obj, msgObj) {
  96. sendMessageToThread({
  97. threadId: obj.zoneId,
  98. msg: {
  99. method: 'updateObject',
  100. args: {
  101. id: obj.id,
  102. obj: msgObj
  103. }
  104. }
  105. });
  106. },
  107. queueAction: function (obj, action) {
  108. sendMessageToThread({
  109. threadId: obj.zoneId,
  110. msg: {
  111. method: 'queueAction',
  112. args: {
  113. id: obj.id,
  114. action: action
  115. }
  116. }
  117. });
  118. },
  119. performAction: function (obj, action) {
  120. sendMessageToThread({
  121. threadId: obj.zoneId,
  122. msg: {
  123. method: 'performAction',
  124. args: {
  125. id: obj.id,
  126. action: action
  127. }
  128. }
  129. });
  130. },
  131. registerCallback: function (callback) {
  132. return registerCallback(callback);
  133. },
  134. resolveCallback: function (msg) {
  135. const callback = removeCallback(msg.msg.id);
  136. if (!callback)
  137. return;
  138. callback.callback(msg.msg.result);
  139. },
  140. returnWhenZonesIdle: async function () {
  141. await returnWhenThreadsIdle();
  142. },
  143. forceSavePlayer: async function (playerId, zoneId) {
  144. const thread = getThreadFromId(zoneId);
  145. if (!thread)
  146. return;
  147. return new Promise(res => {
  148. const callbackId = this.registerCallback(res);
  149. thread.worker.send({
  150. method: 'forceSavePlayer',
  151. args: {
  152. playerId,
  153. callbackId
  154. }
  155. });
  156. });
  157. }
  158. };