Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | import { NextResponse } from 'next/server' import { withAuth } from '@/lib/auth/withAuth' import { readFileSync, writeFileSync } from 'fs' import { join } from 'path' /** * Dev-only API endpoint to save/delete crop coordinates in customCrops.ts * Only works in development mode * * POST: Save a new crop * DELETE: Remove a crop */ const CUSTOM_CROPS_PATH = join(process.cwd(), 'src/arcade-games/know-your-world/customCrops.ts') function parseCropsFile(): Record<string, Record<string, string>> { const currentContent = readFileSync(CUSTOM_CROPS_PATH, 'utf-8') // Find the customCrops assignment and extract the object // Look for the pattern and then find the matching closing brace const startMatch = currentContent.match(/export const customCrops: CropOverrides = /) if (!startMatch || startMatch.index === undefined) { console.error('[DevCropTool] Could not find customCrops declaration') return {} } const startIndex = startMatch.index + startMatch[0].length let braceCount = 0 let endIndex = startIndex let inString = false let stringChar = '' for (let i = startIndex; i < currentContent.length; i++) { const char = currentContent[i] // Handle string literals if ((char === "'" || char === '"') && currentContent[i - 1] !== '\\') { if (!inString) { inString = true stringChar = char } else if (char === stringChar) { inString = false } continue } if (inString) continue if (char === '{') { braceCount++ } else if (char === '}') { braceCount-- if (braceCount === 0) { endIndex = i + 1 break } } } const objectStr = currentContent.slice(startIndex, endIndex) try { const cleanedObject = objectStr .replace(/\/\/.*$/gm, '') // Remove comments .replace(/,(\s*[}\]])/g, '$1') // Remove trailing commas .replace(/'/g, '"') // Convert single quotes to double // Add quotes around unquoted keys (e.g., `world:` -> `"world":`) .replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)(\s*:)/g, '$1"$2"$3') return JSON.parse(cleanedObject) } catch (e) { console.error('[DevCropTool] Failed to parse crops:', e, objectStr) return {} } } function writeCropsFile(crops: Record<string, Record<string, string>>): void { const currentContent = readFileSync(CUSTOM_CROPS_PATH, 'utf-8') // Find the customCrops assignment const startMatch = currentContent.match(/export const customCrops: CropOverrides = /) if (!startMatch || startMatch.index === undefined) { console.error('[DevCropTool] Could not find customCrops declaration for writing') return } const declStart = startMatch.index const objStart = declStart + startMatch[0].length // Find the matching closing brace let braceCount = 0 let endIndex = objStart let inString = false let stringChar = '' for (let i = objStart; i < currentContent.length; i++) { const char = currentContent[i] if ((char === "'" || char === '"') && currentContent[i - 1] !== '\\') { if (!inString) { inString = true stringChar = char } else if (char === stringChar) { inString = false } continue } if (inString) continue if (char === '{') { braceCount++ } else if (char === '}') { braceCount-- if (braceCount === 0) { endIndex = i + 1 break } } } // Format the new crops object let formattedCrops: string if (Object.keys(crops).length === 0) { formattedCrops = '{}' } else { formattedCrops = JSON.stringify(crops, null, 2) // Only remove quotes from keys that are valid JS identifiers (no hyphens, spaces, etc.) // Valid identifiers: start with letter/$/_, contain only letters/digits/$/_ .replace(/"([a-zA-Z_$][a-zA-Z0-9_$]*)":/g, '$1:') // Keep quotes but convert to single quotes for keys with special chars (like hyphens) .replace(/"([^"]+)":/g, "'$1':") .replace(/"/g, "'") // Use single quotes for values // Add trailing commas before closing braces/brackets .replace(/([^,{\s])\n(\s*[}\]])/g, '$1,\n$2') } // Replace the object const newContent = currentContent.slice(0, objStart) + formattedCrops + currentContent.slice(endIndex) console.log('[DevCropTool] Writing new content to file:', formattedCrops) writeFileSync(CUSTOM_CROPS_PATH, newContent, 'utf-8') console.log('[DevCropTool] File written successfully') } interface CropRequest { mapId: string continentId: string viewBox?: string } export const POST = withAuth( async (request) => { // Only allow in development if (process.env.NODE_ENV !== 'development') { return NextResponse.json( { error: 'This endpoint is only available in development mode' }, { status: 403 } ) } try { const body: CropRequest = await request.json() const { mapId, continentId, viewBox } = body if (!mapId || !continentId || !viewBox) { return NextResponse.json( { error: 'Missing required fields: mapId, continentId, viewBox' }, { status: 400 } ) } const crops = parseCropsFile() // Update the crops if (!crops[mapId]) { crops[mapId] = {} } crops[mapId][continentId] = viewBox writeCropsFile(crops) console.log(`[DevCropTool] Saved crop for ${mapId}/${continentId}: ${viewBox}`) return NextResponse.json({ success: true, message: `Saved crop for ${mapId}/${continentId}`, crops, }) } catch (error) { console.error('[DevCropTool] Error saving crop:', error) return NextResponse.json( { error: 'Failed to save crop', details: String(error) }, { status: 500 } ) } }, { role: 'admin' } ) export const DELETE = withAuth( async (request) => { // Only allow in development if (process.env.NODE_ENV !== 'development') { return NextResponse.json( { error: 'This endpoint is only available in development mode' }, { status: 403 } ) } try { const { searchParams } = new URL(request.url) const mapId = searchParams.get('mapId') const continentId = searchParams.get('continentId') if (!mapId || !continentId) { return NextResponse.json( { error: 'Missing required query params: mapId, continentId' }, { status: 400 } ) } const crops = parseCropsFile() console.log('[DevCropTool] Parsed crops before delete:', JSON.stringify(crops)) // Check if crop exists if (!crops[mapId]?.[continentId]) { console.log(`[DevCropTool] No crop found for ${mapId}/${continentId}`) return NextResponse.json({ success: true, message: `No crop found for ${mapId}/${continentId}`, crops, }) } // Delete the crop console.log(`[DevCropTool] Deleting ${mapId}/${continentId}`) delete crops[mapId][continentId] // Clean up empty map objects if (Object.keys(crops[mapId]).length === 0) { console.log(`[DevCropTool] Removing empty map object for ${mapId}`) delete crops[mapId] } console.log('[DevCropTool] Crops after delete:', JSON.stringify(crops)) writeCropsFile(crops) console.log(`[DevCropTool] Deleted crop for ${mapId}/${continentId}`) return NextResponse.json({ success: true, message: `Deleted crop for ${mapId}/${continentId}`, crops, }) } catch (error) { console.error('[DevCropTool] Error deleting crop:', error) return NextResponse.json( { error: 'Failed to delete crop', details: String(error) }, { status: 500 } ) } }, { role: 'admin' } ) |