Skip to content

Commit 4a0b444

Browse files
committed
clean up
1 parent b39ae95 commit 4a0b444

File tree

3 files changed

+114
-82
lines changed

3 files changed

+114
-82
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Ecosystem } from "../topiaInit.js";
22

3-
export const grantBadge = async ({ credentials, visitor, visitorInventory, badgeName }) => {
3+
export const awardBadge = async ({ credentials, visitor, visitorInventory, badgeName }) => {
44
try {
55
if (visitorInventory[badgeName]) return { success: true };
66

server/utils/badges/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from "./getTimeInSeconds.js";
2-
export * from "./grantBadge.js";
2+
export * from "./awardBadge.js";
33
export * from "./isNewHighScoreTop3.js";

server/utils/checkpoints/finishLineEntered.js

Lines changed: 112 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
World,
44
errorHandler,
55
getVisitor,
6-
grantBadge,
6+
awardBadge,
77
isNewHighScoreTop3,
88
timeToValue,
99
updateVisitorProgress,
@@ -13,6 +13,8 @@ export const finishLineEntered = async ({ credentials, currentElapsedTime, wasWr
1313
try {
1414
const { assetId, displayName, profileId, sceneDropId, urlSlug } = credentials;
1515

16+
const promises = [];
17+
1618
const world = World.create(urlSlug, { credentials });
1719
await world.fetchDataObject();
1820
const raceObject = world.dataObject?.[sceneDropId] || {};
@@ -27,12 +29,14 @@ export const finishLineEntered = async ({ credentials, currentElapsedTime, wasWr
2729
!highScore || timeToValue(currentElapsedTime) < timeToValue(highScore) ? currentElapsedTime : highScore;
2830

2931
if (newHighScore !== highScore) {
30-
world.triggerActivity({ type: WorldActivityType.GAME_HIGH_SCORE, assetId }).catch((error) =>
31-
errorHandler({
32-
error,
33-
functionName: "finishLineEntered",
34-
message: "Error triggering world activity",
35-
}),
32+
promises.push(
33+
world.triggerActivity({ type: WorldActivityType.GAME_HIGH_SCORE, assetId }).catch((error) =>
34+
errorHandler({
35+
error,
36+
functionName: "finishLineEntered",
37+
message: "Error triggering world activity",
38+
}),
39+
),
3640
);
3741
}
3842

@@ -55,102 +59,130 @@ export const finishLineEntered = async ({ credentials, currentElapsedTime, wasWr
5559
});
5660
if (updateVisitorResult instanceof Error) throw updateVisitorResult;
5761

58-
visitor
59-
.fireToast({
60-
groupId: "race",
61-
title: "🏁 Finish",
62-
text: `You finished the race! Your time: ${currentElapsedTime}`,
63-
})
64-
.catch((error) =>
65-
errorHandler({
66-
error,
67-
functionName: "finishLineEntered",
68-
message: "Error firing toast",
69-
}),
70-
);
71-
72-
visitor
73-
.triggerParticle({
74-
name: "trophy_float",
75-
duration: 3,
76-
})
77-
.catch((error) =>
78-
errorHandler({
79-
error,
80-
functionName: "finishLineEntered",
81-
message: "Error triggering particle effects",
82-
}),
83-
);
84-
85-
// Grant Race Rookie badge if this is the visitor's first high score
62+
promises.push(
63+
visitor
64+
.fireToast({
65+
groupId: "race",
66+
title: "🏁 Finish",
67+
text: `You finished the race! Your time: ${currentElapsedTime}`,
68+
})
69+
.catch((error) =>
70+
errorHandler({
71+
error,
72+
functionName: "finishLineEntered",
73+
message: "Error firing toast",
74+
}),
75+
),
76+
);
77+
78+
promises.push(
79+
visitor
80+
.triggerParticle({
81+
name: "trophy_float",
82+
duration: 3,
83+
})
84+
.catch((error) =>
85+
errorHandler({
86+
error,
87+
functionName: "finishLineEntered",
88+
message: "Error triggering particle effects",
89+
}),
90+
),
91+
);
92+
93+
// Award Race Rookie badge if this is the visitor's first high score
8694
if (!visitorProgress.highScore) {
87-
grantBadge({ credentials, visitor, visitorInventory, badgeName: "Race Rookie" }).catch((error) =>
88-
errorHandler({
89-
error,
90-
functionName: "finishLineEntered",
91-
message: "Error granting Race Rookie badge",
92-
}),
95+
promises.push(
96+
awardBadge({ credentials, visitor, visitorInventory, badgeName: "Race Rookie" }).catch((error) =>
97+
errorHandler({
98+
error,
99+
functionName: "finishLineEntered",
100+
message: "Error awarding Race Rookie badge",
101+
}),
102+
),
93103
);
94104
}
95105

96-
// Grant Top 3 Racer badge if newHighScore is in top 3 of leaderboard
106+
// Award Top 3 Racer badge if newHighScore is in top 3 of leaderboard
97107
const shouldGetTop3Badge = await isNewHighScoreTop3(raceObject.leaderboard, newHighScore);
98108
if (shouldGetTop3Badge) {
99-
grantBadge({ credentials, visitor, visitorInventory, badgeName: "Top 3 Racer" }).catch((error) =>
100-
errorHandler({
101-
error,
102-
functionName: "finishLineEntered",
103-
message: "Error granting Top 3 Racer badge",
104-
}),
109+
promises.push(
110+
awardBadge({ credentials, visitor, visitorInventory, badgeName: "Top 3 Racer" }).catch((error) =>
111+
errorHandler({
112+
error,
113+
functionName: "finishLineEntered",
114+
message: "Error awarding Top 3 Racer badge",
115+
}),
116+
),
105117
);
106118
}
107119

108-
// Grant Speed Demon badge if newHighScore is less than 30 seconds
120+
// Award Speed Demon badge if newHighScore is less than 30 seconds or Slow & Steady badge if more than 2 minutes
109121
const [min, sec, mili] = currentElapsedTime.split(":").map(Number);
110122
const totalSeconds = min * 60 + sec + mili / 100;
111123
if (totalSeconds < 30) {
112-
grantBadge({ credentials, visitor, visitorInventory, badgeName: "Speed Demon" }).catch((error) =>
113-
errorHandler({
114-
error,
115-
functionName: "finishLineEntered",
116-
message: "Error granting Speed Demon badge",
117-
}),
124+
promises.push(
125+
awardBadge({ credentials, visitor, visitorInventory, badgeName: "Speed Demon" }).catch((error) =>
126+
errorHandler({
127+
error,
128+
functionName: "finishLineEntered",
129+
message: "Error awarding Speed Demon badge",
130+
}),
131+
),
118132
);
119-
}
120-
121-
// Grant Race Pro badge if visitor has completed 100 races
122-
if (visitor.dataObject.racesCompleted + 1 >= 100) {
123-
grantBadge({ credentials, visitor, visitorInventory, badgeName: "Race Pro" }).catch((error) =>
124-
errorHandler({
125-
error,
126-
functionName: "finishLineEntered",
127-
message: "Error granting Race Pro badge",
128-
}),
133+
} else if (totalSeconds > 120) {
134+
promises.push(
135+
awardBadge({ credentials, visitor, visitorInventory, badgeName: "Slow & Steady" }).catch((error) =>
136+
errorHandler({
137+
error,
138+
functionName: "finishLineEntered",
139+
message: "Error awarding Slow & Steady badge",
140+
}),
141+
),
129142
);
130143
}
131144

132-
// Grant Race Expert badge if visitor has completed 1000 races
133-
if (visitor.dataObject.racesCompleted + 1 >= 1000) {
134-
grantBadge({ credentials, visitor, visitorInventory, badgeName: "Race Expert" }).catch((error) =>
135-
errorHandler({
136-
error,
137-
functionName: "finishLineEntered",
138-
message: "Error granting Race Expert badge",
139-
}),
145+
// Award Race Pro badge if visitor has completed 100 races or Race Expert badge if visitor has completed 1000 races
146+
if (visitor.dataObject.racesCompleted + 1 === 100) {
147+
promises.push(
148+
awardBadge({ credentials, visitor, visitorInventory, badgeName: "Race Pro" }).catch((error) =>
149+
errorHandler({
150+
error,
151+
functionName: "finishLineEntered",
152+
message: "Error awarding Race Pro badge",
153+
}),
154+
),
155+
);
156+
} else if (visitor.dataObject.racesCompleted + 1 === 1000) {
157+
promises.push(
158+
awardBadge({ credentials, visitor, visitorInventory, badgeName: "Race Expert" }).catch((error) =>
159+
errorHandler({
160+
error,
161+
functionName: "finishLineEntered",
162+
message: "Error awarding Race Expert badge",
163+
}),
164+
),
140165
);
141166
}
142167

143-
// Grant Never Give Up badge if visitor completed the race after previously entering a wrong checkpoint
168+
// Award Never Give Up badge if visitor completed the race after previously entering a wrong checkpoint
144169
if (wasWrongCheckpointEntered) {
145-
grantBadge({ credentials, visitor, visitorInventory, badgeName: "Never Give Up" }).catch((error) =>
146-
errorHandler({
147-
error,
148-
functionName: "finishLineEntered",
149-
message: "Error granting Never Give Up badge",
150-
}),
170+
promises.push(
171+
awardBadge({ credentials, visitor, visitorInventory, badgeName: "Never Give Up" }).catch((error) =>
172+
errorHandler({
173+
error,
174+
functionName: "finishLineEntered",
175+
message: "Error awarding Never Give Up badge",
176+
}),
177+
),
151178
);
152179
}
153180

181+
const results = await Promise.allSettled(promises);
182+
results.forEach((result) => {
183+
if (result.status === "rejected") console.error(result.reason);
184+
});
185+
154186
return;
155187
} catch (error) {
156188
return new Error(error);

0 commit comments

Comments
 (0)