Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

187 lignes
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. return;
  72. if (thread.instanced && (await gePlayerCountInThread(thread)) === 1) {
  73. this.removeObjectFromInstancedZone(thread, playerId, callback);
  74. return;
  75. }
  76. let callbackId = null;
  77. if (callback)
  78. callbackId = this.registerCallback(callback);
  79. sendMessageToThread({
  80. threadId: obj.zoneId,
  81. msg: {
  82. method: 'removeObject',
  83. args: {
  84. obj: obj.getSimple(true),
  85. callbackId: callbackId
  86. }
  87. }
  88. });
  89. },
  90. updateObject: function (obj, msgObj) {
  91. sendMessageToThread({
  92. threadId: obj.zoneId,
  93. msg: {
  94. method: 'updateObject',
  95. args: {
  96. id: obj.id,
  97. obj: msgObj
  98. }
  99. }
  100. });
  101. },
  102. queueAction: function (obj, action) {
  103. sendMessageToThread({
  104. threadId: obj.zoneId,
  105. msg: {
  106. method: 'queueAction',
  107. args: {
  108. id: obj.id,
  109. action: action
  110. }
  111. }
  112. });
  113. },
  114. performAction: function (obj, action) {
  115. sendMessageToThread({
  116. threadId: obj.zoneId,
  117. msg: {
  118. method: 'performAction',
  119. args: {
  120. id: obj.id,
  121. action: action
  122. }
  123. }
  124. });
  125. },
  126. registerCallback: function (callback) {
  127. return registerCallback(callback);
  128. },
  129. resolveCallback: function (msg) {
  130. const callback = removeCallback(msg.msg.id);
  131. if (!callback)
  132. return;
  133. callback.callback(msg.msg.result);
  134. },
  135. returnWhenZonesIdle: async function () {
  136. await returnWhenThreadsIdle();
  137. },
  138. forceSavePlayer: async function (playerId, zoneId) {
  139. const thread = getThreadFromId(zoneId);
  140. if (!thread)
  141. return;
  142. return new Promise(res => {
  143. const callbackId = this.registerCallback(res);
  144. thread.worker.send({
  145. method: 'forceSavePlayer',
  146. args: {
  147. playerId,
  148. callbackId
  149. }
  150. });
  151. });
  152. }
  153. };