Files
SerpentRace/SerpentRace_Frontend/src/api/gameApi.js
T

81 lines
2.2 KiB
JavaScript

import { apiClient } from './userApi';
/**
* Create a new game
* @param {Object} gameData - Game creation data
* @param {string[]} gameData.deckids - Array of deck UUIDs
* @param {number} gameData.maxplayers - Maximum players (2-8)
* @param {number} gameData.logintype - 0=PUBLIC, 1=PRIVATE, 2=ORGANIZATION
* @returns {Promise<Object>} Game data with gameCode
*/
export const createGame = async (gameData) => {
try {
const response = await apiClient.post('/games/start', gameData);
return response.data;
} catch (error) {
console.error('Error creating game:', error);
throw error;
}
};
/**
* Join an existing game
* @param {Object} joinData - Join game data
* @param {string} joinData.gameCode - 6-character game code
* @param {string} [joinData.playerName] - Player name (required for public games)
* @returns {Promise<Object>} Game data with gameToken
*/
export const joinGame = async (joinData) => {
try {
const response = await apiClient.post('/games/join', joinData);
return response.data;
} catch (error) {
console.error('Error joining game:', error);
console.error('Join game error response:', error.response?.data);
throw error;
}
};
/**
* Start the game (gamemaster only)
* @param {string} gameId - Game UUID
* @returns {Promise<Object>} Game data with board
*/
export const startGame = async (gameId) => {
try {
const response = await apiClient.post(`/games/${gameId}/start`);
return response.data;
} catch (error) {
console.error('Error starting game:', error);
throw error;
}
};
/**
* Get user's games
* @returns {Promise<Array>} Array of games
*/
export const getMyGames = async () => {
try {
const response = await apiClient.get('/games/my-games');
return response.data;
} catch (error) {
console.error('Error fetching games:', error);
throw error;
}
};
/**
* Get active public games
* @returns {Promise<Array>} Array of active games
*/
export const getActiveGames = async () => {
try {
const response = await apiClient.get('/games/active');
return response.data;
} catch (error) {
console.error('Error fetching active games:', error);
throw error;
}
};