forked from Ridepad/uwu-logs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs_base.py
552 lines (463 loc) · 17.9 KB
/
logs_base.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
546
547
548
549
550
551
552
import json
import os
from collections import defaultdict
import file_functions
import logs_fight_separator
import logs_player_spec
import logs_units_guid
import logs_get_time
import logs_spells_list
from constants import (
COMBINE_SPELLS,
CUSTOM_SPELL_NAMES,
LOGGER_REPORTS,
LOGS_CUT_NAME_OLD,
LOGS_DIR,
LOGS_CUT_NAME,
MONTHS,
UNKNOWN_ICON,
SPELL_ICONS_DB,
get_now,
get_report_name_info,
running_time,
setup_logger,
to_dt_year_precise,
)
PLAYER = "0x0"
TYPES = (str, bool, type(None))
PASSIVE_SPELLS = {
49497: {
'name': 'Spell Deflection',
'school': '0x1',
'color': 'physical',
'icon': 'spell_deathknight_spelldeflection'
},
52286: {
'name': 'Will of the Necropolis',
'school': '0x1',
'color': 'physical',
'icon': 'ability_creature_cursed_02'
},
}
TOTAL_DUMMY_SPEC = {
"spec": "Total",
"icon": "ability_hunter_readiness",
"name": "Total",
"class": "total",
}
@running_time
def get_spells_int():
spells_json = file_functions.json_read(SPELL_ICONS_DB)
return {
int(spell_id): icon_name
for icon_name, _spells in spells_json.items()
for spell_id in _spells
}
get_spells = file_functions.cache_file_until_new(SPELL_ICONS_DB, get_spells_int)
def cache_wrap(func):
def cache_inner(self, s, f, *args, **kwargs):
slice_ID = f"{s}_{f}"
cached_data = self.CACHE[func.__name__]
for arg in args:
if not isinstance(arg, TYPES):
break
cached_data = cached_data[arg]
if slice_ID in cached_data:
return cached_data[slice_ID]
data = func(self, s, f, *args, **kwargs)
cached_data[slice_ID] = data
return data
return cache_inner
class THE_LOGS:
def __init__(self, logs_name: str) -> None:
self.NAME = logs_name
self.year = int(logs_name[:2]) + 2000
self.last_access = get_now()
self.CACHE = defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
self.CONTROLLED_UNITS: dict[str, set[str]] = {}
@property
def path(self):
try:
return self.__path
except AttributeError:
self.__path = file_functions.new_folder_path(LOGS_DIR, self.NAME, check_backup=True)
return self.__path
def relative_path(self, s: str):
return os.path.join(self.path, s)
@property
def FORMATTED_NAME(self):
try:
return self.__FORMATTED_NAME
except AttributeError:
report_name_info = get_report_name_info(self.NAME)
time = report_name_info['time'].replace('-', ':')
year, month, day = report_name_info['date'].split("-")
month = MONTHS[int(month)-1][:3]
date = f"{day} {month} {year}"
author = report_name_info['author']
self.__FORMATTED_NAME = f"{date}, {time} - {author}"
return self.__FORMATTED_NAME
@property
def LOGGER(self):
try:
return self.__LOGGER
except AttributeError:
log_file = self.relative_path('log.log')
self.__LOGGER = setup_logger(f"{self.NAME}_logger", log_file)
return self.__LOGGER
def cache_files_missing(self, files):
try:
return not os.path.isfile(files)
except TypeError:
return not all(os.path.isfile(file) for file in files)
@running_time
def __open_logs(self):
logs_cut_file_name = self.relative_path(LOGS_CUT_NAME)
if os.path.isfile(logs_cut_file_name):
return file_functions.zstd_text_read(logs_cut_file_name).splitlines()
logs_cut_file_name_old = self.relative_path(LOGS_CUT_NAME_OLD)
if os.path.isfile(logs_cut_file_name_old):
return file_functions.zlib_text_read(logs_cut_file_name_old).splitlines()
return []
@property
def LOGS(self):
try:
return self.__LOGS
except AttributeError:
self.__LOGS = self.__open_logs()
return self.__LOGS
def get_logs(self, s=None, f=None):
if not s and f is None:
return self.LOGS
return self.LOGS[s:f]
def get_timedelta(self, last, now):
return to_dt_year_precise(now, self.year) - to_dt_year_precise(last, self.year)
def get_timedelta_seconds(self, last, now):
return self.get_timedelta(last, now).total_seconds()
@cache_wrap
def get_slice_duration(self, s: int=None, f: int=None):
if s is None:
s = 0
if f is None:
f = 0
first_line = self.LOGS[s]
last_line = self.LOGS[f-1]
return self.get_timedelta(first_line, last_line).total_seconds()
def get_fight_duration_total(self, segments):
return sum(self.get_slice_duration(s, f) for s, f in segments)
def get_enc_data(self, rewrite=False):
try:
return self.ENCOUNTER_DATA
except AttributeError:
enc_data_file_name = self.relative_path("ENCOUNTER_DATA.json")
if rewrite or self.cache_files_missing(enc_data_file_name):
self.ENCOUNTER_DATA = logs_fight_separator.Fights(self.LOGS).main()
file_functions.json_write(enc_data_file_name, self.ENCOUNTER_DATA, indent=None)
else:
enc_data: dict[str, list[tuple[int, int]]]
enc_data = file_functions.json_read(enc_data_file_name)
self.ENCOUNTER_DATA = enc_data
return self.ENCOUNTER_DATA
def new_guids(self):
enc_data = self.get_enc_data()
parsed = logs_units_guid.guids_main(self.LOGS, enc_data)
if parsed['missing_owner']:
LOGGER_REPORTS.error(f"{self.NAME} | Missing owners: {parsed['missing_owner']}")
guids_data_file_name = self.relative_path("GUIDS_DATA.json")
players_data_file_name = self.relative_path("PLAYERS_DATA.json")
classes_data_file_name = self.relative_path("CLASSES_DATA.json")
_guids = parsed['everything']
_players = parsed['players']
_classes = parsed['classes']
file_functions.json_write(guids_data_file_name, _guids)
file_functions.json_write(players_data_file_name, _players)
file_functions.json_write(classes_data_file_name, _classes)
return _guids, _players, _classes
def get_guids(self, rewrite=False):
try:
return self.GUIDS, self.PLAYERS, self.CLASSES
except AttributeError:
_guids: dict[str, dict[str, str]]
_players: dict[str, str]
_classes: dict[str, str]
files = [
self.relative_path("GUIDS_DATA.json"),
self.relative_path("PLAYERS_DATA.json"),
self.relative_path("CLASSES_DATA.json")
]
if rewrite or self.cache_files_missing(files):
_guids, _players, _classes = self.new_guids()
else:
try:
_guids, _players, _classes = [
file_functions.json_read_no_exception(_file_name)
for _file_name in files
]
except Exception:
return self.get_guids(rewrite=True)
self.GUIDS, self.PLAYERS, self.CLASSES = _guids, _players, _classes
return self.GUIDS, self.PLAYERS, self.CLASSES
def get_all_guids(self):
return self.get_guids()[0]
def get_players_guids(self, whitelist_guids=None, whitelist_names=None):
players = self.get_guids()[1]
if whitelist_guids is not None:
return {k:v for k,v in players.items() if k in whitelist_guids}
elif whitelist_names is not None:
return {k:v for k,v in players.items() if v in whitelist_names}
else:
return players
def get_classes(self):
return self.get_guids()[2]
@cache_wrap
def get_players_specs_in_segments(self, s, f) -> dict[str, int]:
'''specs = {guid: spec_index}'''
logs_slice = self.LOGS[s:f]
players = self.get_players_guids()
classes = self.get_classes()
return logs_player_spec.get_specs(logs_slice, players, classes)
def get_slice_spec_info(self, s, f):
new_specs: dict[str, tuple(str, str)] = {}
specs = self.get_players_specs_in_segments(s, f)
for unit_guid, spec_index in specs.items():
spec, icon = logs_player_spec.get_spec_info(spec_index)
new_specs[unit_guid] = {
"spec": spec,
"icon": icon,
"name": self.guid_to_name(unit_guid),
"class": self.CLASSES[unit_guid],
}
new_specs["Total"] = TOTAL_DUMMY_SPEC
return new_specs
@property
def PLAYERS_NAMES(self):
try:
return self.__PLAYERS_NAMES
except AttributeError:
players = self.get_players_guids()
self.__PLAYERS_NAMES = {v:k for k,v in players.items()}
return self.__PLAYERS_NAMES
@property
def unit_id_to_name(self):
try:
return self.__unit_id_to_name
except AttributeError:
guids = self.get_all_guids()
__d = {
guid[6:-6]: info["name"]
for guid, info in guids.items()
}
__d |= self.get_players_guids()
__d["000000"] = "nil"
__d["0x0000000000000000"] = "nil"
self.__unit_id_to_name = __d
return self.__unit_id_to_name
def name_to_guid(self, name: str) -> str:
if name.startswith("0x"):
return name
if name in self.PLAYERS_NAMES:
return self.PLAYERS_NAMES[name]
guids = self.get_all_guids()
for guid, data in guids.items():
if data['name'] == name:
return guid
return None
def guid_to_name(self, guid: str):
name = self.unit_id_to_name.get(guid)
if not name:
name = self.get_all_guids().get(guid, {}).get("name", "Unknown")
return name
def sort_data_guids_by_name(self, data: dict):
return dict(sorted(data.items(), key=lambda x: self.guid_to_name(x[0])))
def convert_dict_guids_to_names(self, data: dict):
return {
self.guid_to_name(guid): value
for guid, value in data.items()
}
def get_master_guid(self, guid: str):
guids = self.get_all_guids()
master_guid = guids[guid].get('master_guid')
if not master_guid:
return guid
return guids.get(master_guid, {}).get('master_guid', master_guid)
def get_pet_name(self, guid):
is_pet = guid != self.get_master_guid(guid)
if is_pet:
return self.guid_to_name(guid)
return None
def get_units_controlled_by(self, master_guid: str):
if not master_guid.startswith("0x"):
master_guid = self.name_to_guid(master_guid)
if master_guid in self.CONTROLLED_UNITS:
return self.CONTROLLED_UNITS[master_guid]
all_guids = self.get_all_guids()
controlled_units = {
guid
for guid, p in all_guids.items()
if p.get("master_guid") == master_guid or master_guid in guid
}
controlled_units.add(master_guid)
self.CONTROLLED_UNITS[master_guid] = controlled_units
return controlled_units
@property
def PLAYERS_PETS(self):
try:
return self.__PLAYERS_PETS
except AttributeError:
guids = self.get_all_guids()
self.__PLAYERS_PETS = {
guid
for guid, p in guids.items()
if p.get("master_guid", "").startswith(PLAYER)
}
return self.__PLAYERS_PETS
@property
def FRIENDLY_IDS(self):
try:
return self.__PLAYERS_PETS_IDS
except AttributeError:
self.__PLAYERS_PETS_IDS = {
guid[6:12]
for guid in self.PLAYERS_PETS
}
self.__PLAYERS_PETS_IDS.add("000000")
return self.__PLAYERS_PETS_IDS
def get_players_and_pets_guids(self):
try:
return self.PLAYERS_AND_PETS
except AttributeError:
players = set(self.get_players_guids())
self.PLAYERS_AND_PETS = players | self.PLAYERS_PETS
return self.PLAYERS_AND_PETS
def add_missing_players(self, data, default=0, players=None):
if players is None:
players = self.get_players_guids()
for guid in players:
if guid not in data:
data[guid] = default
return data
@property
def CLASSES_NAMES(self):
try:
return self.__CLASSES_NAMES
except AttributeError:
classes = self.get_classes()
_classes_names: dict[str, str] = self.convert_dict_guids_to_names(classes)
self.__CLASSES_NAMES = _classes_names
return _classes_names
def get_classes_with_names_json(self):
return json.dumps(self.CLASSES_NAMES)
def get_timestamp(self, rewrite=False):
try:
return self.TIMESTAMP
except AttributeError:
timestamp_data_file_name = self.relative_path("TIMESTAMP_DATA.json")
if rewrite or self.cache_files_missing(timestamp_data_file_name):
self.TIMESTAMP = logs_get_time.get_timestamps(self.LOGS)
file_functions.json_write(timestamp_data_file_name, self.TIMESTAMP, indent=None, sep=(",", ""))
else:
timestamp_data: list[int]
timestamp_data = file_functions.json_read(timestamp_data_file_name)
self.TIMESTAMP = timestamp_data
return self.TIMESTAMP
def find_index(self, n, shift=0):
if n is None:
return
ts = self.get_timestamp()
for i, line_n in enumerate(ts, -shift):
if n <= line_n:
return max(i, 0)
def find_sec_from_start(self, s):
return self.get_timedelta_seconds(self.LOGS[0], self.LOGS[s])
@running_time
def precise_shift(self, from_index: int, shift_seconds: int):
if not shift_seconds:
return from_index
ts = self.get_timestamp()
first_line = self.LOGS[from_index]
shifted_index = shift_seconds + int(self.find_sec_from_start(from_index))
s = ts[shifted_index-1]
f = ts[shifted_index+1]
for i, current_line in enumerate(self.LOGS[s:f]):
td = self.get_timedelta(first_line, current_line).total_seconds()
if td > shift_seconds:
return s+i
return ts[shifted_index]
def get_spells(self, rewrite=False):
try:
return self.SPELLS
except AttributeError:
spells_data_file_name = self.relative_path("SPELLS_DATA.json")
if rewrite or self.cache_files_missing(spells_data_file_name):
_spells = logs_spells_list.get_all_spells(self.LOGS)
file_functions.json_write(spells_data_file_name, _spells)
else:
try:
_spells = file_functions.json_read_no_exception(spells_data_file_name)
except Exception:
return self.get_spells(rewrite=True)
for spell_id, new_name in CUSTOM_SPELL_NAMES.items():
if spell_id in _spells:
_spells[spell_id]["name"] = new_name
self.SPELLS = logs_spells_list.spell_id_to_int(_spells)
return self.SPELLS
def convert_to_main_spell_id(self, spell_id: str):
if spell_id not in COMBINE_SPELLS:
return spell_id
_spell_id = COMBINE_SPELLS[spell_id]
if int(_spell_id) not in self.get_spells():
return spell_id
return _spell_id
@property
def SPELLS_WITH_ICONS(self):
try:
return self.__SPELLS_WITH_ICONS
except AttributeError:
_spells = self.get_spells()
_icons = get_spells()
for spell_id, spell_data in _spells.items():
spell_data["icon"] = _icons.get(spell_id, UNKNOWN_ICON)
_spells |= PASSIVE_SPELLS
self.__SPELLS_WITH_ICONS = _spells
return _spells
def get_spell_name(self, spell_id):
_spells = self.get_spells()
spell_id = abs(int(spell_id))
if spell_id in _spells:
return _spells[spell_id]["name"]
return "Unknown spell"
def get_spells_colors(self, spells) -> dict[int, str]:
if not spells:
return {}
all_spells = self.get_spells()
return {
spell_id: all_spells[abs(spell_id)]['color']
for spell_id in spells
if abs(spell_id) in all_spells
}
def get_spells_lower(self):
try:
return self.SPELLS_LOWER
except AttributeError:
spells = self.get_spells()
self.SPELLS_LOWER = {spell_id: v["name"].lower() for spell_id, v in spells.items()}
return self.SPELLS_LOWER
def get_spells_ids(self):
try:
return self.SPELLS_IDS
except AttributeError:
spells = self.get_spells()
self.SPELLS_IDS = {spell_id: str(spell_id) for spell_id in spells}
return self.SPELLS_IDS
@running_time
def filtered_spell_list(self, request: dict[str, str]):
if 'filter' not in request:
return {}
INPUT = request['filter'].lower()
SPELLS = self.get_spells()
_spells = self.get_spells_ids() if INPUT.isdigit() else self.get_spells_lower()
return {
spell_id: SPELLS[spell_id]['name']
for spell_id, spell_v in _spells.items()
if INPUT in spell_v
}