-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandleSwitchTrack.js
More file actions
69 lines (56 loc) · 2.15 KB
/
handleSwitchTrack.js
File metadata and controls
69 lines (56 loc) · 2.15 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { World, Visitor, errorHandler, getCredentials } from "../utils/index.js";
export const handleSwitchTrack = async (req, res) => {
try {
const credentials = getCredentials(req.query);
const { assetId, profileId, interactiveNonce, interactivePublicKey, urlSlug, visitorId, sceneDropId } = credentials;
const { trackSceneId } = req.query;
const world = await World.create(urlSlug, { credentials });
const visitor = await Visitor.get(visitorId, urlSlug, { credentials });
if (!visitor?.isAdmin) {
return res.status(403).json({
success: false,
msg: "The user is not an admin.",
});
}
const allRaceAssets = await world.fetchDroppedAssetsBySceneDropId({ sceneDropId });
const trackContainerAsset = allRaceAssets?.find((raceAsset) => {
return raceAsset.uniqueName === "race-track-container";
});
if (!trackContainerAsset || !trackContainerAsset?.position) {
return res.status(404).json({
msg: "Race Track Container asset not found. Please surround the race with a big rectangle object with race-track-container uniqueName",
});
}
let droppedAssetIds = [];
for (const raceAsset in allRaceAssets) {
droppedAssetIds.push(allRaceAssets?.[raceAsset]?.id);
}
await World.deleteDroppedAssets(urlSlug, droppedAssetIds, process.env.INTERACTIVE_SECRET, credentials);
await world.dropScene({
sceneId: trackSceneId,
position: trackContainerAsset?.position,
sceneDropId,
});
const numberOfCheckpoints = await world.fetchDroppedAssetsWithUniqueName({
uniqueName: "race-track-checkpoint",
isPartial: true,
});
await world.updateDataObject(
{
[`${sceneDropId}.numberOfCheckpoints`]: numberOfCheckpoints?.length,
[`${sceneDropId}.profiles`]: {},
},
{ analytics: [{ analyticName: "trackUpdates", profileId, uniqueKey: profileId }] },
);
await visitor.closeIframe(assetId);
return res.json({ success: true });
} catch (error) {
return errorHandler({
error,
functionName: "handleSwitchTrack",
message: "Error in switch track",
req,
res,
});
}
};