UNPKG

node-insim

Version:

An InSim library for NodeJS with TypeScript support

59 lines (58 loc) 1.96 kB
import { __decorate } from "tslib"; import { byte } from '../decorators'; import { InSimError } from '../errors'; import { copyBuffer, unpack } from '../lfspack'; import { Packet } from './base'; import { PacketType } from './enums'; import { NodeLap } from './structs'; /** * Node and Lap Packet - variable size * * To receive {@link IS_NLP} packets at a specified interval: * * - Set the Interval field in the {@link IS_ISI} (InSimInit) packet (10, 20, 30... 8000 ms) * - Set {@link ISF_NLP} flag in the {@link IS_ISI} packet * * If {@link ISF_NLP} flag is set, one {@link IS_NLP} packet is sent... */ export class IS_NLP extends Packet { constructor() { super(...arguments); /** 4 + NumP * 6 (PLUS 2 if needed to make it a multiple of 4) */ this.Size = 4; this.Type = PacketType.ISP_NLP; /** 0 unless this is a reply to a {@link TINY_NLP} request */ this.ReqI = 0; /** Number of players in race */ this.NumP = 0; /** Node and lap of each player, 1 to {@link NLP_MAX_CARS} (NumP) */ this.Info = []; } unpack(buffer) { super.unpack(buffer); const data = unpack(this.getFormat(), buffer.buffer); if (!data) { throw new InSimError('IS_MSO - Unpacked no data from buffer'); } const nodeLapLength = new NodeLap().getFormatSize(); for (let i = 0; i < this.NumP; i++) { const start = data.length + nodeLapLength * i; const nodeLapBuffer = copyBuffer(buffer.slice(start, start + nodeLapLength)); this.Info.push(new NodeLap().unpack(nodeLapBuffer)); } return this; } } __decorate([ byte() ], IS_NLP.prototype, "Size", void 0); __decorate([ byte() ], IS_NLP.prototype, "Type", void 0); __decorate([ byte() ], IS_NLP.prototype, "ReqI", void 0); __decorate([ byte() ], IS_NLP.prototype, "NumP", void 0); export const NLP_MAX_CARS = 40;