Skip to content

Commit

Permalink
Swap online players API
Browse files Browse the repository at this point in the history
  • Loading branch information
petrvecera committed Dec 12, 2024
1 parent e958d90 commit a6965d6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 93 deletions.
61 changes: 0 additions & 61 deletions __tests__/pages/api/onlineSteamPlayers.test.ts

This file was deleted.

19 changes: 14 additions & 5 deletions components/online-players.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Badge, Flex, Tooltip } from "@mantine/core";
import React, { useEffect, useState } from "react";
import { getNumberOfOnlinePlayersSteamUrl } from "../src/apis/steam-api";

export const OnlinePlayers: React.FC = () => {
const [onlinePlayersData, setOnlinePlayersData] = useState<null | {
Expand All @@ -15,8 +16,13 @@ export const OnlinePlayers: React.FC = () => {
onlinePlayersData.timeStampMs < new Date().getTime() - 1000 * 60 * 4) ||
!onlinePlayersData
) {
const fetchData = await fetch("/api/onlineSteamPlayers");
setOnlinePlayersData(await fetchData.json());
const fetchData = await fetch(getNumberOfOnlinePlayersSteamUrl());
// reader header last-modified:
const data = await fetchData.json();
setOnlinePlayersData({
playerCount: data.response.player_count,
timeStampMs: new Date(fetchData.headers.get("last-modified") || "").getTime(),
});
}

// Update the data every 5 minutes
Expand All @@ -28,10 +34,13 @@ export const OnlinePlayers: React.FC = () => {
onlinePlayersData.timeStampMs < new Date().getTime() - 1000 * 60 * 4) ||
!onlinePlayersData
) {
const fetchData = await fetch("/api/onlineSteamPlayers");
const fetchData = await fetch(getNumberOfOnlinePlayersSteamUrl());
const data = await fetchData.json();
if (data && data.playerCount > 0) {
setOnlinePlayersData(data);
if (data && data.player_count > 0) {
setOnlinePlayersData({
playerCount: data.response.player_count,
timeStampMs: new Date(fetchData.headers.get("last-modified") || "").getTime(),
});
}
}
} catch (e) {
Expand Down
26 changes: 0 additions & 26 deletions pages/api/onlineSteamPlayers.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/apis/steam-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { logger } from "../logger";

const baseUrl = "https://api.steampowered.com/";
const baseUrlProxied = "https://steam.coh3stats.com/";

const COH3_STEAM_APP_ID = 1677280;

const steamImagesBaseUrl = "https://clan.cloudflare.steamstatic.com/images";
Expand All @@ -21,7 +23,9 @@ export type COH3SteamNewsType = {
};

const getNumberOfOnlinePlayersSteamUrl = (appId: number | string = COH3_STEAM_APP_ID) => {
return encodeURI(`${baseUrl}ISteamUserStats/GetNumberOfCurrentPlayers/v1/?appid=${appId}`);
return encodeURI(
`${baseUrlProxied}ISteamUserStats/GetNumberOfCurrentPlayers/v1/?appid=${appId}`,
);
};

const getCOH3SteamNewsUrl = (count: number = 20) => {
Expand Down

0 comments on commit a6965d6

Please sign in to comment.