lfs-akairo
Version:
LFS Akairo is a framework designed to simplify the creation of InSim applications.
223 lines (222 loc) • 9.72 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 akairo_exports = {};
__export(akairo_exports, {
Akairo: () => Akairo
});
module.exports = __toCommonJS(akairo_exports);
var import_event = require("#core/event");
var import_cars = require("#managers/cars");
var import_players = require("#managers/players");
var packageJson = __toESM(require("#package"));
var import_i18n = require("#utils/i18n");
var import_logger = require("#utils/logger");
var import_i18next = __toESM(require("i18next"));
var import_node_insim = require("node-insim");
var import_packets = require("node-insim/packets");
class Akairo {
/**
* Creates a new Akairo instance.
* @param settings Configuration settings for the Akairo instance
* @param settings.id Unique identifier for InSim
* @param settings.name Display name for InSim
* @param settings.prefix Command prefix for InSim
* @param settings.interval Update interval in milliseconds (used in IS_MCI updates)
* @param settings.interface Interface update interval in milliseconds (used in button interface updates)
* @param settings.flags InSim flags for connection configuration
* @param settings.filters.userNameLowerCase Player username will be stored in lower case
*/
constructor(settings) {
this.settings = settings;
var _a;
import_i18next.default.init({ ns: [], resources: {} });
this.insim = new import_node_insim.InSim((_a = this.settings) == null ? void 0 : _a.id);
this.players = new import_players.Players(this);
this.cars = new import_cars.Cars(this);
this.modules = /* @__PURE__ */ new Map();
this.locales = [];
this.insim.on("connect", () => this.onInSimConnect());
this.insim.on("disconnect", () => this.onInSimDisconnect());
}
/**
* Establishes connection to the game server.
* @param options - Connection configuration options
* @param options.host - Server hostname or IP address
* @param options.port - Server InSim port number
* @param options.password - Server admin password
*/
connect(options) {
var _a, _b, _c, _d;
this.options = options;
new import_event.Event(this, [
import_packets.PacketType.ISP_CNL,
import_packets.PacketType.ISP_PLP,
import_packets.PacketType.ISP_PLL
]);
for (const [, module2] of this.modules) {
module2.bind();
}
new import_event.Event(this, [
import_packets.PacketType.ISP_NCN,
import_packets.PacketType.ISP_NCI,
import_packets.PacketType.ISP_NPL,
import_packets.PacketType.ISP_TOC,
import_packets.PacketType.ISP_CPR,
import_packets.PacketType.ISP_MCI
]);
this.insim.connect({
Host: this.options.host,
Port: this.options.port,
Admin: this.options.password,
IName: (_a = this.settings) == null ? void 0 : _a.name,
Prefix: (_b = this.settings) == null ? void 0 : _b.prefix,
Interval: (_c = this.settings) == null ? void 0 : _c.interval,
Flags: (_d = this.settings) == null ? void 0 : _d.flags
});
}
/**
* Disconnects from the game server.
*/
disconnect() {
this.insim.disconnect();
}
/**
* Sets a handler for connection events.
* @param callback Function to be called when connection is established
*/
onConnect(callback) {
this.onConnectHandler = callback;
}
/**
* Sets a handler for disconnection events.
* @param callback Function to be called when connection is lost
*/
onDisconnect(callback) {
this.onDisconnectHandler = callback;
}
/**
* Get a module instantiated internally.
* @param module Module class to be get
*/
getModule(module2) {
return this.modules.get(module2);
}
/**
* Loads a module into the system.
* @param module Module class to be loaded
*/
loadModule(module2) {
this.modules.set(module2, new module2(this));
import_logger.logger.info(`Module "${module2.name}" was load.`);
}
/**
* Unloads a module from the system.
* @param module Module class to be unloaded
*/
unloadModule(module2) {
const found = this.modules.get(module2);
if (found) {
this.modules.delete(module2);
import_logger.logger.warn(`Module "${module2.name}" was unload.`);
} else {
import_logger.logger.error(`Module "${module2.name}" was not found.`);
}
}
/**
* Loads localization data for a specific language.
* @param language Language identifier
* @param content Localization content
* @template T Type of the localization content
*/
loadLocale(language, content) {
var _a;
const locale = (0, import_i18n.convertLanguage)(language);
const parsed = content;
const values = (_a = parsed == null ? void 0 : parsed.default) != null ? _a : parsed;
import_i18next.default.addResourceBundle(locale, locale, values);
this.locales.push({ language, content: values });
import_logger.logger.info(`Locale "${import_packets.Language[language]}" was load.`);
}
/**
* Unloads localization data for a specific language.
* @param language - Language identifier to unload
*/
unloadLocale(language) {
const index = this.locales.findIndex((l) => l.language === language);
if (index !== -1) {
this.locales.splice(index, 1);
import_logger.logger.warn(`Locale "${import_packets.Language[language]}" was unload.`);
} else {
import_logger.logger.error(`Locale "${import_packets.Language[language]}" was not found.`);
}
}
onInSimConnect() {
var _a, _b, _c;
const { host, port } = this.options;
const id = (_b = (_a = this.settings) == null ? void 0 : _a.id) != null ? _b : "-";
import_logger.logger.info(`InSim "${id}" was connected to: "${host}:${port}"`);
this.bindInSimReceptors();
(_c = this.onConnectHandler) == null ? void 0 : _c.call(this);
}
onInSimDisconnect() {
var _a;
import_logger.logger.warn("InSim was disconnected.");
(_a = this.onDisconnectHandler) == null ? void 0 : _a.call(this);
}
bindInSimReceptors() {
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_VER, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_NCN, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_NCI, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_NPL, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_NLP, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_MCI, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_SCP, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_SST, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_GTH, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_ISM, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_RES, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_REO, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_RST, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_AXI, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_RIP, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_ALC, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_AXM, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_SLC, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_MAL, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_PLH, ReqI: 2 }));
this.insim.send(new import_packets.IS_TINY({ SubT: import_packets.TinyType.TINY_IPB, ReqI: 2 }));
import_logger.logger.info(
`LFS Akairo v${packageJson.version} was successfully connected and working!`
);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Akairo
});