Участник:Гиперболоид инженера Мошонкина/скрипт: различия между версиями
Daruk (обсуждение | вклад) Нет описания правки |
Daruk (обсуждение | вклад) Нет описания правки |
||
| Строка 16: | Строка 16: | ||
num_teams = int(match.group(2)) | num_teams = int(match.group(2)) | ||
legs_indicator = int(match.group(3)) | legs_indicator = int(match.group(3)) # Читаем цифру после А (1 или 2) | ||
leg_val = 1 if legs_indicator == 2 else 0 | leg_val = 1 if legs_indicator == 2 else 0 # 1 - два круга (дома/выезд), 0 - один круг (нейтральное) | ||
body = match.group(4) | body = match.group(4) | ||
parts = [p.strip() for p in body.split('|')] | parts = [p.strip() for p in body.split('|')] | ||
| Строка 42: | Строка 41: | ||
lua_output.append(' matches = {') | lua_output.append(' matches = {') | ||
# Распределение матчей | |||
for i, team in enumerate(teams): | for i, team in enumerate(teams): | ||
score_idx = 0 | score_idx = 0 | ||
for j, opp in enumerate(teams): | for j, opp in enumerate(teams): | ||
if i == j: continue | if i == j: continue # Сама с собой не играет | ||
# Считываем счёт в любом случае, чтобы правильно двигаться по матрице | |||
if score_idx < len(team["scores"]): | if score_idx < len(team["scores"]): | ||
score_str = team["scores"][score_idx] | score_str = team["scores"][score_idx] | ||
score_idx += 1 | score_idx += 1 | ||
if ':' in score_str: | |||
# ЛОГИКА ФИЛЬТРАЦИИ: | |||
# Если 2 круга (leg_val == 1) - пишем все матчи | |||
# Если 1 круг (leg_val == 0) - пишем только уникальные пары (где индекс i < j) | |||
if leg_val == 1 or (leg_val == 0 and i < j): | |||
if ':' in score_str: | |||
g1, g2 = score_str.split(':') | |||
lua_output.append(f' {{"{team["name"]}", "{opp["name"]}", {g1.strip()}, {g2.strip()}, {leg_val}}},') | |||
lua_output.append(' }') | lua_output.append(' }') | ||
| Строка 57: | Строка 64: | ||
# --- 2. Парсинг матчей на вылет --- | # --- 2. Парсинг матчей на вылет --- | ||
knockout_pattern = re.compile(r'\{\{И\d+\s*\|?(.*?)\}\}', re.IGNORECASE | re.DOTALL) | knockout_pattern = re.compile(r'\{\{И\d+\s*\|?(.*?)\}\}', re.IGNORECASE | re.DOTALL) | ||
ko_counter = 1 | ko_counter = 1 | ||
| Строка 63: | Строка 69: | ||
for match in knockout_pattern.finditer(wiki_text): | for match in knockout_pattern.finditer(wiki_text): | ||
body = match.group(1) | body = match.group(1) | ||
parts = [p.strip() for p in body.split('|')] | parts = [p.strip() for p in body.split('|')] | ||
| Строка 85: | Строка 90: | ||
base_scores = score_str | base_scores = score_str | ||
if '(' in score_str: | if '(' in score_str: | ||
base_scores, ext = score_str.split('(', 1) | base_scores, ext = score_str.split('(', 1) | ||
| Строка 99: | Строка 103: | ||
p1, p2 = pen_match.groups() | p1, p2 = pen_match.groups() | ||
matches_list = [s.strip() for s in base_scores.split(',')] | matches_list = [s.strip() for s in base_scores.split(',')] | ||
if len(matches_list) == 2: | if len(matches_list) == 2: | ||
m1 = matches_list[0] | m1 = matches_list[0] | ||
m2 = matches_list[1] | m2 = matches_list[1] | ||
| Строка 119: | Строка 121: | ||
) | ) | ||
else: | else: | ||
m1 = matches_list[0] | m1 = matches_list[0] | ||
g1, g2 = "nil", "nil" | g1, g2 = "nil", "nil" | ||
| Строка 139: | Строка 140: | ||
# --- ЗАПУСК --- | # --- ЗАПУСК --- | ||
if __name__ == "__main__": | if __name__ == "__main__": | ||
# Тестовые данные | # Тестовые данные с группами А2 (2 круга) и А1 (1 круг) | ||
data = """ | data = """ | ||
{{ | ; Группа 1 | ||
{{Группа 3-А2| | |||
G|КИР|2:0|3:3| | |||
G|ЦАР|0:2|4:2| | |||
R|ДОМ|3:3|2:4}} | |||
{{ | ; Группа 2 | ||
{{Группа 3-А1| | |||
G|КИР|2:0|3:3| | |||
G|ЦАР|0:2|4:2| | |||
R|ДОМ|3:3|2:4}} | |||
""" | """ | ||