123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- const API_BASE = process.env.REACT_APP_API_BASE;
- export const getStatus = async () => {
- try {
- const res = await fetch(`${API_BASE}/health`);
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- } catch (err) {
- return {status: err.message, version: null}
- }
- }
- 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();
- }
- export const createGame = async (timer, rounds, onlyAmerica, generationMethod, ruleSet) => {
- const res = await fetch(`${API_BASE}/game`, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ timer, rounds, onlyAmerica, generationMethod, ruleSet }),
- });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- const { gameId } = await res.json();
- return gameId;
- }
- export const getGameConfig = async (gameId) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/config`);
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
- export const getGameCoords = async (gameId) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/coords`);
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
- export const getPlayers = async (gameId) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/players`);
- if (!res.ok) {
- throw Error(res.statusText);
- }
- const { players } = await res.json();
- return players;
- }
- export const getLinkedGame = async (gameId) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/linked`);
- if (!res.ok) {
- throw Error(res.statusText);
- }
- const { linkedGame } = await res.json();
- return linkedGame;
- }
- export const linkGame = async (gameId, linkedGame) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/linked`, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ linkedGame }),
- });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- }
- export const getFirstSubmitter = async (gameId, round) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/round/${round}/first`);
- if (!res.ok) {
- return null; // API is that 404 means no submitter yet (or wrong game)
- }
- const { first } = await res.json();
- return first;
- }
- 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}/players/${playerId}/current`);
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
- export const sendGuess = async (gameId, playerId, round, point, timeRemaining) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/round/${round}/guess/${playerId}`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ timeRemaining, ...point }),
- });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
- export const sendTimeout = async (gameId, playerId, round) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/round/${round}/timeout/${playerId}`, { method: "POST" });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
|