| 1 | // src/lib/api.ts |
| 2 | import axios from 'axios'; |
| 3 | |
| 4 | const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api'; |
| 5 | |
| 6 | const api = axios.create({ |
| 7 | baseURL: API_BASE_URL, |
| 8 | headers: { |
| 9 | 'Content-Type': 'application/json', |
| 10 | }, |
| 11 | }); |
| 12 | |
| 13 | export interface TreeNode { |
| 14 | name: string; |
| 15 | path: string; |
| 16 | is_fhs: boolean; |
| 17 | description: string; |
| 18 | has_mole: boolean; |
| 19 | children: TreeNode[]; |
| 20 | } |
| 21 | |
| 22 | export interface FileSystemTree { |
| 23 | id: number; |
| 24 | name: string; |
| 25 | created_at: string; |
| 26 | seed: number; |
| 27 | player_location: string; |
| 28 | is_completed: boolean; |
| 29 | completed_at: string | null; |
| 30 | tree_data: TreeNode; |
| 31 | total_directories: number; |
| 32 | moles_killed?: number; |
| 33 | total_commands?: number; |
| 34 | total_directories_visited?: number; |
| 35 | } |
| 36 | |
| 37 | export interface MoleDirection { |
| 38 | direction: string; |
| 39 | angle: number; |
| 40 | } |
| 41 | |
| 42 | export interface CommandResponse { |
| 43 | command: string; |
| 44 | success: boolean; |
| 45 | output: string; |
| 46 | current_path: string; |
| 47 | game_won?: boolean; |
| 48 | mole_spawned?: boolean; |
| 49 | mole_direction?: MoleDirection | null; |
| 50 | score?: number; |
| 51 | moles_killed?: number; |
| 52 | new_mole_location?: string; |
| 53 | } |
| 54 | |
| 55 | export interface GameCreationResponse { |
| 56 | tree: FileSystemTree; |
| 57 | session_id: number; |
| 58 | mole_hint: string; |
| 59 | home_directory: string; |
| 60 | } |
| 61 | |
| 62 | export interface HintResponse { |
| 63 | hints: string[]; |
| 64 | } |
| 65 | |
| 66 | export interface FHSDirectory { |
| 67 | path: string; |
| 68 | name: string; |
| 69 | desc: string; |
| 70 | } |
| 71 | |
| 72 | export interface FHSReferenceResponse { |
| 73 | directories: FHSDirectory[]; |
| 74 | } |
| 75 | |
| 76 | export interface CommandCategory { |
| 77 | command: string; |
| 78 | description: string; |
| 79 | examples: string[]; |
| 80 | options?: Record<string, string>; |
| 81 | variables?: Record<string, string>; |
| 82 | } |
| 83 | |
| 84 | export interface SpecialPath { |
| 85 | path: string; |
| 86 | description: string; |
| 87 | examples: string[]; |
| 88 | } |
| 89 | |
| 90 | export interface CommandReferenceResponse { |
| 91 | navigation: CommandCategory[]; |
| 92 | exploration: CommandCategory[]; |
| 93 | utility: CommandCategory[]; |
| 94 | game: CommandCategory[]; |
| 95 | special_paths: SpecialPath[]; |
| 96 | } |
| 97 | |
| 98 | export interface TimerWarning { |
| 99 | level: string; |
| 100 | message: string; |
| 101 | } |
| 102 | |
| 103 | export interface CommandResponse { |
| 104 | command: string; |
| 105 | success: boolean; |
| 106 | output: string; |
| 107 | current_path: string; |
| 108 | game_won?: boolean; |
| 109 | mole_spawned?: boolean; |
| 110 | mole_direction?: MoleDirection | null; |
| 111 | score?: number; |
| 112 | moles_killed?: number; |
| 113 | new_mole_location?: string; |
| 114 | timer_remaining?: number; |
| 115 | timer_warnings?: TimerWarning[]; |
| 116 | new_timer?: number; |
| 117 | timer_reason?: string; |
| 118 | timer_distance?: number; |
| 119 | } |
| 120 | |
| 121 | export interface TimerStatusResponse { |
| 122 | remaining: number; |
| 123 | total: number; |
| 124 | percentage: number; |
| 125 | warning_level: string | null; |
| 126 | expired: boolean; |
| 127 | paused: boolean; |
| 128 | } |
| 129 | |
| 130 | export interface EscapeData { |
| 131 | escaped: boolean; |
| 132 | old_location: string; |
| 133 | new_location: string; |
| 134 | total_escapes: number; |
| 135 | new_timer: number; |
| 136 | timer_reason: string; |
| 137 | distance: number; |
| 138 | mole_direction?: MoleDirection | null; |
| 139 | } |
| 140 | |
| 141 | export interface CheckTimerResponse { |
| 142 | timer_remaining: number; |
| 143 | timer_expired: boolean; |
| 144 | mole_location: string; |
| 145 | timer_paused: boolean; |
| 146 | mole_escaped?: boolean; |
| 147 | escape_data?: EscapeData; |
| 148 | message?: string; |
| 149 | } |
| 150 | |
| 151 | export interface GameCreationResponse { |
| 152 | tree: FileSystemTree; |
| 153 | session_id: number; |
| 154 | mole_hint: string; |
| 155 | home_directory: string; |
| 156 | initial_timer?: number; |
| 157 | timer_reason?: string; |
| 158 | timer_distance?: number; |
| 159 | } |
| 160 | |
| 161 | export const gameApi = { |
| 162 | createGame: async (playerName: string = 'Anonymous'): Promise<GameCreationResponse> => { |
| 163 | const response = await api.post('/trees/filesystem-trees/create_game/', { |
| 164 | player_name: playerName, |
| 165 | max_depth: 4, |
| 166 | dirs_per_level: 3, |
| 167 | }); |
| 168 | return response.data; |
| 169 | }, |
| 170 | |
| 171 | executeCommand: async ( |
| 172 | treeId: number, |
| 173 | command: string, |
| 174 | sessionId?: number |
| 175 | ): Promise<CommandResponse> => { |
| 176 | const response = await api.post(`/trees/filesystem-trees/${treeId}/execute_command/`, { |
| 177 | command, |
| 178 | session_id: sessionId, |
| 179 | }); |
| 180 | return response.data; |
| 181 | }, |
| 182 | |
| 183 | getHint: async (treeId: number): Promise<HintResponse> => { |
| 184 | const response = await api.get(`/trees/filesystem-trees/${treeId}/hint/`); |
| 185 | return response.data; |
| 186 | }, |
| 187 | |
| 188 | getCurrentDirectory: async (treeId: number) => { |
| 189 | const response = await api.get(`/trees/filesystem-trees/${treeId}/current_directory/`); |
| 190 | return response.data; |
| 191 | }, |
| 192 | |
| 193 | getFHSReference: async (): Promise<FHSReferenceResponse> => { |
| 194 | const response = await api.get('/trees/filesystem-trees/fhs_reference/'); |
| 195 | return response.data; |
| 196 | }, |
| 197 | |
| 198 | getCommandReference: async (): Promise<CommandReferenceResponse> => { |
| 199 | const response = await api.get('/trees/filesystem-trees/command_reference/'); |
| 200 | return response.data; |
| 201 | }, |
| 202 | |
| 203 | getTimerStatus: async (treeId: number): Promise<TimerStatusResponse> => { |
| 204 | const response = await api.get(`/trees/filesystem-trees/${treeId}/timer_status/`); |
| 205 | return response.data; |
| 206 | }, |
| 207 | |
| 208 | checkTimer: async (treeId: number, sessionId?: number): Promise<CheckTimerResponse> => { |
| 209 | const url = `/trees/filesystem-trees/${treeId}/check_timer/`; |
| 210 | const params = sessionId ? `?session_id=${sessionId}` : ''; |
| 211 | const response = await api.get(url + params); |
| 212 | return response.data; |
| 213 | }, |
| 214 | }; |