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.
 
 
 

342 lines
7.2 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 = this.spawnMap(map);
  30. await new Promise(res => setTimeout(res, 2000));
  31. } else
  32. thread = this.getThreadFromName(map.name);
  33. }
  34. obj.zoneName = thread.name;
  35. obj.zoneId = thread.id;
  36. serverObj.zoneId = thread.id;
  37. serverObj.zoneName = thread.name;
  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: function ({ name, path, instanced }) {
  142. const worker = childProcess.fork('./world/worker', [name]);
  143. const id = instanced ? _.getGuid() : name;
  144. const thread = {
  145. id,
  146. name,
  147. instanced,
  148. path,
  149. worker
  150. };
  151. const onMessage = this.onMessage.bind(this, thread);
  152. worker.on('message', function (m) {
  153. onMessage(m);
  154. });
  155. this.threads.push(thread);
  156. return thread;
  157. },
  158. onMessage: function (thread, message) {
  159. if (message.module) {
  160. try {
  161. global[message.module][message.method](message);
  162. } catch (e) {
  163. console.log('No global method found', message.module, message.method);
  164. process.exit();
  165. }
  166. } else if (message.event === 'onCrashed') {
  167. thread.worker.kill();
  168. process.exit();
  169. } else
  170. this.thread[message.method].call(this, thread, message);
  171. },
  172. messageAllThreads: function (message) {
  173. this.threads.forEach(t => t.worker.send(message));
  174. },
  175. fireEventOnAllThreads: function ({ msg: { event, data } }) {
  176. this.threads.forEach(t => t.worker.send({ event, data }));
  177. },
  178. thread: {
  179. onReady: function (thread) {
  180. thread.worker.send({
  181. method: 'init',
  182. args: {
  183. zoneName: thread.name,
  184. zoneId: thread.id,
  185. path: thread.path
  186. }
  187. });
  188. },
  189. event: function (thread, message) {
  190. objects.sendEvent(message, thread);
  191. },
  192. events: function (thread, message) {
  193. objects.sendEvents(message, thread);
  194. },
  195. object: function (thread, message) {
  196. objects.updateObject(message);
  197. },
  198. track: function (thread, message) {
  199. let player = objects.objects.find(o => o.id === message.serverId);
  200. if (!player)
  201. return;
  202. player.auth.gaTracker.track(message.obj);
  203. },
  204. callDifferentThread: function (thread, message) {
  205. let obj = connections.players.find(p => (p.name === message.playerName));
  206. if (!obj)
  207. return;
  208. let newThread = this.getThreadFromName(obj.zoneName);
  209. if (!newThread)
  210. return;
  211. newThread.worker.send({
  212. module: message.data.module,
  213. method: message.data.method,
  214. args: message.data.args
  215. });
  216. },
  217. rezone: async function (thread, message) {
  218. const { args: { obj, newZone, keepPos = true } } = message;
  219. //When messages are sent from map threads, they have an id (id of the object in the map thread)
  220. // as well as a serverId (id of the object in the main thread)
  221. const serverId = obj.serverId;
  222. obj.id = serverId;
  223. obj.destroyed = false;
  224. const serverObj = objects.objects.find(o => o.id === obj.id);
  225. const mapExists = mapList.mapList.some(m => m.name === newZone);
  226. if (mapExists) {
  227. serverObj.zoneName = newZone;
  228. obj.zoneName = newZone;
  229. } else {
  230. obj.zoneName = clientConfig.config.defaultZone;
  231. serverObj.zoneName = clientConfig.config.defaultZone;
  232. }
  233. delete serverObj.zoneId;
  234. delete obj.zoneId;
  235. serverObj.player.broadcastSelf();
  236. const isRezone = true;
  237. await this.addObject(obj, keepPos, isRezone);
  238. },
  239. onZoneIdle: function (thread) {
  240. listenersOnZoneIdle.forEach(l => l(thread));
  241. }
  242. },
  243. returnWhenZonesIdle: async function () {
  244. return new Promise(res => {
  245. const waiting = [...this.threads];
  246. const onZoneIdle = thread => {
  247. waiting.spliceWhere(w => w === thread);
  248. if (waiting.length)
  249. return;
  250. listenersOnZoneIdle.spliceWhere(l => l === onZoneIdle);
  251. res();
  252. };
  253. listenersOnZoneIdle.push(onZoneIdle);
  254. this.threads.forEach(t => {
  255. t.worker.send({
  256. method: 'notifyOnceIdle'
  257. });
  258. });
  259. });
  260. },
  261. forceSavePlayer: async function (playerName, zoneId) {
  262. const thread = this.threads.find(t => t.id === zoneId);
  263. if (!thread)
  264. return;
  265. return new Promise(res => {
  266. const callbackId = this.registerCallback(res);
  267. thread.worker.send({
  268. method: 'forceSavePlayer',
  269. args: {
  270. playerName,
  271. callbackId
  272. }
  273. });
  274. });
  275. }
  276. };