|
1 | 1 | import { |
2 | 2 | getCredentials, |
3 | | - World, |
4 | 3 | errorHandler, |
5 | | - checkpointZeroEntered, |
6 | 4 | finishLineEntered, |
7 | 5 | checkpointEntered, |
8 | 6 | Visitor, |
| 7 | + getVisitor, |
| 8 | + getElapsedTime, |
9 | 9 | } from "../utils/index.js"; |
10 | 10 | import { CHECKPOINT_NAMES } from "../constants.js"; |
11 | 11 | import redisObj from "../redis/redis.js"; |
12 | 12 |
|
13 | 13 | export const handleCheckpointEntered = async (req, res) => { |
14 | 14 | try { |
15 | 15 | const credentials = getCredentials(req.body); |
16 | | - const { profileId, sceneDropId, urlSlug, visitorId } = credentials; |
| 16 | + const { profileId, urlSlug, visitorId } = credentials; |
17 | 17 | const { uniqueName } = req.body; |
18 | 18 | const channel = `${process.env.INTERACTIVE_KEY}_RACE`; |
19 | 19 |
|
| 20 | + const { visitorProgress } = await getVisitor(credentials); |
| 21 | + const { startTimestamp } = visitorProgress; |
| 22 | + |
| 23 | + const currentTimestamp = Date.now(); |
| 24 | + const currentElapsedTime = getElapsedTime(currentTimestamp, startTimestamp); |
20 | 25 | const checkpointNumber = uniqueName === CHECKPOINT_NAMES.START ? 0 : parseInt(uniqueName.split("-").pop(), 10); |
21 | 26 |
|
| 27 | + if (!startTimestamp) return { success: false, message: "Race has not started yet" }; |
| 28 | + |
| 29 | + const cachedCheckpoints = JSON.parse(await redisObj.get(profileId)) || {}; |
| 30 | + |
22 | 31 | if (checkpointNumber !== 0) { |
23 | | - const cachedCheckpoints = JSON.parse(await redisObj.get(profileId)) || {}; |
24 | 32 | if (checkpointNumber > 1 && !cachedCheckpoints[checkpointNumber - 2]) { |
25 | 33 | redisObj.publish(channel, { |
26 | 34 | profileId, |
@@ -52,25 +60,24 @@ export const handleCheckpointEntered = async (req, res) => { |
52 | 60 | } |
53 | 61 | } |
54 | 62 |
|
55 | | - const world = World.create(urlSlug, { credentials }); |
56 | | - const dataObject = await world.fetchDataObject(); |
57 | | - const raceObject = dataObject?.[sceneDropId] || {}; |
58 | | - const profileObject = raceObject?.profiles?.[profileId] || {}; |
59 | | - |
60 | | - const { startTimestamp } = profileObject; |
61 | | - if (!startTimestamp) return { success: false, message: "Race has not started yet" }; |
62 | | - const currentTimestamp = Date.now(); |
63 | | - |
64 | 63 | if (checkpointNumber === 0) { |
65 | | - const currentElapsedTime = checkpointZeroEntered(currentTimestamp, profileObject); |
66 | 64 | redisObj.publish(channel, { |
67 | 65 | profileId, |
68 | 66 | checkpointNumber, |
69 | 67 | currentRaceFinishedElapsedTime: currentElapsedTime, |
70 | 68 | }); |
71 | | - await finishLineEntered({ credentials, currentElapsedTime, profileObject, raceObject, world }); |
| 69 | + const result = await finishLineEntered({ credentials, currentElapsedTime }); |
| 70 | + console.log("🚀 ~ handleCheckpointEntered.js:70 ~ result:", result); |
| 71 | + if (result instanceof Error) throw result; |
72 | 72 | } else { |
73 | | - await checkpointEntered({ checkpointNumber, currentTimestamp, credentials, profileObject, world }); |
| 73 | + const result = await checkpointEntered({ |
| 74 | + checkpoints: cachedCheckpoints, |
| 75 | + checkpointNumber, |
| 76 | + currentTimestamp, |
| 77 | + credentials, |
| 78 | + }); |
| 79 | + console.log("🚀 ~ handleCheckpointEntered.js:79 ~ result:", result); |
| 80 | + if (result instanceof Error) throw result; |
74 | 81 | } |
75 | 82 |
|
76 | 83 | return res.status(200).json({ success: true }); |
|
0 commit comments