Skip to content

Commit

Permalink
Sum spectators and players, fetch servers more frequently
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Jul 31, 2024
1 parent a036fc9 commit 53b77ab
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ app.use((req, res, next) => {

// Timers
servers.fetchServers(app);
setInterval(() => servers.fetchServers(app), 5000); // 5s
setInterval(() => servers.fetchServers(app), 3000);

// WebSocket
const wsInstance = expressWs(app);
Expand Down
19 changes: 10 additions & 9 deletions src/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ let servers = [];
function fetchServers(app) {
axios.get(process.env.SERVER_LIST_JSON)
.then(response => {
servers = response.data;
let totalServers = 0;
let totalPlayers = 0;
servers.forEach(sv => {
totalServers++;
totalPlayers += sv.num_playing + sv.num_spectating;
});
servers = response.data.map(server => ({
...server,
num_online: server.num_playing + server.num_spectating
}));

let totalServers = servers.length;
let totalPlayers = servers.reduce((acc, sv) => acc + sv.num_online, 0);

app.locals.players_ingame = totalPlayers;
app.locals.online_servers = totalServers;
})
Expand All @@ -24,8 +25,8 @@ function fetchServers(app) {

router.get('/', function (req, res) {
servers.sort((a, b) => {
if (a.num_playing !== b.num_playing) {
return b.num_playing - a.num_playing;
if (a.num_online !== b.num_online) {
return b.num_online - a.num_online;
} else {
return a.name.localeCompare(b.name);
}
Expand Down
2 changes: 1 addition & 1 deletion views/server.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</tr>
</thead>
<tbody>
<% if (sv.num_playing == 0 && sv.num_spectating == 0) { %>
<% if (sv.num_online == 0) { %>
<tr>
<td colspan="3">Server is empty</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion views/servers.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<% } %>
<a class="white" href="/servers/<%= v.site_displayed_address %>"><%= v.name %></a>
</td>
<td class="<%= v.num_playing === 0 ? 'zeroplayers' : '' %>" data-sort="<%= v.num_playing %>"><%= v.num_playing %>/<%= v.slots %></td>
<td class="<%= v.num_online === 0 ? 'zeroplayers' : '' %>" data-sort="<%= v.num_online %>"><%= v.num_online %>/<%= v.slots %></td>
<td><%= v.arena %></td>
<td><%= v.game_mode %></td>
<td><a href="steam://run/2660970//<%= v.site_displayed_address %>/"><i class="fa-brands fa-steam"></i><%= v.site_displayed_address %></a></td>
Expand Down

0 comments on commit 53b77ab

Please sign in to comment.