Участник:Гиперболоид инженера Мошонкина/скрипт: различия между версиями
Daruk (обсуждение | вклад) Нет описания правки |
Daruk (обсуждение | вклад) Нет описания правки |
||
| Строка 4: | Строка 4: | ||
lua_output = ["return {", ' ["Tournament_Data"] = {'] | lua_output = ["return {", ' ["Tournament_Data"] = {'] | ||
# --- 1. Парсинг групп --- | # --- 1. Парсинг групп (БЕЗ ИЗМЕНЕНИЙ) --- | ||
group_pattern = re.compile( | group_pattern = re.compile( | ||
r'(?:;\s*Группа\s+([^\n]+)\s*)?\{\{Группа\s+(\d+)-А(\d+)\s*\|?(.*?)\}\}', | r'(?:;\s*Группа\s+([^\n]+)\s*)?\{\{Группа\s+(\d+)-А(\d+)\s*\|?(.*?)\}\}', | ||
| Строка 18: | Строка 17: | ||
num_teams = int(match.group(2)) | num_teams = int(match.group(2)) | ||
legs_indicator = int(match.group(3)) | legs_indicator = int(match.group(3)) | ||
leg_val = 1 if legs_indicator == 2 else 0 | leg_val = 1 if legs_indicator == 2 else 0 | ||
body = match.group(4) | body = match.group(4) | ||
parts = [p.strip() for p in body.split('|') if p.strip()] | parts = [p.strip() for p in body.split('|') if p.strip()] | ||
| Строка 27: | Строка 25: | ||
chunk_size = num_teams + 1 | chunk_size = num_teams + 1 | ||
for i in range(num_teams): | for i in range(num_teams): | ||
chunk = parts[i * chunk_size : (i + 1) * chunk_size] | chunk = parts[i * chunk_size : (i + 1) * chunk_size] | ||
| Строка 36: | Строка 33: | ||
teams.append({"name": name, "status": status, "scores": scores}) | teams.append({"name": name, "status": status, "scores": scores}) | ||
lua_output.append(f'\n ["Group_{group_name}"] = {{') | lua_output.append(f'\n ["Group_{group_name}"] = {{') | ||
lua_output.append(' type = "group",') | lua_output.append(' type = "group",') | ||
| Строка 45: | Строка 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] | ||
| Строка 60: | Строка 55: | ||
lua_output.append(' },') | lua_output.append(' },') | ||
# --- 2. Парсинг матчей на вылет --- | # --- 2. Парсинг матчей на вылет (ОБНОВЛЕНО) --- | ||
# | # Регулярка теперь читает `{{И` и любые символы до первого знака `|` | ||
knockout_pattern = re.compile(r'\{\{И\ | knockout_pattern = re.compile(r'\{\{И[^\|\}]*\|(.*?)\}\}', re.IGNORECASE | re.DOTALL) | ||
ko_counter = 1 | ko_counter = 1 | ||
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('|' | # Убрали `if p.strip()`, чтобы сохранять пустые значения (например, отсутствие цвета/статуса) | ||
parts = [p.strip() for p in body.split('|')] | |||
lua_output.append(f'\n ["Knockout_Stage_{ko_counter}"] = {{') | lua_output.append(f'\n ["Knockout_Stage_{ko_counter}"] = {{') | ||
| Строка 74: | Строка 70: | ||
ko_counter += 1 | ko_counter += 1 | ||
# | # Гарантируем, что берем только полные блоки по 5 элементов | ||
for i in range(0, len(parts), 5): | for i in range(0, len(parts) - 4, 5): | ||
chunk = parts[i:i+5] | chunk = parts[i:i+5] | ||
st1 = chunk[0].replace('LY', 'L') | st1 = chunk[0].replace('LY', 'L') | ||
| Строка 85: | Строка 80: | ||
st2 = chunk[4].replace('LY', 'L') | st2 = chunk[4].replace('LY', 'L') | ||
mod = "nil" | mod = "nil" | ||
p1, p2 = "nil", "nil" | p1, p2 = "nil", "nil" | ||
| Строка 96: | Строка 89: | ||
ext = ext.replace(')', '').strip().lower() | ext = ext.replace(')', '').strip().lower() | ||
if 'et' in ext: | if 'et' in ext or 'дв' in ext: | ||
mod = '"aet"' | mod = '"aet"' | ||
elif 'пен' in ext: | elif 'пен' in ext or 'pen' in ext: | ||
mod = '"pen"' | mod = '"pen"' | ||
pen_match = re.search(r'(\d+):(\d+)', ext) | pen_match = re.search(r'(\d+)\s*:\s*(\d+)', ext) | ||
if pen_match: | if pen_match: | ||
p1, p2 = pen_match.groups() | p1, p2 = pen_match.groups() | ||
# | # Парсинг счета (разделяем матчи запятой, если их два) | ||
matches_scores = [pair.strip() for pair in base_scores.split(',')] | |||
g1_1, g2_1 = "nil", "nil" # Первый матч | |||
g1_2, g2_2 = "nil", "nil" # Второй матч (если есть) | |||
# Собираем строку | if len(matches_scores) > 0 and ':' in matches_scores[0]: | ||
s1, s2 = matches_scores[0].split(':') | |||
g1_1, g2_1 = s1.strip(), s2.strip() | |||
if len(matches_scores) > 1 and ':' in matches_scores[1]: | |||
s1, s2 = matches_scores[1].split(':') | |||
g1_2, g2_2 = s1.strip(), s2.strip() | |||
# Собираем новую строку: Команда1, Команда2, Голы1_1, Голы2_1, Голы1_2, Голы2_2, Модификатор, Пен1, Пен2, Статус1, Статус2 | |||
lua_output.append( | lua_output.append( | ||
f' {{"{t1}", "{t2}", { | f' {{"{t1}", "{t2}", {g1_1}, {g2_1}, {g1_2}, {g2_2}, {mod}, {p1}, {p2}, "{st1}", "{st2}"}},' | ||
) | ) | ||
| Строка 127: | Строка 125: | ||
# --- ЗАПУСК --- | # --- ЗАПУСК --- | ||
if __name__ == "__main__": | if __name__ == "__main__": | ||
# | # Тестовые данные (двойной матч + матч с пропущенными цветами) | ||
data = """ | data = """ | ||
{{Ичто-то там | |||
|G|КИР|4:3, 2:0|АЛЯ|R | |||
}} | |||
{{И1||АЛЯ|3:5|СЕН|}} | |||
""" | """ | ||
result = convert_to_lua(data) | result = convert_to_lua(data) | ||
print(result) | print(result) | ||
</syntaxhighlight> | </syntaxhighlight> | ||