Skip to content

Commit 09e82c4

Browse files
committed
Load ccu from steam api
1 parent 7d3de3f commit 09e82c4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

scripts/store/app.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,13 +545,44 @@ function DrawLowestPrice()
545545
} );
546546
}
547547

548+
async function FetchSteamApiCurrentPlayers()
549+
{
550+
const params = new URLSearchParams();
551+
params.set( 'origin', location.origin );
552+
params.set( 'appid', GetCurrentAppID() );
553+
554+
const response = await fetch(
555+
`https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?${params.toString()}`,
556+
{
557+
headers: {
558+
Accept: 'application/json',
559+
}
560+
}
561+
);
562+
563+
if( !response.ok )
564+
{
565+
return 0;
566+
}
567+
568+
const data = await response.json();
569+
570+
if( data && data.response && data.response.player_count > 0 )
571+
{
572+
return data.response.player_count;
573+
}
574+
575+
return 0;
576+
}
577+
548578
function DrawOnlineStatsWidget( items )
549579
{
550580
let block = null;
551581
let onlineNow = null;
552582
let peakToday = null;
553583
let peakAll = null;
554584
let followers = null;
585+
let steamApiPlayersFetch = null;
555586

556587
if( items[ 'online-stats' ] )
557588
{
@@ -562,6 +593,8 @@ function DrawOnlineStatsWidget( items )
562593
return;
563594
}
564595

596+
steamApiPlayersFetch = FetchSteamApiCurrentPlayers();
597+
565598
const blockInner = document.createElement( 'div' );
566599
blockInner.className = 'block_content_inner';
567600

@@ -745,6 +778,28 @@ function DrawOnlineStatsWidget( items )
745778
followers.previousElementSibling.remove();
746779
followers.remove();
747780
}
781+
782+
steamApiPlayersFetch.then( ( livePlayers ) =>
783+
{
784+
if( livePlayers < 1 )
785+
{
786+
return;
787+
}
788+
789+
WriteLog( 'FetchSteamApiCurrentPlayers loaded' );
790+
791+
onlineNow.textContent = FormatNumber( livePlayers );
792+
793+
if( livePlayers > response.data.mdp )
794+
{
795+
peakToday.textContent = FormatNumber( livePlayers );
796+
}
797+
798+
if( livePlayers > response.data.mp )
799+
{
800+
peakAll.textContent = FormatNumber( livePlayers );
801+
}
802+
} );
748803
}
749804

750805
if( items[ 'steamdb-last-update' ] && response.data.u )

0 commit comments

Comments
 (0)