TypeScript · 5795 bytes Raw Blame History
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 TimerWarning {
43 level: string;
44 message: string;
45 }
46
47 export interface GameStats {
48 score: number;
49 moles_killed: number;
50 moles_escaped: number;
51 commands_used: number;
52 time_taken: string;
53 directories_visited: number;
54 }
55
56 export interface CommandResponse {
57 command: string;
58 success: boolean;
59 output: string;
60 current_path: string;
61 game_won?: boolean;
62 mole_spawned?: boolean;
63 mole_direction?: MoleDirection | null;
64 score?: number;
65 moles_killed?: number;
66 new_mole_location?: string;
67 timer_remaining?: number;
68 timer_warnings?: TimerWarning[];
69 new_timer?: number;
70 timer_reason?: string;
71 timer_distance?: number;
72 game_completed?: boolean;
73 session_completed?: boolean;
74 final_stats?: GameStats;
75 }
76
77 export interface GameCreationResponse {
78 tree: FileSystemTree;
79 session_id: number;
80 mole_hint: string;
81 home_directory: string;
82 initial_timer?: number;
83 timer_reason?: string;
84 timer_distance?: number;
85 }
86
87 export interface HintResponse {
88 hints: string[];
89 }
90
91 export interface FHSDirectory {
92 path: string;
93 name: string;
94 desc: string;
95 }
96
97 export interface FHSReferenceResponse {
98 directories: FHSDirectory[];
99 }
100
101 export interface CommandCategory {
102 command: string;
103 description: string;
104 examples: string[];
105 options?: Record<string, string>;
106 variables?: Record<string, string>;
107 }
108
109 export interface SpecialPath {
110 path: string;
111 description: string;
112 examples: string[];
113 }
114
115 export interface CommandReferenceResponse {
116 navigation: CommandCategory[];
117 exploration: CommandCategory[];
118 utility: CommandCategory[];
119 game: CommandCategory[];
120 special_paths: SpecialPath[];
121 }
122
123 export interface TimerStatusResponse {
124 remaining: number;
125 total: number;
126 percentage: number;
127 warning_level: string | null;
128 expired: boolean;
129 paused: boolean;
130 }
131
132 export interface EscapeData {
133 escaped: boolean;
134 old_location: string;
135 new_location: string;
136 total_escapes: number;
137 new_timer: number;
138 timer_reason: string;
139 distance: number;
140 mole_direction?: MoleDirection | null;
141 }
142
143 export interface CheckTimerResponse {
144 timer_remaining: number;
145 timer_expired: boolean;
146 mole_location: string;
147 timer_paused: boolean;
148 mole_escaped?: boolean;
149 escape_data?: EscapeData;
150 message?: string;
151 }
152
153 export interface LeaderboardEntry {
154 rank: number;
155 player_name: string;
156 score: number;
157 moles_killed: number;
158 moles_escaped: number;
159 commands_used: number;
160 time_taken: string;
161 completed_at: string;
162 }
163
164 export interface LeaderboardResponse {
165 leaderboard: LeaderboardEntry[];
166 total_games: number;
167 }
168
169 export interface SavePlayerNameResponse {
170 success: boolean;
171 player_name: string;
172 score: number;
173 leaderboard_position: number;
174 }
175
176 export const gameApi = {
177 createGame: async (playerName: string = 'Anonymous'): Promise<GameCreationResponse> => {
178 const response = await api.post('/trees/filesystem-trees/create_game/', {
179 player_name: playerName,
180 max_depth: 4,
181 dirs_per_level: 3,
182 });
183 return response.data;
184 },
185
186 executeCommand: async (
187 treeId: number,
188 command: string,
189 sessionId?: number
190 ): Promise<CommandResponse> => {
191 const response = await api.post(`/trees/filesystem-trees/${treeId}/execute_command/`, {
192 command,
193 session_id: sessionId,
194 });
195 return response.data;
196 },
197
198 getHint: async (treeId: number): Promise<HintResponse> => {
199 const response = await api.get(`/trees/filesystem-trees/${treeId}/hint/`);
200 return response.data;
201 },
202
203 getCurrentDirectory: async (treeId: number) => {
204 const response = await api.get(`/trees/filesystem-trees/${treeId}/current_directory/`);
205 return response.data;
206 },
207
208 getFHSReference: async (): Promise<FHSReferenceResponse> => {
209 const response = await api.get('/trees/filesystem-trees/fhs_reference/');
210 return response.data;
211 },
212
213 getCommandReference: async (): Promise<CommandReferenceResponse> => {
214 const response = await api.get('/trees/filesystem-trees/command_reference/');
215 return response.data;
216 },
217
218 getTimerStatus: async (treeId: number): Promise<TimerStatusResponse> => {
219 const response = await api.get(`/trees/filesystem-trees/${treeId}/timer_status/`);
220 return response.data;
221 },
222
223 checkTimer: async (treeId: number, sessionId?: number): Promise<CheckTimerResponse> => {
224 const url = `/trees/filesystem-trees/${treeId}/check_timer/`;
225 const params = sessionId ? `?session_id=${sessionId}` : '';
226 const response = await api.get(url + params);
227 return response.data;
228 },
229
230 savePlayerName: async (sessionId: number, playerName: string): Promise<SavePlayerNameResponse> => {
231 const response = await api.post('/trees/game-sessions/save_player_name/', {
232 session_id: sessionId,
233 player_name: playerName,
234 });
235 return response.data;
236 },
237
238 getLeaderboard: async (): Promise<LeaderboardResponse> => {
239 const response = await api.get('/trees/game-sessions/leaderboard/');
240 return response.data;
241 },
242 };