SHARE
TWEET

Untitled

a guest May 23rd, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @module stdlib
  3.  */
  4. import { Library, PacketHook, Client } from '../core';
  5. import { EventEmitter } from 'events';
  6. import { ObjectData, UpdatePacket, NewTickPacket } from '../networking';
  7.  
  8. /**
  9.  * An event listener for events emitted by the `ObjectTracker`.
  10.  */
  11. export type ObjectEventListener = (obj: ObjectData, client: Client) => void;
  12.  
  13. @Library({
  14.   name: 'Object Tracker',
  15.   author: 'tcrane',
  16.   description: 'A utility library for keeping track of objects.'
  17. })
  18. export class ObjectTracker {
  19.   private emitter: EventEmitter;
  20.   private readonly trackedTypes: {
  21.     [type: number]: boolean;
  22.   };
  23.   private readonly trackedObjects: {
  24.     [guid: string]: ObjectData[];
  25.   };
  26.  
  27.   constructor() {
  28.     this.emitter = new EventEmitter();
  29.     this.trackedTypes = {};
  30.     this.trackedObjects = {};
  31.   }
  32.  
  33.   /**
  34.    * Attaches an event listener to the specified event.
  35.    * @param event The event to attach the listener to.
  36.    * @param listener The function to invoke when the event is fired.
  37.    */
  38.   on(event: 'leave' | 'enter' | 'update', listener: ObjectEventListener): this {
  39.     this.emitter.on(event, listener);
  40.     return this;
  41.   }
  42.  
  43.   /**
  44.    * Starts tracking the specified object,
  45.    * and optionally attaches an event listener.
  46.    * @param objectType The object type to start track.
  47.    * @param listener An optional event listener to attach.
  48.    */
  49.   startTracking(objectType: number, listener?: ObjectEventListener): this {
  50.     this.trackedTypes[objectType] = true;
  51.     if (listener) {
  52.       this.on(objectType, listener);
  53.     }
  54.     return this;
  55.   }
  56.  
  57.   /**
  58.    * Stops tracking the specified object and
  59.    * removes any event listeners for it.
  60.    * @param objectType The object type to stop tracking.
  61.    */
  62.   stopTracking(objectType: number): this {
  63.     if (!this.trackedTypes.hasOwnProperty(objectType)) {
  64.       return;
  65.     }
  66.     delete this.trackedTypes[objectType];
  67.     this.emitter.removeAllListeners(objectType.toString());
  68.   }
  69.  
  70.   @PacketHook()
  71.   private onUpdate(client: Client, update: UpdatePacket): void {
  72.     for (const obj of update.newObjects) {
  73.       if (this.trackedTypes[obj.objectType]) {
  74.         if (!this.trackedObjects.hasOwnProperty(client.guid)) {
  75.           this.trackedObjects[client.guid] = [];
  76.         }
  77.         this.trackedObjects[client.guid].push(obj);
  78.         this.emitter.emit('enter', obj, client);
  79.       }
  80.     }
  81.  
  82.     if (!this.trackedObjects.hasOwnProperty(client.guid)) {
  83.       return;
  84.     }
  85.     for (const drop of update.drops) {
  86.       for (let n = 0; n < this.trackedObjects[client.guid].length; n++) {
  87.         if (this.trackedObjects[client.guid][n].status.objectId === drop) {
  88.           this.trackedObjects[client.guid].splice(n, 1);
  89.           this.emitter.emit('leave', this.trackedObjects[client.guid][n], client)
  90.           break;
  91.         }
  92.       }
  93.     }
  94.   }
  95.  
  96.   @PacketHook()
  97.   private onNewTick(client: Client, newTick: NewTickPacket): void {
  98.     if (!this.trackedObjects.hasOwnProperty(client.guid) || this.trackedObjects[client.guid].length < 1) {
  99.       return;
  100.     }
  101.     for (const status of newTick.statuses) {
  102.       for (const obj of this.trackedObjects[client.guid]) {
  103.         if (obj.status.objectId === status.objectId) {
  104.           obj.status = status;
  105.           this.emitter.emit('update', obj, client);
  106.           break;
  107.         }
  108.       }
  109.     }
  110.   }
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top