diff --git a/client/src/store/turtleStore.js b/client/src/store/turtleStore.js index 88c1214..68265bf 100644 --- a/client/src/store/turtleStore.js +++ b/client/src/store/turtleStore.js @@ -12,6 +12,7 @@ export const useTurtleStore = create((set, get) => ({ turtles: {}, players: {}, worldBlocks: [], + chunkAnalyses: {}, selectedTurtleId: null, connected: false, ws: null, @@ -97,6 +98,42 @@ export const useTurtleStore = create((set, get) => ({ return { worldBlocks: Array.from(blockMap.values()) }; }); } + } else if (data.type === 'turtle_event') { + // Handle live turtle events (inventory, peripherals) + const turtleId = data.turtleID; + if (turtleId && data.eventType === 'inventory_update' && data.inventory) { + set(state => { + const turtle = state.turtles[turtleId]; + if (!turtle) return state; + return { + turtles: { + ...state.turtles, + [turtleId]: { ...turtle, inventory: data.inventory } + } + }; + }); + } else if (turtleId && (data.eventType === 'peripheral_attached' || data.eventType === 'peripheral_detached')) { + set(state => { + const turtle = state.turtles[turtleId]; + if (!turtle) return state; + return { + turtles: { + ...state.turtles, + [turtleId]: { ...turtle, peripherals: data.peripherals || turtle.peripherals } + } + }; + }); + } + } else if (data.type === 'chunk_analysis') { + // Store chunk analysis results + if (data.chunk) { + set(state => ({ + chunkAnalyses: { + ...state.chunkAnalyses, + [`${data.chunk.x},${data.chunk.z}`]: data.chunk + } + })); + } } } catch (error) { console.error('Error processing message:', error); @@ -241,6 +278,55 @@ export const useTurtleStore = create((set, get) => ({ } }, + // Fetch chunk analyses from server + fetchChunkAnalyses: async () => { + try { + const response = await fetch(`${API_URL}/chunks`); + if (response.ok) { + const data = await response.json(); + const analyses = {}; + data.forEach(c => { analyses[`${c.x},${c.z}`] = c; }); + set({ chunkAnalyses: analyses }); + } + } catch (error) { + console.error(' ❌ Error fetching chunks:', error); + } + }, + + // Request chunk analysis for a specific chunk + analyzeChunk: async (chunkX, chunkZ) => { + try { + const response = await fetch(`${API_URL}/chunks/${chunkX}/${chunkZ}/analyze`, { + method: 'POST' + }); + if (response.ok) { + const data = await response.json(); + set(state => ({ + chunkAnalyses: { + ...state.chunkAnalyses, + [`${chunkX},${chunkZ}`]: data + } + })); + return data; + } + } catch (error) { + console.error(' ❌ Error analyzing chunk:', error); + } + }, + + // Search blocks by name pattern + searchBlocks: async (namePattern) => { + try { + const response = await fetch(`${API_URL}/world/blocks/search?name=${encodeURIComponent(namePattern)}`); + if (response.ok) { + return await response.json(); + } + } catch (error) { + console.error(' ❌ Error searching blocks:', error); + } + return []; + }, + // Helper getters getTurtleArray: () => { return Object.values(get().turtles);