lfs-akairo
Version:
LFS Akairo is a framework designed to simplify the creation of InSim applications.
234 lines (233 loc) • 8.16 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var player_exports = {};
__export(player_exports, {
Player: () => Player
});
module.exports = __toCommonJS(player_exports);
var import_button = require("#classes/button");
var import_data = require("#utils/data");
var import_i18n = require("#utils/i18n");
var import_i18next = __toESM(require("i18next"));
var import_packets = require("node-insim/packets");
class Player {
constructor(akairo) {
this.akairo = akairo;
/** Determines if player is fully assigned inside Akairo (useful in IS_NCI packet) */
this.isReady = false;
/** Attributes saved in player instance */
this.selfData = {};
this.buttons = /* @__PURE__ */ new Map();
}
/**
* Translate an content to player.
* @param scope Translation scope
* @param options Translation scope options
*/
t(path, scope) {
const locale = (0, import_i18n.convertLanguage)(this.gameLanguage);
return import_i18next.default.t(path, __spreadProps(__spreadValues({}, scope), { ns: locale, lng: locale }));
}
/**
* Send a message to player.
* @param content Content to be sent (it can be translated)
* @param sound Sound of message
*/
message(content, sound) {
this.akairo.insim.send(
new import_packets.IS_MTC({
Text: String(content),
Sound: sound,
UCID: this.uniqueId
})
);
return this;
}
/**
* Create an button instance to player.
*/
button() {
var _a;
let availableId = -1;
for (let id = 0; id < 240; id++) {
if (!this.buttons.has(id)) {
availableId = id;
break;
}
}
if (availableId === -1) {
const oldestEntry = this.buttons.entries().next().value;
if (!oldestEntry) return null;
availableId = oldestEntry[0];
const previous = this.buttons.get(availableId);
this.buttons.delete(availableId);
(_a = previous == null ? void 0 : previous.destroy) == null ? void 0 : _a.call(previous);
}
const instance = new import_button.Button(this);
instance.setId(() => availableId);
this.buttons.set(availableId, instance);
return instance;
}
/**
* Send player to pitlane.
*/
pitlane() {
this.akairo.insim.sendMessage(`/pitlane ${this.userName}`);
return this;
}
/**
* Send player to spectator.
*/
spectate() {
this.akairo.insim.sendMessage(`/spec ${this.userName}`);
return this;
}
/**
* Nove player from spectator to track.
*/
ujoin() {
this.akairo.insim.sendMessage(`/ujoin ${this.userName}`);
return this;
}
/**
* Set player vehicle added mass.
*/
mass(percentage) {
this.akairo.insim.sendMessage(
`/h_mass ${this.userName} ${Math.max(percentage, 0)}`
);
return this;
}
/**
* Set player vehicle intake restriction (0% - 50%).
*/
restriction(percentage) {
this.akairo.insim.sendMessage(
`/h_tres ${this.userName} ${Math.min(Math.max(percentage, 0), 50)}`
);
return this;
}
/**
* Toggle player available siren.
*/
siren(enabled) {
this.akairo.insim.sendMessage(
`/cansiren ${this.userName} ${enabled ? 1 : 0}`
);
return this;
}
/**
* Detects if player is closer to an position.
* @param x X coordinates of position
* @param y Y coordinates of position
* @param z Z coordinates of position
* @param radius Margin radius to compare if player is in position
*/
close(x, y, z, radius) {
const pitStatus = this.get("essentials.pit-status");
if (pitStatus !== "TRACK") return false;
const positionX = this.get("essentials.position.x");
const positionY = this.get("essentials.position.y");
const positionZ = this.get("essentials.position.z");
return Math.abs(positionX - x) <= radius && Math.abs(positionY - y) <= radius && Math.abs(positionZ - z) <= radius;
}
/**
* Get an list of closer players in radius.
* @param radius Margin radius to search for players
*/
closer(radius) {
const pitStatus = this.get("essentials.pit-status");
if (pitStatus !== "TRACK") return [];
const positionX = this.get("essentials.position.x");
const positionY = this.get("essentials.position.y");
const positionZ = this.get("essentials.position.z");
return this.akairo.players.array().filter((player) => {
const otherPitStatus = player.get("essentials.pit-status");
if (otherPitStatus !== "TRACK") return false;
const otherPositionX = player.get("essentials.position.x");
const otherPositionY = player.get("essentials.position.y");
const otherPositionZ = player.get("essentials.position.z");
return player.uniqueId !== this.uniqueId && player.playerId !== this.playerId && Math.abs(positionX - otherPositionX) <= radius && Math.abs(positionY - otherPositionY) <= radius && Math.abs(positionZ - otherPositionZ) <= radius;
});
}
/**
* Retrieves the value from the nested object structure based on the given path.
* @template T
* @param path The path to the desired value, using dot notation
*/
get(path) {
return (0, import_data.retrieve)(path, this.selfData);
}
/**
* Sets a value in the nested object structure based on the given path.
* @template T
* @param path The path where the value should be set, using dot notation
* @param value The value to set at the specified path
*/
set(path, value) {
this.selfData = (0, import_data.merge)(this.selfData, (0, import_data.objectify)(path, value));
return this;
}
/**
* Removes a specific key from the nested object structure based on the given path.
* @param {string} path The path to the key that should be removed, using dot notation
*/
remove(path) {
this.selfData = (0, import_data.remove)(this.selfData, path.split("."));
return this;
}
/**
* Clears all data from the Player instance.
*/
clear() {
this.selfData = {};
return this;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Player
});