Skip to content

Commit

Permalink
Reduce cpu load
Browse files Browse the repository at this point in the history
Fixes #214
  • Loading branch information
Konstantin Kalinovsky committed Dec 29, 2019
1 parent deb8ed7 commit b68bdf0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
52 changes: 27 additions & 25 deletions public/js/app/counter.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
const delay = 1000;

setInterval(updatePlayerCounter, delay);
const delay = refreshCountersSeconds * 1000;

function updateGameCounter() {
$.get("lobby_api", {resource: "games"}, function (gameCount) {
if ($("#game_counter").text() === gameCount.toString()) {
return;
}

setTimeout(function(){
setInterval(updateGameCounter, delay);
}, delay/2);

function updateGameCounter(){
$.get('lobby_api', { resource: "games"}, function (body) {
let games = body;

if ($("#game_counter").text() == games.length.toString()) return;

$("#game_counter").fadeOut('fast', function() {
$(this).text(games.length).fadeIn('fast');
})
$("#game_counter").fadeOut("fast", function() {
$(this).text(gameCount).fadeIn("fast");
});
});
}

function updatePlayerCounter(){
$.get('lobby_api', { resource: "players"}, function (body) {
let players = body;
if ($("#player_counter").text() == players.length.toString()) return;
$("#player_counter").fadeOut('fast', function() {
$(this).text(players.length).fadeIn('fast');
})
function updatePlayerCounter() {
$.get("lobby_api", {resource: "players"}, function (playerCount) {
if ($("#player_counter").text() === playerCount.toString()) {
return;
}

$("#player_counter").fadeOut("fast", function() {
$(this).text(playerCount).fadeIn("fast");
});
});
}

setInterval(updatePlayerCounter, delay);

setTimeout(function() {
setInterval(updateGameCounter, delay);
}, delay / 2);

updateGameCounter();
updatePlayerCounter();

const waitBeforeShowing = setInterval(
function(){
if ($("#player_counter").text() != '0' && $("#game_counter").text() != '0'){
function() {
if ($("#player_counter").text() !== "0" && $("#game_counter").text() !== "0") {
$(".counters").css("opacity", 1);
clearInterval(waitBeforeShowing);
}
Expand Down
18 changes: 10 additions & 8 deletions routes/lobby_api.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
let cache = {};
const request = require("request");
const cache = {};
let isFetching = false;

const PLAYER_COUNT_UPDATE_INTERVAL = parseInt(process.env.PLAYER_COUNT_UPDATE_INTERVAL) * 1000;

exports = module.exports = function(req, res) {
const resource = req.query.resource;
if (cache[resource]){
if (isFetching || Date.now() - cache[resource].pollTime < parseInt(process.env.PLAYER_COUNT_UPDATE_INTERVAL) * 1000){
return res.send(cache[resource].data);
if (cache[resource]) {
if (isFetching || Date.now() - cache[resource].pollTime < PLAYER_COUNT_UPDATE_INTERVAL) {
return res.send(cache[resource].count.toString());
}
}
isFetching = true;
const request = require('request');
request(process.env.LOBBY_API_URL + '/' + resource, function (error, response, body) {
request(process.env.LOBBY_API_URL + "/" + resource, function (error, response, body) {
const data = JSON.parse(body);
cache[resource] = {
pollTime: Date.now(),
data: data
count: data.length
};
isFetching = false;
return res.send(data);
return res.send(cache[resource].count.toString());
});
};
18 changes: 8 additions & 10 deletions routes/views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@ exports = module.exports = function(req, res) {
locals.section = 'home';

fs.readFile('members/top5.json', 'utf8', function (err, data) {
if(data) {
if (data) {
locals.topPlayers = JSON.parse(data);
}
else {
locals.topPlayers = {}
} else {
locals.topPlayers = {};
}

let flash = {};

if (req.query.flash){
if (req.query.flash) {
let buff = Buffer.from(req.query.flash, 'base64');
let text = buff.toString('ascii');

try{
try {
flash = JSON.parse(text);
}
catch(e){
} catch(e) {
console.err("Parsing error while trying to decode a flash error: "+text);
console.err(e);
flasg = [{msg: "Unknown error"}];
flash = [{msg: "Unknown error"}];
}
}

// Render the view
res.render('index', {flash: flash});
res.render('index', {flash: flash, refreshCountersSeconds: parseInt(process.env.PLAYER_COUNT_UPDATE_INTERVAL)});
});

};
5 changes: 3 additions & 2 deletions templates/views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ block content
h3 In order to play Forged Alliance Forever, you must first install Supreme Commander: Forged alliance. You can purchase it for less than $20, below:
.col-md-3
.col-md-3
a(href=" https://www.amazon.com/Supreme-Commander-Forged-Alliance-PC/dp/B000U8AYOO" class="btn btn-default btn-lg btn-outro btn-danger") Buy from Amazon
a(href="https://www.amazon.com/Supreme-Commander-Forged-Alliance-PC/dp/B000U8AYOO" class="btn btn-default btn-lg btn-outro btn-danger") Buy from Amazon
.col-md-3
a(href="https://store.steampowered.com/app/9420" class="btn btn-default btn-lg btn-outro btn-danger") Buy from Steam
.container

block js
script(src="/js/app/counter.js")
script const refreshCountersSeconds = !{refreshCountersSeconds};
script(src="/js/app/counter.js")

0 comments on commit b68bdf0

Please sign in to comment.