generated from metaversecloud-com/sdk-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandleSwitchTrack.js
More file actions
102 lines (86 loc) · 2.85 KB
/
handleSwitchTrack.js
File metadata and controls
102 lines (86 loc) · 2.85 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { DEFAULT_PROGRESS } from "../constants.js";
import {
DroppedAsset,
World,
errorHandler,
getCredentials,
getVisitor,
updateVisitorProgress,
} from "../utils/index.js";
export const handleSwitchTrack = async (req, res) => {
try {
const credentials = getCredentials(req.query);
const { assetId, profileId, urlSlug, sceneDropId } = credentials;
const { selectedTrack } = req.body;
const { sceneId, name } = selectedTrack;
const world = await World.create(urlSlug, { credentials });
const { visitor } = await getVisitor(credentials);
const allRaceAssets = await world.fetchDroppedAssetsBySceneDropId({ sceneDropId });
const trackContainerAsset = allRaceAssets?.find((raceAsset) => {
return raceAsset.uniqueName === "race-track-container";
});
let position = trackContainerAsset?.position;
if (!trackContainerAsset || !position) {
await world.fetchDataObject();
position = world.dataObject?.[sceneDropId]?.position;
if (!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) {
if (allRaceAssets?.[raceAsset]?.id !== assetId) {
droppedAssetIds.push(allRaceAssets?.[raceAsset]?.id);
}
}
await visitor.closeIframe(assetId).catch((error) =>
errorHandler({
error,
functionName: "handleSwitchTrack",
message: "Error closing iframe",
}),
);
await World.deleteDroppedAssets(urlSlug, droppedAssetIds, process.env.INTERACTIVE_SECRET, credentials);
await world.dropScene({
allowNonAdmins: true,
sceneId,
position,
sceneDropId,
});
const numberOfCheckpoints = await world.fetchDroppedAssetsWithUniqueName({
uniqueName: "race-track-checkpoint",
isPartial: true,
});
const sceneData = {
trackName: name,
numberOfCheckpoints: numberOfCheckpoints?.length,
leaderboard: {},
position,
};
await world.updateDataObject(
{
[sceneDropId]: sceneData,
},
{ analytics: [{ analyticName: "trackUpdates", profileId, uniqueKey: profileId }] },
);
const updateVisitorResult = await updateVisitorProgress({
credentials,
updatedProgress: DEFAULT_PROGRESS,
visitor,
});
if (updateVisitorResult instanceof Error) throw updateVisitorResult;
const droppedAsset = DroppedAsset.create(assetId, urlSlug, { credentials });
await droppedAsset.deleteDroppedAsset();
return res.json({ success: true, sceneData });
} catch (error) {
return errorHandler({
error,
functionName: "handleSwitchTrack",
message: "Error in switch track",
req,
res,
});
}
};