node-insim
Version:
An InSim library for NodeJS with TypeScript support
63 lines (62 loc) • 2.57 kB
JavaScript
import { __decorate } from "tslib";
import { byte } from '../decorators';
import { copyBuffer } from '../lfspack';
import { SendablePacket } from './base';
import { PacketType } from './enums';
import { PlayerHCap } from './structs';
/**
* PLayer Handicaps - variable size
*
* These handicaps will remain until the player spectates or rejoins after returning from pits or garage (an {@link IS_NPL} will be sent in that case).
*
* An output IS_PLH is sent to all InSim clients after an IS_PLH is received. The output IS_PLH contains an entry for all valid players that had handicaps updated. An IS_PLH is also output when a handicap is set by a text command /h_mass username X or /h_tres username X
*/
export class IS_PLH extends SendablePacket {
constructor(data) {
super();
/** 4 + NumP * 4 */
this.Size = 4;
this.Type = PacketType.ISP_PLH;
/** 0 unless this is a reply to a {@link TINY_PLH} request */
this.ReqI = 0;
/** Number of players in this packet */
this.NumP = 0;
/** 0 to {@link IS_PLH.PLH_MAX_PLAYERS} ({@link NumP}) */
this.HCaps = [];
this.handicapsOffset = 4;
this.initialize(data);
}
unpack(buffer) {
super.unpack(buffer);
const playerHandicapLength = new PlayerHCap().getFormatSize();
for (let i = 0; i < this.NumP; i++) {
const start = this.handicapsOffset + playerHandicapLength * i;
const objectInfoBuffer = copyBuffer(buffer.slice(start, start + playerHandicapLength));
this.HCaps.push(new PlayerHCap().unpack(objectInfoBuffer));
}
return this;
}
pack() {
if (this.HCaps.length > IS_PLH.PLH_MAX_PLAYERS) {
throw new RangeError(`IS_PLH - Too many players set (max is ${IS_PLH.PLH_MAX_PLAYERS}`);
}
const playerHandicapLength = new PlayerHCap().getFormatSize();
this.Size = this.handicapsOffset + this.HCaps.length * playerHandicapLength;
const dataBuffer = super.pack();
const handicapBuffer = this.HCaps.reduce((acc, handicap) => new Uint8Array([...acc, ...handicap.pack()]), new Uint8Array());
return new Uint8Array([...dataBuffer, ...handicapBuffer]);
}
}
IS_PLH.PLH_MAX_PLAYERS = 40; // NOTE: Increase if MAX_CARS_S2 is increased
__decorate([
byte()
], IS_PLH.prototype, "Size", void 0);
__decorate([
byte()
], IS_PLH.prototype, "Type", void 0);
__decorate([
byte()
], IS_PLH.prototype, "ReqI", void 0);
__decorate([
byte()
], IS_PLH.prototype, "NumP", void 0);