-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_lst.py
121 lines (102 loc) · 4.18 KB
/
parse_lst.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
import os
from _operator import attrgetter as atr
from dataclasses import dataclass
from itertools import groupby
from tqdm import tqdm
from lst.backup_game import save_run, assert_lst_correct_game
from lst.restore_from_backup import restore_from_backup
from smn_game import Game
from smn_logs import read_log_file
@dataclass
class Run:
full_path: str
log_name: str
games: [Game]
original: bool = True
def __post_init__(self):
if self.original:
self.sessions = []
current_offset = 0
current_games = []
for game in self.games:
if current_offset != game.game_offset:
current_offset = game.game_offset
self.sessions.append(Run(self.full_path, self.log_name, current_games, False))
current_games = []
current_games.append(game)
self.sessions.append(Run(self.full_path, self.log_name, current_games, False))
@property
def last_offset_games(self) -> [Game]:
current_offset = 0
games = []
for game in self.games:
if current_offset != game.game_offset:
current_offset = game.game_offset
games = []
games.append(game)
return games
@dataclass
class Runs:
runs: [Run]
@property
def all_games(self) -> [Game]:
return [game for run in self.runs for game in run.games]
@property
def all_sessions(self) -> [Run]:
return [session for run in self.runs for session in run.sessions]
def find_backup(logs_path, log_file):
backup_name = log_file.replace('.log', '') + '.bkp'
backup_path = os.path.join(logs_path, 'backup', backup_name)
if os.path.exists(backup_path):
return backup_path
else:
return None
def read_all_games(logs_path, log_names=None, use_backup=True):
if log_names is None:
log_names = []
runs = []
if not os.path.exists(logs_path + "\\backup"):
os.mkdir(logs_path + "\\backup")
for log_file in tqdm(os.listdir(logs_path), desc="Reading Zone logs"):
log_path = os.path.join(logs_path, log_file)
if os.path.isdir(log_path):
continue
if log_file not in log_names and len(log_names) != 0:
continue
if not log_file.endswith('.log'):
continue
if use_backup and (backup_path := find_backup(logs_path, log_file)) is not None:
current_games = restore_from_backup(backup_path)
runs.append(Run(log_path, log_file, current_games))
else:
current_games = []
minions = read_log_file(log_path)
carry_over = []
for list_id, minions_group in groupby(minions.values(), lambda minion: minion.list_id):
minions_list_all = [minion for minion in list(minions_group)]
minions_list = [minion for minion in minions_list_all if not minion.child_card]
only_minions_list = [minion for minion in minions_list if not minion.spell]
if len(only_minions_list) != 14:
if len(only_minions_list) > 0 and only_minions_list[-1].card_id == 'SCH_199':
carry_over = only_minions_list
continue
if len(carry_over) == 0:
continue
else:
only_minions_list = [*only_minions_list, *carry_over]
carry_over = []
if len(only_minions_list) != 14:
continue
only_minions_list.sort(key=atr('sort_key'))
game = Game(only_minions_list, [])
assert_lst_correct_game(game)
if not game.lst_complete:
continue
current_games.append(game)
run = Run(log_path, log_file, current_games)
if use_backup and find_backup(logs_path, log_file) is None:
backup_name = run.log_name
save_path = logs_path + "\\" + "backup\\" + backup_name.replace(".log", "") + ".bkp"
save_run(save_path, run)
runs.append(run)
return Runs(runs)