-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHermitowskiePlikiMapy.js
314 lines (284 loc) · 11.5 KB
/
HermitowskiePlikiMapy.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* Script for downloading map files and configs.
* Created by Hermitowski on 04/08/2019
*
* 2023-03-17: added option for fetching recent conquers
*/
async function get_world_info(settings) {
const start = Date.now();
const namespace = 'Hermitowski.MapFiles';
const _bonuses = {
0: 'none',
1: 'wood',
2: 'stone',
3: 'iron',
4: 'farm',
5: 'barracks',
6: 'stable',
7: 'garage',
8: 'eco',
9: 'storage',
};
const _regex = new RegExp(/\+/, 'g');
const _decode = function (encodedString) {
return decodeURIComponent(encodedString.replace(_regex, ' '));
}
const _bonus = function (id) {
return _bonuses[id];
}
const entities_mapping_info = {
// sts - server to storage mapper
// sto - storage to object mapper
player: {
id: { idx: 0, sts_mapper: Number, sto_mapper: String },
name: { idx: 1, sts_mapper: _decode, },
ally_id: { idx: 2, sts_mapper: Number, sto_mapper: String },
villages_count: { idx: 3, sts_mapper: Number },
points: { idx: 4, sts_mapper: Number },
ranking: { idx: 5, sts_mapper: Number }
},
ally: {
id: { idx: 0, sts_mapper: Number, sto_mapper: String },
name: { idx: 1, sts_mapper: _decode, },
tag: { idx: 2, sts_mapper: _decode, },
players_count: { idx: 3, sts_mapper: Number, },
villages_count: { idx: 4, sts_mapper: Number },
top40_points: { idx: 5, sts_mapper: Number },
points: { idx: 6, sts_mapper: Number },
ranking: { idx: 7, sts_mapper: Number }
},
village: {
id: { idx: 0, sts_mapper: Number, sto_mapper: String },
name: { idx: 1, sts_mapper: _decode },
x: { idx: 2, sts_mapper: Number },
y: { idx: 3, sts_mapper: Number },
player_id: { idx: 4, sts_mapper: Number, sto_mapper: String },
points: { idx: 5, sts_mapper: Number },
bonus: { idx: 6, sts_mapper: Number, sto_mapper: _bonus }
}
}
function get_key(enity_name, column_name) {
let key = `${namespace}.${enity_name}`;
if (column_name) {
key += `.${column_name}`;
}
return key;
}
function get_item(key) {
const value = localStorage.getItem(key);
return value
? JSON.parse(value)
: null;
}
function remove_item(key) {
localStorage.removeItem(key);
}
function set_item(key, item) {
localStorage.setItem(key, JSON.stringify(item));
}
function get_json_from_xml_string(xml_string) {
const parser = new window.DOMParser();
const document = parser.parseFromString(xml_string, 'text/xml');
return convert_xml_to_json(document.children[0]);
}
function convert_xml_to_json(root) {
let obj = {};
if (root.childElementCount === 0) {
return root.textContent;
}
for (const node of root.children) {
obj[node.nodeName] = convert_xml_to_json(node);
}
return obj;
}
function random(min, max) {
return Math.random() * (max - min) + min;
}
function validate_entity_cache(entity_name) {
const entity_key = get_key(entity_name);
const entity_info = get_item(entity_key);
if (entity_info) {
if (entity_info.expiration_time < Date.now()) {
for (const column_name of entity_info.column_names) {
const column_key = get_key(entity_name, column_name);
remove_item(column_key);
}
remove_item(entity_key);
}
}
}
async function save_entity(entity_name, columns, last_modified) {
const key = get_key(entity_name);
const entity_info = get_item(key);
// if entity does not exist in local storage, set expiration time to next update
// add some random delay to distribute requests to server
const expiration_time = entity_info
? entity_info.expiration_time
: (last_modified.getTime() + (3600 + random(15, 120)) * 1000);
const column_names = entity_info
? [new Set(...Object.keys(columns), ...entity_info.column_names)]
: Object.keys(columns);
for (const column_name in columns) {
const column_key = get_key(entity_name, column_name);
set_item(column_key, columns[column_name]);
}
set_item(key, { expiration_time, column_names });
}
async function fetch_missing_columns_from_server(entity_name, column_names) {
const response = await fetch(`/map/${entity_name}.txt`)
const last_modified = new Date(response.headers.get('last-modified'))
const text = await response.text();
const lines = text.split('\n').filter(x => x.length > 0);
const columns = Object.fromEntries(column_names.map(column_name => [column_name, []]));
const entity_mapping_info = entities_mapping_info[entity_name];
for (const line of lines) {
const raw = line.split(',');
for (const column_name of column_names) {
const column_mapping_info = entity_mapping_info[column_name];
const raw_value = raw[column_mapping_info.idx];
const mapped_value = column_mapping_info.sts_mapper(raw_value);
columns[column_name].push(mapped_value);
}
}
await save_entity(entity_name, columns, last_modified);
}
function get_from_local_storage(entity_name, column_names) {
const values = {};
const entity_mapping_info = entities_mapping_info[entity_name];
for (const column_name of column_names) {
const sto_mapper = entity_mapping_info[column_name].sto_mapper;
const column_key = get_key(entity_name, column_name);
const column_values = get_item(column_key);
values[column_name] = sto_mapper
? column_values.map(column_value => sto_mapper(column_value))
: column_values;
}
const length = values[column_names[0]].length;
const records = [];
for (let i = 0; i < length; i++) {
records.push(Object.fromEntries(column_names.map(column_name => [column_name, values[column_name][i]])));
}
return records;
}
function get_missing_columns(entity_name, requested_column_names) {
const key = get_key(entity_name);
const entity_info = get_item(key);
if (!entity_info) {
return requested_column_names;
}
return requested_column_names.filter(column_name => !entity_info.column_names.includes(column_name));
}
async function get_entity_records(entity_name, column_names) {
validate_entity_cache(entity_name);
const missing_columns = get_missing_columns(entity_name, column_names);
if (missing_columns.length != 0) {
await fetch_missing_columns_from_server(entity_name, missing_columns);
}
return [entity_name, get_from_local_storage(entity_name, column_names)];
}
async function get_config(config_name) {
let config = get_config_from_local_storage(config_name);
if (!config) {
config = await get_config_from_server(config_name);
}
return [config_name, config];
}
function get_config_from_local_storage(config_name) {
const key = get_key(config_name);
const item = get_item(key);
return item;
}
async function get_config_from_server(config_name) {
const response = await fetch(`interface.php?func=get_${config_name}`);
const content = await response.text();
const config = get_json_from_xml_string(content);
const key = get_key(config_name);
set_item(key, config);
return config;
}
async function get_conquers(settings) {
let conquers = get_conquers_from_local_storage(settings);
if (!conquers) {
conquers = await get_conquers_from_server();
}
return ['conquers', conquers];
}
function get_conquers_from_local_storage(settings) {
const key = get_key('conquers');
const payload = get_item(key);
const now_s = ~~(Date.now() / 1000);
if (payload) {
let freshness_s = 60;
if (settings.hasOwnProperty('freshness')) {
freshness_s = settings['freshness'];
}
if (freshness_s < 15) {
freshness_s = 15;
}
if ((payload.last_update_s + freshness_s) > now_s) {
return payload;
}
}
return null;
}
async function get_conquers_from_server() {
// -24h + 5 minutes, so we won't get ERR ONLY_ONE_DAY_AGO
const now_s = Date.now();
const since_timestamp_s = ~~((now_s - 24 * 3600 * 1000 + 5 * 60 * 1000) / 1000);
const last_update_s = ~~(now_s / 1000);
const response = await fetch(`/interface.php?func=get_conquer&since=${since_timestamp_s}`);
const content = await response.text();
const lines = content.split('\n');
const conquers = [];
for (let i = 0; i < lines.length; i++) {
const columns = lines[i].split(',');
if (columns.length < 4) { continue; }
conquers.push({
village_id: Number(columns[0]),
timestamp: Number(columns[1]),
owner_new: columns[2],
owner_old: columns[3]
});
}
const payload = { conquers, since_timestamp_s, last_update_s };
const key = get_key('conquers');
set_item(key, payload);
return payload;
}
let requests = [];
if (settings.hasOwnProperty('entities')) {
const entities = settings['entities'];
for (const entity_name of ['player', 'ally', 'village']) {
if (entities.hasOwnProperty(entity_name)) {
requests.push(get_entity_records(entity_name, entities[entity_name]))
}
}
}
if (settings.hasOwnProperty('configs')) {
const configs = settings['configs'];
for (const config_name of ['config', 'building_info', 'unit_info']) {
if (configs.includes(config_name)) {
requests.push(get_config(config_name));
}
}
}
if (settings.hasOwnProperty('conquers')) {
requests.push(get_conquers(settings['conquers']));
}
const results = await Promise.all(requests);
console.log(`${namespace} | Elapsed time: ${Date.now() - start} [ms]`);
return Object.fromEntries(results);
};
// Example:
// world_settings = { // AVAILABLE options
// configs: ['config', 'unit_info'], // config, unit_info, building_info
// entities: {
// 'ally': ['id'], // id, name, tag, players_count, villages_count, top40_points, points, ranking
// 'player': ['id', 'ally_id', 'name'], // id, name, ally_id, villages_count, points, ranking
// 'village': ['id', 'x', 'y', 'player_id'] // id, name, x, y, player_id, points, bonus
// },
// conquers: {
// freshness: 15 // (optional) indicates for how long (in seconds) to cache recent results; default: 60, min: 15
// }
// };
// await get_world_info(world_settings);