From b68bdf02bd42f1c8e31686b7aac582911ce37139 Mon Sep 17 00:00:00 2001 From: Konstantin Kalinovsky Date: Sun, 29 Dec 2019 21:41:00 +0100 Subject: [PATCH] Reduce cpu load Fixes #214 --- public/js/app/counter.js | 52 ++++++++++++++++++++------------------- routes/lobby_api.js | 18 ++++++++------ routes/views/index.js | 18 ++++++-------- templates/views/index.pug | 5 ++-- 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/public/js/app/counter.js b/public/js/app/counter.js index 44185bce..e9989bc3 100644 --- a/public/js/app/counter.js +++ b/public/js/app/counter.js @@ -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); } diff --git a/routes/lobby_api.js b/routes/lobby_api.js index efc67a09..f14e1d13 100644 --- a/routes/lobby_api.js +++ b/routes/lobby_api.js @@ -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()); }); }; diff --git a/routes/views/index.js b/routes/views/index.js index 49627a4b..2e57eee2 100644 --- a/routes/views/index.js +++ b/routes/views/index.js @@ -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)}); }); }; diff --git a/templates/views/index.pug b/templates/views/index.pug index 1920a2b5..435f9370 100644 --- a/templates/views/index.pug +++ b/templates/views/index.pug @@ -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") \ No newline at end of file + script const refreshCountersSeconds = !{refreshCountersSeconds}; + script(src="/js/app/counter.js")