|
1 | 1 | import type { WortgeflechtLetterRow } from '$lib/games/wortgeflecht'; |
2 | 2 | import { parseWortgeflechtWords } from '$lib/games/wortgeflecht-generator'; |
3 | 3 |
|
4 | | -export const normalizeWortgeflechtWordLineValue = (value: string) => value; |
| 4 | +const MIN_WORTGEFLECHT_WORD_LENGTH = 4; |
| 5 | + |
| 6 | +export const normalizeWortgeflechtWordLineValue = (value: string) => value.toLocaleLowerCase('de-DE'); |
5 | 7 | export const normalizeWortgeflechtWordKey = (value: string) => value.trim().toLocaleLowerCase('de-DE'); |
6 | 8 |
|
| 9 | +const getUniqueWordsByKey = (words: string[]) => { |
| 10 | + const wordsByKey = new Map<string, string>(); |
| 11 | + |
| 12 | + for (const word of words) { |
| 13 | + const trimmedWord = word.trim(); |
| 14 | + const key = normalizeWortgeflechtWordKey(trimmedWord); |
| 15 | + if (!trimmedWord || wordsByKey.has(key)) continue; |
| 16 | + wordsByKey.set(key, trimmedWord); |
| 17 | + } |
| 18 | + |
| 19 | + return Array.from(wordsByKey.values()); |
| 20 | +}; |
| 21 | + |
| 22 | +export const analyzeWortgeflechtGenerationInput = (wordLines: string[]) => { |
| 23 | + const parsed = parseWortgeflechtWords(wordLines.join('\n')); |
| 24 | + const tooShortWords = getUniqueWordsByKey( |
| 25 | + parsed.words.filter(word => Array.from(word.trim()).length < MIN_WORTGEFLECHT_WORD_LENGTH), |
| 26 | + ); |
| 27 | + |
| 28 | + const wordCountByKey = new Map<string, number>(); |
| 29 | + for (const word of parsed.words) { |
| 30 | + const key = normalizeWortgeflechtWordKey(word); |
| 31 | + if (!key) continue; |
| 32 | + wordCountByKey.set(key, (wordCountByKey.get(key) ?? 0) + 1); |
| 33 | + } |
| 34 | + |
| 35 | + const duplicateWords = getUniqueWordsByKey( |
| 36 | + parsed.words.filter(word => (wordCountByKey.get(normalizeWortgeflechtWordKey(word)) ?? 0) > 1), |
| 37 | + ); |
| 38 | + |
| 39 | + return { |
| 40 | + parsed, |
| 41 | + tooShortWords, |
| 42 | + duplicateWords, |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
7 | 46 | export const normalizeWortgeflechtWordLines = (lines: string[]) => { |
8 | 47 | const next = lines.map(line => line ?? ''); |
9 | 48 | if (next.length === 0) next.push(''); |
@@ -42,36 +81,42 @@ export const hasSameWordSetForWortgeflecht = ({ |
42 | 81 | }; |
43 | 82 |
|
44 | 83 | export const validateWortgeflechtGenerationInput = (wordLines: string[]) => { |
45 | | - const parsed = parseWortgeflechtWords(wordLines.join('\n')); |
| 84 | + const analysis = analyzeWortgeflechtGenerationInput(wordLines); |
| 85 | + const { parsed, tooShortWords, duplicateWords } = analysis; |
46 | 86 |
|
47 | 87 | if (parsed.words.length === 0) { |
48 | 88 | return { |
49 | | - parsed, |
| 89 | + ...analysis, |
50 | 90 | error: 'Bitte mindestens ein Wort eingeben (ein Wort pro Zeile).', |
51 | 91 | }; |
52 | 92 | } |
53 | 93 | if (parsed.invalidWords.length > 0) { |
54 | 94 | return { |
55 | | - parsed, |
| 95 | + ...analysis, |
56 | 96 | error: 'Ungültige Zeichen gefunden. Erlaubt sind nur Buchstaben (inkl. ÄÖÜẞ).', |
57 | 97 | }; |
58 | 98 | } |
59 | | - if (parsed.totalLetters !== 48) { |
| 99 | + if (tooShortWords.length > 0) { |
60 | 100 | return { |
61 | | - parsed, |
62 | | - error: `Die Wörter müssen zusammen genau 48 Buchstaben ergeben (aktuell: ${parsed.totalLetters}).`, |
| 101 | + ...analysis, |
| 102 | + error: `Jedes Wort muss mindestens ${MIN_WORTGEFLECHT_WORD_LENGTH} Buchstaben haben.`, |
63 | 103 | }; |
64 | 104 | } |
65 | | - const uniqueWordKeys = Array.from(new Set(parsed.words.map(normalizeWortgeflechtWordKey))); |
66 | | - if (uniqueWordKeys.length !== parsed.words.length) { |
| 105 | + if (duplicateWords.length > 0) { |
67 | 106 | return { |
68 | | - parsed, |
| 107 | + ...analysis, |
69 | 108 | error: 'Doppelte Wörter sind nicht erlaubt.', |
70 | 109 | }; |
71 | 110 | } |
| 111 | + if (parsed.totalLetters !== 48) { |
| 112 | + return { |
| 113 | + ...analysis, |
| 114 | + error: `Die Wörter müssen zusammen genau 48 Buchstaben ergeben (aktuell: ${parsed.totalLetters}).`, |
| 115 | + }; |
| 116 | + } |
72 | 117 |
|
73 | 118 | return { |
74 | | - parsed, |
| 119 | + ...analysis, |
75 | 120 | error: null, |
76 | 121 | }; |
77 | 122 | }; |
0 commit comments