/
functions.ts
81 lines (75 loc) · 2.12 KB
/
functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { sendMessage, createMessage } from "../message";
import { Vector3 } from "../utils";
export type GetGroupsPayload = {
coalitionId: number;
groupCategory?: number;
};
export type GetGroupsReturn = {
category: number;
coalition: number;
name: string;
id: number;
initialSize: number;
size: number;
};
/**
* Allows to retrieve informations about groups from a coalition
* @param coalitionId The id of the coalition you want to retrieve the groups from. Note: you should use the constants in the COALITIONS object
* @param groupCategory The category of the group, I'm not sure what it corresponds to
*/
export const getGroups = (coalitionId: number, groupCategory?: number) =>
executeFunction<GetGroupsPayload, GetGroupsReturn[]>("getGroups", {
coalitionId,
groupCategory
});
export type GetUnitsPayload = {
groupName: string;
};
export type GetUnitsReturn = {
ammo: {
count: number;
desc: {
[key: string]: any;
};
}[];
callsign: string;
desc: { [key: string]: any };
fuel: number;
hasRadar: boolean;
id: number;
inAir: boolean;
isActive: boolean;
life: number;
life0: number;
name: string;
position: Vector3;
sensors: { [key: string]: any }[];
type: string;
velocity: Vector3;
};
/**
* Allows to retrieve informations about units from a group
* @param groupName The name of the group you want to retrieve the units from
*/
export const getUnits = (groupName: string) =>
executeFunction<GetUnitsPayload, GetUnitsReturn[]>("getUnits", { groupName });
/**
* Allows to execute any function in the lua `niod.function` table
* @typeParam T The type of the arguments you want to pass to the function.
* @typeParam R The type of the result you expect.
* @param functionName The name of the function you want to execute
* @param args The arguments you want to pass to the function
*/
export const executeFunction = <T, R>(functionName: string, args: T) =>
new Promise<R>((resolve, reject) =>
sendMessage(
createMessage<R>(
"function",
{
functionName,
args
},
result => resolve(result)
)
)
);