-
Notifications
You must be signed in to change notification settings - Fork 6
/
load.py
545 lines (462 loc) · 18.8 KB
/
load.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# -*- coding: utf-8 -*-
import csv
import functools
import os
import logging
import webbrowser
import traceback
import tkinter as tk
from datetime import datetime
from urllib.parse import quote_plus
from contextlib import closing
from queue import Queue
from semantic_version import Version
### third-party модули ###
import requests
### модули EDMC ###
import l10n
import myNotebook as nb
import plug
import edmc_data
from config import appname
### модули плагина ###
from modules import canonn_api, codex
from modules import friendfoe as FF
from modules import (
legacy,
message_label,
patrol,
release,
bgs
)
from modules.debug import Debug
from modules.lib import http
from modules.lib import context as contextlib
from modules.lib import journal, thread
from modules.release import Release
from modules.systems import SystemsModule
import settings
_ = functools.partial(l10n.Translations.translate, context=__file__)
plugin_name = os.path.basename(os.path.dirname(__file__))
logger = logging.getLogger(f'{appname}.{plugin_name}')
if not logger.hasHandlers():
level = logging.INFO
logger.setLevel(level)
logger_channel = logging.StreamHandler()
logger_formatter = logging.Formatter(f'%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(lineno)d:%(funcName)s: %(message)s')
logger_formatter.default_time_format = '%Y-%m-%d %H:%M:%S'
logger_formatter.default_msec_format = '%s.%03d'
logger_channel.setFormatter(logger_formatter)
logger.addHandler(logger_channel)
# хранилище данных плагина
context = contextlib.global_context
# алиас для совместимости с легаси кодом
this = context
this.log = logger
this.nearloc = {
"Latitude": None,
"Longitude": None,
"Altitude": None,
"Heading": None,
"Time": None,
}
myPlugin = "EDMC-Triumvirate"
this.version = Version(settings.version)
this.SQNag = 0
this.client_version = "{}.{}".format(myPlugin, this.version)
this.body = None
this.body_name = None
this.systemAddress = None
this.odyssey = None
this.odyssey_events = settings.odyssey_events
this.pending_jump_system = None
this.SysFactionState = None
this.DistFromStarLS = None
this.SysFactionAllegiance = None # variable for allegiance of
# controlling faction
this.Nag = 0
this.cmdr_SQID = None # variable for allegiance check
this.CMDR = None
this.SQ = None
this.SRVmode, this.Fightermode = False, False
this.old_time = 0
this.fuel = 0
this.fuel_cons = 0.0
context.help_page_opened = False
context.latest_dashboard_entry = None
def plugin_prefs(parent, cmdr, is_beta):
"""
Return a TK Frame for adding to the EDMC settings dialog.
"""
frame = nb.Frame(parent)
frame.columnconfigure(1, weight=1)
# context.by_class(news.CECNews).draw_settings(frame, cmdr, is_beta, 1)
context.by_class(release.Release).draw_settings(frame, cmdr, is_beta, 1)
context.by_class(patrol.PatrolModule).draw_settings(frame, cmdr, is_beta, 2)
Debug.plugin_prefs(frame, cmdr, is_beta, 3)
this.codexcontrol.plugin_prefs(frame, cmdr, is_beta, 4)
context.by_class(bgs.BGS).draw_settings(frame, cmdr, is_beta, 5)
nb.Label(frame, text=settings.support_message,).grid(row=8, column=0, sticky="NW")
return frame
def prefs_changed(cmdr, is_beta):
"""
Save settings.
"""
for mod in context.modules:
mod.on_settings_changed(cmdr, is_beta)
this.codexcontrol.prefs_changed(cmdr, is_beta)
Debug.prefs_changed()
def Alegiance_get(CMDR, SQ_old):
SQ = this.cmdr_SQID
if CMDR != this.CMDR:
logger.debug("Community Check started")
url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTXE8HCavThmJt1Wshy3GyF2ZJ-264SbNRVucsPUe2rbEgpm-e3tqsX-8K2mwsG4ozBj6qUyOOd4RMe/pub?gid=1832580214&single=true&output=tsv"
with closing(requests.get(url, stream=True)) as r:
try:
reader = csv.reader(r.content.splitlines(),
delimiter='\t') # .decode('utf-8')
next(reader)
except:
reader = csv.reader(r.content.decode(
'utf-8').splitlines(), delimiter='\t')
next(reader)
for row in reader:
cmdr, squadron, SQID = row
if cmdr == CMDR:
try:
SQ = SQID
logger.debug(f"your SQID is {SQ}")
except:
logger.debug("Set SQID Failed")
if SQ != None:
logger.debug("SQ ID IS OK")
this.CMDR = CMDR
this.patrol.sqid = SQ # Функция для отправки данных о
# сквадроне в модули, использовать как
# шаблон
return SQ
else:
if this.Nag == 0:
logger.debug("SQID need to be instaled")
url = "https://docs.google.com/forms/d/e/1FAIpQLSeERKxF6DlrQ3bMqFdceycSlBV0kwkzziIhYD0ctDzrytm8ug/viewform?usp=pp_url"
url += "&entry.42820869=" + quote_plus(CMDR)
this.Nag = this.Nag + 1
logger.debug(f"SQID {url}")
webbrowser.open(url)
else:
return SQ_old
def plugin_start3(plugin_dir):
"""
EDMC вызывает эту функцию при первом запуске плагина (Python 3).
"""
this.plugin_dir = plugin_dir
Debug.setup(logger)
this.journal_entry_processor = JournalEntryProcessor()
this.journal_entry_processor.start()
# префикс логов
codex.CodexTypes.plugin_start(plugin_dir)
# в логах пишется с префиксом Triumvirate
logger.info(f"Plugin (v{this.version}) loaded successfully.")
return f"Triumvirate-{this.version}"
def plugin_start(plugin_dir):
"""
EDMC вызывает эту функцию при первом запуске плагина в режиме Python 2.
"""
raise EnvironmentError("Плагин несовместим с версиями EDMC младше 3.50 beta0.")
def plugin_stop():
"""
EDMC is closing
"""
logger.debug("Stopping the plugin")
for mod in context.modules:
mod.close()
thread.Thread.stop_all()
def kill_notification():
this.wrongEDMCBanner.destroy()
this.wrongEDMCBannerInstructions.destroy()
this.hyperlink.destroy()
this.button.destroy()
def plugin_app(parent):
this.parent = parent
frame = this.frame = tk.Frame(parent)
frame.columnconfigure(0, weight=1)
rows = tk.Frame(frame)
rows.grid_columnconfigure(1, weight=1)
rows.grid(sticky="NSEW")
table = tk.Frame(rows)
table.columnconfigure(2, weight=1)
table.grid(sticky="NSEW")
this.codexcontrol = codex.CodexTypes(table, 0)
this.systems_module = SystemsModule()
this.canonn_rt_api = canonn_api.CanonnRealtimeAPI()
rel = release.Release(this.plugin_dir, table, this.version, 1)
this.patrol = patrol.PatrolModule(table, 2)
this.bgs_module = bgs.BGS()
this.modules = [
rel,
this.patrol,
this.systems_module,
this.bgs_module,
this.canonn_rt_api
]
# лейбл, в котором содержится текст из вывода модулей
this.message_label = message_label.MessageLabel(rows, row=3)
for mod in context.modules:
mod.on_start(context.plugin_dir)
return frame
def Squadronsend(CMDR, entry):
if this.SQNag == 0:
logger.debug("SQName need to be sended")
url = "https://docs.google.com/forms/d/e/1FAIpQLScZvs3MB2AK6pPwFoSCpdaarfAeu_P-ineIhtO1mOPgr09q8A/formResponse?usp=pp_url"
url += "&entry.558317192=" + quote_plus(CMDR)
url += "&entry.1042067605=" + quote_plus(entry)
this.SQNag = this.Nag + 1
legacy.Reporter(url).start()
def journal_entry(cmdr, is_beta, system, station, entry, state):
this.journal_entry_processor(
cmdr, is_beta, system, station, entry, state,
this.body_name, # эти три параметра обновляются через dashboard_entry и
this.nearloc["Latitude"], # могут измениться к моменту, как мы дойдём до обработки записи в очереди.
this.nearloc["Longitude"] # почему не берём body_name из state - см. https://github.com/EDCD/EDMarketConnector/blob/main/PLUGINS.md#journal-entry
)
class JournalEntryProcessor(thread.Thread):
def __init__(self):
super().__init__(name="Triumvirate journal entry processor")
self.queue = Queue()
def __call__(self, cmdr, is_beta, system, station, entry, state, body_name, lat, lon):
self.queue.put((cmdr, is_beta, system, station, entry, state, body_name, lat, lon))
def do_run(self):
while True:
if self.queue.empty():
self.sleep(1)
else:
try:
self.__process_entry()
except:
logger.error(traceback.format_exc())
# TODO: отправка логов
def __process_entry(self):
cmdr, is_beta, system, station, entry, state, body, lat, lon = self.queue.get(block = False)
SysFactionState = this.SysFactionState,
SysFactionAllegiance = this.SysFactionAllegiance
DistFromStarLS = this.DistFromStarLS
client = this.client_version
# capture some stats when we launch
# not read for that yet
startup_stats(cmdr)
# отключено, потому что API не работает, а логи засоряются
#if entry["event"] == "Scan" and entry["ScanType"] in {"Detailed", "AutoScan"}:
#thread.BasicThread(target=lambda: submit_expedition(cmdr, entry)).start()
if this.odyssey == None:
if entry["event"] == "LoadGame": # ВАЖНО: Fileheader даёт Odyssey=true и в Горизонтах тоже
this.odyssey = entry.get("Odyssey", False)
# ивенты, возможные лишь в Одиссее
elif entry["event"] in this.odyssey_events:
this.odyssey = True
if "SystemFaction" in entry:
""" "SystemFaction": { “Name”:"Mob of Eranin", "FactionState":"CivilLiberty" } }"""
SystemFaction = entry.get("SystemFaction")
try:
this.SysFactionState = SystemFaction["FactionState"]
except:
this.SysFactionState = None
logger.debug("SysFaction's state is" + str(this.SysFactionState))
if "SystemAllegiance" in entry:
SystemAllegiance = entry.get("SystemAllegiance")
logger.debug(SystemAllegiance)
try:
this.SysFactionAllegiance = SystemAllegiance
except:
this.SysFactionAllegiance = None
logger.debug("SysFaction's allegiance is" + str(this.SysFactionAllegiance))
if "DistFromStarLS" in entry:
""""DistFromStarLS":144.821411"""
try:
this.DistFromStarLS = entry.get("DistFromStarLS")
except:
this.DistFromStarLS = None
logger.debug("DistFromStarLS=" + str(this.DistFromStarLS))
# иногда FSDJump приходит позже, чем нам хотелось бы, и имеющаяся у нас система не совпадает с реальной
if (
"SystemAddress" in entry
and this.systemAddress != entry["SystemAddress"]
and entry["event"] != "NavRoute"
):
if entry["event"] == "StartJump":
# мы ещё в актуальной системе, но сохраним ту, в которую прыгаем
this.pending_jump_system = entry.get("StarSystem")
else:
# мы уже прыгнули, но FSDJump в логах пока не получили
this.systemAddress = entry["SystemAddress"]
system = this.pending_jump_system or system # чисто на всякий случай
systemAddress = this.systemAddress
if entry.get("event") == "FSDJump":
this.DistFromStarLS = None
this.body = None
this.pending_jump_system = None
if "Body" in entry:
this.body = entry["Body"]
logger.debug(f"Body: {this.body}")
if entry["event"] == "JoinedSquadron":
Squadronsend(cmdr, entry["SquadronName"])
x, y, z = None, None, None
if system:
val = this.systems_module.get_system_coords(system)
if val is not None:
x, y, z = val
journal_entry = journal.JournalEntry(
cmdr=cmdr,
is_beta=is_beta,
system=system,
systemAddress=systemAddress,
sys_faction_state=SysFactionState,
sys_faction_allegiance=SysFactionAllegiance,
dist_from_star=DistFromStarLS,
station=station,
data=entry,
state=state,
x=x,
y=y,
z=z,
body=body,
lat=lat,
lon=lon,
client=client,
)
status_message = None
if journal_entry.data["event"] in {"SendText", "ReceiveText"}:
for mod in context.enabled_modules:
# TODO переписать на менеджер контекста?
try:
val = mod.on_chat_message(journal_entry)
except:
logger.error(f"Error while sending chat message to module {mod}", exc_info=1)
# TODO: отправка логов
status_message = status_message or val
else:
for mod in context.enabled_modules:
try:
val = mod.on_journal_entry(journal_entry)
except:
event = entry["event"]
logger.error(f"Error while processing a {event} event in module {mod}", exc_info=1)
# TODO: отправка логов
status_message = status_message or val
this.codexcontrol.journal_entry(
cmdr, is_beta, system, station, entry, state, x, y, z, body, lat, lon, client
)
# legacy logging to google sheets
legacy.GusonExpeditions(cmdr, is_beta, system, entry)
if status_message is not None:
this.message_label.text = status_message
def submit_expedition(cmdr, entry: dict): # не работает
resp = http.WebClient(
settings.cec_url
).post("/api/expeditions/v1/scan/submit", json={
"cmdr": cmdr,
"log": entry
})
if not resp.ok:
logger.error("Ошибка при запросе к %s:\n%s", resp.request.url, resp.text)
def startup_stats(cmdr):
try:
this.first_event
except:
this.first_event = True
addr = requests.get('https://api.ipify.org').text
try:
addr6 = requests.get('https://api6.ipify.org').text
except:
addr6 = addr
url="https://docs.google.com/forms/d/1h7LG5dEi07ymJCwp9Uqf_1phbRnhk1R3np7uBEllT-Y/formResponse?usp=pp_url"
url+="&entry.1181808218="+quote_plus(cmdr)
url+="&entry.254549730="+quote_plus(str(this.version))
url+="&entry.1622540328="+quote_plus(addr)
if addr6 != addr:
url+="&entry.488844173="+quote_plus(addr6)
else:
url+="&entry.488844173="+quote_plus("0")
url+="&entry.1210213202="+str(Release.get_auto())
legacy.Reporter(url).start()
def fuel_consumption(entry, old_fuel, old_timestamp, old_fuel_cons):
# debug(old_timestamp==entry["timestamp"])
if entry["timestamp"] != old_timestamp and old_fuel != 0:
try:
fuel_cons = (
(old_fuel["FuelMain"] + old_fuel["FuelReservoir"])
- (entry["Fuel"]["FuelMain"] + entry["Fuel"]["FuelReservoir"])
) / float(
(
datetime.strptime(entry["timestamp"], "%Y-%m-%dT%H:%M:%SZ")
- old_timestamp
).total_seconds()
)
except ZeroDivisionError:
return old_fuel_cons
# debug("Fuel consumption is
# "+str(fuel_cons))
return fuel_cons
else:
# debug("Can't calculate fuel
# consumption")
return old_fuel_cons
this.FuelCount = 9
this.plug_start = False
def dashboard_entry(cmdr, is_beta, entry):
context.latest_dashboard_entry = entry
if this.plug_start == 0:
this.plug_start = 1
this.old_time = datetime.strptime(entry["timestamp"], "%Y-%m-%dT%H:%M:%SZ")
try:
# logger.debug(f"Checking fuel consumption {this.FuelCount}")
if entry.get("Fuel") is not None:
if this.FuelCount == 10:
this.fuel_cons = fuel_consumption(
entry, this.fuel, this.old_time, this.fuel_cons
)
this.old_time = datetime.strptime(entry["timestamp"], "%Y-%m-%dT%H:%M:%SZ")
this.fuel = entry["Fuel"]
this.FuelCount = 0
else:
this.FuelCount += 1
except NameError:
# debug("Can't check fuel
# consumption, waiting for
# data")
this.fuel_cons = 0
# debug("Dashboard update
# "+str(entry["Fuel"]))
this.landed = bool(entry["Flags"] & edmc_data.FlagsLanded)
this.inSupercruise = bool(entry["Flags"] & edmc_data.FlagsSupercruise)
this.onSRV = bool(entry["Flags"] & edmc_data.FlagsInSRV)
this.onFighter = bool(entry["Flags"] & edmc_data.FlagsInFighter)
this.onFoot = bool(entry.get("Flags2", 0) & edmc_data.Flags2OnFoot)
this.onSurface = this.landed or this.SRVmode or this.onFoot
# print 'LatLon =
# {}'.format(entry['Flags'] &
# 1<<21 and True or False)
# print entry
if entry["Flags"] & edmc_data.FlagsHasLatLong:
if "Latitude" in entry:
this.nearloc["Latitude"] = entry["Latitude"]
this.nearloc["Longitude"] = entry["Longitude"]
else:
this.nearloc["Latitude"] = None
this.nearloc["Longitude"] = None
if entry.get("BodyName"):
this.body_name = entry.get("BodyName")
else:
this.body_name = None
this.cmdr_SQID = Alegiance_get(cmdr, this.cmdr_SQID)
# logger.debug(this.body_name)
for mod in context.enabled_modules:
try:
mod.on_dashboard_entry(cmdr, is_beta, entry)
except:
logger.error(f"Error while sending a dashboard entry to module {mod}", exc_info=1)
def cmdr_data(data, is_beta):
"""
We have new data on our commander
"""
for mod in context.enabled_modules:
mod.on_cmdr_data(data, is_beta)