Skip to content

Commit 6b2703b

Browse files
committed
update highScore logic
1 parent 903d83f commit 6b2703b

File tree

7 files changed

+19
-15
lines changed

7 files changed

+19
-15
lines changed

client/src/components/Leaderboard/Leaderboard.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const Leaderboard = () => {
5353
</h2>
5454
<div className="card-primary grid gap-2">
5555
<h3>Personal Best</h3>
56-
<p>{highScore || "No highScore available"}</p>
56+
<p>{highScore || "No high score available"}</p>
5757
</div>
5858
<div className="card-outline">
5959
{leaderboard?.length > 0 ? (

client/src/context/reducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ const globalReducer = (state, action) => {
121121
return {
122122
...state,
123123
leaderboard: payload.leaderboard,
124+
highScore: payload.highScore,
124125
error: "",
125126
};
126127
case SET_ERROR:

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"dependencies": {
66
"@googleapis/sheets": "^7.0.0",
7-
"@rtsdk/topia": "^0.18.3",
7+
"@rtsdk/topia": "^0.19.3",
88
"axios": "^1.6.7",
99
"body-parser": "^1.20.2",
1010
"concurrently": "^8.2.2",

server/controllers/handleGetLeaderboard.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { errorHandler, formatLeaderboard, getCredentials, World } from "../utils
33
export const handleGetLeaderboard = async (req, res) => {
44
try {
55
const credentials = getCredentials(req.query);
6-
const { urlSlug, sceneDropId } = credentials;
6+
const { profileId, urlSlug, sceneDropId } = credentials;
77

88
const world = await World.create(urlSlug, { credentials });
99
await world.fetchDataObject();
1010
const sceneData = world.dataObject?.[sceneDropId];
1111

12-
const leaderboard = await formatLeaderboard(sceneData?.leaderboard || {});
12+
const { leaderboardArray, highScore } = await formatLeaderboard(sceneData.leaderboard, profileId);
1313

14-
return res.json({ success: true, leaderboard });
14+
return res.json({ success: true, leaderboard: leaderboardArray, highScore });
1515
} catch (error) {
1616
return errorHandler({
1717
error,

server/controllers/handleLoadGameState.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { TRACKS } from "../constants.js";
1111
export const handleLoadGameState = async (req, res) => {
1212
try {
1313
const credentials = getCredentials(req.query);
14-
const { urlSlug, sceneDropId } = credentials;
14+
const { profileId, urlSlug, sceneDropId } = credentials;
1515
const now = Date.now();
1616

1717
const world = await World.create(urlSlug, { credentials });
@@ -65,10 +65,10 @@ export const handleLoadGameState = async (req, res) => {
6565
);
6666
}
6767

68-
const { visitor, visitorProgress, visitorInventory } = await getVisitor(credentials, true);
69-
const { checkpoints, highScore, startTimestamp } = visitorProgress;
68+
const { leaderboardArray, highScore } = await formatLeaderboard(sceneData.leaderboard, profileId);
7069

71-
const leaderboardArray = await formatLeaderboard(sceneData.leaderboard);
70+
const { visitor, visitorProgress, visitorInventory } = await getVisitor(credentials, true);
71+
let { checkpoints, startTimestamp } = visitorProgress;
7272

7373
const { badges } = await getInventoryItems(credentials);
7474

server/utils/formatLeaderboard.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const formatLeaderboard = async (leaderboard) => {
1+
export const formatLeaderboard = async (leaderboard, profileId) => {
22
const leaderboardArray = [];
33
for (const profileId in leaderboard) {
44
const data = leaderboard[profileId];
@@ -19,5 +19,8 @@ export const formatLeaderboard = async (leaderboard) => {
1919
};
2020
leaderboardArray.sort((a, b) => timeToSeconds(a.highScore) - timeToSeconds(b.highScore)).slice(0, 20);
2121

22-
return leaderboardArray;
22+
let highScore;
23+
if (Object.keys(leaderboard).includes(profileId)) highScore = leaderboard[profileId].split("|")[1];
24+
25+
return { leaderboardArray, highScore };
2326
};

0 commit comments

Comments
 (0)