-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathq_logist.py
405 lines (375 loc) · 22.4 KB
/
q_logist.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
""" Q.Logist (desktop/mobile)
Prerequisites:
* Have a Python 3 environment available to you (possibly by using a
virtual environment: https://virtualenv.pypa.io/en/stable/).
* Run pip install -r requirements.txt --user with this directory.
or
Run pip install -r requirements.txt with this directory as your root.
* Copy q_industrialist_settings.py.template into q_industrialist_settings.py and
mood for your needs.
* Copy q_logist_settings.py.template into q_logist_settings.py and
mood for your needs.
* Create an SSO application at developers.eveonline.com with the scopes
from g_client_scope list declared in q_industrialist_settings.py and the
callback URL "https://localhost/callback/".
Note: never use localhost as a callback in released applications.
To run this example, make sure you have completed the prerequisites and then
run the following command from this directory as the root:
$ chcp 65001 & @rem on Windows only!
$ python eve_sde_tools.py --cache_dir=~/.q_industrialist
$ python q_logist.py --pilot="Qandra Si" --online --cache_dir=~/.q_industrialist
Required application scopes:
* esi-assets.read_corporation_assets.v1 - Requires role(s): Director
* esi-universe.read_structures.v1 - Requires: access token
"""
import sys
import json
import requests
import eve_esi_interface as esi
import eve_esi_tools
import eve_sde_tools
import console_app
import render_html_logist
import q_industrialist_settings
import q_logist_settings
from __init__ import __version__
# type_id - тип требуемых данных
# * None : корневой, т.е. данные по текущей локации маршрута циносети
# * <number> : значение type_id, поиск которого осуществляется (солнечная система, или топляк)
# при type_id > 0 поиск осуществляется вверх по дереву
# при type_id < 0 поиск осуществляется вниз по дереву
def get_cyno_solar_system_details(location_id, corp_assets_tree, subtype=None):
if not (str(location_id) in corp_assets_tree):
return None
loc_dict = corp_assets_tree[str(location_id)]
if subtype is None:
if (location_id >= 30000000) and (location_id <= 32000000):
solar_system_id = location_id
elif "location_id" in loc_dict: # иногда ESI присылает содержимое контейнеров, которые исчезают из ангаров, кораблей и звёздных систем
solar_system_id = get_cyno_solar_system_details(
loc_dict["location_id"],
corp_assets_tree,
-5 # Solar System (поиск вниз по дереву)
)
else:
solar_system_id = None
badger_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 648) # Badger
venture_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 32880) # Venture
liquid_ozone_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 16273) # Liquid Ozone
indus_cyno_gen_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 52694) # Industrial Cynosural Field Generator
exp_cargohold_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 1317) # Expanded Cargohold I
cargohold_rigs_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 31117) # Small Cargohold Optimization I
nitrogen_isotope_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 17888) # Nitrogen Isotopes
hydrogen_isotope_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 17889) # Hydrogen Isotopes
oxygen_isotope_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 17887) # Oxygen Isotopes
helium_isotope_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 16274) # Helium Isotopes
return {"solar_system": solar_system_id,
"badger": badger_ids,
"venture": venture_ids,
"liquid_ozone": liquid_ozone_ids,
"indus_cyno_gen": indus_cyno_gen_ids,
"exp_cargohold": exp_cargohold_ids,
"cargohold_rigs": cargohold_rigs_ids,
"nitrogen_isotope": nitrogen_isotope_ids,
"hydrogen_isotope": hydrogen_isotope_ids,
"oxygen_isotope": oxygen_isotope_ids,
"helium_isotope": helium_isotope_ids}
else:
type_id = loc_dict["type_id"] if "type_id" in loc_dict else None
if not (type_id is None) and (type_id == abs(subtype)): # нашли
return location_id # выдаём как item_id
if subtype < 0:
if "location_id" in loc_dict:
return get_cyno_solar_system_details(loc_dict["location_id"], corp_assets_tree, subtype)
else:
return None
else: # subtype > 0
result = []
items = loc_dict["items"] if "items" in loc_dict else None
if not (items is None):
for i in items:
item_ids = get_cyno_solar_system_details(i, corp_assets_tree, subtype)
if not (item_ids is None):
if isinstance(item_ids, list):
result.extend(item_ids)
else:
result.append(item_ids)
if len(result) > 0:
return result
else:
return None
return None
def main():
# работа с параметрами командной строки, получение настроек запуска программы, как то: работа в offline-режиме,
# имя пилота ранее зарегистрированного и для которого имеется аутентификационный токен, регистрация нового и т.д.
argv_prms = console_app.get_argv_prms()
sde_inv_names = eve_sde_tools.read_converted(argv_prms["workspace_cache_files_dir"], "invNames")
sde_inv_items = eve_sde_tools.read_converted(argv_prms["workspace_cache_files_dir"], "invItems")
sde_inv_positions = eve_sde_tools.read_converted(argv_prms["workspace_cache_files_dir"], "invPositions")
logist_data = []
for pilot_name in argv_prms["character_names"]:
# настройка Eve Online ESI Swagger interface
auth = esi.EveESIAuth(
'{}/auth_cache'.format(argv_prms["workspace_cache_files_dir"]),
debug=True)
client = esi.EveESIClient(
auth,
q_industrialist_settings.g_client_id,
keep_alive=True,
debug=argv_prms["verbose_mode"],
logger=True,
user_agent='Q.Industrialist v{ver}'.format(ver=__version__),
restrict_tls13=q_industrialist_settings.g_client_restrict_tls13)
interface = esi.EveOnlineInterface(
client,
q_industrialist_settings.g_client_scope,
cache_dir='{}/esi_cache'.format(argv_prms["workspace_cache_files_dir"]),
offline_mode=argv_prms["offline_mode"])
authz = interface.authenticate(pilot_name)
character_id = authz["character_id"]
character_name = authz["character_name"]
# Public information about a character
character_data = interface.get_esi_data(
"characters/{}/".format(character_id),
fully_trust_cache=True)
# Public information about a corporation
corporation_data = interface.get_esi_data(
"corporations/{}/".format(character_data["corporation_id"]),
fully_trust_cache=True)
corporation_id = character_data["corporation_id"]
corporation_name = corporation_data["name"]
print("\n{} is from '{}' corporation".format(character_name, corporation_name))
sys.stdout.flush()
# Requires role(s): Director
corp_assets_data = interface.get_esi_paged_data(
"corporations/{}/assets/".format(corporation_id))
print("\n'{}' corporation has {} assets".format(corporation_name, len(corp_assets_data)))
sys.stdout.flush()
# Получение названий контейнеров, станций, и т.п. - всё что переименовывается ingame
corp_ass_named_ids = eve_esi_tools.get_assets_named_ids(corp_assets_data)
# Requires role(s): Director
corp_ass_names_data = interface.get_esi_piece_data(
"corporations/{}/assets/names/".format(corporation_id),
corp_ass_named_ids)
print("\n'{}' corporation has {} custom asset's names".format(corporation_name, len(corp_ass_names_data)))
sys.stdout.flush()
del corp_ass_named_ids
# Поиск тех станций, которые не принадлежат корпорации (на них имеется офис, но самой станции в ассетах нет)
foreign_structures_data = {}
foreign_structures_ids = eve_esi_tools.get_foreign_structures_ids(corp_assets_data)
foreign_structures_forbidden_ids = []
if len(foreign_structures_ids) > 0:
# Requires: access token
for structure_id in foreign_structures_ids:
try:
universe_structure_data = interface.get_esi_data(
"universe/structures/{}/".format(structure_id),
fully_trust_cache=True)
foreign_structures_data.update({str(structure_id): universe_structure_data})
except requests.exceptions.HTTPError as err:
status_code = err.response.status_code
if status_code == 403: # это нормально, что часть структур со временем могут оказаться Forbidden
foreign_structures_forbidden_ids.append(structure_id)
else:
raise
except:
print(sys.exc_info())
raise
print("\n'{}' corporation has offices in {} foreign stations".format(corporation_name, len(foreign_structures_data)))
if len(foreign_structures_forbidden_ids) > 0:
print("\n'{}' corporation has offices in {} forbidden stations : {}".format(corporation_name, len(foreign_structures_forbidden_ids), foreign_structures_forbidden_ids))
sys.stdout.flush()
# Построение дерева ассетов, с узлами в роли станций и систем, и листьями в роли хранящихся
# элементов, в виде:
# { location1: {items:[item1,item2,...],type_id,location_id},
# location2: {items:[item3],type_id} }
corp_assets_tree = eve_esi_tools.get_assets_tree(corp_assets_data, foreign_structures_data, sde_inv_items)
eve_esi_tools.dump_debug_into_file(argv_prms["workspace_cache_files_dir"], "corp_assets_tree.{}".format(corporation_name), corp_assets_tree)
logist_data.append({
"corporation_ticker": corporation_data["ticker"],
"corp_assets_tree": corp_assets_tree,
"corp_assets_data": corp_assets_data,
})
del corp_assets_tree
del corp_assets_data
del foreign_structures_data
# Фильтрация (вручную) ассетов, которые расположены на станках циносети
corp_cynonetwork = {}
for cn in q_logist_settings.g_cynonetworks:
cn_route = cn["route"]
jump_num = 1
for location_id in cn_route:
# если системы в цино сети повторяются, не гоняем искалочку зазря (повторно)
if not (str(location_id) in corp_cynonetwork):
datas = []
found_tickers = []
for corp_logist in logist_data:
data = get_cyno_solar_system_details(location_id, corp_logist["corp_assets_tree"])
if data is None:
continue
datas.append(data)
datas[-1].update({"corporation_ticker": corp_logist["corporation_ticker"]})
found_tickers.append(corp_logist["corporation_ticker"])
# ---
# signalling_level = 0 - normal, 1 - warning, 2 - danger, 3 - ошибка получения данных
# оптимальный набор: 10 баджеров, 10 цин, 10'000 (по 950 на прожиг) озона
# плюс: 10 вентур, 10 цин, 10'000 (по 200 на прожиг) озона, 30 риг, 10 каргохолда
# минимальный набор: 1 баджер, 1 вентурка, 2 цины, 1150 озона, 3 риги, 1 каргохолд
if not datas:
# print('{} {}'.format(location_id, data))
system_id = None
loc_name = "NO-DATA!"
loc_id = location_id
if int(loc_id) < 1000000000000:
if str(loc_id) in sde_inv_names:
loc_name = sde_inv_names[str(loc_id)]
# print(loc_name)
if str(loc_id) in sde_inv_items:
root_item = sde_inv_items[str(loc_id)]
# print("root_item", root_item)
if root_item["typeID"] == 5: # Solar System
system_id = loc_id
# print(" >>> >>> ", loc_name)
else: # not Solar System (may be Station?)
loc_id = root_item["locationID"]
root_item = sde_inv_items[str(loc_id)]
# print(" >>> ", loc_id, root_item)
if root_item["typeID"] == 5: # Solar System
system_id = loc_id
loc_name = sde_inv_names[str(loc_id)] # Solar System (name)
# print(" >>> >>> ", loc_name)
data = {"error": "no data" if system_id is None else "no solar system",
"system_id": system_id,
"solar_system": loc_name,
"signalling_level": 3,
# -- используется, если 'error'='no solar system'
"badger": 0,
"venture": 0,
"liquid_ozone": 0,
"indus_cyno_gen": 0,
"exp_cargohold": 0,
"cargohold_rigs": 0,
"nitrogen_isotope": 0,
"hydrogen_isotope": 0,
"oxygen_isotope": 0,
"helium_isotope": 0}
else:
system_id = None
badger_num = 0
venture_num = 0
liquid_ozone_num = 0
indus_cyno_gen_num = 0
exp_cargohold_num = 0
cargohold_rigs_num = 0
nitrogen_isotope_num = 0
hydrogen_isotope_num = 0
oxygen_isotope_num = 0
helium_isotope_num = 0
# ---
for data in datas:
corporation_ticker = data["corporation_ticker"]
if system_id is None:
system_id = data["solar_system"]
badger_ids = data["badger"]
venture_ids = data["venture"]
liquid_ozone_ids = data["liquid_ozone"]
indus_cyno_gen_ids = data["indus_cyno_gen"]
exp_cargohold_ids = data["exp_cargohold"]
cargohold_rigs_ids = data["cargohold_rigs"]
nitrogen_isotope_ids = data["nitrogen_isotope"]
hydrogen_isotope_ids = data["hydrogen_isotope"]
oxygen_isotope_ids = data["oxygen_isotope"]
helium_isotope_ids = data["helium_isotope"]
# ---
corp_assets_data = []
for corp_logist in logist_data:
if corporation_ticker == corp_logist["corporation_ticker"]:
corp_assets_data = corp_logist["corp_assets_data"]
break
# ---
for a in corp_assets_data:
item_id = int(a["item_id"])
quantity = int(a["quantity"])
if not (badger_ids is None) and badger_ids.count(item_id) > 0:
badger_num = badger_num + quantity
elif not (venture_ids is None) and venture_ids.count(item_id) > 0:
venture_num = venture_num + quantity
elif not (liquid_ozone_ids is None) and liquid_ozone_ids.count(item_id) > 0:
liquid_ozone_num = liquid_ozone_num + quantity
elif not (indus_cyno_gen_ids is None) and indus_cyno_gen_ids.count(item_id) > 0:
indus_cyno_gen_num = indus_cyno_gen_num + quantity
elif not (exp_cargohold_ids is None) and exp_cargohold_ids.count(item_id) > 0:
exp_cargohold_num = exp_cargohold_num + quantity
elif not (cargohold_rigs_ids is None) and cargohold_rigs_ids.count(item_id) > 0:
cargohold_rigs_num = cargohold_rigs_num + quantity
elif not (nitrogen_isotope_ids is None) and nitrogen_isotope_ids.count(item_id) > 0:
nitrogen_isotope_num = nitrogen_isotope_num + quantity
elif not (hydrogen_isotope_ids is None) and hydrogen_isotope_ids.count(item_id) > 0:
hydrogen_isotope_num = hydrogen_isotope_num + quantity
elif not (oxygen_isotope_ids is None) and oxygen_isotope_ids.count(item_id) > 0:
oxygen_isotope_num = oxygen_isotope_num + quantity
elif not (helium_isotope_ids is None) and helium_isotope_ids.count(item_id) > 0:
helium_isotope_num = helium_isotope_num + quantity
# ---
del corp_assets_data
if system_id is None:
system_name = "NO-DATA!"
signalling_level = 3
else:
system_name = sde_inv_names[str(system_id)]
if (badger_num >= 10) and\
(venture_num >= 10) and\
(liquid_ozone_num >= 20000) and\
(indus_cyno_gen_num >= 20) and\
(exp_cargohold_num >= 10) and\
(cargohold_rigs_num >= 30):
signalling_level = 0
elif (badger_num >= 1) and \
(venture_num >= 1) and \
(liquid_ozone_num >= 1150) and \
(indus_cyno_gen_num >= 2) and \
(exp_cargohold_num >= 1) and \
(cargohold_rigs_num >= 3):
signalling_level = 1
else:
signalling_level = 2
# ---
data = {
"found_tickers": found_tickers,
"system_id": system_id,
"solar_system": system_name,
"badger": badger_num,
"venture": venture_num,
"liquid_ozone": liquid_ozone_num,
"indus_cyno_gen": indus_cyno_gen_num,
"exp_cargohold": exp_cargohold_num,
"cargohold_rigs": cargohold_rigs_num,
"nitrogen_isotope": nitrogen_isotope_num,
"hydrogen_isotope": hydrogen_isotope_num,
"oxygen_isotope": oxygen_isotope_num,
"helium_isotope": helium_isotope_num,
"signalling_level": signalling_level,
}
if system_id is None:
data.update({"error": "no solar system"})
corp_cynonetwork.update({str(location_id): data})
del datas
jump_num = jump_num + 1
eve_esi_tools.dump_debug_into_file(argv_prms["workspace_cache_files_dir"], "corp_cynonetwork", corp_cynonetwork)
del logist_data
del sde_inv_items
del sde_inv_names
print("\nBuilding cyno network report...")
sys.stdout.flush()
render_html_logist.dump_cynonetwork_into_report(
# путь, где будет сохранён отчёт
argv_prms["workspace_cache_files_dir"],
# sde данные, загруженные из .converted_xxx.json файлов
sde_inv_positions,
# данные, полученные в результате анализа и перекомпоновки входных списков
corp_cynonetwork
)
# Вывод в лог уведомления, что всё завершилось (для отслеживания с помощью tail)
print("\nDone")
if __name__ == "__main__":
main()