@aifivem/lib 0.0.0-stage → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +34 -4
- package/src/client/action.ts +38 -0
- package/src/client/adminWorld.ts +95 -0
- package/src/client/appearance.ts +29 -0
- package/src/client/areaFollower.ts +41 -0
- package/src/client/blip.ts +79 -0
- package/src/client/cache.ts +66 -0
- package/src/client/callback.ts +70 -0
- package/src/client/controls.ts +73 -0
- package/src/client/entities.ts +83 -0
- package/src/client/entity.ts +26 -0
- package/src/client/fuel.ts +18 -0
- package/src/client/gestures.ts +18 -0
- package/src/client/headshot.ts +26 -0
- package/src/client/hud.ts +24 -0
- package/src/client/index.ts +131 -0
- package/src/client/inventory.ts +117 -0
- package/src/client/keyHints.ts +13 -0
- package/src/client/keybind.ts +163 -0
- package/src/client/localPed.ts +169 -0
- package/src/client/location.ts +54 -0
- package/src/client/mdt.ts +10 -0
- package/src/client/medical.ts +28 -0
- package/src/client/membership.ts +41 -0
- package/src/client/menu/dialogs.ts +77 -0
- package/src/client/menu/minigames.ts +79 -0
- package/src/client/menu/requests.ts +65 -0
- package/src/client/modelScan/def.ts +29 -0
- package/src/client/modelScan/objectIndex.ts +41 -0
- package/src/client/modelScan/scan.ts +69 -0
- package/src/client/modelScan/scanner.ts +164 -0
- package/src/client/netEvent.ts +40 -0
- package/src/client/notify.ts +14 -0
- package/src/client/nuiScreen.ts +87 -0
- package/src/client/player/anim.ts +98 -0
- package/src/client/player/fade.ts +17 -0
- package/src/client/player/player.ts +80 -0
- package/src/client/player/teleport.ts +87 -0
- package/src/client/playerData.ts +83 -0
- package/src/client/players.ts +44 -0
- package/src/client/points/interact.ts +144 -0
- package/src/client/points/modelHost.ts +117 -0
- package/src/client/points/modelPoint.ts +181 -0
- package/src/client/points/point.ts +159 -0
- package/src/client/points/points.ts +170 -0
- package/src/client/points/targets/adapter.ts +40 -0
- package/src/client/points/targets/interactTarget.ts +45 -0
- package/src/client/points/targets/optionShape.ts +22 -0
- package/src/client/points/targets/oxTarget.ts +42 -0
- package/src/client/points/targets/qbTarget.ts +71 -0
- package/src/client/points/targets/targets.ts +20 -0
- package/src/client/preview.ts +51 -0
- package/src/client/progress.ts +132 -0
- package/src/client/prompt.ts +26 -0
- package/src/client/prop.ts +79 -0
- package/src/client/radial.ts +16 -0
- package/src/client/raycast.ts +48 -0
- package/src/client/scriptCamera.ts +167 -0
- package/src/client/seatbelt.ts +18 -0
- package/src/client/shot.ts +29 -0
- package/src/client/streaming.ts +91 -0
- package/src/client/tickLoop.ts +18 -0
- package/src/client/vehicleKeys.ts +14 -0
- package/src/client/vehicleProps.ts +107 -0
- package/src/client/vehicles.ts +46 -0
- package/src/client/waypoint.ts +10 -0
- package/src/client/world.ts +10 -0
- package/src/client/zoneEngine.ts +174 -0
- package/src/client/zones.ts +161 -0
- package/src/fivem/byResource.ts +146 -0
- package/src/fivem/callbackNames.ts +12 -0
- package/src/fivem/exports.ts +70 -0
- package/src/fivem/index.ts +13 -0
- package/src/fivem/locale.ts +57 -0
- package/src/fivem/playerData.ts +13 -0
- package/src/fivem/scheduler/every.ts +101 -0
- package/src/fivem/scheduler/group.ts +97 -0
- package/src/fivem/scheduler/scheduler.ts +77 -0
- package/src/fivem/state/entityState.ts +75 -0
- package/src/fivem/state/globalState.ts +80 -0
- package/src/fivem/state/mirror.ts +47 -0
- package/src/fivem/state/playerState.ts +60 -0
- package/src/server/admin/descriptions.ts +57 -0
- package/src/server/admin/edit.ts +127 -0
- package/src/server/admin/jsonSchema.ts +156 -0
- package/src/server/admin/panel.ts +167 -0
- package/src/server/admin/panelFiles.ts +128 -0
- package/src/server/admin/patch.ts +46 -0
- package/src/server/admin/validate.ts +147 -0
- package/src/server/appearance.ts +28 -0
- package/src/server/areaStream/areaStream.ts +141 -0
- package/src/server/areaStream/followers.ts +154 -0
- package/src/server/areaStream/grid.ts +74 -0
- package/src/server/areaStream/options.ts +16 -0
- package/src/server/banking.ts +15 -0
- package/src/server/bans.ts +47 -0
- package/src/server/billing.ts +20 -0
- package/src/server/buckets.ts +12 -0
- package/src/server/callback.ts +112 -0
- package/src/server/characters.ts +129 -0
- package/src/server/command/addCommand.ts +83 -0
- package/src/server/command/command.ts +116 -0
- package/src/server/command/commandSet.ts +74 -0
- package/src/server/dataFile.ts +81 -0
- package/src/server/database.ts +117 -0
- package/src/server/definitions.ts +65 -0
- package/src/server/dispatch.ts +13 -0
- package/src/server/doors.ts +15 -0
- package/src/server/ems.ts +15 -0
- package/src/server/fuel.ts +15 -0
- package/src/server/garages.ts +34 -0
- package/src/server/gate.ts +148 -0
- package/src/server/index.ts +165 -0
- package/src/server/inventory.ts +106 -0
- package/src/server/jobs.ts +42 -0
- package/src/server/log.ts +21 -0
- package/src/server/mdt.ts +12 -0
- package/src/server/medical.ts +16 -0
- package/src/server/minigameFor.ts +65 -0
- package/src/server/money.ts +70 -0
- package/src/server/netEntity.ts +19 -0
- package/src/server/netEvent.ts +58 -0
- package/src/server/notify.ts +17 -0
- package/src/server/online/mirror.ts +176 -0
- package/src/server/online/online.ts +190 -0
- package/src/server/pending.ts +70 -0
- package/src/server/phone.ts +22 -0
- package/src/server/players.ts +197 -0
- package/src/server/position.ts +31 -0
- package/src/server/primeTime.ts +7 -0
- package/src/server/prison.ts +15 -0
- package/src/server/progressFor.ts +62 -0
- package/src/server/proximity.ts +98 -0
- package/src/server/rateLimit.ts +53 -0
- package/src/server/state.ts +45 -0
- package/src/server/stress.ts +25 -0
- package/src/server/vehicle/nearby.ts +66 -0
- package/src/server/vehicle/plate.ts +14 -0
- package/src/server/vehicle/spawn.ts +125 -0
- package/src/server/vehicle/vehicleType.ts +50 -0
- package/src/server/vehicleKeys.ts +23 -0
- package/src/server/watchers.ts +46 -0
- package/src/server/world.ts +16 -0
- package/src/shared/access.ts +94 -0
- package/src/shared/action.ts +28 -0
- package/src/shared/blipStyle.ts +26 -0
- package/src/shared/clock.ts +22 -0
- package/src/shared/format.ts +36 -0
- package/src/shared/grid.ts +20 -0
- package/src/shared/index.ts +179 -0
- package/src/shared/lang.ts +39 -0
- package/src/shared/placePed.ts +96 -0
- package/src/shared/random.ts +11 -0
- package/src/shared/read.ts +60 -0
- package/src/shared/records.ts +148 -0
- package/src/shared/spec.ts +20 -0
- package/src/shared/strict.ts +70 -0
- package/src/shared/vec.ts +48 -0
- package/src/shared/vehicleProps.ts +138 -0
- package/src/shared/wait.ts +27 -0
- package/src/shared/zone.ts +144 -0
- package/README.md +0 -3
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE,
|
|
3
|
+
type CarryItem,
|
|
4
|
+
type InventoryRef,
|
|
5
|
+
type InventoryStack,
|
|
6
|
+
type ItemDefinition,
|
|
7
|
+
type ItemMetadata,
|
|
8
|
+
type ShopProduct,
|
|
9
|
+
type UsableItemHandler,
|
|
10
|
+
} from "@aifivem/shared";
|
|
11
|
+
import { resourceExport } from "../fivem/exports";
|
|
12
|
+
|
|
13
|
+
const core = resourceExport(CORE.resource);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Items through ai-core's inventory layer: the active inventory (ai-inventory, or another the panel picks) answers.
|
|
17
|
+
* `inv` is a player server id or another inventory's id (stash name, plate). Calls fail (false / 0 / empty) while
|
|
18
|
+
* no inventory runs.
|
|
19
|
+
*/
|
|
20
|
+
export const inventory = {
|
|
21
|
+
/** Gives items; false when they do not fit or the item does not exist. */
|
|
22
|
+
addItem: (
|
|
23
|
+
inv: InventoryRef,
|
|
24
|
+
item: string,
|
|
25
|
+
count = 1,
|
|
26
|
+
metadata?: ItemMetadata,
|
|
27
|
+
slot?: number,
|
|
28
|
+
): boolean => core.AddItem(inv, item, count, metadata, slot),
|
|
29
|
+
/** Takes items; false when there are not enough. */
|
|
30
|
+
removeItem: (
|
|
31
|
+
inv: InventoryRef,
|
|
32
|
+
item: string,
|
|
33
|
+
count = 1,
|
|
34
|
+
metadata?: ItemMetadata,
|
|
35
|
+
slot?: number,
|
|
36
|
+
): boolean => core.RemoveItem(inv, item, count, metadata, slot),
|
|
37
|
+
/** How many of an item an inventory holds (stacks with matching metadata only when given). */
|
|
38
|
+
getItemCount: (
|
|
39
|
+
inv: InventoryRef,
|
|
40
|
+
item: string,
|
|
41
|
+
metadata?: ItemMetadata,
|
|
42
|
+
): number => core.GetItemCount(inv, item, metadata),
|
|
43
|
+
/** Whether an inventory holds at least `count` of an item. */
|
|
44
|
+
hasItem: (inv: InventoryRef, item: string, count = 1): boolean =>
|
|
45
|
+
core.HasItem(inv, item, count),
|
|
46
|
+
/** Whether the items would fit (weight and space). */
|
|
47
|
+
canCarryItem: (
|
|
48
|
+
inv: InventoryRef,
|
|
49
|
+
item: string,
|
|
50
|
+
count = 1,
|
|
51
|
+
metadata?: ItemMetadata,
|
|
52
|
+
): boolean => core.CanCarryItem(inv, item, count, metadata),
|
|
53
|
+
/** Whether all the items fit together (one trade, one reward): weight and space shared. */
|
|
54
|
+
canCarryItems: (inv: InventoryRef, items: CarryItem[]): boolean =>
|
|
55
|
+
core.CanCarryItems(inv, items),
|
|
56
|
+
/** Every stack of an inventory with its item label. */
|
|
57
|
+
getItems: (inv: InventoryRef): InventoryStack[] => core.GetItems(inv),
|
|
58
|
+
/** Empties an inventory. */
|
|
59
|
+
clearInventory: (inv: InventoryRef): void => core.ClearInventory(inv),
|
|
60
|
+
/** Registers a stash (`jobs` empty: everyone may open it); ai-core registers it again with any inventory that starts later. */
|
|
61
|
+
registerStash: (
|
|
62
|
+
name: string,
|
|
63
|
+
label: string,
|
|
64
|
+
slots: number,
|
|
65
|
+
maxWeight: number,
|
|
66
|
+
jobs: string[] = [],
|
|
67
|
+
): void => core.RegisterStash(name, label, slots, maxWeight, jobs),
|
|
68
|
+
/** Opens a stash or another player's inventory on the player's screen, without range checks (check before). */
|
|
69
|
+
openInventory: (source: number, inv: InventoryRef): boolean =>
|
|
70
|
+
core.OpenInventory(source, inv),
|
|
71
|
+
/** Runs `handler` when a player uses the item; register once at start, ai-core keeps it across inventory restarts. */
|
|
72
|
+
registerUsableItem: (name: string, handler: UsableItemHandler): void =>
|
|
73
|
+
core.RegisterUsableItem(name, handler),
|
|
74
|
+
/** Registers a shop (`{ name, price }` products); ai-core registers it again with any inventory that starts later. */
|
|
75
|
+
registerShop: (name: string, label: string, items: ShopProduct[]): void =>
|
|
76
|
+
core.RegisterShop(name, label, items),
|
|
77
|
+
/** Opens a registered shop on the player's screen, without range or access checks (check before). */
|
|
78
|
+
openShop: (source: number, name: string): boolean =>
|
|
79
|
+
core.OpenShop(source, name),
|
|
80
|
+
/** An item's label, weight and flags; null when the inventory does not know it. */
|
|
81
|
+
getItemDefinition: (name: string): ItemDefinition | null =>
|
|
82
|
+
core.GetItemDefinition(name),
|
|
83
|
+
/** An inventory's stacks by slot. */
|
|
84
|
+
itemsBySlot: (inv: InventoryRef): Record<number, InventoryStack> => {
|
|
85
|
+
const out: Record<number, InventoryStack> = {};
|
|
86
|
+
for (const stack of core.GetItems(inv) ?? []) out[stack.slot] = stack;
|
|
87
|
+
return out;
|
|
88
|
+
},
|
|
89
|
+
/** The first stack of an item in an inventory, or null. */
|
|
90
|
+
firstStack: (inv: InventoryRef, item: string): InventoryStack | null =>
|
|
91
|
+
(core.GetItems(inv) ?? []).find((stack) => stack.name === item) ?? null,
|
|
92
|
+
/** Every item definition of the active inventory, by name. */
|
|
93
|
+
getItemDefinitions: (): Record<string, ItemDefinition> =>
|
|
94
|
+
core.GetItemDefinitions() ?? {},
|
|
95
|
+
/** Replaces the metadata of the stack at `slot` (ammo, components, durability); false when the inventory could not. */
|
|
96
|
+
setItemMetadata: (
|
|
97
|
+
inv: InventoryRef,
|
|
98
|
+
slot: number,
|
|
99
|
+
metadata: ItemMetadata,
|
|
100
|
+
): boolean => core.SetItemMetadata(inv, slot, metadata),
|
|
101
|
+
/** Sets a player's weight limit in grams until the inventory reloads it; false when the inventory has no way. */
|
|
102
|
+
setMaxWeight: (source: number, grams: number): boolean =>
|
|
103
|
+
core.SetMaxWeight(source, grams),
|
|
104
|
+
/** Names of every item a usable handler is registered for. */
|
|
105
|
+
usableItems: (): string[] => core.GetUsableItems(),
|
|
106
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE,
|
|
3
|
+
type GangMembership,
|
|
4
|
+
type JobMembership,
|
|
5
|
+
} from "@aifivem/shared";
|
|
6
|
+
import { resourceExport } from "../fivem/exports";
|
|
7
|
+
|
|
8
|
+
const core = resourceExport(CORE.resource);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Jobs and gangs of a loaded player through ai-core (several per character, one on duty, one active gang). Reads for
|
|
12
|
+
* many players at once are lib `online` (a mirror), this is for the player an action is about.
|
|
13
|
+
*/
|
|
14
|
+
export const jobs = {
|
|
15
|
+
/** The job other scripts see as "the" job: on duty, else last on duty, else the first, else unemployed. */
|
|
16
|
+
current: (source: number): JobMembership | null => core.GetCurrentJob(source),
|
|
17
|
+
/** Every job of the player. */
|
|
18
|
+
list: (source: number): JobMembership[] => core.GetJobs(source) ?? [],
|
|
19
|
+
/** The job the player is on duty in, or null. */
|
|
20
|
+
duty: (source: number): string | null => core.GetDutyJob(source),
|
|
21
|
+
/** On duty in `job` (one of theirs), or off duty with null; false when refused. */
|
|
22
|
+
setDuty: (source: number, job: string | null): boolean =>
|
|
23
|
+
core.SetDuty(source, job),
|
|
24
|
+
/** Adds or regrades a job under the panel's job rules (`maxJobs`, `multiJob`). */
|
|
25
|
+
set: (source: number, job: string, grade: number): boolean =>
|
|
26
|
+
core.SetJob(source, job, grade),
|
|
27
|
+
remove: (source: number, job: string): boolean => core.RemoveJob(source, job),
|
|
28
|
+
/** The job of a character without one (the panel's setting). */
|
|
29
|
+
unemployed: (): string => core.GetUnemployedJob(),
|
|
30
|
+
/** How many players are on duty in a job. */
|
|
31
|
+
onDutyCount: (job: string): number => core.GetOnDutyCount(job),
|
|
32
|
+
/** Every gang of the player. */
|
|
33
|
+
gangs: (source: number): GangMembership[] => core.GetGangs(source) ?? [],
|
|
34
|
+
/** The player's active gang, or null. */
|
|
35
|
+
activeGang: (source: number): string | null => core.GetActiveGang(source),
|
|
36
|
+
setActiveGang: (source: number, gang: string | null): boolean =>
|
|
37
|
+
core.SetActiveGang(source, gang),
|
|
38
|
+
setGang: (source: number, gang: string, grade: number): boolean =>
|
|
39
|
+
core.SetGang(source, gang, grade),
|
|
40
|
+
removeGang: (source: number, gang: string): boolean =>
|
|
41
|
+
core.RemoveGang(source, gang),
|
|
42
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { LogChannel, LogOptions } from "@aifivem/shared";
|
|
2
|
+
import { optionalExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a log through ai-core's log system (Discord, Fivemanage, FiveM Stats, console: whatever its panel turns on).
|
|
6
|
+
* Does nothing while ai-core is not running.
|
|
7
|
+
*
|
|
8
|
+
* aiLog("purchase", `bought ${item} for ${price}`, { source, metadata: { item, price } });
|
|
9
|
+
*/
|
|
10
|
+
export function aiLog(
|
|
11
|
+
channel: string,
|
|
12
|
+
message: string,
|
|
13
|
+
options?: LogOptions,
|
|
14
|
+
): void {
|
|
15
|
+
optionalExport("ai-core")?.Log(channel, message, options);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Describes this resource's log channels (dataset names, on/off); call once at start. */
|
|
19
|
+
export function aiLogChannels(channels: Record<string, LogChannel>): void {
|
|
20
|
+
optionalExport("ai-core")?.LogChannels(channels);
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CORE, type MdtRecord } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/** Police MDT records (fines, warrants, arrests, jail) through ai-core's MDT layer; nothing is written while no MDT runs. */
|
|
7
|
+
export const mdt = {
|
|
8
|
+
/** Writes a record; false when no MDT runs or it takes no such record. */
|
|
9
|
+
record: (record: MdtRecord): boolean => core.AddMdtRecord(record),
|
|
10
|
+
/** The MDT records go to, or null. */
|
|
11
|
+
provider: (): string | null => core.GetMdtSystem(),
|
|
12
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CORE } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/** Revive, heal and the down state through ai-core's medical layer, whatever death / ambulance script runs. */
|
|
7
|
+
export const medical = {
|
|
8
|
+
/** Gets a downed player up where they lie; false when not down or no death script runs. */
|
|
9
|
+
revive: (source: number): boolean => core.RevivePlayer(source),
|
|
10
|
+
/** Full health for a player who is not down; false when no death script runs. */
|
|
11
|
+
heal: (source: number): boolean => core.HealPlayer(source),
|
|
12
|
+
/** Whether the player is down (false without a death script). */
|
|
13
|
+
isDead: (source: number): boolean => core.IsPlayerDead(source),
|
|
14
|
+
/** The death script that answers (ai-death...), or null when none runs. */
|
|
15
|
+
provider: (): string | null => core.GetMedicalSystem(),
|
|
16
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LIB_UI,
|
|
3
|
+
LOCKPICK_AREAS,
|
|
4
|
+
SKILL_STAGES,
|
|
5
|
+
type MinigameRequest,
|
|
6
|
+
} from "@aifivem/shared";
|
|
7
|
+
import { triggerClientCallback } from "./callback";
|
|
8
|
+
|
|
9
|
+
export type MinigameOutcome = "won" | "lost" | "broken";
|
|
10
|
+
|
|
11
|
+
export interface MinigameForOptions {
|
|
12
|
+
game: "skillCheck" | "lockpick";
|
|
13
|
+
difficulty?: "easy" | "medium" | "hard";
|
|
14
|
+
/** Skill check rings (default 2). */
|
|
15
|
+
rounds?: number;
|
|
16
|
+
/** Skill check keys (default E). */
|
|
17
|
+
keys?: string[];
|
|
18
|
+
/** Lockpick pins (default 1) and picks the player may break (default 1). */
|
|
19
|
+
pins?: number;
|
|
20
|
+
picks?: number;
|
|
21
|
+
/** A win answered sooner than this (ms after the request) counts as lost: the client skipped the game. */
|
|
22
|
+
minMs?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** How long a player may play before the answer counts as lost. */
|
|
26
|
+
const TIMEOUT_MS = 120_000;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Plays ai-lib's minigame on the player's client for a server flow: "won", "lost" (missed, gave up, no answer) or
|
|
30
|
+
* "broken" (the lockpick's last pick broke: take the item). The answer is the client's word, so the server still
|
|
31
|
+
* decides what a win gives (a chance roll, `progressFor`, its own checks). `minMs` is the floor for a win: the
|
|
32
|
+
* game's fastest real play (skill check: its green arc starts at 100 degrees, at the stage's speed).
|
|
33
|
+
*/
|
|
34
|
+
export async function minigameFor(
|
|
35
|
+
source: number,
|
|
36
|
+
options: MinigameForOptions,
|
|
37
|
+
): Promise<MinigameOutcome> {
|
|
38
|
+
const level = options.difficulty ?? "medium";
|
|
39
|
+
const request: MinigameRequest =
|
|
40
|
+
options.game === "lockpick"
|
|
41
|
+
? {
|
|
42
|
+
kind: "lockpick",
|
|
43
|
+
pins: Math.min(6, Math.max(1, Math.floor(options.pins ?? 1))),
|
|
44
|
+
area: LOCKPICK_AREAS[level],
|
|
45
|
+
picks: Math.min(10, Math.max(1, Math.floor(options.picks ?? 1))),
|
|
46
|
+
}
|
|
47
|
+
: {
|
|
48
|
+
kind: "skillCheck",
|
|
49
|
+
stages: Array.from(
|
|
50
|
+
{ length: Math.min(10, Math.max(1, options.rounds ?? 2)) },
|
|
51
|
+
() => SKILL_STAGES[level],
|
|
52
|
+
),
|
|
53
|
+
keys: options.keys ?? ["e"],
|
|
54
|
+
};
|
|
55
|
+
const startedAt = GetGameTimer();
|
|
56
|
+
const answer = await triggerClientCallback(
|
|
57
|
+
LIB_UI.minigameForCallback,
|
|
58
|
+
source,
|
|
59
|
+
[request],
|
|
60
|
+
TIMEOUT_MS,
|
|
61
|
+
).catch(() => null);
|
|
62
|
+
if (answer === true || answer === "opened")
|
|
63
|
+
return GetGameTimer() - startedAt < (options.minMs ?? 0) ? "lost" : "won";
|
|
64
|
+
return answer === "broken" ? "broken" : "lost";
|
|
65
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE,
|
|
3
|
+
type LedgerPage,
|
|
4
|
+
type LedgerQuery,
|
|
5
|
+
type LedgerRef,
|
|
6
|
+
type MoneyRef,
|
|
7
|
+
} from "@aifivem/shared";
|
|
8
|
+
import { resourceExport } from "../fivem/exports";
|
|
9
|
+
|
|
10
|
+
const core = resourceExport(CORE.resource);
|
|
11
|
+
type GroupType = "job" | "gang";
|
|
12
|
+
|
|
13
|
+
/** Money through ai-core: a loaded player's accounts, job / gang accounts, transfers and the ledger. */
|
|
14
|
+
export const money = {
|
|
15
|
+
/** A loaded player's balance of an account (`cash`, `bank`...); null when not loaded or no such account. */
|
|
16
|
+
get: (source: number, account: string): number | null =>
|
|
17
|
+
core.GetMoney(source, account),
|
|
18
|
+
/** Adds a whole amount with a ledger row (`reason` defaults to the calling resource); false when refused. */
|
|
19
|
+
add: (
|
|
20
|
+
source: number,
|
|
21
|
+
account: string,
|
|
22
|
+
amount: number,
|
|
23
|
+
reason?: string,
|
|
24
|
+
): boolean => core.AddMoney(source, account, amount, reason),
|
|
25
|
+
/** Takes a whole amount; false when the account cannot pay (or the player is not loaded). */
|
|
26
|
+
remove: (
|
|
27
|
+
source: number,
|
|
28
|
+
account: string,
|
|
29
|
+
amount: number,
|
|
30
|
+
reason?: string,
|
|
31
|
+
): boolean => core.RemoveMoney(source, account, amount, reason),
|
|
32
|
+
set: (
|
|
33
|
+
source: number,
|
|
34
|
+
account: string,
|
|
35
|
+
amount: number,
|
|
36
|
+
reason?: string,
|
|
37
|
+
): boolean => core.SetMoney(source, account, amount, reason),
|
|
38
|
+
/** A job's or gang's account balance (`bank` is the society money); null when it has none. */
|
|
39
|
+
sharedGet: (type: GroupType, id: string, account: string): number | null =>
|
|
40
|
+
core.GetSharedMoney(type, id, account),
|
|
41
|
+
sharedAdd: (
|
|
42
|
+
type: GroupType,
|
|
43
|
+
id: string,
|
|
44
|
+
account: string,
|
|
45
|
+
amount: number,
|
|
46
|
+
reason?: string,
|
|
47
|
+
): boolean => core.AddSharedMoney(type, id, account, amount, reason),
|
|
48
|
+
sharedRemove: (
|
|
49
|
+
type: GroupType,
|
|
50
|
+
id: string,
|
|
51
|
+
account: string,
|
|
52
|
+
amount: number,
|
|
53
|
+
reason?: string,
|
|
54
|
+
): boolean => core.RemoveSharedMoney(type, id, account, amount, reason),
|
|
55
|
+
/**
|
|
56
|
+
* Moves `amount` from one account to another (a player's `{ source, account }` or a job's / gang's
|
|
57
|
+
* `{ owner, id, account }`) in one transaction with both ledger rows; `from` also pays `fee`. False when an
|
|
58
|
+
* account is missing, both are the same or `from` cannot pay.
|
|
59
|
+
*/
|
|
60
|
+
transfer: (
|
|
61
|
+
from: MoneyRef,
|
|
62
|
+
to: MoneyRef,
|
|
63
|
+
amount: number,
|
|
64
|
+
reason?: string,
|
|
65
|
+
fee?: number,
|
|
66
|
+
): boolean => core.TransferMoney(from, to, amount, reason, fee),
|
|
67
|
+
/** Ledger rows of an account, newest first (also a stored character's: `{ owner: "character", id, account }`). */
|
|
68
|
+
history: (account: LedgerRef, query?: LedgerQuery): Promise<LedgerPage> =>
|
|
69
|
+
new Promise((resolve) => core.GetAccountHistory(account, resolve, query)),
|
|
70
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { readId } from "../shared/strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The live entity behind a client's network id, or 0 (not a positive integer, not networked, gone, or not of `type`:
|
|
5
|
+
* 1 ped, 2 vehicle, 3 object; any type when omitted).
|
|
6
|
+
*/
|
|
7
|
+
export function entityFromNet(netId: unknown, type?: 1 | 2 | 3): number {
|
|
8
|
+
if (readId(netId) === null) return 0;
|
|
9
|
+
const entity = NetworkGetEntityFromNetworkId(netId as number);
|
|
10
|
+
if (!entity || !DoesEntityExist(entity)) return 0;
|
|
11
|
+
return type === undefined || GetEntityType(entity) === type ? entity : 0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The live vehicle behind a client's network id, or 0 (not a positive integer, not networked, gone, not a vehicle). */
|
|
15
|
+
export const vehicleFromNet = (netId: unknown): number =>
|
|
16
|
+
entityFromNet(netId, 2);
|
|
17
|
+
|
|
18
|
+
/** The live ped behind a client's network id, or 0 (not a positive integer, not networked, gone, not a ped). */
|
|
19
|
+
export const pedFromNet = (netId: unknown): number => entityFromNet(netId, 1);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createLogger } from "../shared";
|
|
2
|
+
import {
|
|
3
|
+
ClientGate,
|
|
4
|
+
type ClientInputOptions,
|
|
5
|
+
type ParsedInputOptions,
|
|
6
|
+
} from "./gate";
|
|
7
|
+
|
|
8
|
+
const log = createLogger(GetCurrentResourceName());
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A net event a client sends: the only way an ai-* server listens to clients (never bare `onNet`). The source is
|
|
12
|
+
* read first, then the gate runs (real player, rate, guards, `args` parser, optional lock) and only then the
|
|
13
|
+
* handler. A handler that takes arguments gets them only through `args` (options come before it then), so
|
|
14
|
+
* unchecked client data never reaches it. A thrown error is logged, never sent back.
|
|
15
|
+
*
|
|
16
|
+
* onClientEvent("ai-shop:close", (source) => shop.close(source));
|
|
17
|
+
* onClientEvent(
|
|
18
|
+
* "ai-shop:buy",
|
|
19
|
+
* { args: ([item, count]) => (typeof item === "string" && isCount(count) ? [item, count] : null), guards: [hasCharacter()], lock: true },
|
|
20
|
+
* (source, item, count) => shop.buy(source, item, count),
|
|
21
|
+
* );
|
|
22
|
+
*/
|
|
23
|
+
export function onClientEvent(
|
|
24
|
+
name: string,
|
|
25
|
+
handler: (source: number) => unknown,
|
|
26
|
+
options?: ClientInputOptions,
|
|
27
|
+
): void;
|
|
28
|
+
export function onClientEvent<const A extends readonly unknown[]>(
|
|
29
|
+
name: string,
|
|
30
|
+
options: ParsedInputOptions<A>,
|
|
31
|
+
handler: (source: number, ...args: A) => unknown,
|
|
32
|
+
): void;
|
|
33
|
+
export function onClientEvent(
|
|
34
|
+
name: string,
|
|
35
|
+
first: ((source: number) => unknown) | ParsedInputOptions<readonly unknown[]>,
|
|
36
|
+
second?:
|
|
37
|
+
| ClientInputOptions
|
|
38
|
+
| ((source: number, ...args: readonly unknown[]) => unknown),
|
|
39
|
+
): void {
|
|
40
|
+
const [handler, options] =
|
|
41
|
+
typeof first === "function"
|
|
42
|
+
? [first, (second ?? {}) as ClientInputOptions]
|
|
43
|
+
: [
|
|
44
|
+
second as (source: number, ...args: readonly unknown[]) => unknown,
|
|
45
|
+
first,
|
|
46
|
+
];
|
|
47
|
+
const gate = new ClientGate(name, options, 10);
|
|
48
|
+
onNet(name, (...raw: unknown[]) => {
|
|
49
|
+
const source = Number(globalThis.source);
|
|
50
|
+
const args = gate.check(source, raw);
|
|
51
|
+
if (!args) return;
|
|
52
|
+
gate
|
|
53
|
+
.run(source, undefined, () => handler(source, ...args))
|
|
54
|
+
.catch((error: unknown) =>
|
|
55
|
+
log.error(`event ${name} failed for ${source}`, error),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LIB_UI,
|
|
3
|
+
type Notification,
|
|
4
|
+
type NotificationKind,
|
|
5
|
+
} from "@aifivem/shared";
|
|
6
|
+
|
|
7
|
+
/** Shows a notification card on one player's screen (ai-lib draws it, `title` above the text); texts are already translated. */
|
|
8
|
+
export function notify(
|
|
9
|
+
source: number,
|
|
10
|
+
text: string,
|
|
11
|
+
kind: NotificationKind = "info",
|
|
12
|
+
duration?: number,
|
|
13
|
+
title?: string,
|
|
14
|
+
): void {
|
|
15
|
+
const notification: Notification = { text, kind, duration, title };
|
|
16
|
+
emitNet(LIB_UI.notifyEvent, source, notification);
|
|
17
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE,
|
|
3
|
+
type CharacterInfo,
|
|
4
|
+
type GangMembership,
|
|
5
|
+
type JobMembership,
|
|
6
|
+
} from "@aifivem/shared";
|
|
7
|
+
import { resourceExport } from "../../fivem/exports";
|
|
8
|
+
|
|
9
|
+
const core = resourceExport(CORE.resource);
|
|
10
|
+
|
|
11
|
+
/** What a resource keeps of one loaded player. */
|
|
12
|
+
interface OnlinePlayer {
|
|
13
|
+
character: CharacterInfo;
|
|
14
|
+
jobs: readonly JobMembership[];
|
|
15
|
+
jobNames: readonly string[];
|
|
16
|
+
gangs: readonly GangMembership[];
|
|
17
|
+
gangNames: readonly string[];
|
|
18
|
+
duty: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A loaded player's jobs or duty changed, or the player was loaded / unloaded (`online`). */
|
|
22
|
+
export interface JobStateChange {
|
|
23
|
+
source: number;
|
|
24
|
+
citizenid: string;
|
|
25
|
+
jobs: readonly JobMembership[];
|
|
26
|
+
duty: string | null;
|
|
27
|
+
online: boolean;
|
|
28
|
+
/** Before the change (no jobs and no duty at a load). */
|
|
29
|
+
previous: { jobs: readonly JobMembership[]; duty: string | null };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Moves a player between the member sets of `index` from the `before` names to the `after` names. */
|
|
33
|
+
function reindex(
|
|
34
|
+
index: Map<string, Set<number>>,
|
|
35
|
+
source: number,
|
|
36
|
+
before: readonly string[],
|
|
37
|
+
after: readonly string[],
|
|
38
|
+
): void {
|
|
39
|
+
for (const name of before) {
|
|
40
|
+
const set = index.get(name);
|
|
41
|
+
if (!set) continue;
|
|
42
|
+
set.delete(source);
|
|
43
|
+
if (set.size === 0) index.delete(name);
|
|
44
|
+
}
|
|
45
|
+
for (const name of after) {
|
|
46
|
+
let set = index.get(name);
|
|
47
|
+
if (!set) index.set(name, (set = new Set()));
|
|
48
|
+
set.add(source);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The loaded players and their member sets, read from ai-core once on first use and then kept by its local events. */
|
|
53
|
+
export class OnlineMirror {
|
|
54
|
+
protected readonly players = new Map<number, OnlinePlayer>();
|
|
55
|
+
protected readonly onDuty = new Map<string, Set<number>>();
|
|
56
|
+
protected readonly jobIndex = new Map<string, Set<number>>();
|
|
57
|
+
protected readonly gangIndex = new Map<string, Set<number>>();
|
|
58
|
+
/** citizenid -> server id of each loaded character. */
|
|
59
|
+
protected readonly byCitizenid = new Map<string, number>();
|
|
60
|
+
protected readonly jobListeners = new Set<(change: JobStateChange) => void>();
|
|
61
|
+
private started = false;
|
|
62
|
+
/** Set while a load fills one player: its jobs and duty go out as one change. */
|
|
63
|
+
private loading = false;
|
|
64
|
+
|
|
65
|
+
protected start(): void {
|
|
66
|
+
if (this.started) return;
|
|
67
|
+
this.started = true;
|
|
68
|
+
// Memberships and duty of a loaded character are read once from ai-core (one player, at load), then follow its deltas.
|
|
69
|
+
const load = (source: number, character: CharacterInfo): void => {
|
|
70
|
+
this.drop(source);
|
|
71
|
+
this.players.set(source, {
|
|
72
|
+
character,
|
|
73
|
+
jobs: [],
|
|
74
|
+
jobNames: [],
|
|
75
|
+
gangs: [],
|
|
76
|
+
gangNames: [],
|
|
77
|
+
duty: null,
|
|
78
|
+
});
|
|
79
|
+
this.byCitizenid.set(character.citizenid, source);
|
|
80
|
+
this.loading = true;
|
|
81
|
+
this.setJobs(source, core.GetJobs(source) ?? []);
|
|
82
|
+
this.setGangs(source, core.GetGangs(source) ?? []);
|
|
83
|
+
this.setDuty(source, core.GetDutyJob(source));
|
|
84
|
+
this.loading = false;
|
|
85
|
+
const player = this.players.get(source);
|
|
86
|
+
if (player) this.emitJobs(source, player, [], null, true);
|
|
87
|
+
};
|
|
88
|
+
on(CORE.playerLoadedEvent, load);
|
|
89
|
+
on(CORE.playerUnloadedEvent, (source: number) => this.drop(source));
|
|
90
|
+
on(CORE.jobsChangedEvent, (source: number, jobs: JobMembership[]) =>
|
|
91
|
+
this.setJobs(source, jobs),
|
|
92
|
+
);
|
|
93
|
+
on(CORE.gangsChangedEvent, (source: number, gangs: GangMembership[]) =>
|
|
94
|
+
this.setGangs(source, gangs),
|
|
95
|
+
);
|
|
96
|
+
on(CORE.dutyChangedEvent, (source: number, job: string | null) =>
|
|
97
|
+
this.setDuty(source, job),
|
|
98
|
+
);
|
|
99
|
+
for (const source of core.GetLoadedSources()) {
|
|
100
|
+
const character = core.GetCharacter(source);
|
|
101
|
+
if (character) load(source, character);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Tells the job listeners about one player (nothing to do without listeners). */
|
|
106
|
+
private emitJobs(
|
|
107
|
+
source: number,
|
|
108
|
+
player: OnlinePlayer,
|
|
109
|
+
jobs: readonly JobMembership[],
|
|
110
|
+
duty: string | null,
|
|
111
|
+
online: boolean,
|
|
112
|
+
): void {
|
|
113
|
+
if (this.jobListeners.size === 0) return;
|
|
114
|
+
const change: JobStateChange = {
|
|
115
|
+
source,
|
|
116
|
+
citizenid: player.character.citizenid,
|
|
117
|
+
jobs: online ? player.jobs : [],
|
|
118
|
+
duty: online ? player.duty : null,
|
|
119
|
+
online,
|
|
120
|
+
previous: { jobs, duty },
|
|
121
|
+
};
|
|
122
|
+
for (const listener of this.jobListeners) listener(change);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private drop(source: number): void {
|
|
126
|
+
const player = this.players.get(source);
|
|
127
|
+
if (!player) return;
|
|
128
|
+
const [jobs, duty] = [player.jobs, player.duty];
|
|
129
|
+
this.loading = true;
|
|
130
|
+
this.setDuty(source, null);
|
|
131
|
+
this.loading = false;
|
|
132
|
+
this.emitJobs(source, player, jobs, duty, false);
|
|
133
|
+
reindex(this.jobIndex, source, player.jobNames, []);
|
|
134
|
+
reindex(this.gangIndex, source, player.gangNames, []);
|
|
135
|
+
this.byCitizenid.delete(player.character.citizenid);
|
|
136
|
+
this.players.delete(source);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private setJobs(source: number, jobs: readonly JobMembership[]): void {
|
|
140
|
+
const player = this.players.get(source);
|
|
141
|
+
if (!player) return;
|
|
142
|
+
const names = jobs.map((m) => m.job);
|
|
143
|
+
reindex(this.jobIndex, source, player.jobNames, names);
|
|
144
|
+
const before = player.jobs;
|
|
145
|
+
player.jobs = jobs;
|
|
146
|
+
player.jobNames = names;
|
|
147
|
+
if (!this.loading) this.emitJobs(source, player, before, player.duty, true);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private setGangs(source: number, gangs: readonly GangMembership[]): void {
|
|
151
|
+
const player = this.players.get(source);
|
|
152
|
+
if (!player) return;
|
|
153
|
+
const names = gangs.map((m) => m.gang);
|
|
154
|
+
reindex(this.gangIndex, source, player.gangNames, names);
|
|
155
|
+
player.gangs = gangs;
|
|
156
|
+
player.gangNames = names;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private setDuty(source: number, job: string | null): void {
|
|
160
|
+
const player = this.players.get(source);
|
|
161
|
+
if (!player || player.duty === job) return;
|
|
162
|
+
const before = player.duty;
|
|
163
|
+
if (player.duty) {
|
|
164
|
+
const set = this.onDuty.get(player.duty);
|
|
165
|
+
set?.delete(source);
|
|
166
|
+
if (set?.size === 0) this.onDuty.delete(player.duty);
|
|
167
|
+
}
|
|
168
|
+
player.duty = job;
|
|
169
|
+
if (job) {
|
|
170
|
+
let set = this.onDuty.get(job);
|
|
171
|
+
if (!set) this.onDuty.set(job, (set = new Set()));
|
|
172
|
+
set.add(source);
|
|
173
|
+
}
|
|
174
|
+
if (!this.loading) this.emitJobs(source, player, player.jobs, before, true);
|
|
175
|
+
}
|
|
176
|
+
}
|