-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for updating previous leaderboard submissions; UUID is …
…generated and stored in localStorage, and then tied with the player's leaderboard entries server-side
- Loading branch information
1 parent
d838046
commit 1ca48f7
Showing
6 changed files
with
203 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
frontend/app/hooks/api_access/leaderboards/useGetPastPlayerLeaderboardEntry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { EXTERNAL_API_ROOT } from "@/app/constants"; | ||
import { TimeAttackEntry, SurvivalEntry } from "@/app/interfaces"; | ||
|
||
import useAuthFetch from "../../security/useAuthFetch"; | ||
|
||
export default function useGetPastPlayerLeaderboardEntry() { | ||
|
||
const authFetch = useAuthFetch(); | ||
|
||
const getPastPlayerLeaderboardEntry = async ( | ||
quizId: number, | ||
playerUUID: string, | ||
shareableToken?: string | undefined | ||
): Promise<TimeAttackEntry | SurvivalEntry | null> => { | ||
|
||
const requestUrl: string = `${EXTERNAL_API_ROOT}/quizzes/${quizId}/leaderboard/${playerUUID}`; | ||
|
||
try { | ||
const response: Response = await authFetch(requestUrl, {}, shareableToken); | ||
if (!response.ok) { | ||
if (response.status === 404) { | ||
console.log("No previous leaderboard entry found for player."); | ||
} else if (response.status >= 400 && response.status < 500) { | ||
console.error(`Client request rejected: ${response.status}`); | ||
} else if (response.status >= 500) { | ||
console.error(`Server failed request: ${response.status}`); | ||
} | ||
return null; | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
} | ||
|
||
return getPastPlayerLeaderboardEntry; | ||
} |
47 changes: 47 additions & 0 deletions
47
frontend/app/hooks/api_access/leaderboards/usePatchLeaderboardEntry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { EXTERNAL_API_ROOT } from "@/app/constants"; | ||
import { PostTimeAttackEntry, PostSurvivalEntry } from "@/app/interfaces"; | ||
|
||
import useAuthFetch from "../../security/useAuthFetch"; | ||
|
||
export default function usePatchLeaderboardEntry() { | ||
|
||
const authFetch = useAuthFetch(); | ||
|
||
const patchLeaderboardEntry = async ( | ||
quizId: number, | ||
entryId: number, | ||
request: PostTimeAttackEntry | PostSurvivalEntry, | ||
shareableToken?: string | undefined | ||
): Promise<string | null> => { | ||
|
||
// We determine the request type by checking for a unique property | ||
const requestUrl: string = 'score' in request | ||
? `${EXTERNAL_API_ROOT}/quizzes/${quizId}/leaderboard/${entryId}/time-attack` | ||
: `${EXTERNAL_API_ROOT}/quizzes/${quizId}/leaderboard/${entryId}/survival`; | ||
|
||
try { | ||
const response: Response = await authFetch(requestUrl, { | ||
method: "PATCH", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify(request) | ||
}, shareableToken); | ||
if (!response.ok) { | ||
if (response.status >= 400 && response.status < 500) { | ||
console.error(`Client request rejected: ${response.status}`); | ||
return "Client request rejected"; | ||
} else if (response.status >= 500) { | ||
console.error(`Server failed request: ${response.status}`); | ||
return "Server failed to process request"; | ||
} | ||
} | ||
return null; | ||
} catch (error) { | ||
console.error(error); | ||
return "Client failed to process request"; | ||
} | ||
} | ||
|
||
return patchLeaderboardEntry; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters