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.
 
 
 

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