TypeScript · 1867 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 }
47
48 export interface HintResponse {
49 hints: string[];
50 }
51
52 export const gameApi = {
53 createGame: async (playerName: string = 'Anonymous'): Promise<GameCreationResponse> => {
54 const response = await api.post('/trees/filesystem-trees/create_game/', {
55 player_name: playerName,
56 max_depth: 4,
57 dirs_per_level: 3,
58 });
59 return response.data;
60 },
61
62 executeCommand: async (
63 treeId: number,
64 command: string,
65 sessionId?: number
66 ): Promise<CommandResponse> => {
67 const response = await api.post(`/trees/filesystem-trees/${treeId}/execute_command/`, {
68 command,
69 session_id: sessionId,
70 });
71 return response.data;
72 },
73
74 getHint: async (treeId: number): Promise<HintResponse> => {
75 const response = await api.get(`/trees/filesystem-trees/${treeId}/hint/`);
76 return response.data;
77 },
78
79 getCurrentDirectory: async (treeId: number) => {
80 const response = await api.get(`/trees/filesystem-trees/${treeId}/current_directory/`);
81 return response.data;
82 },
83 };