mrfrys-node-server/stat.js
YourFriendlyNeighborhoodDealer 66e1aa329b Added license
2018-11-21 15:58:15 +01:00

66 lines
1.7 KiB
JavaScript
Executable file

/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/YourFriendlyNeighborhoodDealer/question-node-server>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
------------------------------------------------------------------------- */
module.exports = {
Inc: Inc,
Load: Load
};
var utils = require('./utils.js');
var logger = require('./logger.js');
const statFile = "stats/stats";
const writeInterval = 10;
var data = {};
var writes = 0;
function Load() {
try {
var prevData = utils.ReadFile(statFile);
data = JSON.parse(prevData);
} catch (e) {
logger.Log("[STAT]: Error at loading logs!", logger.GetColor("redbg"));
console.log(e);
}
}
function Inc(value) {
if (value.startsWith("/?"))
value = "/";
if (data[value] == undefined)
data[value] = 0;
data[value]++;
Save();
}
function Save() {
writes++;
if (writes == writeInterval) {
try {
utils.WriteFile(JSON.stringify(data), statFile);
writes = 0;
// logger.Log("[STAT] Stats wrote.");
} catch (e) {
logger.Log("[STAT]: Error at writing logs!", logger.GetColor("redbg"));
console.log(e);
}
}
}