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.
 
 
 

287 rivejä
6.1 KiB

  1. let childProcess = require('child_process');
  2. let objects = require('../objects/objects');
  3. let mapList = require('../config/maps/mapList');
  4. let connections = require('../security/connections');
  5. let serverConfig = require('../config/serverConfig');
  6. let events = require('../misc/events');
  7. const listenersOnZoneIdle = [];
  8. module.exports = {
  9. nextId: 0,
  10. lastCallbackId: 0,
  11. threads: [],
  12. callbacks: [],
  13. init: function () {
  14. this.getMapFiles();
  15. },
  16. addObject: function (obj, keepPos, transfer) {
  17. events.emit('onBeforePlayerEnterWorld', obj);
  18. let thread = this.getThreadFromName(obj.zoneName);
  19. let instanceId = obj.instanceId;
  20. if ((!thread) || (obj.zoneName !== thread.name))
  21. instanceId = -1;
  22. if (!thread) {
  23. thread = this.getThreadFromName(serverConfig.defaultZone);
  24. obj.zoneName = thread.name;
  25. }
  26. obj.zone = thread.id;
  27. this.send(obj.zone, {
  28. method: 'addObject',
  29. args: {
  30. keepPos: keepPos,
  31. obj: obj.getSimple ? obj.getSimple(true, true) : obj,
  32. instanceId: instanceId,
  33. transfer: transfer
  34. }
  35. });
  36. },
  37. removeObject: function (obj, skipLocal, callback) {
  38. if (!skipLocal)
  39. objects.removeObject(obj);
  40. let thread = this.getThreadFromName(obj.zoneName);
  41. if (!thread)
  42. return;
  43. let callbackId = null;
  44. if (callback)
  45. callbackId = this.registerCallback(callback);
  46. obj.zone = thread.id;
  47. this.send(obj.zone, {
  48. method: 'removeObject',
  49. args: {
  50. obj: obj.getSimple(true),
  51. instanceId: obj.instanceId,
  52. callbackId: callbackId
  53. }
  54. });
  55. },
  56. updateObject: function (obj, msgObj) {
  57. this.send(obj.zone, {
  58. method: 'updateObject',
  59. args: {
  60. id: obj.id,
  61. instanceId: obj.instanceId,
  62. obj: msgObj
  63. }
  64. });
  65. },
  66. queueAction: function (obj, action) {
  67. this.send(obj.zone, {
  68. method: 'queueAction',
  69. args: {
  70. id: obj.id,
  71. instanceId: obj.instanceId,
  72. action: action
  73. }
  74. });
  75. },
  76. performAction: function (obj, action) {
  77. this.send(obj.zone, {
  78. method: 'performAction',
  79. args: {
  80. id: obj.id,
  81. instanceId: obj.instanceId,
  82. action: action
  83. }
  84. });
  85. },
  86. registerCallback: function (callback) {
  87. this.callbacks.push({
  88. id: ++this.lastCallbackId,
  89. callback: callback
  90. });
  91. return this.lastCallbackId;
  92. },
  93. resolveCallback: function (msg) {
  94. let callback = this.callbacks.spliceFirstWhere(c => c.id === msg.msg.id);
  95. if (!callback)
  96. return;
  97. callback.callback(msg.msg.result);
  98. },
  99. send: function (zone, msg) {
  100. let thread = this.getThreadFromId(zone);
  101. if (thread)
  102. thread.worker.send(msg);
  103. },
  104. getThreadFromId: function (id) {
  105. return this.threads.find(t => t.id === id);
  106. },
  107. getThreadFromName: function (name) {
  108. return this.threads.find(t => t.name === name);
  109. },
  110. getMapFiles: function () {
  111. mapList.mapList.filter(m => !m.disabled).forEach(m => this.spawnMap(m));
  112. },
  113. spawnMap: function (map) {
  114. let worker = childProcess.fork('./world/worker');
  115. let thread = {
  116. id: this.nextId++,
  117. name: map.name,
  118. path: map.path,
  119. worker: worker
  120. };
  121. let onMessage = this.onMessage.bind(this, thread);
  122. worker.on('message', function (m) {
  123. onMessage(m);
  124. });
  125. this.threads.push(thread);
  126. },
  127. onMessage: function (thread, message) {
  128. if (message.module)
  129. global[message.module][message.method](message);
  130. else if (message.event === 'onCrashed') {
  131. thread.worker.kill();
  132. process.exit();
  133. } else
  134. this.thread[message.method].call(this, thread, message);
  135. },
  136. messageAllThreads: function (message) {
  137. this.threads.forEach(t => t.worker.send(message));
  138. },
  139. fireEventOnAllThreads: function ({ msg: { event, data } }) {
  140. this.threads.forEach(t => t.worker.send({ event, data }));
  141. },
  142. thread: {
  143. onReady: function (thread) {
  144. thread.worker.send({
  145. method: 'init',
  146. args: {
  147. name: thread.name,
  148. path: thread.path,
  149. zoneId: thread.id
  150. }
  151. });
  152. },
  153. event: function (thread, message) {
  154. objects.sendEvent(message);
  155. },
  156. events: function (thread, message) {
  157. objects.sendEvents(message);
  158. },
  159. object: function (thread, message) {
  160. objects.updateObject(message);
  161. },
  162. track: function (thread, message) {
  163. let player = objects.objects.find(o => o.id === message.serverId);
  164. if (!player)
  165. return;
  166. player.auth.gaTracker.track(message.obj);
  167. },
  168. callDifferentThread: function (thread, message) {
  169. let obj = connections.players.find(p => (p.name === message.playerName));
  170. if (!obj)
  171. return;
  172. let newThread = this.getThreadFromName(obj.zoneName);
  173. if (!newThread)
  174. return;
  175. newThread.worker.send({
  176. module: message.data.module,
  177. method: message.data.method,
  178. args: message.data.args
  179. });
  180. },
  181. rezone: function (thread, message) {
  182. const { args: { obj, newZone, keepPos = true } } = message;
  183. obj.destroyed = false;
  184. obj.zoneName = newZone;
  185. obj.id = obj.serverId;
  186. let serverObj = objects.objects.find(o => o.id === obj.id);
  187. serverObj.zoneName = obj.zoneName;
  188. let newThread = this.getThreadFromName(obj.zoneName);
  189. if (!newThread) {
  190. newThread = this.getThreadFromName(serverConfig.defaultZone);
  191. obj.zoneName = newThread.name;
  192. serverObj.zoneName = newThread.name;
  193. }
  194. serverObj.zone = newThread.id;
  195. obj.zone = newThread.id;
  196. serverObj.player.broadcastSelf();
  197. const isRezone = true;
  198. this.addObject(obj, keepPos, isRezone);
  199. },
  200. onZoneIdle: function (thread) {
  201. listenersOnZoneIdle.forEach(l => l(thread));
  202. }
  203. },
  204. returnWhenZonesIdle: async function () {
  205. return new Promise(res => {
  206. const waiting = [...this.threads];
  207. const onZoneIdle = thread => {
  208. waiting.spliceWhere(w => w === thread);
  209. if (waiting.length)
  210. return;
  211. listenersOnZoneIdle.spliceWhere(l => l === onZoneIdle);
  212. res();
  213. };
  214. listenersOnZoneIdle.push(onZoneIdle);
  215. this.threads.forEach(t => {
  216. t.worker.send({
  217. method: 'notifyOnceIdle'
  218. });
  219. });
  220. });
  221. },
  222. forceSavePlayer: async function (playerName, zoneName) {
  223. const thread = this.getThreadFromName(zoneName);
  224. if (!thread)
  225. return;
  226. return new Promise(res => {
  227. const callbackId = this.registerCallback(res);
  228. thread.worker.send({
  229. method: 'forceSavePlayer',
  230. args: {
  231. playerName,
  232. callbackId
  233. }
  234. });
  235. });
  236. }
  237. };