forked from babylordddd/CodingChallengeLeaderboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
143 lines (122 loc) · 4.37 KB
/
app.js
File metadata and controls
143 lines (122 loc) · 4.37 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//load env
require("dotenv").config();
const express = require("express");
const Redis = require("ioredis");
const app = express();
const PORT = process.env.PORT || 3000;
const CATEGORY_UPDATES_KEY = "leaderboard_updates";
const LATEST_SUBMISSION_PREFIX = "leaderboard_latest_submission:";
//connect to frontend
const cors = require("cors");
app.use(cors());
//Connect to Redis
const redis = new Redis({
host: process.env.REDIS_HOST || "127.0.0.1",
port: process.env.REDIS_PORT || 6379,
//password: process.env.REDIS_PASSWORD
})
app.use(express.json())
/**
* Helper function to generate leaderboard key
*/
function getLeaderboardKey(category) {
return `leaderboard:${category}`;
}
function getLatestSubmissionKey(category) {
return `${LATEST_SUBMISSION_PREFIX}${category}`;
}
/**
* @route GET /leaderboard/categories
* @description Get all available leaderboard categories
*/
app.get("/leaderboard/categories", async (req, res) => {
try {
const keys = await redis.keys("leaderboard:*");
const categories = keys.map(key => key.replace("leaderboard:", ""));
const recentlyUpdated = await redis.zrevrange(CATEGORY_UPDATES_KEY, 0, -1);
const orderedUpdatedCategories = recentlyUpdated.filter(category => categories.includes(category));
const remainingCategories = categories
.filter(category => !orderedUpdatedCategories.includes(category))
.sort();
const orderedCategories = [...orderedUpdatedCategories, ...remainingCategories];
res.json({
success: true,
data: orderedCategories
});
} catch (error) {
console.error("Categories Fetch Error:", error);
res.status(500).json({ message: "Internal server error" });
}
});
/**
* @route PUT /leaderboard/update
* @description update the leaderboard
*/
app.put("/leaderboard/update", async (req, res) => {
try {
const { player, timing, category } = req.body;
if (!player || !timing || !category) {
return res.status(400).json({ message: "Player, timing, and category required"})
}
const leaderboardKey = getLeaderboardKey(category);
const numericTiming = Number.parseFloat(timing);
await redis.zadd(leaderboardKey, numericTiming, player);
await redis.zadd(CATEGORY_UPDATES_KEY, Date.now(), category);
await redis.set(
getLatestSubmissionKey(category),
JSON.stringify({
player,
timing: numericTiming,
updatedAt: Date.now()
})
);
res.json({message: "Leaderboard updated", category});
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" })
}
})
/**
* @route GET /leaderboard/rank/:category
* @description Fetches the rank of leaderboard for a specific category
*/
app.get("/leaderboard/rank/:category", async (req, res) => {
try {
const { category } = req.params;
if (!category) {
return res.status(400).json({ message: "Category required" });
}
const leaderboardKey = getLeaderboardKey(category);
const latestSubmissionRaw = await redis.get(getLatestSubmissionKey(category));
const latestSubmission = latestSubmissionRaw ? JSON.parse(latestSubmissionRaw) : null;
const rank = await redis.zrange(leaderboardKey, 0, 9, "WITHSCORES")
const leaderboard = []
for (let i = 0; i < rank.length; i += 2) {
const player = rank[i];
const timing = rank[i + 1];
const numericTiming = Number.parseFloat(timing);
const isLatest = Boolean(
latestSubmission &&
latestSubmission.player === player &&
Math.abs(Number.parseFloat(latestSubmission.timing) - numericTiming) < 0.0000001
);
leaderboard.push({
player,
timing,
isLatest
})
}
res.json({
success : true,
category : category,
data : leaderboard,
latestSubmission
})
} catch (error) {
console.error(("Leaderboard Fetch Error:", error))
res.status(500).json({ message: "Internal server error" });
}
})
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}`)
});