Skip to content

Commit 53e87e1

Browse files
authored
Merge pull request #142 from cephie-studios/canary
fix: public ranks & crowns
2 parents b18815f + 6564609 commit 53e87e1

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/routes/data.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { fileURLToPath } from 'url';
55
import { getTesterSettings } from '../db/testers.js';
66
import { getActiveNotifications } from '../db/notifications.js';
77
import { mainDb, flightsDb, redisConnection } from '../db/connection.js';
8-
import { getTopUsers, STATS_KEYS } from '../db/leaderboard.js';
8+
import { getTopUsers, STATS_KEYS, getUserRank } from '../db/leaderboard.js';
99
import { getUserById } from '../db/users.js';
1010
import { getWaypointData } from '../utils/getData.js';
1111
import { findPath } from '../utils/findRoute.js';
@@ -555,6 +555,32 @@ router.get('/leaderboard', async (req, res) => {
555555
}
556556
});
557557

558+
// GET: /api/data/ranks/:userId - Fetch leaderboard ranks for a specific user
559+
router.get('/ranks/:userId', async (req, res) => {
560+
try {
561+
const { userId } = req.params;
562+
563+
if (!userId) {
564+
return res.status(400).json({ error: 'Missing userId parameter' });
565+
}
566+
567+
const user = await getUserById(userId);
568+
if (!user) {
569+
return res.status(404).json({ error: 'User not found' });
570+
}
571+
572+
const ranks: Record<string, number | null> = {};
573+
for (const key of STATS_KEYS) {
574+
ranks[key] = await getUserRank(userId, key);
575+
}
576+
577+
res.json(ranks);
578+
} catch (error) {
579+
console.error('Error fetching user ranks:', error);
580+
res.status(500).json({ error: 'Failed to fetch user ranks' });
581+
}
582+
});
583+
558584
// GET: /api/data/tester-settings - get tester gate settings
559585
router.get('/tester-settings', async (req, res) => {
560586
try {

src/pages/Flights.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,6 @@ export default function Flights() {
301301
setFlights((prev) => prev.filter((flight) => flight.id !== flightId));
302302
};
303303

304-
const handleFlightError = (error: string) => {
305-
console.error('Flight websocket error:', error);
306-
};
307-
308304
const socket = createFlightsSocket(
309305
sessionId,
310306
accessId,
@@ -313,7 +309,9 @@ export default function Flights() {
313309
handleFlightUpdate,
314310
handleFlightAdded,
315311
handleFlightDeleted,
316-
handleFlightError
312+
(error: { action: string; flightId?: string | number; error: string }) => {
313+
console.error('Flight websocket error:', error);
314+
}
317315
);
318316
socket.socket.on('sessionUpdated', (updates) => {
319317
setSession((prev) => (prev ? { ...prev, ...updates } : null));

src/pages/PilotProfile.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { SiRoblox } from 'react-icons/si';
3838
import { fetchPilotProfile } from '../utils/fetch/pilot';
3939
import { getCurrentUser } from '../utils/fetch/auth';
4040
import { useAuth } from '../hooks/auth/useAuth';
41-
import { fetchBackgrounds } from '../utils/fetch/data';
41+
import { fetchBackgrounds, fetchUserRanks } from '../utils/fetch/data';
4242
import type { PilotProfile, Role } from '../types/pilot';
4343
import Button from '../components/common/Button';
4444
import Loader from '../components/common/Loader';
@@ -146,8 +146,13 @@ export default function PilotProfile() {
146146
const data = await fetchPilotProfile(username!);
147147
if (data) {
148148
setProfile(data);
149-
if (!isCurrentUser) {
150-
setUserStats(data.user.statistics || {});
149+
setUserStats(data.user.statistics || {});
150+
151+
try {
152+
const profileRanks = await fetchUserRanks(data.user.id);
153+
setRanks(profileRanks || {});
154+
} catch {
155+
// ignore
151156
}
152157
} else {
153158
setError('Pilot not found');
@@ -163,7 +168,6 @@ export default function PilotProfile() {
163168
try {
164169
const userData = await getCurrentUser();
165170
setUserStats(userData.statistics || {});
166-
setRanks(userData.ranks || {});
167171
} catch {
168172
// ignore
169173
}
@@ -323,7 +327,7 @@ export default function PilotProfile() {
323327
profile.user.vatsim_rating_long
324328
);
325329

326-
const hasCrown = isCurrentUser && isRankOne(ranks);
330+
const hasCrown = isRankOne(ranks);
327331

328332
return (
329333
<div className="min-h-screen bg-zinc-950 text-white">

src/utils/fetch/data.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ export async function fetchActiveNotifications(): Promise<AdminNotification[]> {
105105
return await response.json();
106106
}
107107

108+
export async function fetchUserRanks(
109+
userId: string
110+
): Promise<Record<string, number | null>> {
111+
const response = await fetch(
112+
`${import.meta.env.VITE_SERVER_URL}/api/data/ranks/${userId}`
113+
);
114+
if (!response.ok) {
115+
throw new Error('Failed to fetch user ranks');
116+
}
117+
return response.json();
118+
}
119+
108120
export async function fetchRoute(from: string, to: string): Promise<{
109121
path: Array<{ name: string; x: number; y: number; type: string }>;
110122
distance: number;

0 commit comments

Comments
 (0)