apiMethods.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const API_BASE = process.env.REACT_APP_API_BASE;
  2. export const getStatus = async () => {
  3. try {
  4. const res = await fetch(`${API_BASE}/health`);
  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, ruleSet) => {
  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, ruleSet }),
  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: "PUT",
  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 getFirstSubmitter = async (gameId, round) => {
  83. const res = await fetch(`${API_BASE}/game/${gameId}/round/${round}/first`);
  84. if (!res.ok) {
  85. return null; // API is that 404 means no submitter yet (or wrong game)
  86. }
  87. const { first } = await res.json();
  88. return first;
  89. }
  90. export const joinGame = async (gameId, playerName) => {
  91. const res = await fetch(`${API_BASE}/game/${gameId}/join`, {
  92. method: "POST",
  93. headers: {
  94. "Content-Type": "application/json",
  95. },
  96. body: JSON.stringify({ playerName }),
  97. });
  98. if (!res.ok) {
  99. throw Error(res.statusText);
  100. }
  101. return await res.json();
  102. }
  103. export const getCurrentRound = async (gameId, playerId) => {
  104. const res = await fetch(`${API_BASE}/game/${gameId}/players/${playerId}/current`);
  105. if (!res.ok) {
  106. throw Error(res.statusText);
  107. }
  108. return await res.json();
  109. }
  110. export const sendGuess = async (gameId, playerId, round, point, timeRemaining) => {
  111. const res = await fetch(`${API_BASE}/game/${gameId}/round/${round}/guess/${playerId}`, {
  112. method: "POST",
  113. headers: {
  114. "Content-Type": "application/json",
  115. },
  116. body: JSON.stringify({ timeRemaining, ...point }),
  117. });
  118. if (!res.ok) {
  119. throw Error(res.statusText);
  120. }
  121. return await res.json();
  122. }
  123. export const sendTimeout = async (gameId, playerId, round) => {
  124. const res = await fetch(`${API_BASE}/game/${gameId}/round/${round}/timeout/${playerId}`, { method: "POST" });
  125. if (!res.ok) {
  126. throw Error(res.statusText);
  127. }
  128. return await res.json();
  129. }