UNPKG

lfs-akairo

Version:

LFS Akairo is a framework designed to simplify the creation of InSim applications.

498 lines (490 loc) 17.1 kB
import { PacketType, InSimFlags, Language, MessageSound, ButtonStyle, ButtonTextColour } from 'node-insim/packets'; import { Locale } from './types/locale.js'; import { InSim } from 'node-insim'; import { Content, Data } from './types/player.js'; import { $Dictionary } from 'i18next/typescript/helpers'; declare class Players { readonly akairo: Akairo; /** * List of all active players * @readonly */ readonly list: Map<number, Player>; constructor(akairo: Akairo); /** * Retrieves player list as array. */ array(): Player[]; /** * Retrieves a player by their unique identifier. * @param uniqueId The unique identifier of the player */ getByUniqueId(uniqueId: number): Player; /** * Retrieves a player by their player ID. * @param playerId The player ID to search for */ getByPlayerId(playerId: number): Player; /** * Retrieves a player by their username (case-insensitive). * @param userName The username to search for */ getByUserName(userName: string): Player; /** * Retrieves a player by their player name (case-sensitive). * @param playerName The player name to search for */ getByPlayerName(playerName: string): Player; } type UnknownCommandCallback = (player: Player, command: string, args: string[]) => Promise<void>; declare class Module { readonly akairo: Akairo; private readonly intervals; private readonly events; private readonly commandHandlers; private readonly registeredCommands; private readonly unknownCommandHandlers; constructor(akairo: Akairo); /** * Registers a packet handler for a specific packet type. * @param type The type of packet to handle * @param callback Function to be called when packet is received */ onPacket<T>(type: PacketType, callback: (player: Player, packet: T) => Promise<void>): void; /** * Registers a packet handler for a specific packet type. * @param type The type of packet to handle * @param callback Function to be called when packet is received */ onPacket(type: PacketType.ISP_MCI, callback: (players: Player[], packet: PacketType.ISP_MCI) => Promise<void>): void; /** * Registers a periodic task to be executed at specified intervals. * @param interval Time in milliseconds between executions * @param callback Function to be executed periodically */ onTick(interval: number, callback: (players: Players) => Promise<void>): void; /** * Registers a command handler for one or more commands. * @param commands Command or array of commands to handle * @param callback Function to be called when command is received */ onCommand(commands: string | string[], callback: (player: Player, args: string[]) => Promise<void>): void; /** * Registers a handler for unknown commands. * @param callback Function to be called when an unknown command is received */ onUnknownCommand(callback: UnknownCommandCallback): void; /** * Bind all packets inside this module through InSim */ bind(): void; /** * Unbind all packets insidee this module through InSim */ unbind(): void; private handleMessage; private parseMessage; private handleKnownCommand; private handleUnknownCommand; private getPlayerFromPacket; } declare class Cars { readonly akairo: Akairo; constructor(akairo: Akairo); /** * Moves the player's car to specific position. * @param {Player} player The player whose car will be moved * @param coordinates Coordinates of player car (must be parsed) * @param coordinates.x X position of player car * @param coordinates.y Y position of player car * @param coordinates.z Z position of player car * @param coordinates.heading Heading angle of player car */ movePlayerCar(player: Player, coordinates: { x: number; y: number; z: number; heading: number; }): void; /** * Resets the player's car to its current position. * @param {Player} player The player whose car will be reset */ resetPlayerCar(player: Player): void; /** * Teleports a player's car to another player's location, with an optional distance offset. * @param {Player} from The player whose car will be teleported * @param {Player} to The player to whom the car will be teleported * @param {number} [distance=4] The distance offset in meters */ teleportPlayerCar(from: Player, to: Player, distance?: number): void; /** * Retrieves the coordinates and heading of a player's car. * @param {Player} player The player whose coordinates will be retrieved */ getPlayerCarCoordinates(player: Player): { x: number; y: number; z: number; heading: number; }; /** * Parse and converts player car coordinates. * @param coordinates Coordinates of player car * @param coordinates.x X position of player car * @param coordinates.y Y position of player car * @param coordinates.z Z position of player car * @param coordinates.heading Heading angle of player car */ parseCoordinates(coordinates: { x: number; y: number; z: number; heading: number; }): { x: number; y: number; z: number; heading: number; }; } type ModuleConstructor<T extends Module> = new (akairo: Akairo) => T; declare class Akairo { readonly settings?: { id?: string; name?: string; prefix?: string; interval?: number; interface?: number; flags?: InSimFlags; filters?: { userNameLowerCase?: boolean; }; } | undefined; /** InSim instance for server communication */ insim: InSim; /** List of connected players */ players: Players; /** Utils functions related to players cars */ cars: Cars; /** List of loaded modules */ modules: Map<ModuleConstructor<Module>, Module>; /** List of loaded localization data */ locales: Locale<any>[]; private onConnectHandler; private onDisconnectHandler; private options; /** * 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?: { id?: string; name?: string; prefix?: string; interval?: number; interface?: number; flags?: InSimFlags; filters?: { userNameLowerCase?: boolean; }; } | undefined); /** * 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: { host: string; port: number; password: string; }): void; /** * Disconnects from the game server. */ disconnect(): void; /** * Sets a handler for connection events. * @param callback Function to be called when connection is established */ onConnect(callback: () => void): void; /** * Sets a handler for disconnection events. * @param callback Function to be called when connection is lost */ onDisconnect(callback: () => void): void; /** * Get a module instantiated internally. * @param module Module class to be get */ getModule<T extends Module>(module: ModuleConstructor<T>): T; /** * Loads a module into the system. * @param module Module class to be loaded */ loadModule<T extends Module>(module: ModuleConstructor<T>): void; /** * Unloads a module from the system. * @param module Module class to be unloaded */ unloadModule<T extends Module>(module: ModuleConstructor<T>): void; /** * Loads localization data for a specific language. * @param language Language identifier * @param content Localization content * @template T Type of the localization content */ loadLocale<T>(language: Language, content: T): void; /** * Unloads localization data for a specific language. * @param language - Language identifier to unload */ unloadLocale(language: Language): void; private onInSimConnect; private onInSimDisconnect; private bindInSimReceptors; } declare class Player { readonly akairo: Akairo; /** Unique id inside server (usually it is used in almost all of cases) */ uniqueId: number; /** Car id inside server (usually it is used in packets that handles car's) */ playerId: number; /** Account username (unique in entire LFS) */ userName: string; /** Name visible inside game and servers */ playerName: string; /** Determines if player is using admin password */ isAdministrator: boolean; /** Game language */ gameLanguage: Language; /** Determines if player is fully assigned inside Akairo (useful in IS_NCI packet) */ isReady: boolean; /** Attributes saved in player instance */ selfData: {}; /** Button instantiated list */ buttons: Map<number, Button>; constructor(akairo: Akairo); /** * Translate an content to player. * @param scope Translation scope * @param options Translation scope options */ t(path: string, scope?: $Dictionary): string; /** * Send a message to player. * @param content Content to be sent (it can be translated) * @param sound Sound of message */ message<T>(content: T, sound?: MessageSound): Player; /** * Create an button instance to player. */ button(): Button; /** * Send player to pitlane. */ pitlane(): Player; /** * Send player to spectator. */ spectate(): Player; /** * Nove player from spectator to track. */ ujoin(): Player; /** * Set player vehicle added mass. */ mass(percentage: number): Player; /** * Set player vehicle intake restriction (0% - 50%). */ restriction(percentage: number): Player; /** * Toggle player available siren. */ siren(enabled: boolean): Player; /** * 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: number, y: number, z: number, radius: number): boolean; /** * Get an list of closer players in radius. * @param radius Margin radius to search for players */ closer(radius: number): Player[]; /** * 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<K extends Content>(path: K): K extends keyof Data ? Data[K] : any; /** * 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<K extends Content>(path: K, value: K extends keyof Data ? Data[K] : any): Player; /** * 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<K extends Content>(path: K): Player; /** * Clears all data from the Player instance. */ clear(): Player; } declare class Button { readonly player: Player; private static autoUpdateButtons; private static autoUpdateInterval; /** Unique identifier for the button */ id: () => number; /** Function that returns the button's style */ style: () => ButtonStyle | ButtonTextColour; /** Function that returns the button's title */ title: () => string; /** Function that returns the button's caption */ caption: () => string; /** Function that returns the length of the input field */ length: () => number; /** Function that returns the button's width */ width: () => number; /** Function that returns the button's height */ height: () => number; /** Function that returns the button's left position */ left: () => number; /** Function that returns the button's top position */ top: () => number; /** Function that determines if the button should only be clicked once */ clickOnce: () => boolean; /** Function that determines if the button is visible */ isVisible: () => boolean; /** Reference to the parent button if this is a child button */ parent: Button; /** Array of child buttons */ childs: Button[]; /** Function to unbind event listeners */ unbind: () => void; /** Interval for automatic updates */ interval: NodeJS.Timeout; /** Handler for update events */ private onUpdateHandler; constructor(player: Player); /** * Sets the button's id * @param id Button id */ setId(id: () => number): this; /** * Sets the button's style * @param style Function that returns the button style */ setStyle(style: () => ButtonStyle | ButtonTextColour): this; /** * Sets the button's title * @param title Function that returns the title string */ setTitle(title: () => string): this; /** * Sets the button's textbox title * @param caption Function that returns the textbox title string */ setCaption(caption: () => string): this; /** * Sets the input field length * @param length Function that returns the length value */ setLength(length: () => number): this; /** * Sets the button's width * @param width Function that returns the width value */ setWidth(width: () => number): this; /** * Sets the button's height * @param height Function that returns the height value */ setHeight(height: () => number): this; /** * Sets the button's left position * @param left Function that returns the left position value */ setLeft(left: () => number): this; /** * Sets the button's top position * @param top Function that returns the top position value */ setTop(top: () => number): this; /** * Sets whether the button should only be clicked once * @param clickOnce Function that returns the clickOnce boolean */ setClickOnce(clickOnce: () => boolean): this; /** * Sets the button's visibility * @param isVisible Function that returns the visibility boolean */ setIsVisible(isVisible: () => boolean): this; /** * Adds a click event handler to the button * @param callback Function to be called when the button is clicked */ onClick(callback: ({ text, button, unbind, }: { text: string; button: Button; unbind: () => void; }) => void): Button; /** * Appends a child button to this button * @param callback A single button callback (already instantiated) * @param disableAutoUpdate Whether to disable automatic state updates */ append(callback: (button: Button) => Button, disableAutoUpdate?: boolean): this; /** * Removes a child button * @param button The button to remove */ remove(button: Button): this; /** * Creates the button and starts auto-update if enabled * @param disableAutoUpdate Whether to disable automatic state updates */ create(disableAutoUpdate?: boolean): Button; /** * Updates the button's state and appearance */ update(): Button; /** * Destroys the button, removing it from display */ destroy(): Button; /** * Sets a handler for update events * @param callback Function to be called when the button is updated */ onUpdate(callback: (button: Button) => void): void; private manageAutoUpdate; private queueUpdate; private updateChildren; private destroyAllChildren; private cleanupProperties; private resetProperties; } export { Akairo as A, Button as B, Cars as C, Module as M, Players as P, Player as a };