@aifivem/lib 0.0.0-stage → 0.7.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,147 @@
|
|
|
1
|
+
import { CORE, type DataSpec, type LookupSource } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../../fivem/exports";
|
|
3
|
+
import { isRecord } from "../../shared";
|
|
4
|
+
|
|
5
|
+
const core = resourceExport(CORE.resource);
|
|
6
|
+
|
|
7
|
+
/** Lang keys of the generic refusals (ai-core's locales). */
|
|
8
|
+
export const ERR = {
|
|
9
|
+
type: "ADMIN_ERR_TYPE",
|
|
10
|
+
range: "ADMIN_ERR_RANGE",
|
|
11
|
+
length: "ADMIN_ERR_LENGTH",
|
|
12
|
+
pattern: "ADMIN_ERR_PATTERN",
|
|
13
|
+
required: "ADMIN_ERR_REQUIRED",
|
|
14
|
+
lookup: "ADMIN_ERR_LOOKUP",
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
/** Values of a lookup list from ai-core (items, jobs, gangs, accounts), cached for 2 s; null = not known (accepted). */
|
|
18
|
+
class LookupCache {
|
|
19
|
+
private readonly lists = new Map<
|
|
20
|
+
LookupSource,
|
|
21
|
+
{ at: number; values: Set<string> }
|
|
22
|
+
>();
|
|
23
|
+
|
|
24
|
+
values(source: LookupSource): Set<string> | null {
|
|
25
|
+
const cached = this.lists.get(source);
|
|
26
|
+
if (cached && Date.now() - cached.at < 2000)
|
|
27
|
+
return cached.values.size > 0 ? cached.values : null;
|
|
28
|
+
let list: unknown = [];
|
|
29
|
+
try {
|
|
30
|
+
list = core.GetLookupValues(source);
|
|
31
|
+
} catch {
|
|
32
|
+
// ai-core restarting: the value is checked again on the next edit.
|
|
33
|
+
}
|
|
34
|
+
const values = new Set(
|
|
35
|
+
Array.isArray(list)
|
|
36
|
+
? list.filter((v): v is string => typeof v === "string")
|
|
37
|
+
: [],
|
|
38
|
+
);
|
|
39
|
+
this.lists.set(source, { at: Date.now(), values });
|
|
40
|
+
// An empty list (no inventory running yet) refuses nothing.
|
|
41
|
+
return values.size > 0 ? values : null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const lookups = new LookupCache();
|
|
46
|
+
|
|
47
|
+
export interface Refusal {
|
|
48
|
+
error: string;
|
|
49
|
+
/** Dotted path of the value that failed, for the server console. */
|
|
50
|
+
path: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const isVec = (value: unknown, keys: string[]): boolean =>
|
|
54
|
+
isRecord(value) &&
|
|
55
|
+
keys.every((k) => typeof value[k] === "number" && Number.isFinite(value[k]));
|
|
56
|
+
|
|
57
|
+
/** Checks a value against its spec; the first problem found, or null. */
|
|
58
|
+
export function validate(
|
|
59
|
+
spec: DataSpec,
|
|
60
|
+
value: unknown,
|
|
61
|
+
path: string,
|
|
62
|
+
): Refusal | null {
|
|
63
|
+
if (value === undefined || value === null)
|
|
64
|
+
return spec.optional ? null : { error: ERR.required, path };
|
|
65
|
+
const fail = (error: string): Refusal => ({ error, path });
|
|
66
|
+
switch (spec.type) {
|
|
67
|
+
case "text": {
|
|
68
|
+
if (typeof value !== "string") return fail(ERR.type);
|
|
69
|
+
if (value.length < (spec.min ?? 1) || value.length > (spec.max ?? 120))
|
|
70
|
+
return fail(ERR.length);
|
|
71
|
+
if (spec.pattern && !new RegExp(spec.pattern).test(value))
|
|
72
|
+
return fail(ERR.pattern);
|
|
73
|
+
if (
|
|
74
|
+
spec.lookup &&
|
|
75
|
+
value !== "" &&
|
|
76
|
+
lookups.values(spec.lookup)?.has(value) === false
|
|
77
|
+
)
|
|
78
|
+
return fail(ERR.lookup);
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
case "number": {
|
|
82
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
83
|
+
return fail(ERR.type);
|
|
84
|
+
if (spec.integer && !Number.isInteger(value)) return fail(ERR.type);
|
|
85
|
+
if (
|
|
86
|
+
(spec.min !== undefined && value < spec.min) ||
|
|
87
|
+
(spec.max !== undefined && value > spec.max)
|
|
88
|
+
)
|
|
89
|
+
return fail(ERR.range);
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
case "bool":
|
|
93
|
+
return typeof value === "boolean" ? null : fail(ERR.type);
|
|
94
|
+
case "enum":
|
|
95
|
+
return spec.values.includes(value as string | number)
|
|
96
|
+
? null
|
|
97
|
+
: fail(ERR.type);
|
|
98
|
+
case "names": {
|
|
99
|
+
if (!Array.isArray(value) || value.length > (spec.max ?? 50))
|
|
100
|
+
return fail(ERR.length);
|
|
101
|
+
const pattern = new RegExp(spec.pattern ?? "^[\\w.:-]+$");
|
|
102
|
+
const seen = new Set<string>();
|
|
103
|
+
for (const name of value) {
|
|
104
|
+
if (
|
|
105
|
+
typeof name !== "string" ||
|
|
106
|
+
name.length > (spec.itemMax ?? 64) ||
|
|
107
|
+
seen.has(name)
|
|
108
|
+
)
|
|
109
|
+
return fail(ERR.type);
|
|
110
|
+
if (!pattern.test(name)) return fail(ERR.pattern);
|
|
111
|
+
if (spec.lookup && lookups.values(spec.lookup)?.has(name) === false)
|
|
112
|
+
return fail(ERR.lookup);
|
|
113
|
+
seen.add(name);
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
case "vec3":
|
|
118
|
+
return isVec(value, ["x", "y", "z"]) ? null : fail(ERR.type);
|
|
119
|
+
case "vec4":
|
|
120
|
+
return isVec(value, ["x", "y", "z", "heading"]) ? null : fail(ERR.type);
|
|
121
|
+
case "object": {
|
|
122
|
+
if (!isRecord(value)) return fail(ERR.type);
|
|
123
|
+
for (const key of Object.keys(value))
|
|
124
|
+
if (!(key in spec.fields))
|
|
125
|
+
return { error: ERR.type, path: `${path}.${key}` };
|
|
126
|
+
for (const [key, field] of Object.entries(spec.fields)) {
|
|
127
|
+
const refusal = validate(field, value[key], `${path}.${key}`);
|
|
128
|
+
if (refusal) return refusal;
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
case "list": {
|
|
133
|
+
if (!Array.isArray(value)) return fail(ERR.type);
|
|
134
|
+
if (value.length < (spec.min ?? 0) || value.length > (spec.max ?? 100))
|
|
135
|
+
return fail(ERR.length);
|
|
136
|
+
for (let i = 0; i < value.length; i++) {
|
|
137
|
+
const refusal = validate(spec.of, value[i], `${path}.${i + 1}`);
|
|
138
|
+
if (refusal) return refusal;
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
default: {
|
|
143
|
+
const _exhaustive: never = spec;
|
|
144
|
+
return _exhaustive;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE,
|
|
3
|
+
toCharacterAppearance,
|
|
4
|
+
type CharacterAppearance,
|
|
5
|
+
} from "@aifivem/shared";
|
|
6
|
+
import { resourceExport } from "../fivem/exports";
|
|
7
|
+
|
|
8
|
+
const core = resourceExport(CORE.resource);
|
|
9
|
+
|
|
10
|
+
/** Characters' stored looks through ai-core's appearance layer, whatever clothing script runs (ai-appearance, tgiann-clothing). */
|
|
11
|
+
export const appearance = {
|
|
12
|
+
/**
|
|
13
|
+
* A character's stored look (loaded or not): the ped model and the clothing script's data, null when it has none,
|
|
14
|
+
* no clothing script runs or ai-core is stopped. Hand it to the client's `appearance.apply` unchanged.
|
|
15
|
+
*/
|
|
16
|
+
get: (charId: number): Promise<CharacterAppearance | null> =>
|
|
17
|
+
new Promise((resolve) => {
|
|
18
|
+
try {
|
|
19
|
+
core.GetCharacterAppearance(charId, (look) =>
|
|
20
|
+
resolve(toCharacterAppearance(look)),
|
|
21
|
+
);
|
|
22
|
+
} catch {
|
|
23
|
+
resolve(null);
|
|
24
|
+
}
|
|
25
|
+
}),
|
|
26
|
+
/** The clothing script that answers, or null when none runs. */
|
|
27
|
+
provider: (): string | null => core.GetAppearanceSystem(),
|
|
28
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { coordsOf } from "../position";
|
|
2
|
+
import { AreaFollowers, type Follower } from "./followers";
|
|
3
|
+
import type { AreaItem, Entry, Id } from "./grid";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* World items by grid cell, streamed to the players around them (the sync rule's snapshot + delta for world objects:
|
|
7
|
+
* drops, plants, bodies, props). Each follower's client gets the items of the cells around its own once (`start`,
|
|
8
|
+
* `reset` true), then only puts and removes: an item added, changed, moved or removed, or the follower changing cell
|
|
9
|
+
* (`follow` with the client's reported cell, or `followAll` reading the server's positions). Who has an item is worked
|
|
10
|
+
* out from the cells, so nothing is stored per follower and item. Followers are freed on drop.
|
|
11
|
+
*/
|
|
12
|
+
export class AreaStream<T extends AreaItem, V = T> extends AreaFollowers<
|
|
13
|
+
T,
|
|
14
|
+
V
|
|
15
|
+
> {
|
|
16
|
+
/** `followAll` skipped its walk while there were no items. */
|
|
17
|
+
private stale = false;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Adds or updates an item: the followers around it get a put (with `resend` false only those who did not have it
|
|
21
|
+
* yet, for an item whose clients follow it themselves); followers that lost it when it moved get a remove.
|
|
22
|
+
*/
|
|
23
|
+
set(item: T, resend = true): void {
|
|
24
|
+
if (this.stale) this.followAll();
|
|
25
|
+
const { cx, cy } = this.cellOf(item.x, item.y);
|
|
26
|
+
const also = this.options.alsoTo?.(item);
|
|
27
|
+
const entry = this.items.get(item.id);
|
|
28
|
+
if (!entry) {
|
|
29
|
+
const fresh: Entry<T> = { item, cx, cy, also: undefined };
|
|
30
|
+
this.items.set(item.id, fresh);
|
|
31
|
+
this.file(fresh);
|
|
32
|
+
this.setAlso(fresh, also);
|
|
33
|
+
const put = [this.options.view(item)];
|
|
34
|
+
for (const source of this.who(cx, cy, also)) this.send(source, put, []);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const before = this.who(entry.cx, entry.cy, entry.also);
|
|
38
|
+
if (entry.cx !== cx || entry.cy !== cy) {
|
|
39
|
+
this.unfile(entry);
|
|
40
|
+
entry.cx = cx;
|
|
41
|
+
entry.cy = cy;
|
|
42
|
+
this.file(entry);
|
|
43
|
+
}
|
|
44
|
+
entry.item = item;
|
|
45
|
+
this.setAlso(entry, also);
|
|
46
|
+
const after = this.who(cx, cy, also);
|
|
47
|
+
const gone = [item.id];
|
|
48
|
+
for (const source of before)
|
|
49
|
+
if (!after.has(source)) this.send(source, [], gone);
|
|
50
|
+
const put = [this.options.view(item)];
|
|
51
|
+
for (const source of after)
|
|
52
|
+
if (resend || !before.has(source)) this.send(source, put, []);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Removes an item; the followers who had it get a remove. */
|
|
56
|
+
delete(id: Id): void {
|
|
57
|
+
const entry = this.items.get(id);
|
|
58
|
+
if (!entry) return;
|
|
59
|
+
this.items.delete(id);
|
|
60
|
+
this.unfile(entry);
|
|
61
|
+
const gone = [id];
|
|
62
|
+
for (const source of this.who(entry.cx, entry.cy, entry.also))
|
|
63
|
+
this.send(source, [], gone);
|
|
64
|
+
this.setAlso(entry, undefined);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Files many items without a message each (a load), then sends every follower its area again from scratch. */
|
|
68
|
+
load(items: Iterable<T>): void {
|
|
69
|
+
for (const item of items) {
|
|
70
|
+
const old = this.items.get(item.id);
|
|
71
|
+
if (old) {
|
|
72
|
+
this.unfile(old);
|
|
73
|
+
this.setAlso(old, undefined);
|
|
74
|
+
}
|
|
75
|
+
const { cx, cy } = this.cellOf(item.x, item.y);
|
|
76
|
+
const entry: Entry<T> = { item, cx, cy, also: undefined };
|
|
77
|
+
this.items.set(item.id, entry);
|
|
78
|
+
this.file(entry);
|
|
79
|
+
this.setAlso(entry, this.options.alsoTo?.(item));
|
|
80
|
+
}
|
|
81
|
+
for (const [source, f] of this.followers) this.snapshot(source, f);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The player starts following at a point (default: the ped's server position): a snapshot of its area. */
|
|
85
|
+
start(source: number, at = coordsOf(source)): void {
|
|
86
|
+
const { cx, cy } = this.cellOf(at.x, at.y);
|
|
87
|
+
const old = this.followers.get(source);
|
|
88
|
+
if (old) this.leave(source, old);
|
|
89
|
+
const f: Follower = { cx, cy };
|
|
90
|
+
this.followers.set(source, f);
|
|
91
|
+
this.enter(source, f);
|
|
92
|
+
this.snapshot(source, f);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The player's cell (as its client reports it): a first one starts following, a change sends the difference. */
|
|
96
|
+
follow(source: number, cx: number, cy: number): void {
|
|
97
|
+
const f = this.followers.get(source);
|
|
98
|
+
if (!f) {
|
|
99
|
+
const fresh: Follower = { cx, cy };
|
|
100
|
+
this.followers.set(source, fresh);
|
|
101
|
+
this.enter(source, fresh);
|
|
102
|
+
return this.snapshot(source, fresh);
|
|
103
|
+
}
|
|
104
|
+
this.move(source, f, cx, cy);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* `follow` for a cell a client sent: refused (false) unless it is within one cell of the ped's server position
|
|
109
|
+
* (the client's cell may lag one behind).
|
|
110
|
+
*/
|
|
111
|
+
followReported(source: number, cx: number, cy: number): boolean {
|
|
112
|
+
const p = coordsOf(source);
|
|
113
|
+
const real = this.cellOf(p.x, p.y);
|
|
114
|
+
if (Math.abs(real.cx - cx) > 1 || Math.abs(real.cy - cy) > 1) return false;
|
|
115
|
+
this.follow(source, cx, cy);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Reads every follower's server position and sends the difference to those who changed cell (a timer). */
|
|
120
|
+
followAll(): void {
|
|
121
|
+
if (this.items.size === 0) {
|
|
122
|
+
this.stale = true;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.stale = false;
|
|
126
|
+
for (const [source, f] of this.followers) {
|
|
127
|
+
const p = coordsOf(source);
|
|
128
|
+
const { cx, cy } = this.cellOf(p.x, p.y);
|
|
129
|
+
this.move(source, f, cx, cy);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The player stops following (logout, a private world); `tell` empties its client. */
|
|
134
|
+
stop(source: number, tell = false): void {
|
|
135
|
+
const f = this.followers.get(source);
|
|
136
|
+
if (!f) return;
|
|
137
|
+
this.followers.delete(source);
|
|
138
|
+
this.leave(source, f);
|
|
139
|
+
if (tell) this.emit(source, true, [], []);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { PlayerMap } from "../players";
|
|
2
|
+
import { cellKey } from "../../shared/grid";
|
|
3
|
+
import { AreaGrid, type AreaItem, type Entry, type Id } from "./grid";
|
|
4
|
+
import type { AreaStreamOptions } from "./options";
|
|
5
|
+
|
|
6
|
+
export interface Follower {
|
|
7
|
+
cx: number;
|
|
8
|
+
cy: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The players following a stream: who is in which cell, who has an item, and the messages they get. */
|
|
12
|
+
export class AreaFollowers<T extends AreaItem, V> extends AreaGrid<T> {
|
|
13
|
+
protected readonly followers = new PlayerMap<Follower>({
|
|
14
|
+
onDrop: (source, f) => this.leave(source, f),
|
|
15
|
+
});
|
|
16
|
+
/** Followers by their own cell. */
|
|
17
|
+
protected readonly inCell = new Map<number, Set<number>>();
|
|
18
|
+
/** Items by their `alsoTo` player; a dropped player's are let go (a new player reusing the id never gets them). */
|
|
19
|
+
protected readonly alsoOf = new PlayerMap<Set<Entry<T>>>({
|
|
20
|
+
onDrop: (_source, entries) => {
|
|
21
|
+
for (const entry of entries) entry.also = undefined;
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
protected readonly range: number;
|
|
25
|
+
protected readonly chunk: number;
|
|
26
|
+
|
|
27
|
+
constructor(protected readonly options: AreaStreamOptions<T, V>) {
|
|
28
|
+
super(options.cellSize ?? 64);
|
|
29
|
+
this.range = options.range ?? 1;
|
|
30
|
+
this.chunk = options.chunk ?? 50;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The players whose client has the item now. */
|
|
34
|
+
recipients(id: Id): Set<number> {
|
|
35
|
+
const entry = this.items.get(id);
|
|
36
|
+
return entry ? this.who(entry.cx, entry.cy, entry.also) : new Set();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Whether the player's client has the item now. */
|
|
40
|
+
knows(source: number, id: Id): boolean {
|
|
41
|
+
const entry = this.items.get(id);
|
|
42
|
+
if (!entry) return false;
|
|
43
|
+
if (entry.also === source) return true;
|
|
44
|
+
const f = this.followers.get(source);
|
|
45
|
+
return (
|
|
46
|
+
f !== undefined &&
|
|
47
|
+
Math.abs(f.cx - entry.cx) <= this.range &&
|
|
48
|
+
Math.abs(f.cy - entry.cy) <= this.range
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
following(source: number): boolean {
|
|
53
|
+
return this.followers.has(source);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
protected move(source: number, f: Follower, cx: number, cy: number): void {
|
|
57
|
+
if (f.cx === cx && f.cy === cy) return;
|
|
58
|
+
const r = this.range;
|
|
59
|
+
const removes: Id[] = [];
|
|
60
|
+
const puts: V[] = [];
|
|
61
|
+
// Cells of the old area outside the new one, and the other way round.
|
|
62
|
+
for (let dx = -r; dx <= r; dx++)
|
|
63
|
+
for (let dy = -r; dy <= r; dy++) {
|
|
64
|
+
const ox = f.cx + dx;
|
|
65
|
+
const oy = f.cy + dy;
|
|
66
|
+
if (Math.abs(ox - cx) > r || Math.abs(oy - cy) > r)
|
|
67
|
+
for (const entry of this.cells.get(cellKey(ox, oy)) ?? [])
|
|
68
|
+
if (entry.also !== source) removes.push(entry.item.id);
|
|
69
|
+
const nx = cx + dx;
|
|
70
|
+
const ny = cy + dy;
|
|
71
|
+
if (Math.abs(nx - f.cx) > r || Math.abs(ny - f.cy) > r)
|
|
72
|
+
for (const entry of this.cells.get(cellKey(nx, ny)) ?? [])
|
|
73
|
+
if (entry.also !== source) puts.push(this.options.view(entry.item));
|
|
74
|
+
}
|
|
75
|
+
this.leave(source, f);
|
|
76
|
+
f.cx = cx;
|
|
77
|
+
f.cy = cy;
|
|
78
|
+
this.enter(source, f);
|
|
79
|
+
if (puts.length > 0 || removes.length > 0) this.send(source, puts, removes);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected snapshot(source: number, f: Follower): void {
|
|
83
|
+
const puts: V[] = [];
|
|
84
|
+
const r = this.range;
|
|
85
|
+
for (let dx = -r; dx <= r; dx++)
|
|
86
|
+
for (let dy = -r; dy <= r; dy++)
|
|
87
|
+
for (const entry of this.cells.get(cellKey(f.cx + dx, f.cy + dy)) ?? [])
|
|
88
|
+
puts.push(this.options.view(entry.item));
|
|
89
|
+
for (const entry of this.alsoOf.get(source) ?? [])
|
|
90
|
+
if (Math.abs(entry.cx - f.cx) > r || Math.abs(entry.cy - f.cy) > r)
|
|
91
|
+
puts.push(this.options.view(entry.item));
|
|
92
|
+
this.emit(source, true, puts, []);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Followers whose area holds the cell, plus `also`. */
|
|
96
|
+
protected who(cx: number, cy: number, also: number | undefined): Set<number> {
|
|
97
|
+
const out = new Set<number>();
|
|
98
|
+
const r = this.range;
|
|
99
|
+
for (let dx = -r; dx <= r; dx++)
|
|
100
|
+
for (let dy = -r; dy <= r; dy++)
|
|
101
|
+
for (const source of this.inCell.get(cellKey(cx + dx, cy + dy)) ?? [])
|
|
102
|
+
out.add(source);
|
|
103
|
+
if (also !== undefined) out.add(also);
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
protected send(source: number, puts: V[], removes: Id[]): void {
|
|
108
|
+
this.emit(source, false, puts, removes);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
protected emit(source: number, reset: boolean, puts: V[], removes: Id[]): void {
|
|
112
|
+
const extra = this.options.extra?.() ?? [];
|
|
113
|
+
const { event } = this.options;
|
|
114
|
+
emitNet(event, source, reset, puts.slice(0, this.chunk), removes, ...extra);
|
|
115
|
+
for (let i = this.chunk; i < puts.length; i += this.chunk)
|
|
116
|
+
emitNet(
|
|
117
|
+
event,
|
|
118
|
+
source,
|
|
119
|
+
false,
|
|
120
|
+
puts.slice(i, i + this.chunk),
|
|
121
|
+
[],
|
|
122
|
+
...extra,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
protected setAlso(entry: Entry<T>, also: number | undefined): void {
|
|
127
|
+
if (entry.also === also) return;
|
|
128
|
+
if (entry.also !== undefined) {
|
|
129
|
+
const set = this.alsoOf.get(entry.also);
|
|
130
|
+
set?.delete(entry);
|
|
131
|
+
if (set?.size === 0) this.alsoOf.delete(entry.also);
|
|
132
|
+
}
|
|
133
|
+
entry.also = also;
|
|
134
|
+
if (also === undefined) return;
|
|
135
|
+
let set = this.alsoOf.get(also);
|
|
136
|
+
if (!set) this.alsoOf.set(also, (set = new Set()));
|
|
137
|
+
set.add(entry);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
protected enter(source: number, f: Follower): void {
|
|
142
|
+
const key = cellKey(f.cx, f.cy);
|
|
143
|
+
let set = this.inCell.get(key);
|
|
144
|
+
if (!set) this.inCell.set(key, (set = new Set()));
|
|
145
|
+
set.add(source);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
protected leave(source: number, f: Follower): void {
|
|
149
|
+
const key = cellKey(f.cx, f.cy);
|
|
150
|
+
const set = this.inCell.get(key);
|
|
151
|
+
set?.delete(source);
|
|
152
|
+
if (set?.size === 0) this.inCell.delete(key);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { cellKey, gridCell } from "../../shared/grid";
|
|
2
|
+
|
|
3
|
+
export type Id = number | string;
|
|
4
|
+
|
|
5
|
+
/** What the stream needs of a world item. */
|
|
6
|
+
export interface AreaItem {
|
|
7
|
+
readonly id: Id;
|
|
8
|
+
readonly x: number;
|
|
9
|
+
readonly y: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface Entry<T> {
|
|
13
|
+
item: T;
|
|
14
|
+
cx: number;
|
|
15
|
+
cy: number;
|
|
16
|
+
also: number | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** World items by grid cell: where each item is and what lies around a point. */
|
|
20
|
+
export class AreaGrid<T extends AreaItem> {
|
|
21
|
+
protected readonly items = new Map<Id, Entry<T>>();
|
|
22
|
+
protected readonly cells = new Map<number, Set<Entry<T>>>();
|
|
23
|
+
protected readonly size_: number;
|
|
24
|
+
|
|
25
|
+
constructor(cellSize: number) {
|
|
26
|
+
this.size_ = cellSize;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get size(): number {
|
|
30
|
+
return this.items.size;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get(id: Id): T | undefined {
|
|
34
|
+
return this.items.get(id)?.item;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
*values(): Generator<T> {
|
|
38
|
+
for (const entry of this.items.values()) yield entry.item;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The cell of a point on this stream's grid. */
|
|
42
|
+
cellOf(x: number, y: number): { cx: number; cy: number } {
|
|
43
|
+
return gridCell(x, y, this.size_);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Items in the cell of a point (a per-cell cap). */
|
|
47
|
+
countAt(x: number, y: number): number {
|
|
48
|
+
const { cx, cy } = this.cellOf(x, y);
|
|
49
|
+
return this.cells.get(cellKey(cx, cy))?.size ?? 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Items in the cell of a point and `cells` cells around it (spacing and merge checks). */
|
|
53
|
+
*around(x: number, y: number, cells = 1): Generator<T> {
|
|
54
|
+
const { cx, cy } = this.cellOf(x, y);
|
|
55
|
+
for (let dx = -cells; dx <= cells; dx++)
|
|
56
|
+
for (let dy = -cells; dy <= cells; dy++)
|
|
57
|
+
for (const entry of this.cells.get(cellKey(cx + dx, cy + dy)) ?? [])
|
|
58
|
+
yield entry.item;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected file(entry: Entry<T>): void {
|
|
62
|
+
const key = cellKey(entry.cx, entry.cy);
|
|
63
|
+
let set = this.cells.get(key);
|
|
64
|
+
if (!set) this.cells.set(key, (set = new Set()));
|
|
65
|
+
set.add(entry);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
protected unfile(entry: Entry<T>): void {
|
|
69
|
+
const key = cellKey(entry.cx, entry.cy);
|
|
70
|
+
const set = this.cells.get(key);
|
|
71
|
+
set?.delete(entry);
|
|
72
|
+
if (set?.size === 0) this.cells.delete(key);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface AreaStreamOptions<T, V> {
|
|
2
|
+
/** Client event: `(reset: boolean, puts: V[], removes: id[], ...extra)`. */
|
|
3
|
+
event: string;
|
|
4
|
+
/** What a client gets of an item. */
|
|
5
|
+
view: (item: T) => V;
|
|
6
|
+
/** Cell size in metres (default 64). */
|
|
7
|
+
cellSize?: number;
|
|
8
|
+
/** Cells around the follower's own cell it knows (default 1: the 3x3 around it). */
|
|
9
|
+
range?: number;
|
|
10
|
+
/** Puts per event; a long list goes as several small reliable events, which keep their order (default 50). */
|
|
11
|
+
chunk?: number;
|
|
12
|
+
/** A player who gets the item wherever they are (the body a player carries). */
|
|
13
|
+
alsoTo?: (item: T) => number | undefined;
|
|
14
|
+
/** Values appended to every event (a server clock for countdowns). */
|
|
15
|
+
extra?: () => unknown[];
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CORE } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/** The banking script through ai-core's banking layer; job money itself always goes through lib `money` / ai-core. */
|
|
7
|
+
export const banking = {
|
|
8
|
+
/** Opens the banking script's screen for a player; false when it cannot be opened from outside or none runs. */
|
|
9
|
+
open: (source: number): boolean => core.OpenBank(source),
|
|
10
|
+
/** A job's society balance (display): ai-core's job account in the `core` account mode, the script's own in `own`; null when none runs. */
|
|
11
|
+
societyBalance: (job: string): number | null =>
|
|
12
|
+
core.GetBankSocietyBalance(job),
|
|
13
|
+
/** The banking script the layer uses, or null. */
|
|
14
|
+
provider: (): string | null => core.GetBankingSystem(),
|
|
15
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE,
|
|
3
|
+
type BanEdit,
|
|
4
|
+
type BanInfo,
|
|
5
|
+
type BanListPage,
|
|
6
|
+
type BanQuery,
|
|
7
|
+
type BanRecord,
|
|
8
|
+
type BansPage,
|
|
9
|
+
type BanTarget,
|
|
10
|
+
} from "@aifivem/shared";
|
|
11
|
+
import { resourceExport } from "../fivem/exports";
|
|
12
|
+
|
|
13
|
+
const core = resourceExport(CORE.resource);
|
|
14
|
+
|
|
15
|
+
/** Bans through ai-core (checked when a player connects; logged on ai-core's `bans` channel). */
|
|
16
|
+
export const bans = {
|
|
17
|
+
/**
|
|
18
|
+
* Bans an online player (dropped at once) or identifiers (`{ identifiers: { license, fivem, discord, ip } }`);
|
|
19
|
+
* `minutes` left out = permanent. Resolves the ban id once written (null when the arguments are wrong or the write
|
|
20
|
+
* failed; the ban holds from the call either way).
|
|
21
|
+
*/
|
|
22
|
+
ban: (
|
|
23
|
+
target: BanTarget,
|
|
24
|
+
reason: string,
|
|
25
|
+
minutes?: number,
|
|
26
|
+
by?: number | string,
|
|
27
|
+
): Promise<number | null> =>
|
|
28
|
+
new Promise((resolve) => {
|
|
29
|
+
if (!core.BanPlayer(target, reason, minutes, by, resolve)) resolve(null);
|
|
30
|
+
}),
|
|
31
|
+
/** Lifts an active ban; false when no active ban has that id. */
|
|
32
|
+
unban: (banId: number, by?: number | string): boolean =>
|
|
33
|
+
core.UnbanPlayer(banId, by),
|
|
34
|
+
/** Changes an active ban's reason and / or end (`minutes` from now, null = permanent); false when refused. */
|
|
35
|
+
edit: (banId: number, edit: BanEdit, by?: number | string): boolean =>
|
|
36
|
+
core.EditBan(banId, edit, by),
|
|
37
|
+
/** The active ban of an online player or identifiers, or null. */
|
|
38
|
+
isBanned: (target: BanTarget): BanInfo | null => core.IsBanned(target),
|
|
39
|
+
/** Active bans, newest first, 50 per page. */
|
|
40
|
+
list: (page = 1): BansPage => core.GetBans(page),
|
|
41
|
+
/** Every ban of a player or identifiers (active, expired, revoked), newest first, at most 100. */
|
|
42
|
+
history: (target: BanTarget): Promise<BanRecord[]> =>
|
|
43
|
+
new Promise((resolve) => core.GetBanHistory(target, resolve)),
|
|
44
|
+
/** A page of bans by state and search, keyset paged (`page.next` as the next `cursor`); null on a wrong query. */
|
|
45
|
+
search: (query: BanQuery): Promise<BanListPage | null> =>
|
|
46
|
+
new Promise((resolve) => core.ListBans(query, resolve)),
|
|
47
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CORE, type BillRequest, type UnpaidBill } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Bills through ai-core's billing layer: whatever billing script runs (tgiann-phone, okokBilling...), else ai-core's
|
|
8
|
+
* own bills (the player pays from a list on screen, the money moves when they pay).
|
|
9
|
+
*/
|
|
10
|
+
export const billing = {
|
|
11
|
+
/** Sends a bill from a job (`job`, fines and service fees) or a player (`from`) to `to` (or `toCitizenid`, also offline with ai-core's own bills); false when refused. */
|
|
12
|
+
send: (bill: BillRequest): boolean => core.SendBill(bill),
|
|
13
|
+
/** The player's open bills (null when the billing script cannot list them). */
|
|
14
|
+
unpaid: (source: number): Promise<UnpaidBill[] | null> =>
|
|
15
|
+
new Promise((resolve) => core.GetUnpaidBills(source, resolve)),
|
|
16
|
+
/** The player pays one of their open bills; false when refused or the billing script takes payments only in its own screen. */
|
|
17
|
+
pay: (source: number, id: string): boolean => core.PayBill(source, id),
|
|
18
|
+
/** The billing script bills go to ("ai-core" for ai-core's own bills). */
|
|
19
|
+
provider: (): string | null => core.GetBillingSystem(),
|
|
20
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CORE } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/** Routing buckets from ai-core's allocator: no two scripts ever share one; never `base + n` per script. */
|
|
7
|
+
export const buckets = {
|
|
8
|
+
/** A free bucket for this resource (no population, strict lockdown); `tag` names it in logs ("property:12"). */
|
|
9
|
+
acquire: (tag: string): number => core.AcquireBucket(tag),
|
|
10
|
+
/** Gives a bucket back once its players are out; every bucket of a stopped resource goes back by itself. */
|
|
11
|
+
release: (id: number): boolean => core.ReleaseBucket(id),
|
|
12
|
+
};
|