some more styling to the frontend
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { query } from '@/lib/db';
|
||||
import { attachAvatars } from '@/lib/steamApi';
|
||||
|
||||
interface PlayerRow {
|
||||
steamid: string;
|
||||
@@ -7,6 +8,7 @@ interface PlayerRow {
|
||||
current_country: string | null;
|
||||
matched_name: string | null;
|
||||
total_minutes: number | null;
|
||||
last_connected: string | null;
|
||||
}
|
||||
|
||||
const TOTAL_MINUTES_SUBQUERY = `
|
||||
@@ -17,6 +19,12 @@ const TOTAL_MINUTES_SUBQUERY = `
|
||||
LIMIT 1) AS total_minutes
|
||||
`;
|
||||
|
||||
const LAST_CONNECTED_SUBQUERY = `
|
||||
(SELECT MAX(s.session_end_dt)
|
||||
FROM playtime_display_sessions s
|
||||
WHERE s.steamid = p.steamid) AS last_connected
|
||||
`;
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const q = req.nextUrl.searchParams.get('q')?.trim() ?? '';
|
||||
const sort = req.nextUrl.searchParams.get('sort') ?? 'recent';
|
||||
@@ -28,37 +36,41 @@ export async function GET(req: NextRequest) {
|
||||
? 'p.current_name'
|
||||
: 'p.last_seen DESC';
|
||||
|
||||
// No search term: just list players, ordered per the chosen sort.
|
||||
let rows: PlayerRow[];
|
||||
|
||||
if (!q) {
|
||||
const rows = await query<PlayerRow>(
|
||||
// No search term: just list players, ordered per the chosen sort.
|
||||
rows = await query<PlayerRow>(
|
||||
`SELECT p.steamid, p.current_name, p.current_country, NULL AS matched_name,
|
||||
${TOTAL_MINUTES_SUBQUERY}
|
||||
${TOTAL_MINUTES_SUBQUERY},
|
||||
${LAST_CONNECTED_SUBQUERY}
|
||||
FROM playtime_display_players p
|
||||
ORDER BY ${orderBy}
|
||||
LIMIT 50`,
|
||||
);
|
||||
return NextResponse.json({ players: rows });
|
||||
} else {
|
||||
// Search matches steamid directly, or current/previous names.
|
||||
const like = `%${q}%`;
|
||||
rows = await query<PlayerRow>(
|
||||
`SELECT p.steamid, p.current_name, p.current_country, h.player_name AS matched_name,
|
||||
${TOTAL_MINUTES_SUBQUERY},
|
||||
${LAST_CONNECTED_SUBQUERY}
|
||||
FROM playtime_display_players p
|
||||
LEFT JOIN playtime_display_name_history h
|
||||
ON h.steamid = p.steamid AND h.player_name LIKE ?
|
||||
WHERE p.steamid LIKE ?
|
||||
OR p.current_name LIKE ?
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM playtime_display_name_history h2
|
||||
WHERE h2.steamid = p.steamid AND h2.player_name LIKE ?
|
||||
)
|
||||
GROUP BY p.steamid
|
||||
ORDER BY ${orderBy}
|
||||
LIMIT 50`,
|
||||
[like, like, like, like],
|
||||
);
|
||||
}
|
||||
|
||||
// Search matches steamid directly, or current/previous names.
|
||||
const like = `%${q}%`;
|
||||
const rows = await query<PlayerRow>(
|
||||
`SELECT p.steamid, p.current_name, p.current_country, h.player_name AS matched_name,
|
||||
${TOTAL_MINUTES_SUBQUERY}
|
||||
FROM playtime_display_players p
|
||||
LEFT JOIN playtime_display_name_history h
|
||||
ON h.steamid = p.steamid AND h.player_name LIKE ?
|
||||
WHERE p.steamid LIKE ?
|
||||
OR p.current_name LIKE ?
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM playtime_display_name_history h2
|
||||
WHERE h2.steamid = p.steamid AND h2.player_name LIKE ?
|
||||
)
|
||||
GROUP BY p.steamid
|
||||
ORDER BY ${orderBy}
|
||||
LIMIT 50`,
|
||||
[like, like, like, like],
|
||||
);
|
||||
|
||||
return NextResponse.json({ players: rows });
|
||||
const players = await attachAvatars(rows);
|
||||
return NextResponse.json({ players });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user