@antti5/ship-tool-common 1.1.1 → 1.1.3
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/bin/repos +1 -1
- package/lib/Semaphore.ts +1 -2
- package/lib/Util.ts +7 -7
- package/lib/services/publicApi.ts +236 -0
- package/package.json +8 -6
- package/lib/trajectory/test.js +0 -9
package/bin/repos
CHANGED
package/lib/Semaphore.ts
CHANGED
|
@@ -15,11 +15,10 @@ class Semaphore {
|
|
|
15
15
|
#queue: QueueItem[] = [];
|
|
16
16
|
|
|
17
17
|
#rateLimit: number = 0;
|
|
18
|
-
#rateLimitInterval:
|
|
18
|
+
#rateLimitInterval: NodeJS.Timeout | null = null;
|
|
19
19
|
|
|
20
20
|
#waiters: Waiter[] = [];
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
constructor(max = 1, { rateLimit = 0 }: { rateLimit?: number } = {}) {
|
|
24
23
|
this.#max = max;
|
|
25
24
|
this.#rateLimit = rateLimit;
|
package/lib/Util.ts
CHANGED
|
@@ -38,13 +38,13 @@ class Util {
|
|
|
38
38
|
timeout,
|
|
39
39
|
debug
|
|
40
40
|
}: {
|
|
41
|
-
urlSearchParams
|
|
42
|
-
method
|
|
43
|
-
body
|
|
44
|
-
headers
|
|
45
|
-
timeout
|
|
46
|
-
debug
|
|
47
|
-
}
|
|
41
|
+
urlSearchParams?: Record<string,string>,
|
|
42
|
+
method?: string,
|
|
43
|
+
body?: unknown,
|
|
44
|
+
headers?: Record<string,string>,
|
|
45
|
+
timeout?: number,
|
|
46
|
+
debug?: boolean
|
|
47
|
+
} = {}
|
|
48
48
|
) {
|
|
49
49
|
|
|
50
50
|
if (urlSearchParams)
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import Util from '../Util.ts';
|
|
3
|
+
import Semaphore from '../Semaphore.ts';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/* Helper function to call any of the API endpoints */
|
|
7
|
+
|
|
8
|
+
const WgResponseSchema = z.object({
|
|
9
|
+
status: z.string(),
|
|
10
|
+
|
|
11
|
+
// Error is present when status is not "ok"
|
|
12
|
+
|
|
13
|
+
error: z.optional(z.object({
|
|
14
|
+
message: z.string()
|
|
15
|
+
})),
|
|
16
|
+
|
|
17
|
+
// Meta is present when status is "ok"
|
|
18
|
+
|
|
19
|
+
meta: z.optional(z.object({
|
|
20
|
+
page: z.number().optional(),
|
|
21
|
+
page_total: z.number().optional()
|
|
22
|
+
})),
|
|
23
|
+
|
|
24
|
+
// Depending on the response, data is either an object or an array of objects.
|
|
25
|
+
// In either case, the value is parsed with another schema.
|
|
26
|
+
|
|
27
|
+
data: z.union([z.looseObject({}), z.array(z.unknown())])
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const callApi = async (url: string, options = {}) => {
|
|
31
|
+
const data = await Util.fetch(url, options);
|
|
32
|
+
const response = WgResponseSchema.parse(data);
|
|
33
|
+
|
|
34
|
+
if (response.status !== 'ok')
|
|
35
|
+
throw new Error(`Wargaming API error: ${response?.error?.message}`);
|
|
36
|
+
|
|
37
|
+
return response;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
/* /encyclopedia/info -- Contains current game version */
|
|
42
|
+
|
|
43
|
+
const EncyclopediaInfoSchema = z.object({
|
|
44
|
+
game_version: z.string()
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const encyclopediaInfo = async (apiId: string, { debug = false } = {}) => {
|
|
48
|
+
const { data } = await callApi(
|
|
49
|
+
`https://api.worldofwarships.eu/wows/encyclopedia/info/?application_id=${apiId}`,
|
|
50
|
+
{ debug }
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return EncyclopediaInfoSchema.parse(data);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
/* /encyclopedia/ships -- List of ships in the current game version */
|
|
58
|
+
|
|
59
|
+
const EncyclopediaShipsSchema = z.record(
|
|
60
|
+
z.number(),
|
|
61
|
+
z.object({
|
|
62
|
+
ship_id: z.number(),
|
|
63
|
+
tier: z.number(),
|
|
64
|
+
ship_id_str: z.string()
|
|
65
|
+
})
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const encyclopediaShips = async (apiId: string, { concurrency = 1, debug = false } = {}) => {
|
|
69
|
+
const { meta, data } = await callApi(
|
|
70
|
+
`https://api.worldofwarships.eu/wows/encyclopedia/ships/?application_id=${apiId}`,
|
|
71
|
+
{ debug }
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
if (!meta || !meta.page_total)
|
|
75
|
+
throw new Error('Unexpected /encyclopedia/ships response');
|
|
76
|
+
|
|
77
|
+
const ships = EncyclopediaShipsSchema.parse(data);
|
|
78
|
+
console.debug(`Loaded encyclopedia page ${meta.page} / ${meta.page_total}`);
|
|
79
|
+
|
|
80
|
+
const sem = new Semaphore(concurrency);
|
|
81
|
+
|
|
82
|
+
for (let page = 2; page <= meta.page_total; page++)
|
|
83
|
+
void sem.run(async () => {
|
|
84
|
+
const { data } = await callApi(`https://api.worldofwarships.eu/wows/encyclopedia/ships/?application_id=${apiId}&page_no=${page}`);
|
|
85
|
+
Object.assign(ships, EncyclopediaShipsSchema.parse(data));
|
|
86
|
+
console.debug(`Loaded encyclopedia page ${page} / ${meta.page_total}`);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
await sem.wait();
|
|
90
|
+
return ships;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
/* /clans/list -- Clans whose tags match the given string
|
|
95
|
+
|
|
96
|
+
Note: The implementation only returns the one clan whose tag is an exact match */
|
|
97
|
+
|
|
98
|
+
const ClansListSchema = z.array(
|
|
99
|
+
z.object({
|
|
100
|
+
clan_id: z.number(),
|
|
101
|
+
tag: z.string(),
|
|
102
|
+
name: z.string()
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const clansList = async (apiId: string, region: string, clanTag: string, { debug = false } = {}) => {
|
|
107
|
+
const { data } = await callApi(
|
|
108
|
+
`https://api.worldofwarships.${region}/wows/clans/list/?application_id=${apiId}&search=${clanTag}`,
|
|
109
|
+
{ debug }
|
|
110
|
+
);
|
|
111
|
+
const response = ClansListSchema.parse(data);
|
|
112
|
+
const clanInfo = response.find(({ tag }) => tag === clanTag);
|
|
113
|
+
|
|
114
|
+
if (!clanInfo)
|
|
115
|
+
throw new Error('Clan tag not found in /clans/list response: ' + clanTag);
|
|
116
|
+
|
|
117
|
+
return clanInfo;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
/* /clans/info -- Information about the given clan, including a list of members */
|
|
122
|
+
|
|
123
|
+
const ClansInfoSchema = z.record(
|
|
124
|
+
z.number(),
|
|
125
|
+
z.nullable(z.object({ // null for invalid clan identifier
|
|
126
|
+
clan_id: z.number(),
|
|
127
|
+
members_ids: z.array(
|
|
128
|
+
z.number()
|
|
129
|
+
),
|
|
130
|
+
members: z.record(
|
|
131
|
+
z.number(),
|
|
132
|
+
z.object({
|
|
133
|
+
account_id: z.number(),
|
|
134
|
+
account_name: z.string(),
|
|
135
|
+
role: z.string(),
|
|
136
|
+
joined_at: z.number()
|
|
137
|
+
})
|
|
138
|
+
)
|
|
139
|
+
}))
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const clansInfoMany = async (apiId: string, region: string, clanIds: number[], { debug = false } = {}) => {
|
|
143
|
+
const { data } = await callApi(
|
|
144
|
+
`https://api.worldofwarships.${region}/wows/clans/info/?application_id=${apiId}&clan_id=${clanIds.join(',')}&extra=members`,
|
|
145
|
+
{ debug }
|
|
146
|
+
);
|
|
147
|
+
return ClansInfoSchema.parse(data);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const clansInfoOne = async (apiId: string, region: string, clanId: number) => {
|
|
151
|
+
const response = await clansInfoMany(apiId, region, [clanId]);
|
|
152
|
+
|
|
153
|
+
if (!response[clanId])
|
|
154
|
+
throw new Error('Clan not found in /clans/info response: ' + clanId);
|
|
155
|
+
|
|
156
|
+
return response[clanId];
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
/* /account/info -- Information about given players */
|
|
161
|
+
|
|
162
|
+
const AccountInfoSchema = z.record(
|
|
163
|
+
z.number(),
|
|
164
|
+
z.object({
|
|
165
|
+
account_id: z.number(),
|
|
166
|
+
nickname: z.string(),
|
|
167
|
+
created_at: z.number(),
|
|
168
|
+
last_battle_time: z.number(),
|
|
169
|
+
statistics: z.nullable(z.object({ // null for private accounts
|
|
170
|
+
pvp: z.optional(z.object({
|
|
171
|
+
battles: z.number(),
|
|
172
|
+
wins: z.number()
|
|
173
|
+
})),
|
|
174
|
+
pvp_solo: z.optional(z.object({
|
|
175
|
+
battles: z.number()
|
|
176
|
+
}))
|
|
177
|
+
}))
|
|
178
|
+
})
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
const accountInfoMany = async (apiId: string, region: string, accountIds: number[], { debug = false } = {}) => {
|
|
182
|
+
const { data } = await callApi(
|
|
183
|
+
`https://api.worldofwarships.${region}/wows/account/info/?application_id=${apiId}&account_id=${accountIds.join(',')}&extra=statistics.pvp_solo`,
|
|
184
|
+
{ debug }
|
|
185
|
+
);
|
|
186
|
+
return AccountInfoSchema.parse(data);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const accountInfoOne = async (apiId: string, region: string, accountId: number, options = {}) => {
|
|
190
|
+
const response = await accountInfoMany(apiId, region, [accountId], options);
|
|
191
|
+
|
|
192
|
+
if (!response[accountId])
|
|
193
|
+
throw new Error('Account not found in /account/info response: ' + accountId);
|
|
194
|
+
|
|
195
|
+
return response[accountId];
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
/* /ships/stats -- Given players' stats in different ships */
|
|
200
|
+
|
|
201
|
+
const ShipsStatsSchema = z.record(
|
|
202
|
+
z.number(),
|
|
203
|
+
z.array(
|
|
204
|
+
z.object({
|
|
205
|
+
ship_id: z.number(),
|
|
206
|
+
pvp: z.object({
|
|
207
|
+
battles: z.number()
|
|
208
|
+
}).optional()
|
|
209
|
+
})
|
|
210
|
+
)
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
const shipsStats = async (apiId: string, region: string, accountId: number, { debug = false } = {}) => {
|
|
214
|
+
const { data } = await callApi(
|
|
215
|
+
`https://api.worldofwarships.${region}/wows/ships/stats/?application_id=${apiId}&account_id=${accountId}`,
|
|
216
|
+
{ debug }
|
|
217
|
+
);
|
|
218
|
+
const response = ShipsStatsSchema.parse(data);
|
|
219
|
+
|
|
220
|
+
if (!response[accountId])
|
|
221
|
+
throw new Error('Account not found in /ships/stats response: ' + accountId);
|
|
222
|
+
|
|
223
|
+
return response[accountId];
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
export default {
|
|
228
|
+
encyclopediaInfo,
|
|
229
|
+
encyclopediaShips,
|
|
230
|
+
clansList,
|
|
231
|
+
clansInfoMany,
|
|
232
|
+
clansInfoOne,
|
|
233
|
+
accountInfoMany,
|
|
234
|
+
accountInfoOne,
|
|
235
|
+
shipsStats
|
|
236
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antti5/ship-tool-common",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Ship Tool common utils and library code",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,11 +20,17 @@
|
|
|
20
20
|
"./Semaphore": "./lib/Semaphore.ts",
|
|
21
21
|
"./UID": "./lib/UID.ts",
|
|
22
22
|
"./Trajectory": "./lib/trajectory/Trajectory.ts",
|
|
23
|
-
"./node/Util": "./lib/UtilNode.ts"
|
|
23
|
+
"./node/Util": "./lib/UtilNode.ts",
|
|
24
|
+
"./publicApi": "./lib/services/publicApi.ts"
|
|
24
25
|
},
|
|
25
26
|
"scripts": {
|
|
26
27
|
"eslint": "eslint . --ignore-pattern \"node_modules/*\""
|
|
27
28
|
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"dotenv": "17.4.2",
|
|
31
|
+
"find-config": "1.0.0",
|
|
32
|
+
"zod": "4.6.7"
|
|
33
|
+
},
|
|
28
34
|
"devDependencies": {
|
|
29
35
|
"@eslint/js": "9.39.2",
|
|
30
36
|
"@types/find-config": "1.0.4",
|
|
@@ -32,9 +38,5 @@
|
|
|
32
38
|
"eslint": "9.36.0",
|
|
33
39
|
"globals": "16.4.0",
|
|
34
40
|
"typescript-eslint": "8.73.0"
|
|
35
|
-
},
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"dotenv": "17.4.2",
|
|
38
|
-
"find-config": "1.0.0"
|
|
39
41
|
}
|
|
40
42
|
}
|
package/lib/trajectory/test.js
DELETED