@aifivem/shared 0.5.0 → 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 +1 -1
- package/src/action.ts +6 -6
- package/src/commands.ts +80 -80
- package/src/core.ts +125 -125
- package/src/dispatch.ts +103 -103
- package/src/logs.ts +45 -45
- package/src/menu.ts +94 -94
- package/src/minigame.ts +45 -45
- package/src/phone.ts +39 -39
- package/src/player.ts +159 -159
- package/src/radial.ts +44 -44
- package/src/validation.ts +82 -82
- package/src/vehicleKeys.ts +64 -64
- package/src/vrp.ts +51 -51
- package/src/world.ts +26 -26
package/src/player.ts
CHANGED
|
@@ -1,159 +1,159 @@
|
|
|
1
|
-
/** Player and character contracts shared by ai-core, its NUI and other resources. */
|
|
2
|
-
|
|
3
|
-
export type Sex = "male" | "female";
|
|
4
|
-
|
|
5
|
-
/** A character as other resources see it. */
|
|
6
|
-
export interface CharacterInfo {
|
|
7
|
-
id: number;
|
|
8
|
-
citizenid: string;
|
|
9
|
-
slot: number;
|
|
10
|
-
firstname: string;
|
|
11
|
-
lastname: string;
|
|
12
|
-
birthdate: string;
|
|
13
|
-
sex: Sex;
|
|
14
|
-
/** Free text ("American"); "" for characters made before the field existed. QB `charinfo.nationality`. */
|
|
15
|
-
nationality: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** A card on the character screen. */
|
|
19
|
-
export interface CharacterCard extends CharacterInfo {
|
|
20
|
-
/** Already in the game on another client of the same account. */
|
|
21
|
-
online: boolean;
|
|
22
|
-
/** Primary job label, for the card. */
|
|
23
|
-
job: string | null;
|
|
24
|
-
cash: number;
|
|
25
|
-
bank: number;
|
|
26
|
-
lastPlayed: number | null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface ScenePoint {
|
|
30
|
-
x: number;
|
|
31
|
-
y: number;
|
|
32
|
-
z: number;
|
|
33
|
-
heading: number;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** One place in the character screen scene: a character stands there, the camera frames it when it is picked. */
|
|
37
|
-
export interface SceneSpot {
|
|
38
|
-
/** The ped's position (its root, as `GetEntityCoords` reads it) and heading. */
|
|
39
|
-
ped: ScenePoint;
|
|
40
|
-
/** Idle animation dictionary and clip ("" = none). */
|
|
41
|
-
anim: string;
|
|
42
|
-
clip: string;
|
|
43
|
-
/** Metres from the look-at point to the camera. */
|
|
44
|
-
camDistance: number;
|
|
45
|
-
/** Camera height as a slope (0 = level with the look-at point, 0.5 = up by half the distance). */
|
|
46
|
-
camHeight: number;
|
|
47
|
-
/** Degrees around the ped from its front (0 = straight in front). */
|
|
48
|
-
camAngle: number;
|
|
49
|
-
/** Metres above the ped's root the camera looks at. */
|
|
50
|
-
targetHeight: number;
|
|
51
|
-
/** Degrees the camera slowly swings left and right (0 = still). */
|
|
52
|
-
sway: number;
|
|
53
|
-
/** Background blur (depth of field): in focus from `blurNear` to `blurFar` metres, strength 0-100. */
|
|
54
|
-
blur: boolean;
|
|
55
|
-
blurNear: number;
|
|
56
|
-
blurFar: number;
|
|
57
|
-
blurStrength: number;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** The in-world scene behind the character screen (panel setting). */
|
|
61
|
-
export interface CharacterScene {
|
|
62
|
-
/** Camera field of view in degrees. */
|
|
63
|
-
fov: number;
|
|
64
|
-
/** Local time and weather while the screen is open (`enabled: false` keeps the server's). */
|
|
65
|
-
time: { enabled: boolean; hour: number; minute: number; weather: string };
|
|
66
|
-
/** Spot N holds the character of slot N. */
|
|
67
|
-
spots: SceneSpot[];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** The server's name and look on full-screen menus (panel setting). */
|
|
71
|
-
export interface Branding {
|
|
72
|
-
name: string;
|
|
73
|
-
subtitle: string;
|
|
74
|
-
tagline: string;
|
|
75
|
-
taglineSub: string;
|
|
76
|
-
/** Background image of full-screen menus (files in ai-core's web/public/backgrounds); the character screen shows it only when its scene cannot be built. */
|
|
77
|
-
background: "skyline" | "wet-drive";
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export interface CharacterScreen {
|
|
81
|
-
branding: Branding;
|
|
82
|
-
slots: number;
|
|
83
|
-
characters: CharacterCard[];
|
|
84
|
-
canDelete: boolean;
|
|
85
|
-
/** Where the characters stand and how the camera frames them (panel setting). */
|
|
86
|
-
scene: CharacterScene;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface CreateCharacterInput {
|
|
90
|
-
slot: number;
|
|
91
|
-
firstname: string;
|
|
92
|
-
lastname: string;
|
|
93
|
-
/** YYYY-MM-DD */
|
|
94
|
-
birthdate: string;
|
|
95
|
-
sex: Sex;
|
|
96
|
-
nationality: string;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Answer of the character screen actions: `ok`, or a lang key telling why not. */
|
|
100
|
-
export type CharacterActionResult = import("./action").ActionResult;
|
|
101
|
-
|
|
102
|
-
export interface JobMembership {
|
|
103
|
-
job: string;
|
|
104
|
-
grade: number;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export interface GangMembership {
|
|
108
|
-
gang: string;
|
|
109
|
-
grade: number;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface SpawnPoint {
|
|
113
|
-
x: number;
|
|
114
|
-
y: number;
|
|
115
|
-
z: number;
|
|
116
|
-
heading: number;
|
|
117
|
-
model: string;
|
|
118
|
-
/** Body armour 0-100 saved with the character (metadata `armor`). */
|
|
119
|
-
armour: number;
|
|
120
|
-
/** Ped health saved with the character (metadata `health`, GTA scale, above 100); 0 = keep the spawn's full health. */
|
|
121
|
-
health: number;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** The local player's often read values, kept by ai-core's one watcher (lib `cache`); `false` = none. */
|
|
125
|
-
export interface PlayerCache {
|
|
126
|
-
ped: number;
|
|
127
|
-
playerId: number;
|
|
128
|
-
serverId: number;
|
|
129
|
-
vehicle: number | false;
|
|
130
|
-
/** Seat in `vehicle` (-1 driver). */
|
|
131
|
-
seat: number | false;
|
|
132
|
-
/** Weapon hash in hand; `false` when unarmed. */
|
|
133
|
-
weapon: number | false;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** What ai-core pushes to a player's own client: one snapshot at load, then `(key, value)` deltas. */
|
|
137
|
-
export interface AiPlayerData {
|
|
138
|
-
character: CharacterInfo | null;
|
|
139
|
-
money: Record<string, number>;
|
|
140
|
-
jobs: JobMembership[];
|
|
141
|
-
duty: string | null;
|
|
142
|
-
/** The job other scripts see as "the" job: on duty, else last on duty, else the first, else unemployed. */
|
|
143
|
-
job: JobMembership;
|
|
144
|
-
gangs: GangMembership[];
|
|
145
|
-
activeGang: string | null;
|
|
146
|
-
/** The character's free-form metadata bag (framework bridges keep QB / ESX metadata here); patched per key. */
|
|
147
|
-
metadata: Record<string, unknown>;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export type NotificationKind = "success" | "error" | "info" | "warning";
|
|
151
|
-
|
|
152
|
-
/** A notification card (ai-core draws it); texts are already translated by the sender. */
|
|
153
|
-
export interface Notification {
|
|
154
|
-
text: string;
|
|
155
|
-
title?: string;
|
|
156
|
-
kind: NotificationKind;
|
|
157
|
-
/** Milliseconds on screen (default 4000). */
|
|
158
|
-
duration?: number;
|
|
159
|
-
}
|
|
1
|
+
/** Player and character contracts shared by ai-core, its NUI and other resources. */
|
|
2
|
+
|
|
3
|
+
export type Sex = "male" | "female";
|
|
4
|
+
|
|
5
|
+
/** A character as other resources see it. */
|
|
6
|
+
export interface CharacterInfo {
|
|
7
|
+
id: number;
|
|
8
|
+
citizenid: string;
|
|
9
|
+
slot: number;
|
|
10
|
+
firstname: string;
|
|
11
|
+
lastname: string;
|
|
12
|
+
birthdate: string;
|
|
13
|
+
sex: Sex;
|
|
14
|
+
/** Free text ("American"); "" for characters made before the field existed. QB `charinfo.nationality`. */
|
|
15
|
+
nationality: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A card on the character screen. */
|
|
19
|
+
export interface CharacterCard extends CharacterInfo {
|
|
20
|
+
/** Already in the game on another client of the same account. */
|
|
21
|
+
online: boolean;
|
|
22
|
+
/** Primary job label, for the card. */
|
|
23
|
+
job: string | null;
|
|
24
|
+
cash: number;
|
|
25
|
+
bank: number;
|
|
26
|
+
lastPlayed: number | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ScenePoint {
|
|
30
|
+
x: number;
|
|
31
|
+
y: number;
|
|
32
|
+
z: number;
|
|
33
|
+
heading: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** One place in the character screen scene: a character stands there, the camera frames it when it is picked. */
|
|
37
|
+
export interface SceneSpot {
|
|
38
|
+
/** The ped's position (its root, as `GetEntityCoords` reads it) and heading. */
|
|
39
|
+
ped: ScenePoint;
|
|
40
|
+
/** Idle animation dictionary and clip ("" = none). */
|
|
41
|
+
anim: string;
|
|
42
|
+
clip: string;
|
|
43
|
+
/** Metres from the look-at point to the camera. */
|
|
44
|
+
camDistance: number;
|
|
45
|
+
/** Camera height as a slope (0 = level with the look-at point, 0.5 = up by half the distance). */
|
|
46
|
+
camHeight: number;
|
|
47
|
+
/** Degrees around the ped from its front (0 = straight in front). */
|
|
48
|
+
camAngle: number;
|
|
49
|
+
/** Metres above the ped's root the camera looks at. */
|
|
50
|
+
targetHeight: number;
|
|
51
|
+
/** Degrees the camera slowly swings left and right (0 = still). */
|
|
52
|
+
sway: number;
|
|
53
|
+
/** Background blur (depth of field): in focus from `blurNear` to `blurFar` metres, strength 0-100. */
|
|
54
|
+
blur: boolean;
|
|
55
|
+
blurNear: number;
|
|
56
|
+
blurFar: number;
|
|
57
|
+
blurStrength: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The in-world scene behind the character screen (panel setting). */
|
|
61
|
+
export interface CharacterScene {
|
|
62
|
+
/** Camera field of view in degrees. */
|
|
63
|
+
fov: number;
|
|
64
|
+
/** Local time and weather while the screen is open (`enabled: false` keeps the server's). */
|
|
65
|
+
time: { enabled: boolean; hour: number; minute: number; weather: string };
|
|
66
|
+
/** Spot N holds the character of slot N. */
|
|
67
|
+
spots: SceneSpot[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The server's name and look on full-screen menus (panel setting). */
|
|
71
|
+
export interface Branding {
|
|
72
|
+
name: string;
|
|
73
|
+
subtitle: string;
|
|
74
|
+
tagline: string;
|
|
75
|
+
taglineSub: string;
|
|
76
|
+
/** Background image of full-screen menus (files in ai-core's web/public/backgrounds); the character screen shows it only when its scene cannot be built. */
|
|
77
|
+
background: "skyline" | "wet-drive";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface CharacterScreen {
|
|
81
|
+
branding: Branding;
|
|
82
|
+
slots: number;
|
|
83
|
+
characters: CharacterCard[];
|
|
84
|
+
canDelete: boolean;
|
|
85
|
+
/** Where the characters stand and how the camera frames them (panel setting). */
|
|
86
|
+
scene: CharacterScene;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface CreateCharacterInput {
|
|
90
|
+
slot: number;
|
|
91
|
+
firstname: string;
|
|
92
|
+
lastname: string;
|
|
93
|
+
/** YYYY-MM-DD */
|
|
94
|
+
birthdate: string;
|
|
95
|
+
sex: Sex;
|
|
96
|
+
nationality: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Answer of the character screen actions: `ok`, or a lang key telling why not. */
|
|
100
|
+
export type CharacterActionResult = import("./action").ActionResult;
|
|
101
|
+
|
|
102
|
+
export interface JobMembership {
|
|
103
|
+
job: string;
|
|
104
|
+
grade: number;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface GangMembership {
|
|
108
|
+
gang: string;
|
|
109
|
+
grade: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface SpawnPoint {
|
|
113
|
+
x: number;
|
|
114
|
+
y: number;
|
|
115
|
+
z: number;
|
|
116
|
+
heading: number;
|
|
117
|
+
model: string;
|
|
118
|
+
/** Body armour 0-100 saved with the character (metadata `armor`). */
|
|
119
|
+
armour: number;
|
|
120
|
+
/** Ped health saved with the character (metadata `health`, GTA scale, above 100); 0 = keep the spawn's full health. */
|
|
121
|
+
health: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The local player's often read values, kept by ai-core's one watcher (lib `cache`); `false` = none. */
|
|
125
|
+
export interface PlayerCache {
|
|
126
|
+
ped: number;
|
|
127
|
+
playerId: number;
|
|
128
|
+
serverId: number;
|
|
129
|
+
vehicle: number | false;
|
|
130
|
+
/** Seat in `vehicle` (-1 driver). */
|
|
131
|
+
seat: number | false;
|
|
132
|
+
/** Weapon hash in hand; `false` when unarmed. */
|
|
133
|
+
weapon: number | false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** What ai-core pushes to a player's own client: one snapshot at load, then `(key, value)` deltas. */
|
|
137
|
+
export interface AiPlayerData {
|
|
138
|
+
character: CharacterInfo | null;
|
|
139
|
+
money: Record<string, number>;
|
|
140
|
+
jobs: JobMembership[];
|
|
141
|
+
duty: string | null;
|
|
142
|
+
/** The job other scripts see as "the" job: on duty, else last on duty, else the first, else unemployed. */
|
|
143
|
+
job: JobMembership;
|
|
144
|
+
gangs: GangMembership[];
|
|
145
|
+
activeGang: string | null;
|
|
146
|
+
/** The character's free-form metadata bag (framework bridges keep QB / ESX metadata here); patched per key. */
|
|
147
|
+
metadata: Record<string, unknown>;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export type NotificationKind = "success" | "error" | "info" | "warning";
|
|
151
|
+
|
|
152
|
+
/** A notification card (ai-core draws it); texts are already translated by the sender. */
|
|
153
|
+
export interface Notification {
|
|
154
|
+
text: string;
|
|
155
|
+
title?: string;
|
|
156
|
+
kind: NotificationKind;
|
|
157
|
+
/** Milliseconds on screen (default 4000). */
|
|
158
|
+
duration?: number;
|
|
159
|
+
}
|
package/src/radial.ts
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* One item of the radial wheel as a script hands it to ai-core (lib `radial`). The functions run in the resource that
|
|
3
|
-
* added the item; ai-core drops the item when that resource stops.
|
|
4
|
-
*/
|
|
5
|
-
export interface RadialItem {
|
|
6
|
-
/** Unique id; adding an id again replaces the item. */
|
|
7
|
-
id: string;
|
|
8
|
-
/** The text on the card (already translated). */
|
|
9
|
-
label: string;
|
|
10
|
-
/** Font Awesome solid icon name without `fa-` (`car`, `key`), or an image url. */
|
|
11
|
-
icon?: string;
|
|
12
|
-
/** The item id this goes into (a sub menu: the built-in `vehicle`, or another item); left out = the main wheel. */
|
|
13
|
-
parent?: string;
|
|
14
|
-
/** Sub items: the card opens a sub menu (each item with its own `canShow` / `onPressed`). */
|
|
15
|
-
items?: RadialItem[];
|
|
16
|
-
/** Shown only while this returns true (job, duty, a vehicle, an item); read each time the wheel opens or refreshes. */
|
|
17
|
-
canShow?: () => boolean;
|
|
18
|
-
/** Runs when the player picks the item. */
|
|
19
|
-
onPressed?: () => void;
|
|
20
|
-
/** true keeps the wheel open after the press (doors, extras). */
|
|
21
|
-
keepOpen?: boolean;
|
|
22
|
-
/** true keeps the item while the player is down (every other item hides then). */
|
|
23
|
-
whileDead?: boolean;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** The radial built-in category vehicle items go into (`parent: RADIAL_VEHICLE`). */
|
|
27
|
-
export const RADIAL_VEHICLE = "vehicle";
|
|
28
|
-
|
|
29
|
-
/** ai-core's radial client exports (lib `radial`): items reach whichever radial menu runs. */
|
|
30
|
-
export type RadialClientExports = {
|
|
31
|
-
/** Adds or replaces an item of the calling resource (dropped when it stops). */
|
|
32
|
-
AddRadialItem(item: RadialItem): void;
|
|
33
|
-
RemoveRadialItem(id: string): void;
|
|
34
|
-
/** Reads every `canShow` of an open wheel again (call it after what they read changed). */
|
|
35
|
-
RefreshRadial(): void;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/** One radial menu behind ai-core's radial layer (client): ai-core hands it every item scripts add. */
|
|
39
|
-
export interface RadialAdapter {
|
|
40
|
-
resource: string;
|
|
41
|
-
add(item: RadialItem): void;
|
|
42
|
-
remove(item: RadialItem): void;
|
|
43
|
-
refresh(): void;
|
|
44
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* One item of the radial wheel as a script hands it to ai-core (lib `radial`). The functions run in the resource that
|
|
3
|
+
* added the item; ai-core drops the item when that resource stops.
|
|
4
|
+
*/
|
|
5
|
+
export interface RadialItem {
|
|
6
|
+
/** Unique id; adding an id again replaces the item. */
|
|
7
|
+
id: string;
|
|
8
|
+
/** The text on the card (already translated). */
|
|
9
|
+
label: string;
|
|
10
|
+
/** Font Awesome solid icon name without `fa-` (`car`, `key`), or an image url. */
|
|
11
|
+
icon?: string;
|
|
12
|
+
/** The item id this goes into (a sub menu: the built-in `vehicle`, or another item); left out = the main wheel. */
|
|
13
|
+
parent?: string;
|
|
14
|
+
/** Sub items: the card opens a sub menu (each item with its own `canShow` / `onPressed`). */
|
|
15
|
+
items?: RadialItem[];
|
|
16
|
+
/** Shown only while this returns true (job, duty, a vehicle, an item); read each time the wheel opens or refreshes. */
|
|
17
|
+
canShow?: () => boolean;
|
|
18
|
+
/** Runs when the player picks the item. */
|
|
19
|
+
onPressed?: () => void;
|
|
20
|
+
/** true keeps the wheel open after the press (doors, extras). */
|
|
21
|
+
keepOpen?: boolean;
|
|
22
|
+
/** true keeps the item while the player is down (every other item hides then). */
|
|
23
|
+
whileDead?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The radial built-in category vehicle items go into (`parent: RADIAL_VEHICLE`). */
|
|
27
|
+
export const RADIAL_VEHICLE = "vehicle";
|
|
28
|
+
|
|
29
|
+
/** ai-core's radial client exports (lib `radial`): items reach whichever radial menu runs. */
|
|
30
|
+
export type RadialClientExports = {
|
|
31
|
+
/** Adds or replaces an item of the calling resource (dropped when it stops). */
|
|
32
|
+
AddRadialItem(item: RadialItem): void;
|
|
33
|
+
RemoveRadialItem(id: string): void;
|
|
34
|
+
/** Reads every `canShow` of an open wheel again (call it after what they read changed). */
|
|
35
|
+
RefreshRadial(): void;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** One radial menu behind ai-core's radial layer (client): ai-core hands it every item scripts add. */
|
|
39
|
+
export interface RadialAdapter {
|
|
40
|
+
resource: string;
|
|
41
|
+
add(item: RadialItem): void;
|
|
42
|
+
remove(item: RadialItem): void;
|
|
43
|
+
refresh(): void;
|
|
44
|
+
}
|
package/src/validation.ts
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import type { CreateCharacterInput, Sex } from "./player";
|
|
2
|
-
|
|
3
|
-
const NAME = /^[A-Za-zÀ-ɏ][A-Za-zÀ-ɏ' -]{1,19}$/;
|
|
4
|
-
const DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
5
|
-
|
|
6
|
-
/** A first or last name: 2-20 letters (Latin, accents), spaces, apostrophes, dashes; trimmed. Null when invalid. */
|
|
7
|
-
export function toName(value: unknown): string | null {
|
|
8
|
-
if (typeof value !== "string") return null;
|
|
9
|
-
const name = value.trim().replace(/\s+/g, " ");
|
|
10
|
-
return NAME.test(name) ? name : null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** A birth date `YYYY-MM-DD` for an age between `minAge` and 100. Null when invalid. */
|
|
14
|
-
export function toBirthdate(
|
|
15
|
-
value: unknown,
|
|
16
|
-
minAge = 18,
|
|
17
|
-
now = new Date(),
|
|
18
|
-
): string | null {
|
|
19
|
-
if (typeof value !== "string") return null;
|
|
20
|
-
const match = DATE.exec(value);
|
|
21
|
-
if (!match) return null;
|
|
22
|
-
const [year, month, day] = [
|
|
23
|
-
Number(match[1]),
|
|
24
|
-
Number(match[2]),
|
|
25
|
-
Number(match[3]),
|
|
26
|
-
];
|
|
27
|
-
const date = new Date(Date.UTC(year, month - 1, day));
|
|
28
|
-
if (
|
|
29
|
-
date.getUTCFullYear() !== year ||
|
|
30
|
-
date.getUTCMonth() !== month - 1 ||
|
|
31
|
-
date.getUTCDate() !== day
|
|
32
|
-
)
|
|
33
|
-
return null;
|
|
34
|
-
const age =
|
|
35
|
-
now.getUTCFullYear() -
|
|
36
|
-
year -
|
|
37
|
-
(now.getUTCMonth() < month - 1 ||
|
|
38
|
-
(now.getUTCMonth() === month - 1 && now.getUTCDate() < day)
|
|
39
|
-
? 1
|
|
40
|
-
: 0);
|
|
41
|
-
return age >= minAge && age <= 100 ? value : null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function toSex(value: unknown): Sex | null {
|
|
45
|
-
return value === "male" || value === "female" ? value : null;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const NATIONALITY = /^[A-Za-zÀ-ɏ][A-Za-zÀ-ɏ' -]{1,31}$/;
|
|
49
|
-
|
|
50
|
-
/** A nationality: 2-32 letters (Latin, accents), spaces, apostrophes, dashes; trimmed. Null when invalid. */
|
|
51
|
-
export function toNationality(value: unknown): string | null {
|
|
52
|
-
if (typeof value !== "string") return null;
|
|
53
|
-
const text = value.trim().replace(/\s+/g, " ");
|
|
54
|
-
return NATIONALITY.test(text) ? text : null;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** The character creation form from the NUI, or the lang key of the first invalid field. */
|
|
58
|
-
export function toCreateCharacter(
|
|
59
|
-
value: unknown,
|
|
60
|
-
): CreateCharacterInput | { error: string } {
|
|
61
|
-
if (typeof value !== "object" || value === null)
|
|
62
|
-
return { error: "CHAR_ERR_INVALID" };
|
|
63
|
-
const input = value as Record<string, unknown>;
|
|
64
|
-
const slot =
|
|
65
|
-
typeof input.slot === "number" &&
|
|
66
|
-
Number.isInteger(input.slot) &&
|
|
67
|
-
input.slot >= 1
|
|
68
|
-
? input.slot
|
|
69
|
-
: null;
|
|
70
|
-
const firstname = toName(input.firstname);
|
|
71
|
-
const lastname = toName(input.lastname);
|
|
72
|
-
const birthdate = toBirthdate(input.birthdate);
|
|
73
|
-
const sex = toSex(input.sex);
|
|
74
|
-
const nationality = toNationality(input.nationality);
|
|
75
|
-
if (slot === null) return { error: "CHAR_ERR_INVALID" };
|
|
76
|
-
if (!firstname) return { error: "CHAR_ERR_FIRSTNAME" };
|
|
77
|
-
if (!lastname) return { error: "CHAR_ERR_LASTNAME" };
|
|
78
|
-
if (!birthdate) return { error: "CHAR_ERR_BIRTHDATE" };
|
|
79
|
-
if (!sex) return { error: "CHAR_ERR_SEX" };
|
|
80
|
-
if (!nationality) return { error: "CHAR_ERR_NATIONALITY" };
|
|
81
|
-
return { slot, firstname, lastname, birthdate, sex, nationality };
|
|
82
|
-
}
|
|
1
|
+
import type { CreateCharacterInput, Sex } from "./player";
|
|
2
|
+
|
|
3
|
+
const NAME = /^[A-Za-zÀ-ɏ][A-Za-zÀ-ɏ' -]{1,19}$/;
|
|
4
|
+
const DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
5
|
+
|
|
6
|
+
/** A first or last name: 2-20 letters (Latin, accents), spaces, apostrophes, dashes; trimmed. Null when invalid. */
|
|
7
|
+
export function toName(value: unknown): string | null {
|
|
8
|
+
if (typeof value !== "string") return null;
|
|
9
|
+
const name = value.trim().replace(/\s+/g, " ");
|
|
10
|
+
return NAME.test(name) ? name : null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A birth date `YYYY-MM-DD` for an age between `minAge` and 100. Null when invalid. */
|
|
14
|
+
export function toBirthdate(
|
|
15
|
+
value: unknown,
|
|
16
|
+
minAge = 18,
|
|
17
|
+
now = new Date(),
|
|
18
|
+
): string | null {
|
|
19
|
+
if (typeof value !== "string") return null;
|
|
20
|
+
const match = DATE.exec(value);
|
|
21
|
+
if (!match) return null;
|
|
22
|
+
const [year, month, day] = [
|
|
23
|
+
Number(match[1]),
|
|
24
|
+
Number(match[2]),
|
|
25
|
+
Number(match[3]),
|
|
26
|
+
];
|
|
27
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
28
|
+
if (
|
|
29
|
+
date.getUTCFullYear() !== year ||
|
|
30
|
+
date.getUTCMonth() !== month - 1 ||
|
|
31
|
+
date.getUTCDate() !== day
|
|
32
|
+
)
|
|
33
|
+
return null;
|
|
34
|
+
const age =
|
|
35
|
+
now.getUTCFullYear() -
|
|
36
|
+
year -
|
|
37
|
+
(now.getUTCMonth() < month - 1 ||
|
|
38
|
+
(now.getUTCMonth() === month - 1 && now.getUTCDate() < day)
|
|
39
|
+
? 1
|
|
40
|
+
: 0);
|
|
41
|
+
return age >= minAge && age <= 100 ? value : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function toSex(value: unknown): Sex | null {
|
|
45
|
+
return value === "male" || value === "female" ? value : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const NATIONALITY = /^[A-Za-zÀ-ɏ][A-Za-zÀ-ɏ' -]{1,31}$/;
|
|
49
|
+
|
|
50
|
+
/** A nationality: 2-32 letters (Latin, accents), spaces, apostrophes, dashes; trimmed. Null when invalid. */
|
|
51
|
+
export function toNationality(value: unknown): string | null {
|
|
52
|
+
if (typeof value !== "string") return null;
|
|
53
|
+
const text = value.trim().replace(/\s+/g, " ");
|
|
54
|
+
return NATIONALITY.test(text) ? text : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The character creation form from the NUI, or the lang key of the first invalid field. */
|
|
58
|
+
export function toCreateCharacter(
|
|
59
|
+
value: unknown,
|
|
60
|
+
): CreateCharacterInput | { error: string } {
|
|
61
|
+
if (typeof value !== "object" || value === null)
|
|
62
|
+
return { error: "CHAR_ERR_INVALID" };
|
|
63
|
+
const input = value as Record<string, unknown>;
|
|
64
|
+
const slot =
|
|
65
|
+
typeof input.slot === "number" &&
|
|
66
|
+
Number.isInteger(input.slot) &&
|
|
67
|
+
input.slot >= 1
|
|
68
|
+
? input.slot
|
|
69
|
+
: null;
|
|
70
|
+
const firstname = toName(input.firstname);
|
|
71
|
+
const lastname = toName(input.lastname);
|
|
72
|
+
const birthdate = toBirthdate(input.birthdate);
|
|
73
|
+
const sex = toSex(input.sex);
|
|
74
|
+
const nationality = toNationality(input.nationality);
|
|
75
|
+
if (slot === null) return { error: "CHAR_ERR_INVALID" };
|
|
76
|
+
if (!firstname) return { error: "CHAR_ERR_FIRSTNAME" };
|
|
77
|
+
if (!lastname) return { error: "CHAR_ERR_LASTNAME" };
|
|
78
|
+
if (!birthdate) return { error: "CHAR_ERR_BIRTHDATE" };
|
|
79
|
+
if (!sex) return { error: "CHAR_ERR_SEX" };
|
|
80
|
+
if (!nationality) return { error: "CHAR_ERR_NATIONALITY" };
|
|
81
|
+
return { slot, firstname, lastname, birthdate, sex, nationality };
|
|
82
|
+
}
|