/
Coderdev
/
web-nodejs-labs
Обзор
Документация
Войти
/
Coderdev
/
web-nodejs-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
backend/src/services/lab8/worldcup.service.js
167 строк
5 KB
Coderdev
1
12 май 2026, 12:34
12 май 2026, 12:34
f5e899f
Код
Авторство
О чём код?
import fs from 'fs/promises'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const dataDir = path.join(__dirname, '..', '..', '..', 'data', 'lab8', 'task2'); const cupFilePath = path.join(dataDir, 'cup.txt'); const groupsPath = path.join(dataDir, 'groups.json'); const stadiumsPath = path.join(dataDir, 'stadium.json'); class WorldCupService { async parseCupFile() { const text = await fs.readFile(cupFilePath, 'utf8'); const lines = text.split('\n'); const matches = []; let currentGroup = null; let currentDate = null; for (const line of lines) { const trimmed = line.trim(); if (!trimmed) continue; if (trimmed.match(/Group\s+[A-H]/i) && !trimmed.includes('@') && !trimmed.match(/\d+-\d+/)) { const gm = trimmed.match(/Group\s+([A-H])/i); if (gm) currentGroup = 'Group ' + gm[1].toUpperCase(); continue; } if (trimmed.match(/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+\w+\s+\d+/i)) { const dm = trimmed.match(/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+(\w+\s+\d+)/i); if (dm) currentDate = dm[2].trim(); // "Nov 20" continue; } if (!trimmed.match(/\d+-\d+/) || !trimmed.includes('@')) continue; const withoutTime = trimmed.replace(/^\d{1,2}:\d{2}\s*/, '').trim(); const atIdx = withoutTime.indexOf('@'); if (atIdx === -1) continue; const leftPart = withoutTime.slice(0, atIdx).trim(); // "Qatar 0-2 (0-2) Ecuador" const stadiumPart = withoutTime.slice(atIdx + 1).trim(); // "Al Bayt Stadium, Al Khor" const commaIdx = stadiumPart.indexOf(','); const stadium = commaIdx !== -1 ? stadiumPart.slice(0, commaIdx).trim() : stadiumPart.trim(); const city = commaIdx !== -1 ? stadiumPart.slice(commaIdx + 1).trim() : ''; const scoreIdx = leftPart.search(/\d+-\d+/); if (scoreIdx === -1) continue; const team1 = leftPart.slice(0, scoreIdx).trim(); const afterScore = leftPart.slice(scoreIdx); // Убираем счёт и счёт в скобках: "0-2 (0-2) Ecuador" -> "Ecuador" const team2 = afterScore.replace(/\d+-\d+\s*(\(\d+-\d+\))?\s*/, '').trim(); const sm = afterScore.match(/(\d+)-(\d+)/); const score1 = sm ? parseInt(sm[1]) : 0; const score2 = sm ? parseInt(sm[2]) : 0; if (!team1 || !team2) continue; // Время const timeMatch = trimmed.match(/^(\d{1,2}:\d{2})/); const time = timeMatch ? timeMatch[1] : ''; matches.push({ group: currentGroup, date: currentDate || '', time, team1, score1, score2, team2, stadium, city }); } return matches; } // ------------------------------------------------------- // groups.json // ------------------------------------------------------- async buildGroups() { const matches = await this.parseCupFile(); const groupsMap = {}; for (const m of matches) { if (!m.group) continue; if (!groupsMap[m.group]) { groupsMap[m.group] = { group: m.group, matches: [] }; } groupsMap[m.group].matches.push({ date: m.date, time: m.time, team1: m.team1, score: `${m.score1}-${m.score2}`, team2: m.team2, stadium: m.stadium }); } const result = Object.values(groupsMap); await fs.mkdir(dataDir, { recursive: true }); await fs.writeFile(groupsPath, JSON.stringify(result, null, 2), 'utf8'); return result; } // ------------------------------------------------------- // stadium.json // ------------------------------------------------------- async buildStadiums() { const matches = await this.parseCupFile(); const stadiumMap = {}; for (const m of matches) { if (!m.stadium) continue; if (!stadiumMap[m.stadium]) { stadiumMap[m.stadium] = { stadium: m.stadium, city: m.city, matches: [] }; } stadiumMap[m.stadium].matches.push({ date: m.date, group: m.group, team1: m.team1, score: `${m.score1}-${m.score2}`, team2: m.team2 }); } const result = Object.values(stadiumMap); await fs.mkdir(dataDir, { recursive: true }); await fs.writeFile(stadiumsPath, JSON.stringify(result, null, 2), 'utf8'); return result; } async getGroups() { try { const data = await fs.readFile(groupsPath, 'utf8'); return JSON.parse(data); } catch { return await this.buildGroups(); } } async getStadiums() { try { const data = await fs.readFile(stadiumsPath, 'utf8'); return JSON.parse(data); } catch { return await this.buildStadiums(); } } } export default new WorldCupService();