123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- const API_BASE = "https://hiram.services/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 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) => {
- const res = await fetch(`${API_BASE}/game`, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ timer, rounds, onlyAmerica, generationMethod }),
- });
- 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: "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, timeRemaining) => {
- const res = await fetch(`${API_BASE}/game/${gameId}/guesses/${round}`, {
- method: "POST",
- headers: {
- "Authorization": `Player ${playerId}`,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ timeRemaining, ...point }),
- });
- if (!res.ok) {
- throw Error(res.statusText);
- }
- return await res.json();
- }
|