const API_BASE = "https://kirkleon.ddns.net/terrassumptions/api"; 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, rounds, onlyAmerica) => { const res = await fetch(`${API_BASE}/game`, { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ timer, rounds, onlyAmerica }), }); 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 linkGame = async (gameId, linkedGame) => { const res = await fetch(`${API_BASE}/game/${gameId}/linked`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ linkedGame }), }); if (!res.ok) { throw Error(res.statusText); } } 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(); } export const checkScore = async (point1, point2) => { const res = await fetch(`${API_BASE}/score`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ point1, point2 }), }); if (!res.ok) { throw Error(res.statusText); } return await res.json(); }