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.
 
 
 

186 lines
3.6 KiB

  1. //Imports
  2. const objects = require('../objects/objects');
  3. const events = require('../misc/events');
  4. const {
  5. getThread, killThread, sendMessageToThread, getThreadFromId, returnWhenThreadsIdle, gePlayerCountInThread, addPlayerToThread, removePlayerFromThread
  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. playerId: obj.id
  27. });
  28. if (resetObjPosition) {
  29. delete obj.x;
  30. delete obj.y;
  31. }
  32. obj.zoneName = thread.name;
  33. obj.zoneId = thread.id;
  34. serverObj.zoneId = thread.id;
  35. serverObj.zoneName = thread.name;
  36. serverObj.player.broadcastSelf();
  37. const simpleObj = obj.getSimple ? obj.getSimple(true, true) : obj;
  38. addPlayerToThread(thread, obj.id);
  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. if (!skipLocal)
  68. objects.removeObject(obj);
  69. const thread = getThreadFromId(obj.zoneId);
  70. if (!thread) {
  71. callback();
  72. return;
  73. }
  74. removePlayerFromThread(thread, obj.id);
  75. let callbackId = null;
  76. if (callback)
  77. callbackId = this.registerCallback(callback);
  78. sendMessageToThread({
  79. threadId: obj.zoneId,
  80. msg: {
  81. method: 'removeObject',
  82. args: {
  83. obj: obj.getSimple(true),
  84. callbackId: callbackId
  85. }
  86. }
  87. });
  88. },
  89. updateObject: function (obj, msgObj) {
  90. sendMessageToThread({
  91. threadId: obj.zoneId,
  92. msg: {
  93. method: 'updateObject',
  94. args: {
  95. id: obj.id,
  96. obj: msgObj
  97. }
  98. }
  99. });
  100. },
  101. queueAction: function (obj, action) {
  102. sendMessageToThread({
  103. threadId: obj.zoneId,
  104. msg: {
  105. method: 'queueAction',
  106. args: {
  107. id: obj.id,
  108. action: action
  109. }
  110. }
  111. });
  112. },
  113. performAction: function (obj, action) {
  114. sendMessageToThread({
  115. threadId: obj.zoneId,
  116. msg: {
  117. method: 'performAction',
  118. args: {
  119. id: obj.id,
  120. action: action
  121. }
  122. }
  123. });
  124. },
  125. registerCallback: function (callback) {
  126. return registerCallback(callback);
  127. },
  128. resolveCallback: function (msg) {
  129. const callback = removeCallback(msg.msg.id);
  130. if (!callback)
  131. return;
  132. callback.callback(msg.msg.result);
  133. },
  134. returnWhenZonesIdle: async function () {
  135. await returnWhenThreadsIdle();
  136. },
  137. forceSavePlayer: async function (playerId, zoneId) {
  138. const thread = getThreadFromId(zoneId);
  139. if (!thread)
  140. return;
  141. return new Promise(res => {
  142. const callbackId = this.registerCallback(res);
  143. thread.worker.send({
  144. method: 'forceSavePlayer',
  145. args: {
  146. playerId,
  147. callbackId
  148. }
  149. });
  150. });
  151. }
  152. };