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.
 
 
 

129 lines
2.7 KiB

  1. define([
  2. 'socket',
  3. 'js/system/events'
  4. ], function (
  5. io,
  6. events
  7. ) {
  8. let client = {
  9. doneConnect: false,
  10. init: function (onReady) {
  11. this.socket = io({
  12. transports: ['websocket']
  13. });
  14. this.socket.on('connect', this.onConnected.bind(this, onReady));
  15. this.socket.on('handshake', this.onHandshake.bind(this));
  16. this.socket.on('event', this.onEvent.bind(this));
  17. this.socket.on('events', this.onEvents.bind(this));
  18. this.socket.on('dc', this.onDisconnect.bind(this));
  19. Object.entries(this.processAction).forEach(([k, v]) => {
  20. this.processAction[k] = v.bind(this);
  21. });
  22. },
  23. onRezoneStart: function () {
  24. //Fired for mods to listen to
  25. events.emit('rezoneStart');
  26. events.emit('destroyAllObjects');
  27. events.emit('resetRenderer');
  28. events.emit('resetPhysics');
  29. events.emit('clearUis');
  30. client.request({
  31. threadModule: 'rezoneManager',
  32. method: 'clientAck',
  33. data: {}
  34. });
  35. },
  36. onGetMap: function ([msg]) {
  37. events.emit('onGetMap', msg);
  38. client.request({
  39. threadModule: 'instancer',
  40. method: 'clientAck',
  41. data: {}
  42. });
  43. },
  44. onConnected: function (onReady) {
  45. if (this.doneConnect)
  46. this.onDisconnect();
  47. else
  48. this.doneConnect = true;
  49. if (onReady)
  50. onReady();
  51. },
  52. onDisconnect: function () {
  53. window.location = window.location;
  54. },
  55. onHandshake: function () {
  56. events.emit('onHandshake');
  57. this.socket.emit('handshake');
  58. },
  59. request: function (msg) {
  60. this.socket.emit('request', msg, msg.callback);
  61. },
  62. processAction: {
  63. default: function (eventName, msgs) {
  64. msgs.forEach(m => events.emit(eventName, m));
  65. },
  66. rezoneStart: function (eventName, msgs) {
  67. events.emit('rezoneStart');
  68. events.emit('destroyAllObjects');
  69. events.emit('resetRenderer');
  70. events.emit('resetPhysics');
  71. events.emit('clearUis');
  72. client.request({
  73. threadModule: 'rezoneManager',
  74. method: 'clientAck',
  75. data: {}
  76. });
  77. },
  78. getMap: function (eventName, msgs) {
  79. events.emit('onBuildIngameUis');
  80. events.emit('onGetMap', msgs[0]);
  81. },
  82. onGetObject: function (eventName, msgs) {
  83. const prepend = msgs.filter(o => o.self);
  84. msgs.spliceWhere(o => prepend.some(p => p === o));
  85. msgs.unshift.apply(msgs, prepend);
  86. this.processAction.default(eventName, msgs);
  87. }
  88. },
  89. onEvent: function ({ event: eventName, data: eventData }) {
  90. const handler = this.processAction[eventName] || this.processAction.default;
  91. handler(eventName, [eventData]);
  92. },
  93. onEvents: function (response) {
  94. for (let eventName in response) {
  95. const eventMsgs = response[eventName];
  96. const handler = this.processAction[eventName] || this.processAction.default;
  97. handler(eventName, eventMsgs);
  98. }
  99. }
  100. };
  101. return client;
  102. });