-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinishLineEntered.js
More file actions
46 lines (38 loc) · 1.61 KB
/
finishLineEntered.js
File metadata and controls
46 lines (38 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { WorldActivityType } from "@rtsdk/topia";
import { Visitor } from "../topiaInit.js";
import { timeToValue } from "../utils.js";
export const finishLineEntered = async ({ credentials, currentElapsedTime, profileObject, raceObject, world }) => {
try {
const { profileId, sceneDropId, urlSlug, visitorId } = credentials;
const { checkpoints, highscore } = profileObject;
const allCheckpointsCompleted = raceObject.numberOfCheckpoints === Object.keys(checkpoints).length;
if (!allCheckpointsCompleted) return;
const newHighscore =
!highscore || timeToValue(currentElapsedTime) < timeToValue(highscore) ? currentElapsedTime : highscore;
await world.updateDataObject(
{
[`${sceneDropId}.profiles.${profileId}.checkpoints`]: {},
[`${sceneDropId}.profiles.${profileId}.elapsedTime`]: currentElapsedTime,
[`${sceneDropId}.profiles.${profileId}.startTimestamp`]: null,
[`${sceneDropId}.profiles.${profileId}.highscore`]: newHighscore,
},
{ analytics: [{ analyticName: "completions", uniqueKey: profileId }] },
);
if (newHighscore !== highscore) world.triggerActivity({ type: WorldActivityType.GAME_HIGH_SCORE, assetId });
const visitor = await Visitor.get(visitorId, urlSlug, { credentials });
const { x, y } = visitor.moveTo;
visitor.fireToast({
groupId: "race",
title: "🏁 Finish",
text: `You finished the race! Your time: ${currentElapsedTime}`,
});
visitor.triggerParticle({
name: "trophy_float",
duration: 3,
position: { x, y },
});
return;
} catch (error) {
return error;
}
};