1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- const API_BASE = "http://localhost:5000";
- export const getStatus = async () => {
- try {
- const res = await fetch(API_BASE);
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- } catch (err) {
- return {status: err.message, version: null}
- }
- }
- export const createGame = async (timer) => {
- const res = await fetch(`${API_BASE}/game`, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ timer }),
- });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- const { gameId } = await res.json();
- return gameId;
- }
- export const gameInfo = async (gameId) => {
- const res = await fetch(`${API_BASE}/game/${gameId}`);
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
- export const joinGame = async (gameId, playerName) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/join`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ playerName }),
- });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
- export const getCurrentRound = async (gameId, playerId) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/current`, {
- headers: {
- "Authorization": `Player ${playerId}`
- },
- });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
- export const sendGuess = async (gameId, playerId, round, point) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/guesses/${round}`, {
- method: "POST",
- headers: {
- "Authorization": `Player ${playerId}`,
- "Content-Type": "application/json",
- },
- body: JSON.stringify(point),
- });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
|