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.
 
 
 

351 lines
7.5 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 events = require('../misc/events');
  6. const listenersOnZoneIdle = [];
  7. module.exports = {
  8. nextId: 0,
  9. lastCallbackId: 0,
  10. threads: [],
  11. callbacks: [],
  12. init: function () {
  13. this.getMapFiles();
  14. },
  15. addObject: async function (obj, keepPos, transfer) {
  16. const serverObj = objects.objects.find(o => o.id === obj.id);
  17. if (!serverObj)
  18. return;
  19. events.emit('onBeforePlayerEnterWorld', obj);
  20. let thread;
  21. let map = mapList.mapList.find(m => m.name === obj.zoneName);
  22. if (!map)
  23. map = mapList.mapList.find(m => m.name === clientConfig.config.defaultZone);
  24. thread = this.threads.find(t => t.id === obj.zoneId && t.name === obj.zoneName);
  25. if (!thread) {
  26. if (map.instanced) {
  27. delete obj.x;
  28. delete obj.y;
  29. thread = await this.spawnMap(map);
  30. } else
  31. thread = this.getThreadFromName(map.name);
  32. }
  33. obj.zoneName = thread.name;
  34. obj.zoneId = thread.id;
  35. serverObj.zoneId = thread.id;
  36. serverObj.zoneName = thread.name;
  37. serverObj.player.broadcastSelf();
  38. const simpleObj = obj.getSimple ? obj.getSimple(true, true) : obj;
  39. this.send(obj.zoneId, {
  40. method: 'addObject',
  41. args: {
  42. keepPos: keepPos,
  43. obj: simpleObj,
  44. transfer: transfer
  45. }
  46. });
  47. },
  48. removeObjectFromInstancedZone: async function (thread, obj, callback) {
  49. await new Promise(res => {
  50. const cb = this.registerCallback(res);
  51. thread.worker.send({
  52. method: 'forceSavePlayer',
  53. args: {
  54. playerName: obj.name,
  55. callbackId: cb
  56. }
  57. });
  58. });
  59. thread.worker.kill();
  60. this.threads.spliceWhere(t => t === thread);
  61. if (callback)
  62. callback();
  63. },
  64. removeObject: function (obj, skipLocal, callback) {
  65. if (!skipLocal)
  66. objects.removeObject(obj);
  67. let thread = this.findObjectThread(obj);
  68. if (!thread)
  69. return;
  70. if (thread.instanced) {
  71. this.removeObjectFromInstancedZone(thread, obj, callback);
  72. return;
  73. }
  74. let callbackId = null;
  75. if (callback)
  76. callbackId = this.registerCallback(callback);
  77. this.send(obj.zoneId, {
  78. method: 'removeObject',
  79. args: {
  80. obj: obj.getSimple(true),
  81. callbackId: callbackId
  82. }
  83. });
  84. },
  85. updateObject: function (obj, msgObj) {
  86. this.send(obj.zoneId, {
  87. method: 'updateObject',
  88. args: {
  89. id: obj.id,
  90. obj: msgObj
  91. }
  92. });
  93. },
  94. queueAction: function (obj, action) {
  95. this.send(obj.zoneId, {
  96. method: 'queueAction',
  97. args: {
  98. id: obj.id,
  99. action: action
  100. }
  101. });
  102. },
  103. performAction: function (obj, action) {
  104. this.send(obj.zoneId, {
  105. method: 'performAction',
  106. args: {
  107. id: obj.id,
  108. action: action
  109. }
  110. });
  111. },
  112. registerCallback: function (callback) {
  113. this.callbacks.push({
  114. id: ++this.lastCallbackId,
  115. callback: callback
  116. });
  117. return this.lastCallbackId;
  118. },
  119. resolveCallback: function (msg) {
  120. let callback = this.callbacks.spliceFirstWhere(c => c.id === msg.msg.id);
  121. if (!callback)
  122. return;
  123. callback.callback(msg.msg.result);
  124. },
  125. send: function (threadId, msg) {
  126. const thread = this.threads.find(t => t.id === threadId);
  127. if (thread)
  128. thread.worker.send(msg);
  129. },
  130. findObjectThread: function ({ zoneId }) {
  131. return this.threads.find(t => t.id === zoneId);
  132. },
  133. getThreadFromName: function (name) {
  134. return this.threads.find(t => t.name === name);
  135. },
  136. getMapFiles: function () {
  137. mapList.mapList
  138. .filter(m => !m.disabled && !m.instanced)
  139. .forEach(m => this.spawnMap(m));
  140. },
  141. spawnMap: async function ({ name, path, instanced }) {
  142. return new Promise(resolveOnReady => {
  143. const worker = childProcess.fork('./world/worker', [name]);
  144. const id = instanced ? _.getGuid() : name;
  145. const thread = {
  146. id,
  147. name,
  148. instanced,
  149. path,
  150. worker,
  151. cbOnInitialized: resolveOnReady
  152. };
  153. const onMessage = this.onMessage.bind(this, thread);
  154. worker.on('message', function (m) {
  155. onMessage(m);
  156. });
  157. this.threads.push(thread);
  158. },
  159. onMessage: function (thread, message) {
  160. if (message.module) {
  161. try {
  162. global[message.module][message.method](message);
  163. } catch (e) {
  164. /* eslint-disable-next-line no-console */
  165. console.log('No global method found', message.module, message.method);
  166. process.exit();
  167. }
  168. } else if (message.event === 'onCrashed') {
  169. thread.worker.kill();
  170. process.exit();
  171. } else
  172. this.thread[message.method].call(this, thread, message);
  173. },
  174. messageAllThreads: function (message) {
  175. this.threads.forEach(t => t.worker.send(message));
  176. },
  177. fireEventOnAllThreads: function ({ msg: { event, data } }) {
  178. this.threads.forEach(t => t.worker.send({ event, data }));
  179. },
  180. thread: {
  181. onReady: function (thread) {
  182. thread.worker.send({
  183. method: 'init',
  184. args: {
  185. zoneName: thread.name,
  186. zoneId: thread.id,
  187. path: thread.path
  188. }
  189. });
  190. },
  191. onInitialized: function (thread) {
  192. thread.cbOnInitialized(thread);
  193. },
  194. event: function (thread, message) {
  195. objects.sendEvent(message, thread);
  196. },
  197. events: function (thread, message) {
  198. objects.sendEvents(message, thread);
  199. },
  200. object: function (thread, message) {
  201. objects.updateObject(message);
  202. },
  203. track: function (thread, message) {
  204. let player = objects.objects.find(o => o.id === message.serverId);
  205. if (!player)
  206. return;
  207. player.auth.gaTracker.track(message.obj);
  208. },
  209. callDifferentThread: function (thread, message) {
  210. let obj = connections.players.find(p => (p.name === message.playerName));
  211. if (!obj)
  212. return;
  213. let newThread = this.getThreadFromName(obj.zoneName);
  214. if (!newThread)
  215. return;
  216. newThread.worker.send({
  217. module: message.data.module,
  218. method: message.data.method,
  219. args: message.data.args
  220. });
  221. },
  222. rezone: async function (thread, message) {
  223. const { args: { obj, newZone, keepPos = true } } = message;
  224. if (thread.instanced) {
  225. thread.worker.kill();
  226. this.threads.spliceWhere(t => t === thread);
  227. }
  228. //When messages are sent from map threads, they have an id (id of the object in the map thread)
  229. // as well as a serverId (id of the object in the main thread)
  230. const serverId = obj.serverId;
  231. obj.id = serverId;
  232. obj.destroyed = false;
  233. const serverObj = objects.objects.find(o => o.id === obj.id);
  234. const mapExists = mapList.mapList.some(m => m.name === newZone);
  235. if (mapExists) {
  236. serverObj.zoneName = newZone;
  237. obj.zoneName = newZone;
  238. } else {
  239. obj.zoneName = clientConfig.config.defaultZone;
  240. serverObj.zoneName = clientConfig.config.defaultZone;
  241. }
  242. delete serverObj.zoneId;
  243. delete obj.zoneId;
  244. const isRezone = true;
  245. await this.addObject(obj, keepPos, isRezone);
  246. },
  247. onZoneIdle: function (thread) {
  248. listenersOnZoneIdle.forEach(l => l(thread));
  249. }
  250. },
  251. returnWhenZonesIdle: async function () {
  252. return new Promise(res => {
  253. const waiting = [...this.threads];
  254. const onZoneIdle = thread => {
  255. waiting.spliceWhere(w => w === thread);
  256. if (waiting.length)
  257. return;
  258. listenersOnZoneIdle.spliceWhere(l => l === onZoneIdle);
  259. res();
  260. };
  261. listenersOnZoneIdle.push(onZoneIdle);
  262. this.threads.forEach(t => {
  263. t.worker.send({
  264. method: 'notifyOnceIdle'
  265. });
  266. });
  267. });
  268. },
  269. forceSavePlayer: async function (playerName, zoneId) {
  270. const thread = this.threads.find(t => t.id === zoneId);
  271. if (!thread)
  272. return;
  273. return new Promise(res => {
  274. const callbackId = this.registerCallback(res);
  275. thread.worker.send({
  276. method: 'forceSavePlayer',
  277. args: {
  278. playerName,
  279. callbackId
  280. }
  281. });
  282. });
  283. }
  284. };