UNPKG

@peacockproject/core

Version:

Type definitions for Peacock's core.

135 lines (134 loc) 5.14 kB
import type { ContractSession, GameVersion, UserProfile } from "./types/types"; import type * as nodeFs from "node:fs/promises"; import * as atomically from "atomically"; type NodeUnlinkMkdirReaddir = Pick<typeof nodeFs, "unlink" | "mkdir" | "readdir">; type AtomicReadWrite = Pick<typeof atomically, "readFile" | "writeFile">; type ExistsPromise = { exists: (path: string) => Promise<boolean>; }; /** * The fs implementation that this system uses. */ export type DataStorageFs = NodeUnlinkMkdirReaddir & ExistsPromise & AtomicReadWrite; /** * Handles the dispatching of user data in a way that avoids FS operations unless absolutely needed. */ declare class AsyncUserDataGuard { #private; /** * Get the fs implementation to use for file read/writes. * Mainly for test purposes, but could also be used by plugins to make it use a real database. */ getFs(): DataStorageFs; /** * Get a loaded user's profile. * @param id The target user ID. * @returns The profile, or undefined if they're not loaded. * Profiles are loaded when they perform the auth handshake via the game. */ getProfile(id: string): UserProfile | undefined; addLoadedProfile(id: string, profile: UserProfile): void; /** * Saves any modifications to a given profile, called by a background scheduled task. * @param id The user ID. */ save(id: string): void; markDirty(id: string): void; /** * Immediately write all loaded profiles to the disk, even if no changes are pending. */ forceFlush(): Promise<void>; /** * Unload all profiles without saving. */ unloadAll(): void; } /** * If you are touching this, you better know what you're doing. */ export declare const asyncGuard: AsyncUserDataGuard; /** * Gets a user's profile data. * * @param userId The user's ID. * @param gameVersion The game's version. * @returns The user's profile, OR UNDEFINED if not loaded. */ export declare function getUserData(userId: string, gameVersion: GameVersion): UserProfile; /** * Attempts to load a user's profile if it hasn't been loaded yet. * * @param userId The user's ID. * @param gameVersion The game's version. */ export declare function cheapLoadUserData(userId: string, gameVersion: GameVersion): Promise<void>; /** * Loads a user's profile data. * * @param userId The user's ID. * @param gameVersion The game's version. * @returns The raw JSON data. */ export declare function loadUserData(userId: string, gameVersion: GameVersion): Promise<UserProfile>; /** * Marks a user's profile as dirty. * Dirty profiles are automatically saved by a background thread every 3 seconds. * * @param userId The user's ID. * @param gameVersion The game's version. */ export declare function writeUserData(userId: string, gameVersion: GameVersion): void; /** * Writes a previously-non existent user profile. * This is used for creating new profiles. * * @param userId The user's ID. * @param userProfile * @param gameVersion The game's version. */ export declare function writeNewUserData(userId: string, userProfile: UserProfile, gameVersion: GameVersion): void; /** * Gets the value of an external provider binding. * * @param userId The user's ID. * @param externalFolder The folder where this provider's users are stored. * @param gameVersion The game's version. */ export declare function getExternalUserData(userId: string, externalFolder: string, gameVersion: GameVersion): Promise<string>; /** * Writes the value of an external provider binding. * * @param userId The user's ID. * @param externalFolder The folder where this provider's users are stored. * @param userData The data to write to the binding. * @param gameVersion The game's version. */ export declare function writeExternalUserData(userId: string, externalFolder: string, userData: string, gameVersion: GameVersion): Promise<void>; /** * Reads a contract session from the contractSessions folder. * * @param identifier The identifier for the saved session, in the format of token_sessionID. * @returns The contract session. */ export declare function getContractSession(identifier: string): Promise<ContractSession>; /** * Writes a contract session to the contractsSessions folder. * * @param identifier The identifier for the saved session, in the format of slot_token_sessionID. * @param session The contract session. */ export declare function writeContractSession(identifier: string, session: ContractSession): Promise<void>; /** * Deletes a saved contract session from the contractsSessions folder. * * @param fileName The identifier for the saved session, in the format of slot_token_sessionID. * @throws ENOENT if the file is not found. */ export declare function deleteContractSession(fileName: string): Promise<void>; /** * Sets up the required file structure for the server. * * @param joinFunc The path join function to use, defaulting to Node's. You may need to specify it if working in a VFS. */ export declare function setupFileStructure(joinFunc?: (...paths: string[]) => string): Promise<void>; export {};