apiMethods.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const API_BASE = "https://kirkleon.ddns.net/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 createGame = async (timer, rounds, onlyAmerica) => {
  14. const res = await fetch(`${API_BASE}/game`, {
  15. method: "PUT",
  16. headers: {
  17. "Content-Type": "application/json",
  18. },
  19. body: JSON.stringify({ timer, rounds, onlyAmerica }),
  20. });
  21. if (!res.ok) {
  22. throw Error(res.statusText);
  23. }
  24. const { gameId } = await res.json();
  25. return gameId;
  26. }
  27. export const gameInfo = async (gameId) => {
  28. const res = await fetch(`${API_BASE}/game/${gameId}`);
  29. if (!res.ok) {
  30. throw Error(res.statusText);
  31. }
  32. return await res.json();
  33. }
  34. export const linkGame = async (gameId, linkedGame) => {
  35. const res = await fetch(`${API_BASE}/game/${gameId}/linked`, {
  36. method: "POST",
  37. headers: {
  38. "Content-Type": "application/json",
  39. },
  40. body: JSON.stringify({ linkedGame }),
  41. });
  42. if (!res.ok) {
  43. throw Error(res.statusText);
  44. }
  45. }
  46. export const joinGame = async (gameId, playerName) => {
  47. const res = await fetch(`${API_BASE}/game/${gameId}/join`, {
  48. method: "POST",
  49. headers: {
  50. "Content-Type": "application/json",
  51. },
  52. body: JSON.stringify({ playerName }),
  53. });
  54. if (!res.ok) {
  55. throw Error(res.statusText);
  56. }
  57. return await res.json();
  58. }
  59. export const getCurrentRound = async (gameId, playerId) => {
  60. const res = await fetch(`${API_BASE}/game/${gameId}/current`, {
  61. headers: {
  62. "Authorization": `Player ${playerId}`
  63. },
  64. });
  65. if (!res.ok) {
  66. throw Error(res.statusText);
  67. }
  68. return await res.json();
  69. }
  70. export const sendGuess = async (gameId, playerId, round, point) => {
  71. const res = await fetch(`${API_BASE}/game/${gameId}/guesses/${round}`, {
  72. method: "POST",
  73. headers: {
  74. "Authorization": `Player ${playerId}`,
  75. "Content-Type": "application/json",
  76. },
  77. body: JSON.stringify(point),
  78. });
  79. if (!res.ok) {
  80. throw Error(res.statusText);
  81. }
  82. return await res.json();
  83. }
  84. export const checkScore = async (point1, point2) => {
  85. const res = await fetch(`${API_BASE}/score`, {
  86. method: "POST",
  87. headers: {
  88. "Content-Type": "application/json",
  89. },
  90. body: JSON.stringify({ point1, point2 }),
  91. });
  92. if (!res.ok) {
  93. throw Error(res.statusText);
  94. }
  95. return await res.json();
  96. }