apiMethods.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const API_BASE = "https://hiram.services/terrassumptions/api";
  2. export const getStatus = async () => {
  3. try {
  4. const res = await fetch(API_BASE);
  5. if (!res.ok) {
  6. throw Error(res.statusText);
  7. }
  8. return await res.json();
  9. } catch (err) {
  10. return {status: err.message, version: null}
  11. }
  12. }
  13. export const checkScore = async (point1, point2) => {
  14. const res = await fetch(`${API_BASE}/score`, {
  15. method: "POST",
  16. headers: {
  17. "Content-Type": "application/json",
  18. },
  19. body: JSON.stringify({ point1, point2 }),
  20. });
  21. if (!res.ok) {
  22. throw Error(res.statusText);
  23. }
  24. return await res.json();
  25. }
  26. export const createGame = async (timer, rounds, onlyAmerica, generationMethod) => {
  27. const res = await fetch(`${API_BASE}/game`, {
  28. method: "PUT",
  29. headers: {
  30. "Content-Type": "application/json",
  31. },
  32. body: JSON.stringify({ timer, rounds, onlyAmerica, generationMethod }),
  33. });
  34. if (!res.ok) {
  35. throw Error(res.statusText);
  36. }
  37. const { gameId } = await res.json();
  38. return gameId;
  39. }
  40. export const getGameConfig = async (gameId) => {
  41. const res = await fetch(`${API_BASE}/game/${gameId}/config`);
  42. if (!res.ok) {
  43. throw Error(res.statusText);
  44. }
  45. return await res.json();
  46. }
  47. export const getGameCoords = async (gameId) => {
  48. const res = await fetch(`${API_BASE}/game/${gameId}/coords`);
  49. if (!res.ok) {
  50. throw Error(res.statusText);
  51. }
  52. return await res.json();
  53. }
  54. export const getPlayers = async (gameId) => {
  55. const res = await fetch(`${API_BASE}/game/${gameId}/players`);
  56. if (!res.ok) {
  57. throw Error(res.statusText);
  58. }
  59. const { players } = await res.json();
  60. return players;
  61. }
  62. export const getLinkedGame = async (gameId) => {
  63. const res = await fetch(`${API_BASE}/game/${gameId}/linked`);
  64. if (!res.ok) {
  65. throw Error(res.statusText);
  66. }
  67. const { linkedGame } = await res.json();
  68. return linkedGame;
  69. }
  70. export const linkGame = async (gameId, linkedGame) => {
  71. const res = await fetch(`${API_BASE}/game/${gameId}/linked`, {
  72. method: "POST",
  73. headers: {
  74. "Content-Type": "application/json",
  75. },
  76. body: JSON.stringify({ linkedGame }),
  77. });
  78. if (!res.ok) {
  79. throw Error(res.statusText);
  80. }
  81. }
  82. export const joinGame = async (gameId, playerName) => {
  83. const res = await fetch(`${API_BASE}/game/${gameId}/join`, {
  84. method: "POST",
  85. headers: {
  86. "Content-Type": "application/json",
  87. },
  88. body: JSON.stringify({ playerName }),
  89. });
  90. if (!res.ok) {
  91. throw Error(res.statusText);
  92. }
  93. return await res.json();
  94. }
  95. export const getCurrentRound = async (gameId, playerId) => {
  96. const res = await fetch(`${API_BASE}/game/${gameId}/current`, {
  97. headers: {
  98. "Authorization": `Player ${playerId}`
  99. },
  100. });
  101. if (!res.ok) {
  102. throw Error(res.statusText);
  103. }
  104. return await res.json();
  105. }
  106. export const sendGuess = async (gameId, playerId, round, point, timeRemaining) => {
  107. const res = await fetch(`${API_BASE}/game/${gameId}/guesses/${round}`, {
  108. method: "POST",
  109. headers: {
  110. "Authorization": `Player ${playerId}`,
  111. "Content-Type": "application/json",
  112. },
  113. body: JSON.stringify({ timeRemaining, ...point }),
  114. });
  115. if (!res.ok) {
  116. throw Error(res.statusText);
  117. }
  118. return await res.json();
  119. }