Refactor Spielplan-Filterlogik in separate Utility-Funktionen und verbessere die Filterung im PDF-Generierungsprozess
This commit is contained in:
@@ -360,6 +360,7 @@
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { filterSpielplanRows, toApiTeamParam } from '../../utils/spielplan-filter.js'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
@@ -525,183 +526,16 @@ const filterData = () => {
|
||||
return
|
||||
}
|
||||
|
||||
let saisonFiltered = spielplanData.value
|
||||
|
||||
// Dann nach Wettbewerb filtern
|
||||
let wettbewerbFiltered = saisonFiltered
|
||||
if (selectedWettbewerb.value === 'punktrunde') {
|
||||
wettbewerbFiltered = saisonFiltered.filter(row => {
|
||||
const runde = (row.Runde || '').toLowerCase()
|
||||
const staffel = (row.Staffel || '').toLowerCase()
|
||||
const liga = (row.Liga || '').toLowerCase()
|
||||
const isPokal = runde.includes('pokal') || staffel.includes('pokal') || liga.includes('pokal')
|
||||
|
||||
return !isPokal
|
||||
filteredData.value = filterSpielplanRows(spielplanData.value, {
|
||||
team: selectedFilter.value,
|
||||
wettbewerb: selectedWettbewerb.value
|
||||
})
|
||||
} else if (selectedWettbewerb.value === 'pokal') {
|
||||
wettbewerbFiltered = saisonFiltered.filter(row => {
|
||||
const runde = (row.Runde || '').toLowerCase()
|
||||
const staffel = (row.Staffel || '').toLowerCase()
|
||||
const liga = (row.Liga || '').toLowerCase()
|
||||
return runde.includes('pokal') || staffel.includes('pokal') || liga.includes('pokal')
|
||||
})
|
||||
}
|
||||
// "alle" zeigt alle Spiele ohne weitere Filterung
|
||||
|
||||
// Dann nach Mannschaft filtern
|
||||
if (selectedFilter.value === 'all') {
|
||||
filteredData.value = wettbewerbFiltered
|
||||
} else if (selectedFilter.value === 'erwachsene') {
|
||||
filteredData.value = wettbewerbFiltered.filter(row => {
|
||||
const heimMannschaft = (row.HeimMannschaft || '').toLowerCase()
|
||||
const gastMannschaft = (row.GastMannschaft || '').toLowerCase()
|
||||
const heimAltersklasse = (row.HeimMannschaftAltersklasse || '').toLowerCase()
|
||||
const gastAltersklasse = (row.GastMannschaftAltersklasse || '').toLowerCase()
|
||||
|
||||
// Prüfe ob eine der Mannschaften Harheimer TC ist
|
||||
const isHarheimerHeim = heimMannschaft.includes('harheimer tc')
|
||||
const isHarheimerGast = gastMannschaft.includes('harheimer tc')
|
||||
|
||||
if (!isHarheimerHeim && !isHarheimerGast) {
|
||||
return false // Kein Harheimer TC Spiel
|
||||
}
|
||||
|
||||
// Filtere nach Erwachsenen-Mannschaften (NICHT Jugend)
|
||||
const isErwachsenenHeim = isHarheimerHeim &&
|
||||
heimAltersklasse.includes('erwachsene') &&
|
||||
!heimAltersklasse.includes('jugend')
|
||||
const isErwachsenenGast = isHarheimerGast &&
|
||||
gastAltersklasse.includes('erwachsene') &&
|
||||
!gastAltersklasse.includes('jugend')
|
||||
|
||||
return isErwachsenenHeim || isErwachsenenGast
|
||||
})
|
||||
} else if (selectedFilter.value === 'nachwuchs') {
|
||||
filteredData.value = wettbewerbFiltered.filter(row => {
|
||||
const heimMannschaft = (row.HeimMannschaft || '').toLowerCase()
|
||||
const gastMannschaft = (row.GastMannschaft || '').toLowerCase()
|
||||
const heimAltersklasse = (row.HeimMannschaftAltersklasse || '').toLowerCase()
|
||||
const gastAltersklasse = (row.GastMannschaftAltersklasse || '').toLowerCase()
|
||||
|
||||
// Prüfe ob eine der Mannschaften Harheimer TC ist
|
||||
const isHarheimerHeim = heimMannschaft.includes('harheimer tc')
|
||||
const isHarheimerGast = gastMannschaft.includes('harheimer tc')
|
||||
|
||||
if (!isHarheimerHeim && !isHarheimerGast) {
|
||||
return false // Kein Harheimer TC Spiel
|
||||
}
|
||||
|
||||
// Filtere nach Jugend-Mannschaften (NUR Jugend)
|
||||
const isJugendHeim = isHarheimerHeim &&
|
||||
(heimAltersklasse.includes('jugend') || heimMannschaft.includes('jugend'))
|
||||
const isJugendGast = isHarheimerGast &&
|
||||
(gastAltersklasse.includes('jugend') || gastMannschaft.includes('jugend'))
|
||||
|
||||
return isJugendHeim || isJugendGast
|
||||
})
|
||||
} else {
|
||||
// Spezifische Mannschaft - Mapping zwischen CMS-Mannschaften und CSV-Daten
|
||||
filteredData.value = wettbewerbFiltered.filter(row => {
|
||||
const heimMannschaft = (row.HeimMannschaft || '').toLowerCase()
|
||||
const gastMannschaft = (row.GastMannschaft || '').toLowerCase()
|
||||
const heimAltersklasse = (row.HeimMannschaftAltersklasse || '').toLowerCase()
|
||||
const gastAltersklasse = (row.GastMannschaftAltersklasse || '').toLowerCase()
|
||||
|
||||
// Prüfe ob eine der Mannschaften Harheimer TC ist
|
||||
const isHarheimerHeim = heimMannschaft.includes('harheimer tc')
|
||||
const isHarheimerGast = gastMannschaft.includes('harheimer tc')
|
||||
|
||||
if (!isHarheimerHeim && !isHarheimerGast) {
|
||||
return false // Kein Harheimer TC Spiel
|
||||
}
|
||||
|
||||
const cmsMannschaft = selectedFilter.value
|
||||
|
||||
// Mapping zwischen CMS-Mannschaften und CSV-Daten
|
||||
const mannschaftMapping = {
|
||||
'Erwachsene 1': ['harheimer tc'], // Nur ohne römische Zahl
|
||||
'Erwachsene 2': ['harheimer tc ii'],
|
||||
'Erwachsene 3': ['harheimer tc iii'],
|
||||
'Erwachsene 4': ['harheimer tc iv'],
|
||||
'Erwachsene 5': ['harheimer tc v'],
|
||||
'Jugendmannschaft': ['harheimer tc'] // Jugend hat keine römische Zahl
|
||||
}
|
||||
|
||||
const csvVariants = mannschaftMapping[cmsMannschaft] || []
|
||||
|
||||
// Prüfe Mannschafts-Zuordnung UND Altersklasse
|
||||
const mannschaftMatch = csvVariants.some(variant => {
|
||||
// Strikte Übereinstimmung: Prüfe exakte Mannschaftsnamen
|
||||
if (isHarheimerHeim) {
|
||||
// Für "harheimer tc" (Erwachsene 1): Nur wenn KEINE römische Zahl folgt
|
||||
if (variant === 'harheimer tc') {
|
||||
return heimMannschaft === 'harheimer tc' ||
|
||||
heimMannschaft.startsWith('harheimer tc ') &&
|
||||
!heimMannschaft.match(/harheimer tc\s+[ivx]+/i)
|
||||
}
|
||||
// Für andere Mannschaften: Exakte Übereinstimmung
|
||||
return heimMannschaft === variant || heimMannschaft.startsWith(variant + ' ')
|
||||
}
|
||||
if (isHarheimerGast) {
|
||||
// Für "harheimer tc" (Erwachsene 1): Nur wenn KEINE römische Zahl folgt
|
||||
if (variant === 'harheimer tc') {
|
||||
return gastMannschaft === 'harheimer tc' ||
|
||||
gastMannschaft.startsWith('harheimer tc ') &&
|
||||
!gastMannschaft.match(/harheimer tc\s+[ivx]+/i)
|
||||
}
|
||||
// Für andere Mannschaften: Exakte Übereinstimmung
|
||||
return gastMannschaft === variant || gastMannschaft.startsWith(variant + ' ')
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
if (!mannschaftMatch) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Zusätzliche Altersklassen-Prüfung für spezifische Mannschaften
|
||||
if (cmsMannschaft.startsWith('Erwachsene')) {
|
||||
// Erwachsenen-Mannschaften: MUSS Erwachsene sein, DARF NICHT Jugend sein
|
||||
const isErwachsenenHeim = isHarheimerHeim &&
|
||||
heimAltersklasse.includes('erwachsene') &&
|
||||
!heimAltersklasse.includes('jugend')
|
||||
const isErwachsenenGast = isHarheimerGast &&
|
||||
gastAltersklasse.includes('erwachsene') &&
|
||||
!gastAltersklasse.includes('jugend')
|
||||
|
||||
return isErwachsenenHeim || isErwachsenenGast
|
||||
} else if (cmsMannschaft === 'Jugendmannschaft') {
|
||||
// Jugend-Mannschaft: MUSS Jugend sein
|
||||
const isJugendHeim = isHarheimerHeim &&
|
||||
(heimAltersklasse.includes('jugend') || heimMannschaft.includes('jugend'))
|
||||
const isJugendGast = isHarheimerGast &&
|
||||
(gastAltersklasse.includes('jugend') || gastMannschaft.includes('jugend'))
|
||||
|
||||
return isJugendHeim || isJugendGast
|
||||
}
|
||||
|
||||
return true // Fallback für unbekannte Mannschaften
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const downloadPDF = () => {
|
||||
if (!filteredData.value || filteredData.value.length === 0) return
|
||||
|
||||
// Bestimme den Team-Parameter basierend auf dem Filter
|
||||
let teamParam = ''
|
||||
|
||||
if (selectedFilter.value === 'all') {
|
||||
teamParam = 'all'
|
||||
} else if (selectedFilter.value === 'erwachsene') {
|
||||
teamParam = 'erwachsene'
|
||||
} else if (selectedFilter.value === 'nachwuchs') {
|
||||
teamParam = 'nachwuchs'
|
||||
} else {
|
||||
// Für einzelne Mannschaften: Konvertiere Namen
|
||||
teamParam = selectedFilter.value.replace(/\s+/g, '_').toLowerCase()
|
||||
}
|
||||
const teamParam = toApiTeamParam(selectedFilter.value)
|
||||
|
||||
// Erstelle Download-URL für dynamische PDF-Generierung
|
||||
const params = new URLSearchParams({
|
||||
|
||||
@@ -2,6 +2,7 @@ import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { readSpielplanData, validateSeasonSlug } from '../../utils/spielplan-data.js'
|
||||
import { info as loggerInfo, error as loggerError } from '../../utils/logger.js'
|
||||
import { filterSpielplanRows } from '../../../utils/spielplan-filter.js'
|
||||
|
||||
function seasonSlugToLabel(slug) {
|
||||
const match = String(slug || '').match(/^(\d{2})--(\d{2})$/)
|
||||
@@ -35,116 +36,8 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
const dataRows = spielplan.data
|
||||
|
||||
// Filtere Daten basierend auf Team
|
||||
let filteredData = dataRows
|
||||
|
||||
if (team !== 'all') {
|
||||
filteredData = dataRows.filter(row => {
|
||||
const heimMannschaft = (row.HeimMannschaft || '').toLowerCase()
|
||||
const gastMannschaft = (row.GastMannschaft || '').toLowerCase()
|
||||
const heimAltersklasse = (row.HeimMannschaftAltersklasse || '').toLowerCase()
|
||||
const gastAltersklasse = (row.GastMannschaftAltersklasse || '').toLowerCase()
|
||||
|
||||
// Prüfe ob eine der Mannschaften Harheimer TC ist
|
||||
const isHarheimerHeim = heimMannschaft.includes('harheimer tc')
|
||||
const isHarheimerGast = gastMannschaft.includes('harheimer tc')
|
||||
|
||||
if (!isHarheimerHeim && !isHarheimerGast) {
|
||||
return false // Kein Harheimer TC Spiel
|
||||
}
|
||||
|
||||
// Mapping zwischen Team-Namen und CSV-Daten
|
||||
const mannschaftMapping = {
|
||||
'erwachsene': ['harheimer tc'], // Alle Erwachsenen-Mannschaften
|
||||
'nachwuchs': ['harheimer tc'], // Alle Jugend-Mannschaften
|
||||
'erwachsene_1': ['harheimer tc'], // Nur ohne römische Zahl
|
||||
'erwachsene_2': ['harheimer tc ii'],
|
||||
'erwachsene_3': ['harheimer tc iii'],
|
||||
'erwachsene_4': ['harheimer tc iv'],
|
||||
'erwachsene_5': ['harheimer tc v'],
|
||||
'jugendmannschaft': ['harheimer tc'] // Jugend hat keine römische Zahl
|
||||
}
|
||||
|
||||
const csvVariants = mannschaftMapping[team] || []
|
||||
|
||||
if (csvVariants.length === 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Prüfe Mannschafts-Zuordnung UND Altersklasse
|
||||
const mannschaftMatch = csvVariants.some(variant => {
|
||||
// Strikte Übereinstimmung: Prüfe exakte Mannschaftsnamen
|
||||
if (isHarheimerHeim) {
|
||||
// Für "harheimer tc" (Erwachsene 1): Nur wenn KEINE römische Zahl folgt
|
||||
if (variant === 'harheimer tc') {
|
||||
return heimMannschaft === 'harheimer tc' ||
|
||||
heimMannschaft.startsWith('harheimer tc ') &&
|
||||
!heimMannschaft.match(/harheimer tc\s+[ivx]+/i)
|
||||
}
|
||||
// Für andere Mannschaften: Exakte Übereinstimmung
|
||||
return heimMannschaft === variant || heimMannschaft.startsWith(variant + ' ')
|
||||
}
|
||||
if (isHarheimerGast) {
|
||||
// Für "harheimer tc" (Erwachsene 1): Nur wenn KEINE römische Zahl folgt
|
||||
if (variant === 'harheimer tc') {
|
||||
return gastMannschaft === 'harheimer tc' ||
|
||||
gastMannschaft.startsWith('harheimer tc ') &&
|
||||
!gastMannschaft.match(/harheimer tc\s+[ivx]+/i)
|
||||
}
|
||||
// Für andere Mannschaften: Exakte Übereinstimmung
|
||||
return gastMannschaft === variant || gastMannschaft.startsWith(variant + ' ')
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
if (!mannschaftMatch) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Zusätzliche Altersklassen-Prüfung für spezifische Mannschaften
|
||||
if (team.startsWith('erwachsene')) {
|
||||
// Erwachsenen-Mannschaften: MUSS Erwachsene sein, DARF NICHT Jugend sein
|
||||
const isErwachsenenHeim = isHarheimerHeim &&
|
||||
heimAltersklasse.includes('erwachsene') &&
|
||||
!heimAltersklasse.includes('jugend')
|
||||
const isErwachsenenGast = isHarheimerGast &&
|
||||
gastAltersklasse.includes('erwachsene') &&
|
||||
!gastAltersklasse.includes('jugend')
|
||||
|
||||
return isErwachsenenHeim || isErwachsenenGast
|
||||
} else if (team === 'jugendmannschaft' || team === 'nachwuchs') {
|
||||
// Jugend-Mannschaft: MUSS Jugend sein
|
||||
const isJugendHeim = isHarheimerHeim &&
|
||||
(heimAltersklasse.includes('jugend') || heimMannschaft.includes('jugend'))
|
||||
const isJugendGast = isHarheimerGast &&
|
||||
(gastAltersklasse.includes('jugend') || gastMannschaft.includes('jugend'))
|
||||
|
||||
return isJugendHeim || isJugendGast
|
||||
}
|
||||
|
||||
return true // Fallback für unbekannte Mannschaften
|
||||
})
|
||||
}
|
||||
|
||||
// Filtere nach Wettbewerb (Standard: Punktrunde)
|
||||
// Muss zur Frontend-Logik passen: Punktrunde = alles, was kein Pokal ist.
|
||||
const wettbewerb = String(query.wettbewerb || 'punktrunde').toLowerCase().trim()
|
||||
if (wettbewerb !== 'alle') {
|
||||
filteredData = filteredData.filter(row => {
|
||||
const runde = (row.Runde || '').toLowerCase()
|
||||
const staffel = (row.Staffel || '').toLowerCase()
|
||||
const liga = (row.Liga || '').toLowerCase()
|
||||
const isPokal = runde.includes('pokal') || staffel.includes('pokal') || liga.includes('pokal')
|
||||
|
||||
if (wettbewerb === 'punktrunde') {
|
||||
return !isPokal
|
||||
} else if (wettbewerb === 'pokal') {
|
||||
return isPokal
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
const filteredData = filterSpielplanRows(dataRows, { team, wettbewerb })
|
||||
|
||||
// Sammle Halle-Informationen für die jeweilige Mannschaft
|
||||
const hallenMap = new Map()
|
||||
|
||||
125
utils/spielplan-filter.js
Normal file
125
utils/spielplan-filter.js
Normal file
@@ -0,0 +1,125 @@
|
||||
function normalizeText(value) {
|
||||
return String(value || '').trim().toLowerCase().replace(/\s+/g, ' ')
|
||||
}
|
||||
|
||||
function normalizeTeamFilterKey(value) {
|
||||
const normalized = normalizeText(value).replace(/\s+/g, '_')
|
||||
|
||||
if (!normalized) return ''
|
||||
if (normalized === 'all' || normalized === 'erwachsene' || normalized === 'nachwuchs') return normalized
|
||||
if (normalized === 'jugendmannschaft') return 'jugendmannschaft'
|
||||
|
||||
const erwachseneMatch = normalized.match(/^erwachsene_(\d+)$/)
|
||||
if (erwachseneMatch) {
|
||||
return `erwachsene_${erwachseneMatch[1]}`
|
||||
}
|
||||
|
||||
return normalized
|
||||
}
|
||||
|
||||
const MANNSCHAFT_MAPPING = {
|
||||
erwachsene_1: ['harheimer tc'],
|
||||
erwachsene_2: ['harheimer tc ii'],
|
||||
erwachsene_3: ['harheimer tc iii'],
|
||||
erwachsene_4: ['harheimer tc iv'],
|
||||
erwachsene_5: ['harheimer tc v'],
|
||||
jugendmannschaft: ['harheimer tc']
|
||||
}
|
||||
|
||||
function matchMappedVariant(mannschaft, variant) {
|
||||
if (variant === 'harheimer tc') {
|
||||
return mannschaft === 'harheimer tc' ||
|
||||
(mannschaft.startsWith('harheimer tc ') && !mannschaft.match(/harheimer tc\s+[ivx]+/i))
|
||||
}
|
||||
|
||||
return mannschaft === variant || mannschaft.startsWith(`${variant} `)
|
||||
}
|
||||
|
||||
function isErwachsenenTeam(teamKey) {
|
||||
return teamKey === 'erwachsene' || teamKey.startsWith('erwachsene_')
|
||||
}
|
||||
|
||||
export function rowMatchesWettbewerbFilter(row, rawWettbewerb) {
|
||||
const wettbewerb = normalizeText(rawWettbewerb || 'punktrunde')
|
||||
const runde = normalizeText(row?.Runde)
|
||||
const staffel = normalizeText(row?.Staffel)
|
||||
const liga = normalizeText(row?.Liga)
|
||||
const isPokal = runde.includes('pokal') || staffel.includes('pokal') || liga.includes('pokal')
|
||||
|
||||
if (wettbewerb === 'alle') return true
|
||||
if (wettbewerb === 'pokal') return isPokal
|
||||
return !isPokal
|
||||
}
|
||||
|
||||
export function rowMatchesTeamFilter(row, rawTeam) {
|
||||
const teamKey = normalizeTeamFilterKey(rawTeam)
|
||||
if (!teamKey || teamKey === 'all') return true
|
||||
|
||||
const heimMannschaft = normalizeText(row?.HeimMannschaft)
|
||||
const gastMannschaft = normalizeText(row?.GastMannschaft)
|
||||
const heimAltersklasse = normalizeText(row?.HeimMannschaftAltersklasse)
|
||||
const gastAltersklasse = normalizeText(row?.GastMannschaftAltersklasse)
|
||||
|
||||
const isHarheimerHeim = heimMannschaft.includes('harheimer tc')
|
||||
const isHarheimerGast = gastMannschaft.includes('harheimer tc')
|
||||
|
||||
if (!isHarheimerHeim && !isHarheimerGast) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (teamKey === 'erwachsene') {
|
||||
const isErwachsenenHeim = isHarheimerHeim && heimAltersklasse.includes('erwachsene') && !heimAltersklasse.includes('jugend')
|
||||
const isErwachsenenGast = isHarheimerGast && gastAltersklasse.includes('erwachsene') && !gastAltersklasse.includes('jugend')
|
||||
return isErwachsenenHeim || isErwachsenenGast
|
||||
}
|
||||
|
||||
if (teamKey === 'nachwuchs' || teamKey === 'jugendmannschaft') {
|
||||
const isJugendHeim = isHarheimerHeim && (heimAltersklasse.includes('jugend') || heimMannschaft.includes('jugend'))
|
||||
const isJugendGast = isHarheimerGast && (gastAltersklasse.includes('jugend') || gastMannschaft.includes('jugend'))
|
||||
return isJugendHeim || isJugendGast
|
||||
}
|
||||
|
||||
const csvVariants = MANNSCHAFT_MAPPING[teamKey] || []
|
||||
if (csvVariants.length === 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
const mannschaftMatch = csvVariants.some((variant) => {
|
||||
if (isHarheimerHeim && matchMappedVariant(heimMannschaft, variant)) return true
|
||||
if (isHarheimerGast && matchMappedVariant(gastMannschaft, variant)) return true
|
||||
return false
|
||||
})
|
||||
|
||||
if (!mannschaftMatch) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (isErwachsenenTeam(teamKey)) {
|
||||
const isErwachsenenHeim = isHarheimerHeim && heimAltersklasse.includes('erwachsene') && !heimAltersklasse.includes('jugend')
|
||||
const isErwachsenenGast = isHarheimerGast && gastAltersklasse.includes('erwachsene') && !gastAltersklasse.includes('jugend')
|
||||
return isErwachsenenHeim || isErwachsenenGast
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
export function filterSpielplanRows(rows, options = {}) {
|
||||
const sourceRows = Array.isArray(rows) ? rows : []
|
||||
const team = options.team || 'all'
|
||||
const wettbewerb = options.wettbewerb || 'punktrunde'
|
||||
|
||||
return sourceRows.filter((row) => rowMatchesTeamFilter(row, team) && rowMatchesWettbewerbFilter(row, wettbewerb))
|
||||
}
|
||||
|
||||
export function toApiTeamParam(filterValue) {
|
||||
const teamKey = normalizeTeamFilterKey(filterValue)
|
||||
if (teamKey === 'all' || teamKey === 'erwachsene' || teamKey === 'nachwuchs' || teamKey === 'jugendmannschaft') {
|
||||
return teamKey
|
||||
}
|
||||
|
||||
if (teamKey.match(/^erwachsene_\d+$/)) {
|
||||
return teamKey
|
||||
}
|
||||
|
||||
return String(filterValue || '').trim().replace(/\s+/g, '_').toLowerCase()
|
||||
}
|
||||
Reference in New Issue
Block a user