| 1 | <?php |
| 2 | /** |
| 3 | * importWildlife.php |
| 4 | * |
| 5 | * Reads wildlife.txt and generates: |
| 6 | * web/data/wildlife/<slug>.json |
| 7 | * |
| 8 | * Output JSON format matches your requested schema |
| 9 | * (health/moves are empty arrays). |
| 10 | */ |
| 11 | |
| 12 | declare(strict_types=1); |
| 13 | |
| 14 | $inputFile = __DIR__ . DIRECTORY_SEPARATOR . 'wildlife.txt'; |
| 15 | |
| 16 | // Adjust these if your folder differs: |
| 17 | $outputDir = __DIR__ . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'wildlife'; |
| 18 | |
| 19 | $regions = ['Halsand', 'Durand', 'Droquia', 'Isilia', 'Othenia']; |
| 20 | $regionName = "non"; |
| 21 | |
| 22 | $map = [ |
| 23 | 'Size' => 'size', |
| 24 | 'Aggressiveness' => 'aggressiveness', |
| 25 | 'Societal Impact' => 'societalImpact', |
| 26 | 'Rituals/Behaviours' => 'ritualsBehaviour', |
| 27 | 'Defence/Hunting' => 'defence', |
| 28 | 'Diet' => 'diet', |
| 29 | 'Appearance' => 'appearance', |
| 30 | 'Habitat' => 'habitat', |
| 31 | 'Social Structure' => 'societalStructure', |
| 32 | ]; |
| 33 | |
| 34 | function slugify(string $name): string |
| 35 | { |
| 36 | $name = trim($name); |
| 37 | $name = preg_replace('/[^\p{L}\p{N}]+/u', '-', $name) ?? $name; // keep unicode letters/digits |
| 38 | $name = trim($name, '-'); |
| 39 | $name = mb_strtolower($name); |
| 40 | return $name; |
| 41 | } |
| 42 | |
| 43 | function ensureDir(string $dir): void |
| 44 | { |
| 45 | if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) { |
| 46 | throw new RuntimeException("Failed to create directory: $dir"); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (!file_exists($inputFile)) { |
| 51 | throw new RuntimeException("Input file not found: $inputFile"); |
| 52 | } |
| 53 | ensureDir($outputDir); |
| 54 | |
| 55 | $lines = file($inputFile, FILE_IGNORE_NEW_LINES); |
| 56 | if ($lines === false) { |
| 57 | throw new RuntimeException("Failed to read: $inputFile"); |
| 58 | } |
| 59 | |
| 60 | $currentRegion = null; |
| 61 | $entries = []; |
| 62 | |
| 63 | $i = 0; |
| 64 | while ($i < count($lines)) { |
| 65 | $line = trim((string) $lines[$i]); |
| 66 | |
| 67 | // Skip blank lines |
| 68 | if ($line === '') { |
| 69 | $i++; |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | // Region headers (ignored for output, but used to parse) |
| 74 | if (in_array($line, $regions, true)) { |
| 75 | $currentRegion = $line; |
| 76 | $i++; |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | // Creature name (start of a new entry) |
| 81 | $name = $line; |
| 82 | |
| 83 | $data = [ |
| 84 | 'region' => $currentRegion, // not requested in JSON; uncomment if you ever want it |
| 85 | 'type' => 'wildlife', |
| 86 | 'name' => $name, |
| 87 | 'size' => '', |
| 88 | 'aggressiveness' => '', |
| 89 | 'societalImpact' => '', |
| 90 | 'ritualsBehaviour' => '', |
| 91 | 'defence' => '', |
| 92 | 'diet' => '', |
| 93 | 'appearance' => '', |
| 94 | 'habitat' => '', |
| 95 | 'societalStructure' => '', |
| 96 | 'health' => [], |
| 97 | 'moves' => [], |
| 98 | ]; |
| 99 | |
| 100 | $i++; |
| 101 | |
| 102 | // Parse until we hit "Moves:" (end marker) |
| 103 | while ($i < count($lines)) { |
| 104 | $l = trim((string) $lines[$i]); |
| 105 | |
| 106 | // If we hit a region header mid-parse, treat as boundary |
| 107 | if ($l !== '' && in_array($l, $regions, true)) { |
| 108 | $currentRegion = $l; |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | // End marker for an entry |
| 113 | if (str_starts_with($l, 'Moves:')) { |
| 114 | $i++; |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | // Normal "Label: value" |
| 119 | if (preg_match('/^([^:]+):\s*(.*)$/u', $l, $m)) { |
| 120 | $label = trim($m[1]); |
| 121 | $value = trim($m[2]); |
| 122 | |
| 123 | if (isset($map[$label])) { |
| 124 | $data[$map[$label]] = $value; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | $i++; |
| 129 | } |
| 130 | |
| 131 | $entries[] = $data; |
| 132 | } |
| 133 | |
| 134 | // Write files |
| 135 | $written = 0; |
| 136 | foreach ($entries as $e) { |
| 137 | $slug = slugify($e['name']); |
| 138 | $folder = $e["region"]; |
| 139 | unset($e["region"]); |
| 140 | ensureDir($outputDir . DIRECTORY_SEPARATOR . $folder); |
| 141 | $path = $outputDir . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $slug . '.json'; |
| 142 | |
| 143 | // If two names slugify to the same filename, add a suffix |
| 144 | $suffix = 2; |
| 145 | while (file_exists($path)) { |
| 146 | $path = $outputDir . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $slug . '-' . $suffix . '.json'; |
| 147 | $suffix++; |
| 148 | } |
| 149 | |
| 150 | $json = json_encode($e, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
| 151 | if ($json === false) { |
| 152 | throw new RuntimeException("JSON encode failed for: " . $e['name']); |
| 153 | } |
| 154 | |
| 155 | file_put_contents($path, $json . PHP_EOL); |
| 156 | $written++; |
| 157 | } |
| 158 | |
| 159 | echo "Imported " . count($entries) . " entries; wrote $written JSON files to: $outputDir" . PHP_EOL; |