TypeScript · 1893 bytes Raw Blame History
1 // src/lib/api.ts
2 import axios from 'axios';
3
4 const API_BASE_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 }
33
34 export interface CommandResponse {
35 command: string;
36 success: boolean;
37 output: string;
38 current_path: string;
39 game_won?: boolean;
40 }
41
42 export interface GameCreationResponse {
43 tree: FileSystemTree;
44 session_id: number;
45 mole_hint: string;
46 home_directory: string;
47 }
48
49 export interface HintResponse {
50 hints: string[];
51 }
52
53 export const gameApi = {
54 createGame: async (playerName: string = 'Anonymous'): Promise<GameCreationResponse> => {
55 const response = await api.post('/trees/filesystem-trees/create_game/', {
56 player_name: playerName,
57 max_depth: 4,
58 dirs_per_level: 3,
59 });
60 return response.data;
61 },
62
63 executeCommand: async (
64 treeId: number,
65 command: string,
66 sessionId?: number
67 ): Promise<CommandResponse> => {
68 const response = await api.post(`/trees/filesystem-trees/${treeId}/execute_command/`, {
69 command,
70 session_id: sessionId,
71 });
72 return response.data;
73 },
74
75 getHint: async (treeId: number): Promise<HintResponse> => {
76 const response = await api.get(`/trees/filesystem-trees/${treeId}/hint/`);
77 return response.data;
78 },
79
80 getCurrentDirectory: async (treeId: number) => {
81 const response = await api.get(`/trees/filesystem-trees/${treeId}/current_directory/`);
82 return response.data;
83 },
84 };