node-insim
Version:
An InSim library for NodeJS with TypeScript support
115 lines (114 loc) • 5.4 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IS_PLH = void 0;
var decorators_1 = require("../decorators");
var lfspack_1 = require("../lfspack");
var base_1 = require("./base");
var enums_1 = require("./enums");
var structs_1 = require("./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
*/
var IS_PLH = /** @class */ (function (_super) {
__extends(IS_PLH, _super);
function IS_PLH(data) {
var _this = _super.call(this) || this;
/** 4 + NumP * 4 */
_this.Size = 4;
_this.Type = enums_1.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);
return _this;
}
IS_PLH.prototype.unpack = function (buffer) {
_super.prototype.unpack.call(this, buffer);
var playerHandicapLength = new structs_1.PlayerHCap().getFormatSize();
for (var i = 0; i < this.NumP; i++) {
var start = this.handicapsOffset + playerHandicapLength * i;
var objectInfoBuffer = (0, lfspack_1.copyBuffer)(buffer.slice(start, start + playerHandicapLength));
this.HCaps.push(new structs_1.PlayerHCap().unpack(objectInfoBuffer));
}
return this;
};
IS_PLH.prototype.pack = function () {
if (this.HCaps.length > IS_PLH.PLH_MAX_PLAYERS) {
throw new RangeError("IS_PLH - Too many players set (max is ".concat(IS_PLH.PLH_MAX_PLAYERS));
}
var playerHandicapLength = new structs_1.PlayerHCap().getFormatSize();
this.Size = this.handicapsOffset + this.HCaps.length * playerHandicapLength;
var dataBuffer = _super.prototype.pack.call(this);
var handicapBuffer = this.HCaps.reduce(function (acc, handicap) { return new Uint8Array(__spreadArray(__spreadArray([], __read(acc), false), __read(handicap.pack()), false)); }, new Uint8Array());
return new Uint8Array(__spreadArray(__spreadArray([], __read(dataBuffer), false), __read(handicapBuffer), false));
};
IS_PLH.PLH_MAX_PLAYERS = 48; // NOTE: Increase if MAX_CARS_S2 is increased
__decorate([
(0, decorators_1.byte)()
], IS_PLH.prototype, "Size", void 0);
__decorate([
(0, decorators_1.byte)()
], IS_PLH.prototype, "Type", void 0);
__decorate([
(0, decorators_1.byte)()
], IS_PLH.prototype, "ReqI", void 0);
__decorate([
(0, decorators_1.byte)()
], IS_PLH.prototype, "NumP", void 0);
return IS_PLH;
}(base_1.SendablePacket));
exports.IS_PLH = IS_PLH;